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

Fenced code blocks broken if containing empty lines #1473

Closed gynvael-hexarcana closed 1 month ago

gynvael-hexarcana commented 1 month ago

According to the documentation (and usual way markdown works) empty lines – including at the beginning and end – should work as expected. For example, the documentation shows this example:

Fenced code blocks can have a blank line as the first and/or last line of the code block and those lines will be preserved.


a three-line code block

However this doesn't seem to work in the current version of markdown library (tested on 3.6, though I think it's the same behavior on 3.5 and 3.4).

Example code:

import markdown

a = """

a three-line code block

"""

b = """

def func():

return

"""

print(markdown.markdown(a))
print("-" * 70)
print(markdown.markdown(b))

Expected output: (give or take)

<p><code>
a three-line code block

</code></p>
----------------------------------------------------------------------
<p><code>def func():

  return</code></p>

Actual output:

<p>```</p>
<p>a three-line code block</p>
<p>```</p>
----------------------------------------------------------------------
<p>```
def func():</p>
<p>return
```</p>

Note: A workaround is to enable SuperFences extension.

mitya57 commented 1 month ago

Triple-backtick codeblocks are not part of standard Markdown syntax. You need to enable the Fenced Code extension in order to use them.

Note: it does not matter whether there are empty lines or not.

gynvael-hexarcana commented 1 month ago

@mitya57 Thank you! I see I trolled myself testing this on lines like this:

``` One line Two lines ```

...which now I see is just interpreted as "single backtick opening" "single backtick closing", "single backtick opening".

Whoops ;) I'm closing the issue.