From db84411788911d7a073b26aa5478d420ff0b1169 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Mon, 1 May 2017 16:04:41 +0900 Subject: [PATCH] Fix link in md headers Fix #53 --- src/markdown.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/markdown.rs b/src/markdown.rs index 75b8601c..9be64a7d 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -253,6 +253,9 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap, ter }, // Need to handle relative links Event::Start(Tag::Link(ref link, ref title)) => { + if in_header { + return Event::Html(Owned("".to_owned())); + } if link.starts_with("./") { // First we remove the ./ since that's gutenberg specific let clean_link = link.replacen("./", "", 1); @@ -277,6 +280,12 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap, ter Event::Start(Tag::Link(link.clone(), title.clone())) }, + Event::End(Tag::Link(_, _)) => { + if in_header { + return Event::Html(Owned("".to_owned())); + } + event + } // need to know when we are in a code block to disable shortcodes in them Event::Start(Tag::Code) => { in_code_block = true; @@ -535,6 +544,19 @@ A quote ); } + // See https://github.com/Keats/gutenberg/issues/53 + #[test] + fn test_markdown_to_html_insert_anchor_with_link() { + let mut config = Config::default(); + config.insert_anchor_links = Some(true); + let res = markdown_to_html("## [](#xresources)Xresources", &HashMap::new(), &GUTENBERG_TERA, &config).unwrap(); + assert_eq!( + res, + "

🔗\nXresources

\n" + ); + } + + #[test] fn test_markdown_to_html_insert_anchor_with_other_special_chars() { let mut config = Config::default();