From 3ba351ac3142d09e752fcc723ffbc44ec8ef0015 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Mon, 2 Mar 2026 15:22:19 +1300 Subject: Omit outer heading link if heading title contains a link tags can't be nested in HTML, so we omit the outer link on the heading (that links to itself) if the title itself contains a link. This avoids generating a nested tag. --- src/generate_html.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'src/generate_html.rs') diff --git a/src/generate_html.rs b/src/generate_html.rs index 6887f37..daa83d5 100644 --- a/src/generate_html.rs +++ b/src/generate_html.rs @@ -179,9 +179,24 @@ pub fn document_to_html(page: &Page, website: &Website) -> String { Level::Heading2 => "h2", Level::Heading3 => "h3", }; - wrap!(heading_tag, format!("id='{url}'"), { - tag!("a", line, format!("href='#{url}'")); - }); + // Find out whether line contains a link. + let mut contains_link = false; + for token in &line.tokens { + if let Token::InternalLink { .. } | Token::ExternalLink { .. } = token { + contains_link = true; + break; + } + } + if !contains_link { + wrap!(heading_tag, format!("id='{url}'"), { + tag!("a", line, format!("href='#{url}'")); + }); + } else { + // Leave off the heading tag if the heading already contains + // a link ( tags cannot be nested). + tag!(heading_tag, line, format!("id='{url}'")); + } + } Block::Paragraph(line) => tag!("p", line), Block::Math(content) => html!("
{}
", sanitize_text(content, false)), -- cgit v1.2.3-70-g09d2