From 2804b40875a249f05777c3d44e73eeaf42c941b6 Mon Sep 17 00:00:00 2001 From: Michael Plotke Date: Wed, 10 Apr 2019 13:31:33 -0400 Subject: [PATCH] strip wrapping whitespace from newline outward from shortcodes --- components/rendering/src/shortcode.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/components/rendering/src/shortcode.rs b/components/rendering/src/shortcode.rs index 6a47dafd..099eb95e 100644 --- a/components/rendering/src/shortcode.rs +++ b/components/rendering/src/shortcode.rs @@ -16,6 +16,7 @@ pub struct ContentParser; lazy_static! { static ref MULTIPLE_NEWLINE_RE: Regex = Regex::new(r"\n\s*\n").unwrap(); + static ref OUTER_NEWLINE_RE: Regex = Regex::new(r"^\s*\n|\n\s*$").unwrap(); } fn replace_string_markers(input: &str) -> String { @@ -122,6 +123,8 @@ fn render_shortcode( // at indentation, making the output a code block. let res = MULTIPLE_NEWLINE_RE.replace_all(&res, "\n"); + let res = OUTER_NEWLINE_RE.replace_all(&res, ""); + Ok(res.to_string()) } @@ -411,4 +414,20 @@ Some body {{ hello() }}{%/* end */%}"#, let res = render_shortcodes("Body\n {% youtube() %}\nHello \n World{% end %}", &tera); assert_eq!(res, "Body\n Hello \n World"); } + + #[test] + fn outer_newlines_removed_from_shortcodes_with_body() { + let mut tera = Tera::default(); + tera.add_raw_template("shortcodes/youtube.html", " \n {{body}} \n ").unwrap(); + let res = render_shortcodes("\n{% youtube() %} \n content \n {% end %}\n", &tera); + assert_eq!(res, "\n content \n"); + } + + #[test] + fn outer_newlines_removed_from_inline_shortcodes() { + let mut tera = Tera::default(); + tera.add_raw_template("shortcodes/youtube.html", " \n Hello, Zola. \n ").unwrap(); + let res = render_shortcodes("\n{{ youtube() }}\n", &tera); + assert_eq!(res, "\n Hello, Zola. \n"); + } }