Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid HTML produced when using <pre> tag in html mode #951

Closed
pilotmoon opened this issue Aug 11, 2023 · 4 comments
Closed

Invalid HTML produced when using <pre> tag in html mode #951

pilotmoon opened this issue Aug 11, 2023 · 4 comments

Comments

@pilotmoon
Copy link

pilotmoon commented Aug 11, 2023

When <pre> is used in the input in a certain way, and html mode is on, then markdown-it can produce invalid HTML output:

Demo

Input:

<h1>Heading</h1>
<pre>
First line of text

Second line of text
</pre>

Output:

<h1>Heading</h1>
<pre>
First line of text
<p>Second line of text
</pre></p>

(Note that the <p> tag is erroneously inserted around second line of text and then </pre> is inserted before closing </p> tag, violating nesting rule.)

I checked and I noted that GitHub's markdown implementation handles the same input OK .

@kliu57
Copy link
Contributor

kliu57 commented Nov 17, 2023

I would like to work on this.

@kliu57
Copy link
Contributor

kliu57 commented Nov 17, 2023

@puzrin @pilotmoon

I looked into this issue, and I found that although this behavior does not match the GitHub implementation, this is actually expected behavior according to commonmark specs.

commonmark specs

However, if you wish for the code to not follow the commonmark specs behavior, I have a proposed fix:

Add pre to the list of valid html block names in lib/common/html_blocks.js, and the result will then match the GitHub implementation behavior. I have tested this and can provide screenshots of results.

Please let me know what behavior is desired.

@pilotmoon
Copy link
Author

pilotmoon commented Nov 17, 2023

Thank you for the commonmark reference. What a strange spec! I assume there must be a reason for this decision.

In my case, I have worked around the problem by pre-treating the markdown input as follows:

  html = html.replace(/<pre/g, "\n<pre");  

By inserting a blank before the pre tag, it terminates any initial HTML block preceding it. Thus allowing the <pre block to be terminated by its own end tag instead of the blank line inside.

I shall also now add script, style and textarea to this, for completeness.

@puzrin
Copy link
Member

puzrin commented Nov 18, 2023

As written above - works as spec says. Details about spec desitions can be seen on commonmark forum.

@puzrin puzrin closed this as completed Nov 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants