diff --git a/components/rendering/src/markdown.rs b/components/rendering/src/markdown.rs index a6ac28c..4773274 100644 --- a/components/rendering/src/markdown.rs +++ b/components/rendering/src/markdown.rs @@ -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 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("
".to_owned()));
+                        return Event::Html(Owned("
".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("
\n".to_owned())) + return Event::Html(Owned("
\n".to_string())) } // reset highlight and close the code block highlighter = None; - Event::Html(Owned("".to_owned())) + Event::Html(Owned("".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!("", link, title)); + } else { + temp_header.push(&format!("", 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(""); + return Event::Html(Owned(String::new())); } event - } + }, Event::Start(Tag::Code) => { if in_header { temp_header.push(""); diff --git a/components/rendering/tests/markdown.rs b/components/rendering/tests/markdown.rs index 2e547f3..3c66f02 100644 --- a/components/rendering/tests/markdown.rs +++ b/components/rendering/tests/markdown.rs @@ -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, - "

🔗\nXresources

\n" + "

🔗\nRust

\n" ); } @@ -448,3 +448,28 @@ fn can_understand_backtick_in_paragraphs() { "

Hello world

\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, + "

Rust

\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, + "

Rust

\n" + ); +}