From 5a034625f07731459a79590a910d2a5177f316d7 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Thu, 4 Sep 2025 09:25:35 +1200 Subject: Wrap each heading with an tag that links to that heading This is so that a heading can be clicked to get a permalink to that heading, which is useful when sending someone a link to a particular section of a document. --- src/generate_html.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'src/generate_html.rs') diff --git a/src/generate_html.rs b/src/generate_html.rs index c0b26f6..0460cf7 100644 --- a/src/generate_html.rs +++ b/src/generate_html.rs @@ -140,16 +140,19 @@ pub fn document_to_html(document: &MarkdownDocument, page: &Page, website: &Webs for block in &document.blocks { match block { Block::Heading { level, line } => { + if let Level::Heading1 = level { + html!(""); + html!("
"); + }; let id = make_url_safe(strip_appendix(&line.to_string())); - match level { - Level::Heading1 => { - html!("
"); - html!("
"); - tag!("h1", line, format!("id='{id}'")) - } - Level::Heading2 => tag!("h2", line, format!("id='{id}'")), - Level::Heading3 => tag!("h3", line, format!("id='{id}'")), - } + let heading_tag = match level { + Level::Heading1 => "h1", + Level::Heading2 => "h2", + Level::Heading3 => "h3", + }; + wrap!(heading_tag, format!("id='{id}'"), { + tag!("a", line, format!("href='#{id}'")); + }); } Block::Paragraph(line) => tag!("p", line), Block::Math(content) => html!("
{}
", sanitize_text(content, false)), -- cgit v1.2.3-70-g09d2