wooorm / markdown-rs

CommonMark compliant markdown parser in Rust with ASTs and extensions
https://docs.rs/markdown/1.0.0-alpha.21/markdown/
MIT License
966 stars 53 forks source link

HTML output of input with frontmatter strip first newline in first paragraph #143

Open martinhath opened 2 months ago

martinhath commented 2 months ago

I have markdown files with frontmatter that I convert to HTML using to_html_with_options. If the first item after the frontmatter is a paragraph, there's a missing newline in the HTML output.

Here's a failing test to show what I mean:

#[test]
fn to_html() {
    let opt: Options = Default::default();
    let opt_with_frontmatter = Options {
        parse: ParseOptions {
            constructs: Constructs {
                frontmatter: true,
                ..Default::default()
            },
            ..Default::default()
        },
        compile: Default::default(),
    };

    let input = "one.\ntwo.\nthree.\nfour.";
    let output = to_html_with_options(&input, &opt).unwrap();
    assert_eq!(output, "<p>one.\ntwo.\nthree.\nfour.</p>"); // No frontmatter. This passes.

    let output = to_html_with_options(&input, &opt_with_frontmatter).unwrap();
    assert_eq!(output, "<p>one.\ntwo.\nthree.\nfour.</p>"); // Frontmatter in config but not in the markdown. This passes.

    let input = "---\nnumber: 0\n---\none.\ntwo.\nthree.\nfour.";
    let output = to_html_with_options(&input, &opt_with_frontmatter).unwrap();
    assert_eq!(output, "<p>one.\ntwo.\nthree.\nfour.</p>"); // This fails 💥
}

which outputs

---- test::to_html stdout ----
thread 'test::to_html' panicked at src/lib.rs:190:9:
assertion `left == right` failed
  left: "<p>one.two.\nthree.\nfour.</p>"
 right: "<p>one.\ntwo.\nthree.\nfour.</p>"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The test was ran on 4ad5342156cf5f5256fc452c4583be99a3e80412.

wooorm commented 2 months ago

Interesting, PR welcome!