diff --git a/crates/rari-doc/src/html/fix_link.rs b/crates/rari-doc/src/html/fix_link.rs index 30894d71..e8373e66 100644 --- a/crates/rari-doc/src/html/fix_link.rs +++ b/crates/rari-doc/src/html/fix_link.rs @@ -94,26 +94,27 @@ pub fn handle_internal_link( false }; - let remove_href = if !Page::exists(resolved_href_no_hash) && !Page::ignore_link_check(href) { - tracing::debug!("{resolved_href_no_hash} {href}"); - let class = el.get_attribute("class").unwrap_or_default(); - el.set_attribute( - "class", - &concat_strs!( - &class, - if class.is_empty() { "" } else { " " }, - "page-not-created" - ), - )?; - if let Some(href) = el.get_attribute("href") { - el.set_attribute("data-href", &href)?; - } - el.remove_attribute("href"); - el.set_attribute("title", l10n_json_data("Common", "summary", page.locale())?)?; - true - } else { - false - }; + let remove_href = + if !Page::exists_with_fallback(resolved_href_no_hash) && !Page::ignore_link_check(href) { + tracing::debug!("{resolved_href_no_hash} {href}"); + let class = el.get_attribute("class").unwrap_or_default(); + el.set_attribute( + "class", + &concat_strs!( + &class, + if class.is_empty() { "" } else { " " }, + "page-not-created" + ), + )?; + if let Some(href) = el.get_attribute("href") { + el.set_attribute("data-href", &href)?; + } + el.remove_attribute("href"); + el.set_attribute("title", l10n_json_data("Common", "summary", page.locale())?)?; + true + } else { + false + }; if !remove_href && en_us_fallback { let class = el.get_attribute("class").unwrap_or_default(); @@ -135,39 +136,41 @@ pub fn handle_internal_link( 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('-') { - if let Some((line, col)) = start.split_once(':') { - let line = line - .parse::() - .map(|l| l + i64::try_from(page.fm_offset()).unwrap_or(l - 1)) - .ok() - .unwrap_or(-1); - let col = col.parse::().ok().unwrap_or(0); - let ic = get_issue_counter(); - tracing::warn!( - source = "redirected-link", - ic = ic, - line = line, - col = col, - url = original_href, - redirect = resolved_href - ); - if data_issues { - el.set_attribute("data-flaw", &ic.to_string())?; + if !en_us_fallback { + if let Some(pos) = el.get_attribute("data-sourcepos") { + if let Some((start, _)) = pos.split_once('-') { + if let Some((line, col)) = start.split_once(':') { + let line = line + .parse::() + .map(|l| l + i64::try_from(page.fm_offset()).unwrap_or(l - 1)) + .ok() + .unwrap_or(-1); + let col = col.parse::().ok().unwrap_or(0); + let ic = get_issue_counter(); + tracing::warn!( + source = "redirected-link", + ic = ic, + line = line, + col = col, + url = original_href, + redirect = resolved_href + ); + if data_issues { + el.set_attribute("data-flaw", &ic.to_string())?; + } } } - } - } else { - let ic = get_issue_counter(); - tracing::warn!( - source = "redirected-link", - ic = ic, - url = original_href, - redirect = resolved_href - ); - if data_issues { - el.set_attribute("data-flaw", &ic.to_string())?; + } else { + let ic = get_issue_counter(); + tracing::warn!( + source = "redirected-link", + ic = ic, + url = original_href, + redirect = resolved_href + ); + if data_issues { + el.set_attribute("data-flaw", &ic.to_string())?; + } } } diff --git a/crates/rari-doc/src/pages/page.rs b/crates/rari-doc/src/pages/page.rs index 8123acfa..f93ee190 100644 --- a/crates/rari-doc/src/pages/page.rs +++ b/crates/rari-doc/src/pages/page.rs @@ -3,10 +3,11 @@ use std::sync::Arc; use enum_dispatch::enum_dispatch; use rari_types::fm_types::{FeatureStatus, PageType}; -use rari_types::globals::{blog_root, curriculum_root}; +use rari_types::globals::{ + blog_root, contributor_spotlight_root, curriculum_root, generic_content_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; @@ -17,7 +18,7 @@ use crate::pages::types::contributors::ContributorSpotlight; use crate::pages::types::curriculum::CurriculumPage; use crate::pages::types::doc::Doc; use crate::pages::types::spa::SPA; -use crate::resolve::{strip_locale_from_url, url_meta_from, UrlMeta}; +use crate::resolve::{url_meta_from, UrlMeta}; use crate::utils::locale_and_typ_from_path; /// Represents a page in the documentation system. @@ -162,38 +163,57 @@ impl Page { false } - /// 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 whether the page exists for the default locale. + /// Checks whether a page exists for the given URL. /// /// # Arguments - /// - /// * `url` - A string slice that holds the URL to be checked. + /// - `url`: A string slice that holds the URL to check. /// /// # Returns + /// `true` if the page exists; otherwise, `false`. /// - /// * `bool` - Returns `true` if the `Page` for the given URL exists, otherwise `false`. + /// # Examples pub fn exists(url: &str) -> bool { - if url.starts_with("/en-US/blog") && blog_root().is_none() { - return true; - } - if url.starts_with("/en-US/curriculum") && curriculum_root().is_none() { - return true; - } - if strip_locale_from_url(url).1 == "/" { - return true; - } - - if Page::from_url_with_fallback(url).is_ok() { - return true; + if let Ok(meta) = url_meta_from(url) { + match meta.page_category { + PageCategory::BlogPost if blog_root().is_none() => return true, + PageCategory::Curriculum if curriculum_root().is_none() => return true, + PageCategory::ContributorSpotlight if contributor_spotlight_root().is_none() => { + return true + } + PageCategory::GenericPage if generic_content_root().is_none() => return true, + _ => {} + }; + Page::from_url(url).is_ok() + } else { + false } + } - 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); + /// Checks whether a page exists for the given URL, with a fallback mechanism. + /// + /// This function operates similarly to [`Page::exists`], but it uses a en-US fallback + /// when determining if a page exists. + /// + /// # Arguments + /// - `url`: A string slice that holds the URL to check. + /// + /// # Returns + /// `true` if the page exists (with or without fallback); otherwise, `false`. + pub fn exists_with_fallback(url: &str) -> bool { + if let Ok(meta) = url_meta_from(url) { + match meta.page_category { + PageCategory::BlogPost if blog_root().is_none() => return true, + PageCategory::Curriculum if curriculum_root().is_none() => return true, + PageCategory::ContributorSpotlight if contributor_spotlight_root().is_none() => { + return true + } + PageCategory::GenericPage if generic_content_root().is_none() => return true, + _ => {} + }; + Page::from_url_with_fallback(url).is_ok() + } else { + false } - false } }