From 972aab1ac492aa29b2c23ad5958e71ad3729e0a9 Mon Sep 17 00:00:00 2001 From: Peng Guanwen Date: Sat, 5 Jan 2019 23:30:53 +0800 Subject: [PATCH] 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();