diff --git a/components/rendering/src/markdown.rs b/components/rendering/src/markdown.rs index e1834fac..b993dd70 100644 --- a/components/rendering/src/markdown.rs +++ b/components/rendering/src/markdown.rs @@ -70,13 +70,21 @@ pub fn markdown_to_html(content: &str, context: &Context) -> Result<(String, Vec // Defaults to a 0 level so not a real header // It should be an Option ideally but not worth the hassle to update let mut temp_header = TempHeader::default(); + let mut clear_shortcode_block = false; let mut opts = Options::empty(); opts.insert(OPTION_ENABLE_TABLES); opts.insert(OPTION_ENABLE_FOOTNOTES); { - let parser = Parser::new_ext(content, opts).map(|event| match event { + + let parser = Parser::new_ext(content, opts).map(|event| { + if clear_shortcode_block { + clear_shortcode_block = false; + shortcode_block = None; + } + + match event { Event::Text(mut text) => { // Header first if in_header { @@ -129,7 +137,10 @@ pub fn markdown_to_html(content: &str, context: &Context) -> Result<(String, Vec added_shortcode = true; match render_simple_shortcode(context.tera, &name, &args) { - Ok(s) => return Event::Html(Owned(format!("

{}", s))), + // Make before and after cleaning up of extra

/

tags more parallel. + // Or, in other words: + // TERRIBLE HORRIBLE NO GOOD VERY BAD HACK + Ok(s) => return Event::Html(Owned(format!("

{}

", s))), Err(e) => { error = Some(e); return Event::Html(Owned(String::new())); @@ -153,6 +164,7 @@ pub fn markdown_to_html(content: &str, context: &Context) -> Result<(String, Vec if let Some(ref mut shortcode) = shortcode_block { if text.trim() == "{% end %}" { added_shortcode = true; + clear_shortcode_block = true; match shortcode.render(context.tera) { Ok(s) => return Event::Html(Owned(format!("

{}", s))), Err(e) => { @@ -271,7 +283,7 @@ pub fn markdown_to_html(content: &str, context: &Context) -> Result<(String, Vec // println!("event = {:?}", event); event }, - }); + }}); cmark::html::push_html(&mut html, parser); } @@ -282,6 +294,6 @@ pub fn markdown_to_html(content: &str, context: &Context) -> Result<(String, Vec match error { Some(e) => Err(e), - None => Ok((html.replace("

", ""), make_table_of_contents(&headers))), + None => Ok((html.replace("

", "").replace("

", "

"), make_table_of_contents(&headers))), } } diff --git a/components/rendering/tests/markdown.rs b/components/rendering/tests/markdown.rs index e7687bc1..599e5d5d 100644 --- a/components/rendering/tests/markdown.rs +++ b/components/rendering/tests/markdown.rs @@ -137,6 +137,65 @@ fn can_render_body_shortcode_with_markdown_char_in_name() { } } +#[test] +fn can_render_body_shortcode_and_paragraph_after() { + let permalinks_ctx = HashMap::new(); + let mut tera = Tera::default(); + tera.extend(&GUTENBERG_TERA).unwrap(); + + let shortcode = "

{{ body }}

"; + let markdown_string = r#" +{% figure() %} +This is a figure caption. +{% end %} + +Here is another paragraph. +"#; + + let expected = "

This is a figure caption.

+

Here is another paragraph.

+"; + + tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap(); + let context = Context::new(&tera, true, "base16-ocean-dark".to_string(), "", &permalinks_ctx, InsertAnchor::None); + + let res = markdown_to_html(markdown_string, &context).unwrap(); + println!("{:?}", res); + assert_eq!(res.0, expected); +} + +#[test] +fn can_render_two_body_shortcode_and_paragraph_after_with_line_break_between() { + let permalinks_ctx = HashMap::new(); + let mut tera = Tera::default(); + tera.extend(&GUTENBERG_TERA).unwrap(); + + let shortcode = "

{{ body }}

"; + let markdown_string = r#" +{% figure() %} +This is a figure caption. +{% end %} + +{% figure() %} +This is a figure caption. +{% end %} + +Here is another paragraph. +"#; + + let expected = "

This is a figure caption.

+

This is a figure caption.

+

Here is another paragraph.

+"; + + tera.add_raw_template(&format!("shortcodes/{}.html", "figure"), shortcode).unwrap(); + let context = Context::new(&tera, true, "base16-ocean-dark".to_string(), "", &permalinks_ctx, InsertAnchor::None); + + let res = markdown_to_html(markdown_string, &context).unwrap(); + println!("{:?}", res); + assert_eq!(res.0, expected); +} + #[test] fn can_render_several_shortcode_in_row() { let permalinks_ctx = HashMap::new();