Skip to content

Commit

Permalink
fix ignore links to #top when checking anchors (#2519)
Browse files Browse the repository at this point in the history
* fix ignore links to #top when checking anchors

* move logic to check internal links

---------

Co-authored-by: Tanishq <tanishq@levels.fyi>
  • Loading branch information
2 people authored and Keats committed Jun 20, 2024
1 parent 26f6677 commit c5991fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions components/site/src/link_checking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -40,6 +41,7 @@ pub fn check_internal_links_with_anchors(site: &Site) -> Vec<String> {
(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
Expand Down
16 changes: 15 additions & 1 deletion components/utils/src/anchors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -52,4 +58,12 @@ id="fred">"#));
// Non matchers
assert!(!m(r#"<a notid="fred">"#));
}

#[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"));
}
}

0 comments on commit c5991fc

Please sign in to comment.