Browse Source

Fix titles with markdown chars and anchors

index-subcmd
Vincent Prouillet 7 years ago
parent
commit
f35ca24893
3 changed files with 44 additions and 3 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +42
    -0
      src/markdown.rs
  3. +1
    -3
      src/templates/anchor-link.html

+ 1
- 0
CHANGELOG.md View File

@@ -11,6 +11,7 @@
- Use summary if available in RSS feed - Use summary if available in RSS feed
- Add tables and footnotes support in markdown - Add tables and footnotes support in markdown
- Add previous/previous_in_section/next/next_in_section/summary to `Page` - Add previous/previous_in_section/next/next_in_section/summary to `Page`
- Add more language syntaxes


## 0.0.3 (2017-04-05) ## 0.0.3 (2017-04-05)
- Add some colours in console - Add some colours in console


+ 42
- 0
src/markdown.rs View File

@@ -121,6 +121,9 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap<String, String>, ter
let mut in_code_block = false; let mut in_code_block = false;
// If we get text in header, we need to insert the id and a anchor // If we get text in header, we need to insert the id and a anchor
let mut in_header = false; 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 // the rendered html
let mut html = String::new(); let mut html = String::new();
let mut anchors: Vec<String> = vec![]; let mut anchors: Vec<String> = vec![];
@@ -207,6 +210,9 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap<String, String>, ter
} }


if in_header { if in_header {
if header_already_inserted {
return Event::Text(text);
}
let id = find_anchor(&anchors, slugify(&text), 0); let id = find_anchor(&anchors, slugify(&text), 0);
anchors.push(id.clone()); anchors.push(id.clone());
let anchor_link = if config.insert_anchor_links.unwrap() { let anchor_link = if config.insert_anchor_links.unwrap() {
@@ -216,6 +222,7 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap<String, String>, ter
} else { } else {
String::new() String::new()
}; };
header_already_inserted = true;
return Event::Html(Owned(format!(r#"id="{}">{}{}"#, id, anchor_link, text))); return Event::Html(Owned(format!(r#"id="{}">{}{}"#, id, anchor_link, text)));
} }


@@ -288,6 +295,7 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap<String, String>, ter
}, },
Event::End(Tag::Header(_)) => { Event::End(Tag::Header(_)) => {
in_header = false; in_header = false;
header_already_inserted = false;
event event
}, },
// If we added shortcodes, don't close a paragraph since there's none // 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(); let res = markdown_to_html("# Hello\n# Hello", &HashMap::new(), &GUTENBERG_TERA, &Config::default()).unwrap();
assert_eq!(res, "<h1 id=\"hello\">Hello</h1>\n<h1 id=\"hello-1\">Hello</h1>\n"); assert_eq!(res, "<h1 id=\"hello\">Hello</h1>\n<h1 id=\"hello-1\">Hello</h1>\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,
"<h1 id=\"hello\"><a class=\"anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">πŸ”—</a>\nHello</h1>\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,
"<h1 id=\"hello\"><a class=\"anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">πŸ”—</a>\nHello!</h1>\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,
"<h1 id=\"hello\"><a class=\"anchor\" href=\"#hello\" aria-label=\"Anchor link for: hello\">πŸ”—</a>\nHello*_()</h1>\n"
);
}
} }

+ 1
- 3
src/templates/anchor-link.html View File

@@ -1,3 +1 @@
<a class="anchor" href="#{{ id }}" aria-label="Anchor link for: {{ id }}">
πŸ”—
</a>
<a class="anchor" href="#{{ id }}" aria-label="Anchor link for: {{ id }}">πŸ”—</a>

Loading…
Cancel
Save