Skip to content

Commit

Permalink
fix(parser): fix issue with comments inside nested code blocks
Browse files Browse the repository at this point in the history
Code blocks containing comments are now converted correctly when nested in list items.

Closes #288
  • Loading branch information
tivie committed Aug 30, 2016
1 parent 5d2016c commit 799abea
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 13 deletions.
11 changes: 6 additions & 5 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/subParsers/hashHTMLBlocks.js
Expand Up @@ -52,12 +52,13 @@ showdown.subParser('hashHTMLBlocks', function (text, options, globals) {
}

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

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

// PHP and ASP-style processor instructions (<?...?> and <%...%>)
text = text.replace(/(?:\n\n)([ ]{0,3}(?:<([?%])[^\r]*?\2>)[ \t]*(?=\n{2,}))/g,
Expand Down
14 changes: 14 additions & 0 deletions test/cases/html-comments.html
@@ -0,0 +1,14 @@
<!-- a comment -->

<!-- a comment with *bogus* __markdown__ inside -->

<p>words <!-- a comment --> words</p>

<!-- comment -->

<p>words</p>

<!-- comment -->

<pre><code>&lt;!-- comment --&gt;
</code></pre>
11 changes: 11 additions & 0 deletions test/cases/html-comments.md
@@ -0,0 +1,11 @@
<!-- a comment -->

<!-- a comment with *bogus* __markdown__ inside -->

words <!-- a comment --> words

<!-- comment --> words

<!-- comment -->

<!-- comment -->
9 changes: 9 additions & 0 deletions test/cases/html-inside-listed-code.html
@@ -0,0 +1,9 @@
<ul>
<li><p>list item 1</p>

<pre><code class="html language-html">&lt;a href="www.google.com"&gt;google&lt;/a&gt;
&lt;div&gt;
&lt;div&gt;some div&lt;/div&gt;
&lt;/div&gt;
</code></pre></li>
</ul>
8 changes: 8 additions & 0 deletions test/cases/html-inside-listed-code.md
@@ -0,0 +1,8 @@
- list item 1

```html
<a href="www.google.com">google</a>
<div>
<div>some div</div>
</div>
```
3 changes: 3 additions & 0 deletions test/cases/line-starts-with-html.html
@@ -0,0 +1,3 @@
<p><a href="foo">some text</a> words</p>

<p><br> words</p>
3 changes: 3 additions & 0 deletions test/cases/line-starts-with-html.md
@@ -0,0 +1,3 @@
<a href="foo">some text</a> words

<br> words
@@ -0,0 +1,21 @@
<ul>
<li><p>list item 1</p>

<pre><code>&lt;parent&gt;
&lt;child&gt;child1&lt;/child&gt;
&lt;!-- This is a comment --&gt;
&lt;child&gt;child2&lt;/child&gt;
&lt;child&gt;some text &lt;!-- a comment --&gt;&lt;/child&gt;
&lt;/parent&gt;
</code></pre></li>

<li><p>list item 2</p></li>
</ul>

<pre><code>&lt;parent&gt;
&lt;child&gt;child1&lt;/child&gt;
&lt;!-- This is a comment --&gt;
&lt;child&gt;child2&lt;/child&gt;
&lt;child&gt;some text &lt;!-- a comment --&gt;&lt;/child&gt;
&lt;/parent&gt;
</code></pre>
@@ -0,0 +1,21 @@
* list item 1

```
<parent>
<child>child1</child>
<!-- This is a comment -->
<child>child2</child>
<child>some text <!-- a comment --></child>
</parent>
```

* list item 2

```
<parent>
<child>child1</child>
<!-- This is a comment -->
<child>child2</child>
<child>some text <!-- a comment --></child>
</parent>
```

0 comments on commit 799abea

Please sign in to comment.