diff --git a/components/rendering/src/markdown.rs b/components/rendering/src/markdown.rs index e4af6fa..d983a50 100644 --- a/components/rendering/src/markdown.rs +++ b/components/rendering/src/markdown.rs @@ -77,16 +77,11 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result or other things already there - temp_header.title += &text; + temp_header.add_text(&text); header_created = true; return Event::Html(Borrowed("")); } @@ -182,7 +177,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result", fixed_link, title) }; - temp_header.push(&html); + temp_header.add_html(&html); return Event::Html(Borrowed("")); } @@ -190,21 +185,21 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result { if in_header { - temp_header.push(""); + temp_header.add_html(""); return Event::Html(Borrowed("")); } event } Event::Start(Tag::Code) => { if in_header { - temp_header.push(""); + temp_header.add_html(""); return Event::Html(Borrowed("")); } event } Event::End(Tag::Code) => { if in_header { - temp_header.push(""); + temp_header.add_html(""); return Event::Html(Borrowed("")); } event @@ -215,8 +210,13 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result { - // End of a header, reset all the things and return the stringified - // version of the header + // End of a header, reset all the things and return the header string + + let id = find_anchor(&anchors, slugify(&temp_header.title), 0); + anchors.push(id.clone()); + temp_header.permalink = format!("{}#{}", context.current_page_permalink, id); + temp_header.id = id; + in_header = false; header_created = false; let val = temp_header.to_string(context.tera, context.insert_anchor); diff --git a/components/rendering/src/table_of_contents.rs b/components/rendering/src/table_of_contents.rs index 1291d2d..40f11ac 100644 --- a/components/rendering/src/table_of_contents.rs +++ b/components/rendering/src/table_of_contents.rs @@ -31,6 +31,7 @@ pub struct TempHeader { pub id: String, pub permalink: String, pub title: String, + pub html: String, } impl TempHeader { @@ -40,10 +41,16 @@ impl TempHeader { id: String::new(), permalink: String::new(), title: String::new(), + html: String::new(), } } - pub fn push(&mut self, val: &str) { + pub fn add_html(&mut self, val: &str) { + self.html += val; + } + + pub fn add_text(&mut self, val: &str) { + self.html += val; self.title += val; } @@ -58,9 +65,9 @@ impl TempHeader { }; match insert_anchor { - InsertAnchor::None => format!("{t}\n", lvl = self.level, t = self.title, id = self.id), - InsertAnchor::Left => format!("{a}{t}\n", lvl = self.level, a = anchor_link, t = self.title, id = self.id), - InsertAnchor::Right => format!("{t}{a}\n", lvl = self.level, a = anchor_link, t = self.title, id = self.id), + InsertAnchor::None => format!("{t}\n", lvl = self.level, t = self.html, id = self.id), + InsertAnchor::Left => format!("{a}{t}\n", lvl = self.level, a = anchor_link, t = self.html, id = self.id), + InsertAnchor::Right => format!("{t}{a}\n", lvl = self.level, a = anchor_link, t = self.html, id = self.id), } } } diff --git a/components/rendering/tests/markdown.rs b/components/rendering/tests/markdown.rs index b4894dd..7848664 100644 --- a/components/rendering/tests/markdown.rs +++ b/components/rendering/tests/markdown.rs @@ -427,6 +427,38 @@ fn can_make_toc() { assert_eq!(toc[0].children[1].children.len(), 1); } +#[test] +fn can_ignore_tags_in_toc() { + let permalinks_ctx = HashMap::new(); + let config = Config::default(); + let context = RenderContext::new( + &GUTENBERG_TERA, + &config, + "https://mysite.com/something", + &permalinks_ctx, + InsertAnchor::Left, + ); + + let res = render_content(r#" +## header with `code` + +## [anchor](https://duckduckgo.com/) in header + +## **bold** and *italics* + "#, &context).unwrap(); + + let toc = res.toc; + + assert_eq!(toc[0].id, "header-with-code"); + assert_eq!(toc[0].title, "header with code"); + + assert_eq!(toc[1].id, "anchor-in-header"); + assert_eq!(toc[1].title, "anchor in header"); + + assert_eq!(toc[2].id, "bold-and-italics"); + assert_eq!(toc[2].title, "bold and italics"); +} + #[test] fn can_understand_backtick_in_titles() { let permalinks_ctx = HashMap::new();