Browse Source

Fix link in md headers

Fix #53
index-subcmd
Vincent Prouillet 7 years ago
parent
commit
db84411788
1 changed files with 22 additions and 0 deletions
  1. +22
    -0
      src/markdown.rs

+ 22
- 0
src/markdown.rs View File

@@ -253,6 +253,9 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap<String, String>, ter
}, },
// Need to handle relative links // Need to handle relative links
Event::Start(Tag::Link(ref link, ref title)) => { Event::Start(Tag::Link(ref link, ref title)) => {
if in_header {
return Event::Html(Owned("".to_owned()));
}
if link.starts_with("./") { if link.starts_with("./") {
// First we remove the ./ since that's gutenberg specific // First we remove the ./ since that's gutenberg specific
let clean_link = link.replacen("./", "", 1); let clean_link = link.replacen("./", "", 1);
@@ -277,6 +280,12 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap<String, String>, ter


Event::Start(Tag::Link(link.clone(), title.clone())) 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 // need to know when we are in a code block to disable shortcodes in them
Event::Start(Tag::Code) => { Event::Start(Tag::Code) => {
in_code_block = true; 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,
"<h2 id=\"xresources\"><a class=\"anchor\" href=\"#xresources\" aria-label=\"Anchor link for: xresources\">đź”—</a>\nXresources</h2>\n"
);
}


#[test] #[test]
fn test_markdown_to_html_insert_anchor_with_other_special_chars() { fn test_markdown_to_html_insert_anchor_with_other_special_chars() {
let mut config = Config::default(); let mut config = Config::default();


Loading…
Cancel
Save