From 3eb571fdbf08ea2b15e96768840d109dfdd2c297 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Sat, 19 May 2018 12:12:02 +0200 Subject: [PATCH] Trim left every line of a shortcode to avoid the accidental markdown codeblock --- components/rendering/src/shortcode.rs | 8 ++++++- components/rendering/tests/markdown.rs | 22 +++++++++++++++++++ .../documentation/content/shortcodes.md | 3 +++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/components/rendering/src/shortcode.rs b/components/rendering/src/shortcode.rs index 6ccc12f8..e88e8361 100644 --- a/components/rendering/src/shortcode.rs +++ b/components/rendering/src/shortcode.rs @@ -96,7 +96,13 @@ fn render_shortcode(name: String, args: Map, tera: &Tera, config: context.insert("config", config); let tpl_name = format!("shortcodes/{}.html", name); - tera.render(&tpl_name, &context).chain_err(|| format!("Failed to render {} shortcode", name)) + let res = tera + .render(&tpl_name, &context) + .chain_err(|| format!("Failed to render {} shortcode", name))?; + + // We trim left every single line of a shortcode to avoid the accidental + // shortcode counted as code block because of 4 spaces left padding + Ok(res.lines().map(|s| s.trim_left()).collect()) } pub fn render_shortcodes(content: &str, tera: &Tera, config: &Config) -> Result { diff --git a/components/rendering/tests/markdown.rs b/components/rendering/tests/markdown.rs index 2e77b21e..eda40362 100644 --- a/components/rendering/tests/markdown.rs +++ b/components/rendering/tests/markdown.rs @@ -515,3 +515,25 @@ fn can_make_permalinks_with_colocated_assets_for_image() { "

\"alt

\n" ); } + +#[test] +fn markdown_doesnt_wrap_html_in_paragraph() { + let permalinks_ctx = HashMap::new(); + let config = Config::default(); + let context = RenderContext::new(&GUTENBERG_TERA, &config, "https://vincent.is/about/", &permalinks_ctx, InsertAnchor::None); + let res = render_content(r#" +Some text + +

Helo

+ +
+ + MobX flow + +
+ "#, &context).unwrap(); + assert_eq!( + res.0, + "

Some text

\n

Helo

\n
\n\n \"MobX\n \n
\n" + ); +} diff --git a/docs/content/documentation/content/shortcodes.md b/docs/content/documentation/content/shortcodes.md index 8b878a0b..584975f7 100644 --- a/docs/content/documentation/content/shortcodes.md +++ b/docs/content/documentation/content/shortcodes.md @@ -32,6 +32,9 @@ are in a `if` statement, we can assume they are optional. That's it, Gutenberg will now recognise this template as a shortcode named `youtube` (the filename minus the `.html` extension). +The markdown renderer will wrap an inline HTML node like `` or `` into a paragraph. If you want to disable that, +simply wrap your shortcode in a `div`. + ## Using shortcodes There are two kinds of shortcodes: