「Bug」Missing line breaks if content is included in HTML tags
Closed this issue · 7 comments
Hello,missing line breaks if content is included in HTML tags, for example:
## test3
Line breaks are lost:
<div>
line one.
line two.
</div>
I want the line one.
and line two.
to be separate lines, not treated as one line.
I've tried using the nl2br extension, but it doesn't work, it only works on plain markdown content!
Now it's rendered like this:
But I want to render it this way, which is how almost all mainstream editors render it, like Github, Typora, etc:
Version: 3.7
nl2br
will work on any block being processed as markdown. With that said, Python Markdown does not parse block HTML content as Markdown by default. If you enable md_in_html
and apply the markdown
attribute to the block, then Python Markdown will process that content as Markdown, and only then will nl2br
inject the <br>
tag to preserve the new line.
import markdown
print(f'Markdown Version: {markdown.__version__}')
print('-----')
MD = """
## test3
Line breaks are lost:
<div>
line one.
line two.
</div>
"""
extensions = ["nl2br", "md_in_html"]
extension_configs = {}
print(markdown.markdown(MD, extensions=extensions, extension_configs=extension_configs))
Results:
Markdown Version: 3.7
-----
<h2>test3</h2>
<p>Line breaks are lost:</p>
<div>
<p>line one.<br />
line two.</p>
</div>
Yes, i enable md_in_html
and the nl2br extension. but it still doesn't work.
Moreover, I pasted your above code into VS Code for execution, and the result is different from yours, I took a screenshot to show you, is there something wrong with my version?
That's because I accidentally posted the wrong script. You'll notice this time it adds the markdown
attribute that I described earlier. Run this:
import markdown
print(f'Markdown Version: {markdown.__version__}')
print('-----')
MD = """
## test3
Line breaks are lost:
<div markdown>
line one.
line two.
</div>
"""
extensions = ["nl2br", "md_in_html"]
extension_configs = {}
print(markdown.markdown(MD, extensions=extensions, extension_configs=extension_configs))
Okay, got it.
In other words, to parse the line breaks of markdown in HTML, i have to add the markdown
attribute into the div
?
And have md_in_html
enabled.
Ok, solved, thanks.