diff --git a/CHANGELOG.md b/CHANGELOG.md index c31df79..093ef9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Use summary if available in RSS feed - Add tables and footnotes support in markdown - Add previous/previous_in_section/next/next_in_section/summary to `Page` +- Add more language syntaxes ## 0.0.3 (2017-04-05) - Add some colours in console diff --git a/src/markdown.rs b/src/markdown.rs index ce137af..5ba63bb 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -121,6 +121,9 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap, ter let mut in_code_block = false; // If we get text in header, we need to insert the id and a anchor let mut in_header = false; + // pulldown_cmark can send several text events for a title if there are markdown + // specific characters like `!` in them. We only want to insert the anchor the first time + let mut header_already_inserted = false; // the rendered html let mut html = String::new(); let mut anchors: Vec = vec![]; @@ -207,6 +210,9 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap, ter } if in_header { + if header_already_inserted { + return Event::Text(text); + } let id = find_anchor(&anchors, slugify(&text), 0); anchors.push(id.clone()); let anchor_link = if config.insert_anchor_links.unwrap() { @@ -216,6 +222,7 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap, ter } else { String::new() }; + header_already_inserted = true; return Event::Html(Owned(format!(r#"id="{}">{}{}"#, id, anchor_link, text))); } @@ -288,6 +295,7 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap, ter }, Event::End(Tag::Header(_)) => { in_header = false; + header_already_inserted = false; event }, // If we added shortcodes, don't close a paragraph since there's none @@ -505,4 +513,38 @@ A quote let res = markdown_to_html("# Hello\n# Hello", &HashMap::new(), &GUTENBERG_TERA, &Config::default()).unwrap(); assert_eq!(res, "

Hello

\n

Hello

\n"); } + + #[test] + fn test_markdown_to_html_insert_anchor() { + let mut config = Config::default(); + config.insert_anchor_links = Some(true); + let res = markdown_to_html("# Hello", &HashMap::new(), &GUTENBERG_TERA, &config).unwrap(); + assert_eq!( + res, + "

🔗\nHello

\n" + ); + } + + // See https://github.com/Keats/gutenberg/issues/42 + #[test] + fn test_markdown_to_html_insert_anchor_with_exclamation_mark() { + let mut config = Config::default(); + config.insert_anchor_links = Some(true); + let res = markdown_to_html("# Hello!", &HashMap::new(), &GUTENBERG_TERA, &config).unwrap(); + assert_eq!( + res, + "

🔗\nHello!

\n" + ); + } + + #[test] + fn test_markdown_to_html_insert_anchor_with_other_special_chars() { + let mut config = Config::default(); + config.insert_anchor_links = Some(true); + let res = markdown_to_html("# Hello*_()", &HashMap::new(), &GUTENBERG_TERA, &config).unwrap(); + assert_eq!( + res, + "

🔗\nHello*_()

\n" + ); + } } diff --git a/src/templates/anchor-link.html b/src/templates/anchor-link.html index e6f480b..3c60ec9 100644 --- a/src/templates/anchor-link.html +++ b/src/templates/anchor-link.html @@ -1,3 +1 @@ - - 🔗 - +🔗