Skip to content

Commit 799abea

Browse files
committed
fix(parser): fix issue with comments inside nested code blocks
Code blocks containing comments are now converted correctly when nested in list items. Closes #288
1 parent 5d2016c commit 799abea

13 files changed

+105
-13
lines changed

dist/showdown.js

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/subParsers/hashHTMLBlocks.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ showdown.subParser('hashHTMLBlocks', function (text, options, globals) {
5252
}
5353

5454
// HR SPECIAL CASE
55-
text = text.replace(/(\n[ ]{0,3}(<(hr)\b([^<>])*?\/?>)[ \t]*(?=\n{2,}))/g,
55+
text = text.replace(/(\n {0,3}(<(hr)\b([^<>])*?\/?>)[ \t]*(?=\n{2,}))/g,
5656
showdown.subParser('hashElement')(text, options, globals));
5757

58-
// Special case for standalone HTML comments:
59-
text = text.replace(/(<!--[\s\S]*?-->)/g,
60-
showdown.subParser('hashElement')(text, options, globals));
58+
// Special case for standalone HTML comments
59+
text = showdown.helper.replaceRecursiveRegExp(text, function (txt) {
60+
return '\n\n~K' + (globals.gHtmlBlocks.push(txt) - 1) + 'K\n\n';
61+
}, '^(?: |\\t){0,3}<!--', '-->', 'gm');
6162

6263
// PHP and ASP-style processor instructions (<?...?> and <%...%>)
6364
text = text.replace(/(?:\n\n)([ ]{0,3}(?:<([?%])[^\r]*?\2>)[ \t]*(?=\n{2,}))/g,

test/cases/html-comments.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!-- a comment -->
2+
3+
<!-- a comment with *bogus* __markdown__ inside -->
4+
5+
<p>words <!-- a comment --> words</p>
6+
7+
<!-- comment -->
8+
9+
<p>words</p>
10+
11+
<!-- comment -->
12+
13+
<pre><code>&lt;!-- comment --&gt;
14+
</code></pre>

test/cases/html-comments.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!-- a comment -->
2+
3+
<!-- a comment with *bogus* __markdown__ inside -->
4+
5+
words <!-- a comment --> words
6+
7+
<!-- comment --> words
8+
9+
<!-- comment -->
10+
11+
<!-- comment -->
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<ul>
2+
<li><p>list item 1</p>
3+
4+
<pre><code class="html language-html">&lt;a href="www.google.com"&gt;google&lt;/a&gt;
5+
&lt;div&gt;
6+
&lt;div&gt;some div&lt;/div&gt;
7+
&lt;/div&gt;
8+
</code></pre></li>
9+
</ul>

test/cases/html-inside-listed-code.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- list item 1
2+
3+
```html
4+
<a href="www.google.com">google</a>
5+
<div>
6+
<div>some div</div>
7+
</div>
8+
```

test/cases/line-starts-with-html.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p><a href="foo">some text</a> words</p>
2+
3+
<p><br> words</p>

0 commit comments

Comments
 (0)