Allow links in headers
This commit is contained in:
parent
85e13483eb
commit
d39edd8ecb
|
@ -67,10 +67,10 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
|
|||
let id = find_anchor(&anchors, slugify(&text), 0);
|
||||
anchors.push(id.clone());
|
||||
// update the header and add it to the list
|
||||
temp_header.id = id.clone();
|
||||
temp_header.permalink = format!("{}#{}", context.current_page_permalink, id);
|
||||
temp_header.id = id;
|
||||
// += as we might have some <code> or other things already there
|
||||
temp_header.title += &text;
|
||||
temp_header.permalink = format!("{}#{}", context.current_page_permalink, id);
|
||||
header_created = true;
|
||||
return Event::Html(Owned(String::new()));
|
||||
}
|
||||
|
@ -87,8 +87,9 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
|
|||
},
|
||||
Event::Start(Tag::CodeBlock(ref info)) => {
|
||||
if !context.config.highlight_code {
|
||||
return Event::Html(Owned("<pre><code>".to_owned()));
|
||||
return Event::Html(Owned("<pre><code>".to_string()));
|
||||
}
|
||||
|
||||
let theme = &THEME_SET.themes[&context.config.highlight_theme];
|
||||
highlighter = Some(get_highlighter(&theme, info));
|
||||
let snippet = start_coloured_html_snippet(theme);
|
||||
|
@ -96,17 +97,23 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
|
|||
},
|
||||
Event::End(Tag::CodeBlock(_)) => {
|
||||
if !context.config.highlight_code {
|
||||
return Event::Html(Owned("</code></pre>\n".to_owned()))
|
||||
return Event::Html(Owned("</code></pre>\n".to_string()))
|
||||
}
|
||||
// reset highlight and close the code block
|
||||
highlighter = None;
|
||||
Event::Html(Owned("</pre>".to_owned()))
|
||||
Event::Html(Owned("</pre>".to_string()))
|
||||
},
|
||||
// Need to handle relative links
|
||||
Event::Start(Tag::Link(ref link, ref title)) => {
|
||||
if in_header {
|
||||
return Event::Html(Owned("".to_owned()));
|
||||
if !title.is_empty() {
|
||||
temp_header.push(&format!("<a href=\"{}\" title=\"{}\">", link, title));
|
||||
} else {
|
||||
temp_header.push(&format!("<a href=\"{}\">", link));
|
||||
}
|
||||
return Event::Html(Owned(String::new()));
|
||||
}
|
||||
|
||||
if link.starts_with("./") {
|
||||
match resolve_internal_link(link, context.permalinks) {
|
||||
Ok(url) => {
|
||||
|
@ -114,7 +121,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
|
|||
},
|
||||
Err(_) => {
|
||||
error = Some(format!("Relative link {} not found.", link).into());
|
||||
return Event::Html(Owned("".to_string()));
|
||||
return Event::Html(Owned(String::new()));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -123,10 +130,11 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
|
|||
},
|
||||
Event::End(Tag::Link(_, _)) => {
|
||||
if in_header {
|
||||
return Event::Html(Owned("".to_owned()));
|
||||
temp_header.push("</a>");
|
||||
return Event::Html(Owned(String::new()));
|
||||
}
|
||||
event
|
||||
}
|
||||
},
|
||||
Event::Start(Tag::Code) => {
|
||||
if in_header {
|
||||
temp_header.push("<code>");
|
||||
|
|
|
@ -377,10 +377,10 @@ fn can_insert_anchor_with_link() {
|
|||
let permalinks_ctx = HashMap::new();
|
||||
let config = Config::default();
|
||||
let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, InsertAnchor::Left);
|
||||
let res = render_content("## [](#xresources)Xresources", &context).unwrap();
|
||||
let res = render_content("## [Rust](https://rust-lang.org)", &context).unwrap();
|
||||
assert_eq!(
|
||||
res.0,
|
||||
"<h2 id=\"xresources\"><a class=\"gutenberg-anchor\" href=\"#xresources\" aria-label=\"Anchor link for: xresources\">🔗</a>\nXresources</h2>\n"
|
||||
"<h2 id=\"rust\"><a class=\"gutenberg-anchor\" href=\"#rust\" aria-label=\"Anchor link for: rust\">🔗</a>\n<a href=\"https://rust-lang.org\">Rust</a></h2>\n"
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -448,3 +448,28 @@ fn can_understand_backtick_in_paragraphs() {
|
|||
"<p>Hello <code>world</code></p>\n"
|
||||
);
|
||||
}
|
||||
|
||||
// https://github.com/Keats/gutenberg/issues/297
|
||||
#[test]
|
||||
fn can_understand_links_in_header() {
|
||||
let permalinks_ctx = HashMap::new();
|
||||
let config = Config::default();
|
||||
let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
|
||||
let res = render_content("# [Rust](https://rust-lang.org)", &context).unwrap();
|
||||
assert_eq!(
|
||||
res.0,
|
||||
"<h1 id=\"rust\"><a href=\"https://rust-lang.org\">Rust</a></h1>\n"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_understand_link_with_title_in_header() {
|
||||
let permalinks_ctx = HashMap::new();
|
||||
let config = Config::default();
|
||||
let context = RenderContext::new(&GUTENBERG_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
|
||||
let res = render_content("# [Rust](https://rust-lang.org \"Rust homepage\")", &context).unwrap();
|
||||
assert_eq!(
|
||||
res.0,
|
||||
"<h1 id=\"rust\"><a href=\"https://rust-lang.org\" title=\"Rust homepage\">Rust</a></h1>\n"
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue