prevent html tags from appearing in the toc

This commit is contained in:
Michael Plotke 2018-10-18 08:58:50 -04:00
parent 7ef4acbfbc
commit 4db629a060
3 changed files with 56 additions and 17 deletions

View file

@ -77,16 +77,11 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
// Header first // Header first
if in_header { if in_header {
if header_created { if header_created {
temp_header.push(&text); temp_header.add_text(&text);
return Event::Html(Borrowed("")); return Event::Html(Borrowed(""));
} }
let id = find_anchor(&anchors, slugify(&text), 0);
anchors.push(id.clone());
// update the header and add it to the list
temp_header.permalink = format!("{}#{}", context.current_page_permalink, id);
temp_header.id = id;
// += as we might have some <code> or other things already there // += as we might have some <code> or other things already there
temp_header.title += &text; temp_header.add_text(&text);
header_created = true; header_created = true;
return Event::Html(Borrowed("")); return Event::Html(Borrowed(""));
} }
@ -182,7 +177,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
} else { } else {
format!("<a href=\"{}\" title=\"{}\">", fixed_link, title) format!("<a href=\"{}\" title=\"{}\">", fixed_link, title)
}; };
temp_header.push(&html); temp_header.add_html(&html);
return Event::Html(Borrowed("")); return Event::Html(Borrowed(""));
} }
@ -190,21 +185,21 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
} }
Event::End(Tag::Link(_, _)) => { Event::End(Tag::Link(_, _)) => {
if in_header { if in_header {
temp_header.push("</a>"); temp_header.add_html("</a>");
return Event::Html(Borrowed("")); return Event::Html(Borrowed(""));
} }
event event
} }
Event::Start(Tag::Code) => { Event::Start(Tag::Code) => {
if in_header { if in_header {
temp_header.push("<code>"); temp_header.add_html("<code>");
return Event::Html(Borrowed("")); return Event::Html(Borrowed(""));
} }
event event
} }
Event::End(Tag::Code) => { Event::End(Tag::Code) => {
if in_header { if in_header {
temp_header.push("</code>"); temp_header.add_html("</code>");
return Event::Html(Borrowed("")); return Event::Html(Borrowed(""));
} }
event event
@ -215,8 +210,13 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
Event::Html(Borrowed("")) Event::Html(Borrowed(""))
} }
Event::End(Tag::Header(_)) => { Event::End(Tag::Header(_)) => {
// End of a header, reset all the things and return the stringified // End of a header, reset all the things and return the header string
// version of the header
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; in_header = false;
header_created = false; header_created = false;
let val = temp_header.to_string(context.tera, context.insert_anchor); let val = temp_header.to_string(context.tera, context.insert_anchor);

View file

@ -31,6 +31,7 @@ pub struct TempHeader {
pub id: String, pub id: String,
pub permalink: String, pub permalink: String,
pub title: String, pub title: String,
pub html: String,
} }
impl TempHeader { impl TempHeader {
@ -40,10 +41,16 @@ impl TempHeader {
id: String::new(), id: String::new(),
permalink: String::new(), permalink: String::new(),
title: 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; self.title += val;
} }
@ -58,9 +65,9 @@ impl TempHeader {
}; };
match insert_anchor { match insert_anchor {
InsertAnchor::None => format!("<h{lvl} id=\"{id}\">{t}</h{lvl}>\n", lvl = self.level, t = self.title, id = self.id), InsertAnchor::None => format!("<h{lvl} id=\"{id}\">{t}</h{lvl}>\n", lvl = self.level, t = self.html, id = self.id),
InsertAnchor::Left => format!("<h{lvl} id=\"{id}\">{a}{t}</h{lvl}>\n", lvl = self.level, a = anchor_link, t = self.title, id = self.id), InsertAnchor::Left => format!("<h{lvl} id=\"{id}\">{a}{t}</h{lvl}>\n", lvl = self.level, a = anchor_link, t = self.html, id = self.id),
InsertAnchor::Right => format!("<h{lvl} id=\"{id}\">{t}{a}</h{lvl}>\n", lvl = self.level, a = anchor_link, t = self.title, id = self.id), InsertAnchor::Right => format!("<h{lvl} id=\"{id}\">{t}{a}</h{lvl}>\n", lvl = self.level, a = anchor_link, t = self.html, id = self.id),
} }
} }
} }

View file

@ -427,6 +427,38 @@ fn can_make_toc() {
assert_eq!(toc[0].children[1].children.len(), 1); 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] #[test]
fn can_understand_backtick_in_titles() { fn can_understand_backtick_in_titles() {
let permalinks_ctx = HashMap::new(); let permalinks_ctx = HashMap::new();