lepture/mistune

Rendering code comment as heading in html inside markdown

toonarmycaptain opened this issue · 2 comments

We're embedding some pyscript inside out markdown, and having some trouble with code comments being rendered as headings:

It appears to be an issue within a custom tag where there is a blank line before the line prefaced with the #

To reproduce:

$ python3
Python 3.10.4 (main, Jun 29 2022, 12:14:53) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mistune
>>> mistune.__version__
'2.0.4'
>>> mistune.html(r'''<some-tag>
... 
... # a comment
... something with # a trailing comment
... </some-tag>
... 
... ''')
'<some-tag>\n<h1>a comment</h1>\n<p>something with # a trailing comment</p>\n</some-tag>\n'

What is expected:

The code/comments within the custom tags (in our case they're <py-script></py-script>) should not be interpreted as markdown (there weren't in a pre-2.x version of mistune), and produce:
<some-tag>\n<p># a comment</p>\n<p>something with # a trailing comment</p>\n</some-tag>\n

When there is no blank line preceeding the code comment, this behavior is not observed:

>>> mistune.html(r'''<some-tag>
... # a comment
... something with # a trailing comment
... </some-tag>''')
'<some-tag>\n# a comment\nsomething with # a trailing comment\n</some-tag>\n'

This is because mistune v2 is following commonmark's HTML rule, you can check the result in commonmark demo.

Put it into v3:

Adding another HTML parsing rule.