Browse Source

strip wrapping whitespace from newline outward from shortcodes

index-subcmd
Michael Plotke 5 years ago
parent
commit
2804b40875
1 changed files with 19 additions and 0 deletions
  1. +19
    -0
      components/rendering/src/shortcode.rs

+ 19
- 0
components/rendering/src/shortcode.rs View File

@@ -16,6 +16,7 @@ pub struct ContentParser;


lazy_static! { lazy_static! {
static ref MULTIPLE_NEWLINE_RE: Regex = Regex::new(r"\n\s*\n").unwrap(); 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 { fn replace_string_markers(input: &str) -> String {
@@ -122,6 +123,8 @@ fn render_shortcode(
// at indentation, making the output a code block. // at indentation, making the output a code block.
let res = MULTIPLE_NEWLINE_RE.replace_all(&res, "\n"); let res = MULTIPLE_NEWLINE_RE.replace_all(&res, "\n");


let res = OUTER_NEWLINE_RE.replace_all(&res, "");

Ok(res.to_string()) Ok(res.to_string())
} }


@@ -411,4 +414,20 @@ Some body {{ hello() }}{%/* end */%}"#,
let res = render_shortcodes("Body\n {% youtube() %}\nHello \n World{% end %}", &tera); let res = render_shortcodes("Body\n {% youtube() %}\nHello \n World{% end %}", &tera);
assert_eq!(res, "Body\n Hello \n World"); 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");
}
} }

Loading…
Cancel
Save