From 774514f4d4efc65e99a9108a6fc886e9747e567c Mon Sep 17 00:00:00 2001 From: Peng Guanwen Date: Sat, 5 Jan 2019 22:37:24 +0800 Subject: [PATCH 1/3] refactor markdown_to_html this commit contains two refactors: - extract custom link transformations into a function. - separate some trivial markup generation. --- components/rendering/src/markdown.rs | 111 +++++++++++++++------------ 1 file changed, 60 insertions(+), 51 deletions(-) diff --git a/components/rendering/src/markdown.rs b/components/rendering/src/markdown.rs index c21e58b..368901a 100644 --- a/components/rendering/src/markdown.rs +++ b/components/rendering/src/markdown.rs @@ -49,6 +49,56 @@ fn is_colocated_asset_link(link: &str) -> bool { && !link.starts_with("mailto:") } +fn fix_link(link: &str, context: &RenderContext) -> Result { + // A few situations here: + // - it could be a relative link (starting with `./`) + // - it could be a link to a co-located asset + // - it could be a normal link + // - any of those can be in a header or not: if it's in a header + // we need to append to a string + let result = if link.starts_with("./") { + match resolve_internal_link(&link, context.permalinks) { + Ok(url) => url, + Err(_) => { + return Err(format!("Relative link {} not found.", link).into()); + } + } + } else if is_colocated_asset_link(&link) { + format!("{}{}", context.current_page_permalink, link) + } else if context.config.check_external_links + && !link.starts_with('#') + && !link.starts_with("mailto:") { + let res = check_url(&link); + if res.is_valid() { + link.to_string() + } else { + return Err( + format!("Link {} is not valid: {}", link, res.message()).into(), + ); + } + } else { + link.to_string() + }; + Ok(result) +} + +/// returns true if event have been processed +fn push_to_temp_header(event: &Event, temp_header: &mut TempHeader) -> bool { + match event { + Event::End(Tag::Link(_, _)) => { + temp_header.add_html(""); + } + Event::Start(Tag::Code) => { + temp_header.add_html(""); + } + Event::End(Tag::Code) => { + temp_header.add_html(""); + } + _ => return false, + } + true +} + pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result { // the rendered html let mut html = String::with_capacity(content.len()); @@ -76,6 +126,11 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result { // Header first @@ -142,37 +197,12 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result { - // A few situations here: - // - it could be a relative link (starting with `./`) - // - it could be a link to a co-located asset - // - it could be a normal link - // - any of those can be in a header or not: if it's in a header - // we need to append to a string - let fixed_link = if link.starts_with("./") { - match resolve_internal_link(&link, context.permalinks) { - Ok(url) => url, - Err(_) => { - error = Some(format!("Relative link {} not found.", link).into()); - return Event::Html(Borrowed("")); - } - } - } else if is_colocated_asset_link(&link) { - format!("{}{}", context.current_page_permalink, link) - } else if context.config.check_external_links - && !link.starts_with('#') - && !link.starts_with("mailto:") - { - let res = check_url(&link); - if res.is_valid() { - link.to_string() - } else { - error = Some( - format!("Link {} is not valid: {}", link, res.message()).into(), - ); - String::new() + let fixed_link = match fix_link(&link, context) { + Ok(fixed_link) => fixed_link, + Err(err) => { + error = Some(err); + return Event::Html(Borrowed("")) } - } else { - link.to_string() }; if in_header { @@ -187,27 +217,6 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result { - if in_header { - temp_header.add_html(""); - return Event::Html(Borrowed("")); - } - event - } - Event::Start(Tag::Code) => { - if in_header { - temp_header.add_html(""); - return Event::Html(Borrowed("")); - } - event - } - Event::End(Tag::Code) => { - if in_header { - temp_header.add_html(""); - return Event::Html(Borrowed("")); - } - event - } Event::Start(Tag::Header(num)) => { in_header = true; temp_header = TempHeader::new(num); From 972aab1ac492aa29b2c23ad5958e71ad3729e0a9 Mon Sep 17 00:00:00 2001 From: Peng Guanwen Date: Sat, 5 Jan 2019 23:30:53 +0800 Subject: [PATCH 2/3] Add emphasis, strong and code support in header --- components/rendering/src/markdown.rs | 38 ++++++++++++++++++-------- components/rendering/tests/markdown.rs | 27 ++++++++++++++++++ 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/components/rendering/src/markdown.rs b/components/rendering/src/markdown.rs index 368901a..926d0c9 100644 --- a/components/rendering/src/markdown.rs +++ b/components/rendering/src/markdown.rs @@ -82,21 +82,35 @@ fn fix_link(link: &str, context: &RenderContext) -> Result { Ok(result) } +fn start_tag(temp_header: &mut TempHeader, tag: &Tag) -> bool { + match tag { + Tag::Emphasis => temp_header.add_html(""), + Tag::Strong => temp_header.add_html(""), + Tag::Code => temp_header.add_html(""), + // Tag::Link is handled elsewhere + _ => return false, + } + true +} + +fn end_tag(temp_header: &mut TempHeader, tag: &Tag) -> bool { + match tag { + Tag::Emphasis => temp_header.add_html(""), + Tag::Strong => temp_header.add_html(""), + Tag::Code => temp_header.add_html(""), + Tag::Link(_, _) => temp_header.add_html(""), + _ => return false, + } + true +} + /// returns true if event have been processed fn push_to_temp_header(event: &Event, temp_header: &mut TempHeader) -> bool { match event { - Event::End(Tag::Link(_, _)) => { - temp_header.add_html(""); - } - Event::Start(Tag::Code) => { - temp_header.add_html(""); - } - Event::End(Tag::Code) => { - temp_header.add_html(""); - } - _ => return false, + Event::Start(tag) => start_tag(temp_header, tag), + Event::End(tag) => end_tag(temp_header, tag), + _ => false, } - true } pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result { @@ -126,7 +140,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> ResultEmphasis text\n") +} + +#[test] +fn can_understand_strong_in_header() { + let permalinks_ctx = HashMap::new(); + let config = Config::default(); + let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None); + let res = render_content("# **Strong** text", &context).unwrap(); + assert_eq!(res.body, "

Strong text

\n") +} + +#[test] +fn can_understand_code_in_header() { + let permalinks_ctx = HashMap::new(); + let config = Config::default(); + let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None); + let res = render_content("# `Code` text", &context).unwrap(); + assert_eq!(res.body, "

Code text

\n") +} + #[test] fn can_make_valid_relative_link_in_header() { let mut permalinks = HashMap::new(); From 7130616f630646074e40f45c55a3665297da002b Mon Sep 17 00:00:00 2001 From: Peng Guanwen Date: Sun, 6 Jan 2019 19:04:53 +0800 Subject: [PATCH 3/3] Minor fixes --- components/rendering/src/markdown.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/components/rendering/src/markdown.rs b/components/rendering/src/markdown.rs index 926d0c9..6b79077 100644 --- a/components/rendering/src/markdown.rs +++ b/components/rendering/src/markdown.rs @@ -54,8 +54,6 @@ fn fix_link(link: &str, context: &RenderContext) -> Result { // - it could be a relative link (starting with `./`) // - it could be a link to a co-located asset // - it could be a normal link - // - any of those can be in a header or not: if it's in a header - // we need to append to a string let result = if link.starts_with("./") { match resolve_internal_link(&link, context.permalinks) { Ok(url) => url, @@ -82,18 +80,18 @@ fn fix_link(link: &str, context: &RenderContext) -> Result { Ok(result) } -fn start_tag(temp_header: &mut TempHeader, tag: &Tag) -> bool { +fn push_start_tag(temp_header: &mut TempHeader, tag: &Tag) -> bool { match tag { Tag::Emphasis => temp_header.add_html(""), Tag::Strong => temp_header.add_html(""), Tag::Code => temp_header.add_html(""), - // Tag::Link is handled elsewhere + // Tag::Link is handled in `markdown_to_html` _ => return false, } true } -fn end_tag(temp_header: &mut TempHeader, tag: &Tag) -> bool { +fn push_end_tag(temp_header: &mut TempHeader, tag: &Tag) -> bool { match tag { Tag::Emphasis => temp_header.add_html(""), Tag::Strong => temp_header.add_html(""), @@ -107,8 +105,8 @@ fn end_tag(temp_header: &mut TempHeader, tag: &Tag) -> bool { /// returns true if event have been processed fn push_to_temp_header(event: &Event, temp_header: &mut TempHeader) -> bool { match event { - Event::Start(tag) => start_tag(temp_header, tag), - Event::End(tag) => end_tag(temp_header, tag), + Event::Start(tag) => push_start_tag(temp_header, tag), + Event::End(tag) => push_end_tag(temp_header, tag), _ => false, } } @@ -140,7 +138,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result