Hack fix and new tests for body-shortcodes

This commit is contained in:
Reilly Tucker Siemens 2017-10-29 12:55:36 -07:00 committed by Geoff Shannon
parent bfdfe3bba3
commit e662f73438
2 changed files with 75 additions and 4 deletions

View file

@ -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!("</p>{}", s))),
// Make before and after cleaning up of extra <p> / </p> tags more parallel.
// Or, in other words:
// TERRIBLE HORRIBLE NO GOOD VERY BAD HACK
Ok(s) => return Event::Html(Owned(format!("</p>{}<p>", 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!("</p>{}", 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("<p></p>", ""), make_table_of_contents(&headers))),
None => Ok((html.replace("<p></p>", "").replace("</p></p>", "</p>"), make_table_of_contents(&headers))),
}
}

View file

@ -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 = "<p>{{ body }}</p>";
let markdown_string = r#"
{% figure() %}
This is a figure caption.
{% end %}
Here is another paragraph.
"#;
let expected = "<p>This is a figure caption.</p>
<p>Here is another paragraph.</p>
";
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 = "<p>{{ body }}</p>";
let markdown_string = r#"
{% figure() %}
This is a figure caption.
{% end %}
{% figure() %}
This is a figure caption.
{% end %}
Here is another paragraph.
"#;
let expected = "<p>This is a figure caption.</p>
<p>This is a figure caption.</p>
<p>Here is another paragraph.</p>
";
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();