diff options
| author | Ben Bridle <bridle.benjamin@gmail.com> | 2026-03-02 15:22:19 +1300 |
|---|---|---|
| committer | Ben Bridle <bridle.benjamin@gmail.com> | 2026-03-02 15:22:19 +1300 |
| commit | 3ba351ac3142d09e752fcc723ffbc44ec8ef0015 (patch) | |
| tree | a03c38cb756eff4df0b7030f3f8706acc9e95d6d /src | |
| parent | e88199551b5d2beba0fbaa054d0f2209b66627c6 (diff) | |
| download | toaster-3ba351ac3142d09e752fcc723ffbc44ec8ef0015.zip | |
Omit outer heading link if heading title contains a link
<a> 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 <a> tag.
Diffstat (limited to 'src')
| -rw-r--r-- | src/generate_html.rs | 21 |
1 files changed, 18 insertions, 3 deletions
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 <a> tag if the heading already contains + // a link (<a> tags cannot be nested). + tag!(heading_tag, line, format!("id='{url}'")); + } + } Block::Paragraph(line) => tag!("p", line), Block::Math(content) => html!("<div class='math'>{}</div>", sanitize_text(content, false)), |
