fix(lists): codeblocks inside lists are now correctly parsed

Closes #494
This commit is contained in:
Estevao Soares dos Santos 2022-02-25 01:06:09 +00:00
parent 5e0ed809db
commit 8cecdf0382
10 changed files with 99 additions and 9 deletions

BIN
dist/showdown.js vendored

Binary file not shown.

BIN
dist/showdown.js.map vendored

Binary file not shown.

BIN
dist/showdown.min.js vendored

Binary file not shown.

Binary file not shown.

View File

@ -87,14 +87,14 @@ showdown.subParser('makehtml.lists', function (text, options, globals) {
return '¨A' + wm2;
});
// SPECIAL CASE: an heading followed by a paragraph of text that is not separated by a double newline
// SPECIAL CASE: a heading followed by a paragraph of text that is not separated by a double newline
// or/nor indented. ex:
//
// - # foo
// bar is great
//
// While this does now follow the spec per se, not allowing for this might cause confusion since
// header blocks don't need double newlines after
// header blocks don't need double-newlines after
if (/^#+.+\n.+/.test(item)) {
item = item.replace(/^(#+.+)$/m, '$1\n');
}
@ -103,7 +103,47 @@ showdown.subParser('makehtml.lists', function (text, options, globals) {
// Has a double return (multi paragraph)
if (m1 || (item.search(/\n{2,}/) > -1)) {
item = showdown.subParser('makehtml.githubCodeBlocks')(item, options, globals);
item = showdown.subParser('makehtml.blockGamut')(item, options, globals);
item = showdown.subParser('makehtml.blockQuotes')(item, options, globals);
item = showdown.subParser('makehtml.headers')(item, options, globals);
item = showdown.subParser('makehtml.lists')(item, options, globals);
item = showdown.subParser('makehtml.codeBlocks')(item, options, globals);
item = showdown.subParser('makehtml.tables')(item, options, globals);
item = showdown.subParser('makehtml.hashHTMLBlocks')(item, options, globals);
//item = showdown.subParser('makehtml.paragraphs')(item, options, globals);
// TODO: This is a copy of the paragraph parser
// This is a provisory fix for issue #494
// For a permanente fix we need to rewrite the paragraph parser, passing the unhashify logic outside
// so that we can call the paragraph parser without accidently unashifying previously parsed blocks
// Strip leading and trailing lines:
item = item.replace(/^\n+/g, '');
item = item.replace(/\n+$/g, '');
var grafs = item.split(/\n{2,}/g),
grafsOut = [],
end = grafs.length; // Wrap <p> tags
for (var i = 0; i < end; i++) {
var str = grafs[i];
// if this is an HTML marker, copy it
if (str.search(/¨([KG])(\d+)\1/g) >= 0) {
grafsOut.push(str);
// test for presence of characters to prevent empty lines being parsed
// as paragraphs (resulting in undesired extra empty paragraphs)
} else if (str.search(/\S/) >= 0) {
str = showdown.subParser('makehtml.spanGamut')(str, options, globals);
str = str.replace(/^([ \t]*)/g, '<p>');
str += '</p>';
grafsOut.push(str);
}
}
item = grafsOut.join('\n');
// Strip leading and trailing lines:
item = item.replace(/^\n+/g, '');
item = item.replace(/\n+$/g, '');
} else {
// Recursion for sub-lists:

View File

@ -264,14 +264,17 @@ This is a second.
</code></pre></li>
<li><p>Blue</p></li>
<li><p>Red</p>
<pre><code>* Green<pre><code>Try this code:
<pre><code>* Green
This is an embedded code block.
Try this code:
Then this:
This is an embedded code block.
More code!
</code></pre>* Blue
Then this:
More code!
* Blue
* Red
</code></pre></li>
</ul>
@ -365,4 +368,4 @@ This is a code block, fenced-style
<hr />
<hr />
<hr />
<hr />
<hr />

View File

@ -0,0 +1,10 @@
<pre><code>public static void main(String[] args) {
for (int i = 0; i &lt; 10 &amp;&amp; true; i++) {
System.out.println("Hello World");
}
// stuff here is affected as well &lt;&gt;&amp;&amp;%
}
</code></pre>

View File

@ -0,0 +1,11 @@
```
public static void main(String[] args) {
for (int i = 0; i < 10 && true; i++) {
System.out.println("Hello World");
}
// stuff here is affected as well <>&&%
}
```

View File

@ -0,0 +1,13 @@
<ol>
<li><p>Code block as part of list</p>
<pre><code>public static void main(String[] args) {
for (int i = 0; i &lt; 10 &amp;&amp; true; i++) {
System.out.println("Hello World");
}
// stuff here is affected as well &lt;&gt;&amp;&amp;%
}
</code></pre></li>
</ol>

View File

@ -0,0 +1,13 @@
1. Code block as part of list
```
public static void main(String[] args) {
for (int i = 0; i < 10 && true; i++) {
System.out.println("Hello World");
}
// stuff here is affected as well <>&&%
}
```