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
965 stars 53 forks source link

The number of newlines after a list alters output of `to_html` #144

Open martinhath opened 2 months ago

martinhath commented 2 months ago

This markdown:

- one
- two

Next line

should have the same output as this:

- one
- two

Next line

but it doesn't.

The output of the first snippet is (as expected)

<ul>
<li>one</li>
<li>two</li>
</ul>
<p>Next line</p>

but the output of the second snippet is

<ul>
<li>
<p>one</p>
</li>
<li>
<p>two</p>
</li>
</ul>
<p>Next line</p>

which is not expected. The <p> tags should probably not be there.

Here's a failing test

#[test]
fn two_blank_lines_inserts_paragraphs() {
    let out = to_html("- one\n- two\nNext line");
    assert!(!out.contains("<p>one</p>")); // Passes
    let out = to_html("- one\n- two\n\nNext line");
    assert!(!out.contains("<p>one</p>")); // Passes
    let out = to_html("- one\n- two\n\n\nNext line");
    assert!(!out.contains("<p>one</p>")); // Fails 💥
}
wooorm commented 2 months ago

looks like the loose/tight/spread algorithm is taking the line endings after the list/last item into account, where it should only look internally.