Skip to content

Commit 71a5873

Browse files
committed
fix(HTMLParser): fix code tags parsing
Closes #231
1 parent 4c68452 commit 71a5873

15 files changed

Lines changed: 127 additions & 16 deletions

dist/showdown.js

Lines changed: 23 additions & 3 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: 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.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/converter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ showdown.Converter = function (converterOptions) {
276276
});
277277

278278
// run the sub parsers
279+
text = showdown.subParser('hashPreCodeTags')(text, options, globals);
279280
text = showdown.subParser('githubCodeBlocks')(text, options, globals);
280281
text = showdown.subParser('hashHTMLBlocks')(text, options, globals);
281282
text = showdown.subParser('hashHTMLSpans')(text, options, globals);

src/subParsers/hashPreCodeTags.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Hash span elements that should not be parsed as markdown
3+
*/
4+
showdown.subParser('hashPreCodeTags', function (text, config, globals) {
5+
'use strict';
6+
7+
var repFunc = function (wholeMatch, match, left, right) {
8+
// encode html entities
9+
var codeblock = left + showdown.subParser('encodeCode')(match) + right;
10+
return '\n\n~G' + (globals.ghCodeBlocks.push({text: wholeMatch, codeblock: codeblock}) - 1) + 'G\n\n';
11+
};
12+
13+
text = showdown.helper.replaceRecursiveRegExp(text, repFunc, '^(?: |\\t){0,3}<pre\\b[^>]*>\\s*<code\\b[^>]*>', '^(?: |\\t){0,3}</code>\\s*</pre>', 'gim');
14+
return text;
15+
});

src/subParsers/paragraphs.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ showdown.subParser('paragraphs', function (text, options, globals) {
3131
for (i = 0; i < end; i++) {
3232
var blockText = '',
3333
grafsOutIt = grafsOut[i],
34-
child = false,
3534
codeFlag = false;
3635
// if this is a marker for an html block...
3736
while (grafsOutIt.search(/~(K|G)(\d+)\1/) >= 0) {
@@ -42,7 +41,12 @@ showdown.subParser('paragraphs', function (text, options, globals) {
4241
blockText = globals.gHtmlBlocks[num];
4342
} else {
4443
// we need to check if ghBlock is a false positive
45-
blockText = (codeFlag) ? globals.ghCodeBlocks[num].text : globals.ghCodeBlocks[num].codeblock;
44+
if (codeFlag) {
45+
// use encoded version of all text
46+
blockText = showdown.subParser('encodeCode')(globals.ghCodeBlocks[num].text);
47+
} else {
48+
blockText = globals.ghCodeBlocks[num].codeblock;
49+
}
4650
}
4751
blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs
4852

@@ -51,7 +55,6 @@ showdown.subParser('paragraphs', function (text, options, globals) {
5155
if (/^<pre\b[^>]*>\s*<code\b[^>]*>/.test(grafsOutIt)) {
5256
codeFlag = true;
5357
}
54-
child = true;
5558
}
5659
grafsOut[i] = grafsOutIt;
5760
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<p>code inception</p>
2+
3+
<pre><code>&lt;pre&gt;&lt;code&gt;
4+
&lt;div&gt;some html code inside code html tags inside a fenced code block&lt;/div&gt;
5+
&lt;/code&gt;&lt;/pre&gt;
6+
</code></pre>
7+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
code inception
2+
3+
```
4+
<pre><code>
5+
<div>some html code inside code html tags inside a fenced code block</div>
6+
</code></pre>
7+
```
8+

test/cases/pre-code-tags.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<pre>
2+
<code>
3+
foobar
4+
</code>
5+
</pre>
6+
7+
<p>blabla</p>
8+
9+
<pre nhaca="zulu"><code bla="bla">
10+
foobar
11+
</code>
12+
</pre>
13+
14+
<pre><code>
15+
&lt;div&gt;some html code&lt;/div&gt;
16+
</code></pre>

0 commit comments

Comments
 (0)