summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Bridle <ben@derelict.engineering>2025-09-04 09:25:35 +1200
committerBen Bridle <ben@derelict.engineering>2025-09-04 09:25:35 +1200
commit5a034625f07731459a79590a910d2a5177f316d7 (patch)
tree23cb8ffc1e4e8e8d1de05fbe9e7489874dcd1d3e
parent3767f5cd3c2ec26872d52958e94dafbae074039f (diff)
downloadtoaster-5a034625f07731459a79590a910d2a5177f316d7.zip
Wrap each heading with an <a> 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.
-rw-r--r--src/generate_html.rs21
1 files changed, 12 insertions, 9 deletions
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!("</article>");
+ html!("<article>");
+ };
let id = make_url_safe(strip_appendix(&line.to_string()));
- match level {
- Level::Heading1 => {
- html!("</article>");
- html!("<article>");
- 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!("<div class='math'>{}</div>", sanitize_text(content, false)),