From 6bec7be420aa070b903fa8c77b1c58c0259b1073 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Tue, 14 Jan 2025 16:26:21 +1300 Subject: Detect duplicate headings within a page This is an issue because the duplicate headings will have identical ids, and all links to the lower heading will instead link to the upper heading. --- src/collect_files.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/collect_files.rs b/src/collect_files.rs index d9cfb37..49f5cc0 100644 --- a/src/collect_files.rs +++ b/src/collect_files.rs @@ -96,15 +96,23 @@ impl Website { "md" => { let markdown = std::fs::read_to_string(&source_path).unwrap(); let document = MarkdownDocument::from_str(&markdown); + let mut heading_set = HashSet::new(); + let mut duplicates = HashSet::new(); let headings = document.blocks.iter() .filter_map(|block| if let Block::Heading { line, level } = block { let name = line.to_string(); let url = make_url_safe(&name); let level = level.to_owned(); + if !heading_set.insert(url.clone()) { + duplicates.insert(url.clone()); + } Some(Heading { name, url, level }) } else { None }).collect(); + for url in duplicates { + warn!("Page {name:?} contains multiple headings with ID \"#{url}\""); + } if name_url == "+index" { if parents.is_empty() { // This is the index file for the whole site. -- cgit v1.2.3-70-g09d2