Hack fix and new tests for body-shortcodes
This commit is contained in:
parent
bfdfe3bba3
commit
e662f73438
|
@ -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
|
// Defaults to a 0 level so not a real header
|
||||||
// It should be an Option ideally but not worth the hassle to update
|
// It should be an Option ideally but not worth the hassle to update
|
||||||
let mut temp_header = TempHeader::default();
|
let mut temp_header = TempHeader::default();
|
||||||
|
let mut clear_shortcode_block = false;
|
||||||
|
|
||||||
let mut opts = Options::empty();
|
let mut opts = Options::empty();
|
||||||
opts.insert(OPTION_ENABLE_TABLES);
|
opts.insert(OPTION_ENABLE_TABLES);
|
||||||
opts.insert(OPTION_ENABLE_FOOTNOTES);
|
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) => {
|
Event::Text(mut text) => {
|
||||||
// Header first
|
// Header first
|
||||||
if in_header {
|
if in_header {
|
||||||
|
@ -129,7 +137,10 @@ pub fn markdown_to_html(content: &str, context: &Context) -> Result<(String, Vec
|
||||||
|
|
||||||
added_shortcode = true;
|
added_shortcode = true;
|
||||||
match render_simple_shortcode(context.tera, &name, &args) {
|
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) => {
|
Err(e) => {
|
||||||
error = Some(e);
|
error = Some(e);
|
||||||
return Event::Html(Owned(String::new()));
|
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 let Some(ref mut shortcode) = shortcode_block {
|
||||||
if text.trim() == "{% end %}" {
|
if text.trim() == "{% end %}" {
|
||||||
added_shortcode = true;
|
added_shortcode = true;
|
||||||
|
clear_shortcode_block = true;
|
||||||
match shortcode.render(context.tera) {
|
match shortcode.render(context.tera) {
|
||||||
Ok(s) => return Event::Html(Owned(format!("</p>{}", s))),
|
Ok(s) => return Event::Html(Owned(format!("</p>{}", s))),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
@ -271,7 +283,7 @@ pub fn markdown_to_html(content: &str, context: &Context) -> Result<(String, Vec
|
||||||
// println!("event = {:?}", event);
|
// println!("event = {:?}", event);
|
||||||
event
|
event
|
||||||
},
|
},
|
||||||
});
|
}});
|
||||||
|
|
||||||
cmark::html::push_html(&mut html, parser);
|
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 {
|
match error {
|
||||||
Some(e) => Err(e),
|
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))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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]
|
#[test]
|
||||||
fn can_render_several_shortcode_in_row() {
|
fn can_render_several_shortcode_in_row() {
|
||||||
let permalinks_ctx = HashMap::new();
|
let permalinks_ctx = HashMap::new();
|
||||||
|
|
Loading…
Reference in a new issue