Marked: Creates invalid HTML

Created on 26 May 2018  路  7Comments  路  Source: markedjs/marked

Describe the bug

marked('<h2>foo bar</h2><pre>foo\n\nbar\n</pre>')
"<h2>foo bar</h2><pre>foo

<p>bar
</pre></p>
"


Expected behavior
return this:

"<h2>foo bar</h2><pre>foo

bar
</pre>
"

All 7 comments

What version of marked are you using.

It seems to be working correctly in the demo

I just grabbed the latrst from github

Am 28. Mai 2018 00:30:24 MESZ schrieb Tony Brix notifications@github.com:

What version of marked are you using.

It seems to be working correctly in the
demo

--
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
https://github.com/markedjs/marked/issues/1277#issuecomment-392381820

@Sorunome GitHub is not a good place to grab code since the code is always changing and might break.

Try the latest release v0.4.0 and see if that works for you: https://cdn.jsdelivr.net/npm/[email protected]/marked.min.js

Nope, still doesn't work for me. I didn't think may be related, but yeah i'm also using this:

        var markedRenderer = new marked.Renderer();
        markedRenderer.code = function(code, lang) {
            return '<pre>'+code+'</pre>';
        };
        markedRenderer.heading = function(text, level, raw) {
            level++; // we don't allow h1, so we actually do h2....whatever
            return '<h' + level + '>' + text + '</h' + level + '>\n';
        };
        marked.setOptions({
            renderer: markedRenderer,
            headerIds: false,
        });

I tried from the command line and was able to reproduce.

var str = '<h2>foo bar</h2><pre>foo\n\nbar\n</pre>';
marked(str);
/*
"<h2>foo bar</h2><pre>foo

<p>bar
</pre></p>
"
*/

The p tag is wrapping the pre for some strange reason.

This is actually valid output according to the CommonMark spec:

Your first line is parsed as an HTML block (see Condition # 6 in the spec). The end condition for it is:

line is followed by a blank line.

which is exacly what we have here. Thus the following text bar\n</pre> is treated as a paragraph and is wrapped in p tag

Commonmark playground:
https://spec.commonmark.org/dingus/?text=%3Ca%20%20%2F%3E%3Cb2%0Adata%3D%22foo%22%20%3E%0A

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eGavr picture eGavr  路  4Comments

bennycode picture bennycode  路  4Comments

priyesh-diukar picture priyesh-diukar  路  3Comments

gclove picture gclove  路  4Comments

UziTech picture UziTech  路  4Comments