From 1a0695b9db2c7a048d757023a3b2889e7b3e6605 Mon Sep 17 00:00:00 2001 From: Florian Dieminger Date: Tue, 29 Oct 2024 21:21:18 +0100 Subject: [PATCH] feat(html): no href for page-not-found And some fixes for links. --- crates/rari-doc/src/html/rewriter.rs | 29 +++++++++++++++------------- crates/rari-doc/src/pages/page.rs | 24 ++++++++++++++--------- crates/rari-doc/src/resolve.rs | 2 +- rari-npm/package-lock.json | 4 ++-- 4 files changed, 34 insertions(+), 25 deletions(-) diff --git a/crates/rari-doc/src/html/rewriter.rs b/crates/rari-doc/src/html/rewriter.rs index 217d1dba..c3bd6e3b 100644 --- a/crates/rari-doc/src/html/rewriter.rs +++ b/crates/rari-doc/src/html/rewriter.rs @@ -7,7 +7,6 @@ use rari_md::ext::DELIM_START; use rari_md::node_card::NoteCard; use rari_types::fm_types::PageType; use rari_types::globals::settings; -use rari_types::locale::Locale; use rari_utils::concat_strs; use tracing::warn; use url::Url; @@ -164,9 +163,13 @@ pub fn post_process_html( .strip_prefix("https://developer.mozilla.org") .map(|href| if href.is_empty() { "/" } else { href }) .unwrap_or(&original_href); + let href_no_hash = &href[..href.find('#').unwrap_or(href.len())]; let no_locale = strip_locale_from_url(href).0.is_none(); + if no_locale && Page::ignore_link_check(href_no_hash) { + return Ok(()); + } let maybe_prefixed_href = if no_locale { - Cow::Owned(concat_strs!("/", Locale::default().as_url_str(), href)) + Cow::Owned(concat_strs!("/", page.locale().as_url_str(), href)) } else { Cow::Borrowed(href) }; @@ -196,6 +199,11 @@ pub fn post_process_html( } else { false }; + let resolved_href = if no_locale { + strip_locale_from_url(&resolved_href).1 + } else { + resolved_href.as_ref() + }; if original_href != resolved_href { if let Some(pos) = el.get_attribute("data-sourcepos") { if let Some((start, _)) = pos.split_once('-') { @@ -214,7 +222,7 @@ pub fn post_process_html( line = line, col = col, url = original_href, - redirect = resolved_href.as_ref() + redirect = resolved_href ); if data_issues { el.set_attribute("data-flaw", &ic.to_string())?; @@ -227,24 +235,19 @@ pub fn post_process_html( source = "redirected-link", ic = ic, url = original_href, - redirect = resolved_href.as_ref() + redirect = resolved_href ); if data_issues { el.set_attribute("data-flaw", &ic.to_string())?; } } + + if !remove_href { + el.set_attribute("href", resolved_href)?; + } } if remove_href { el.remove_attribute("href"); - } else { - el.set_attribute( - "href", - if no_locale { - strip_locale_from_url(&resolved_href).1 - } else { - &resolved_href - }, - )?; } } else if original_href.starts_with("http:") || original_href.starts_with("https:") { let class = el.get_attribute("class").unwrap_or_default(); diff --git a/crates/rari-doc/src/pages/page.rs b/crates/rari-doc/src/pages/page.rs index f3b626f3..b6ead7ad 100644 --- a/crates/rari-doc/src/pages/page.rs +++ b/crates/rari-doc/src/pages/page.rs @@ -3,9 +3,10 @@ use std::sync::Arc; use enum_dispatch::enum_dispatch; use rari_types::fm_types::{FeatureStatus, PageType}; -use rari_types::globals::blog_root; +use rari_types::globals::{blog_root, curriculum_root}; use rari_types::locale::Locale; use rari_types::RariEnv; +use rari_utils::concat_strs; use super::json::BuiltPage; use super::types::contributors::contributor_spotlight_from_url; @@ -169,6 +170,9 @@ impl Page { /// Checks if a `Page` for a given URL exists. /// + /// Checks for non SPAs owned by the front-end and then calls [Page::from_url_with_fallback]. + /// Also checks if there's no locale and in that case returns wether the page exists for the default locale. + /// /// # Arguments /// /// * `url` - A string slice that holds the URL to be checked. @@ -177,23 +181,25 @@ impl Page { /// /// * `bool` - Returns `true` if the `Page` for the given URL exists, otherwise `false`. pub fn exists(url: &str) -> bool { - if url == "/discord" { - return true; - } - if url.starts_with("/users/") { - return true; - } if url.starts_with("/en-US/blog") && blog_root().is_none() { return true; } - if url.starts_with("/en-US/curriculum") { + if url.starts_with("/en-US/curriculum") && curriculum_root().is_none() { return true; } if strip_locale_from_url(url).1 == "/" { return true; } - Page::from_url_with_fallback(url).is_ok() + if Page::from_url_with_fallback(url).is_ok() { + return true; + } + + if let (None, url) = strip_locale_from_url(url) { + let url_with_default_locale = concat_strs!("/", Locale::default().as_url_str(), url); + return Self::exists(&url_with_default_locale); + } + false } } diff --git a/crates/rari-doc/src/resolve.rs b/crates/rari-doc/src/resolve.rs index 468651c4..3c1493d1 100644 --- a/crates/rari-doc/src/resolve.rs +++ b/crates/rari-doc/src/resolve.rs @@ -74,7 +74,7 @@ pub(crate) fn strip_locale_from_url(url: &str) -> (Option, &str) { } let i = url[1..].find('/').map(|i| i + 1).unwrap_or(url.len()); let locale = Locale::from_str(&url[1..i]).ok(); - (locale, &url[i..]) + (locale, &url[if locale.is_none() { 0 } else { i }..]) } /// Represents metadata extracted from a URL. diff --git a/rari-npm/package-lock.json b/rari-npm/package-lock.json index a0bdc634..f544e3f5 100644 --- a/rari-npm/package-lock.json +++ b/rari-npm/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "@mdn/rari", - "version": "0.0.13", + "version": "0.0.15", "hasInstallScript": true, "license": "MPL-2.0", "dependencies": { @@ -16,7 +16,7 @@ "tar": "^7.4.3" }, "bin": { - "rari": "bin/rari" + "rari": "lib/cli.js" }, "engines": { "node": ">=20"