mirror of
https://github.com/getzola/zola
synced 2024-11-10 14:24:27 +00:00
Fix titles with markdown chars and anchors
This commit is contained in:
parent
f3d9bea3df
commit
f35ca24893
3 changed files with 44 additions and 3 deletions
|
@ -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
|
||||||
|
|
|
@ -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 +1 @@
|
||||||
<a class="anchor" href="#{{ id }}" aria-label="Anchor link for: {{ id }}">
|
<a class="anchor" href="#{{ id }}" aria-label="Anchor link for: {{ id }}">🔗</a>
|
||||||
🔗
|
|
||||||
</a>
|
|
||||||
|
|
Loading…
Reference in a new issue