Python-Markdown / markdown

A Python implementation of John Gruber’s Markdown with Extension support.
https://python-markdown.github.io/
BSD 3-Clause "New" or "Revised" License
3.74k stars 858 forks source link

Multiple newlines break code formatting #1391

Closed mattiaverga closed 11 months ago

mattiaverga commented 11 months ago

Consider these examples:

>>> from markdown import markdown
>>> markdown('```\nsome\ncode\n```')
'<p><code>some\ncode</code></p>'
>>> markdown('```\n<some@email.me>\n```')
'<p><code>&lt;some@email.me&gt;</code></p>'

This is in line with other implementations, as I can see in Babelmark III.

Now this:

>>> from markdown import markdown
>>> markdown('```\nsome\n\ncode\n```')
'<p>```\nsome</p>\n<p>code\n```</p>'
>>> markdown('```\n<some@email.me>\n\n```')
'<p>```\n<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#115;&#111;&#109;&#101;&#64;&#101;&#109;&#97;&#105;&#108;&#46;&#109;&#101;">&#115;&#111;&#109;&#101;&#64;&#101;&#109;&#97;&#105;&#108;&#46;&#109;&#101;</a></p>\n<p>```</p>'

seems a bit unexpected, compared to other Babelmark implementations. To add more fun, the online babelmark tool shows that markdown 3.3.2 produces the correct output, but even if I try to pin the markdown version locally I always get the wrong output...

facelessuser commented 11 months ago

Python Markdown is an old school Markdown parser and fenced code blocks were not part of the spec when it was created. Many parsers these days have fenced code blocks by default, but Python Markdown implements them via extensions as it did originally. You can either use fenced_code or pymdownx.superfences. Personally, I'd recommend the latter only because it plays nice in nested constructs.

mattiaverga commented 11 months ago

Got it, thanks.