Python markdown displays list item (li) as h2 heading when at end of text and followed by line break and - sign

Viewed 18

I am using the python markdown module to allow some markdown formatting of text input.

There is one case I can't figure out:

If I use a line break followed by one or more minus symbols "-", but no other symbols, the last line will be displayed as <h2> instead or additional to other styles. Happens at the end of unordered lists sometimes.

This is unexpected for me and I couldn't find something on it in the documentation. Is this correct?

import markdown
  

print(markdown.markdown("Why is this h2 and not p? \n-"))

# <h2>Why is this h2 and not p?</h2>
1 Answers

This is a setext heading.

A setext heading consists of one or more lines of text, not interrupted by a blank line, of which the first line does not have more than 3 spaces of indentation, followed by a setext heading underline. The lines of text must be such that, were they not followed by the setext heading underline, they would be interpreted as a paragraph: they cannot be interpretable as a code fence, ATX heading, block quote, thematic break, list item, or HTML block.

A setext heading underline is a sequence of = characters or a sequence of - characters, with no more than 3 spaces of indentation and any number of trailing spaces or tabs. If a line containing a single - can be interpreted as an empty list items, it should be interpreted this way and not as a setext heading underline.

The heading is a level 1 heading if = characters are used in the setext heading underline, and a level 2 heading if - characters are used. The contents of the heading are the result of parsing the preceding lines of text as CommonMark inline content.

Examples:

Foo *bar*
=========

Foo *bar*
---------
<h1>Foo <em>bar</em></h1>
<h2>Foo <em>bar</em></h2>
Foo *bar
baz*
====
<h1>Foo <em>bar
baz</em></h1>

And, as in your case, the underline can be any length (though SO's parser doesn't agree):

Foo
-------------------------

Foo
=
<h2>Foo</h2>
<h1>Foo</h1>
Related