From c5991fc814a8151cbc8136ed0a70ea25350e20d7 Mon Sep 17 00:00:00 2001 From: Tanishq Date: Wed, 12 Jun 2024 22:08:51 +0530 Subject: [PATCH] fix ignore links to #top when checking anchors (#2519) * fix ignore links to #top when checking anchors * move logic to check internal links --------- Co-authored-by: Tanishq --- components/site/src/link_checking.rs | 2 ++ components/utils/src/anchors.rs | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/components/site/src/link_checking.rs b/components/site/src/link_checking.rs index c7e4678195..aff8879b73 100644 --- a/components/site/src/link_checking.rs +++ b/components/site/src/link_checking.rs @@ -10,6 +10,7 @@ use crate::Site; use errors::{bail, Result}; use libs::rayon; use libs::url::Url; +use utils::anchors::is_special_anchor; /// Check whether all internal links pointing to explicit anchor fragments are valid. /// @@ -40,6 +41,7 @@ pub fn check_internal_links_with_anchors(site: &Site) -> Vec { (md_path, Some(anchor)) => Some((page_path, md_path, anchor)), _ => None, }) + .filter(|(_, _, anchor)| !is_special_anchor(anchor)) .inspect(|_| anchors_total = anchors_total.saturating_add(1)); // Check for targets existence (including anchors), then keep only the faulty diff --git a/components/utils/src/anchors.rs b/components/utils/src/anchors.rs index a5706e79d9..5c1bd2d16c 100644 --- a/components/utils/src/anchors.rs +++ b/components/utils/src/anchors.rs @@ -10,9 +10,15 @@ fn anchor_id_checks(anchor: &str) -> Regex { Regex::new(&format!(r#"\s(?i)(id|name) *= *("|')*{}("|'| |>)+"#, escape(anchor))).unwrap() } +/// Checks if anchor has a special meaning in HTML +/// https://html.spec.whatwg.org/#select-the-indicated-part +pub fn is_special_anchor(anchor: &str) -> bool { + anchor.is_empty() || anchor.eq_ignore_ascii_case("top") +} + #[cfg(test)] mod tests { - use super::anchor_id_checks; + use super::{anchor_id_checks, is_special_anchor}; fn check(anchor: &str, content: &str) -> bool { anchor_id_checks(anchor).is_match(content) @@ -52,4 +58,12 @@ id="fred">"#)); // Non matchers assert!(!m(r#""#)); } + + #[test] + fn test_is_special_anchor() { + assert!(is_special_anchor("")); + assert!(is_special_anchor("top")); + assert!(is_special_anchor("Top")); + assert!(!is_special_anchor("anchor")); + } }