fix(HTMLParser): fix code tags parsing

Closes #231
This commit is contained in:
Estevão Soares dos Santos 2016-01-25 03:01:54 +00:00
parent 4c68452999
commit 71a5873902
15 changed files with 101 additions and 10 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

@ -276,6 +276,7 @@ showdown.Converter = function (converterOptions) {
}); });
// run the sub parsers // run the sub parsers
text = showdown.subParser('hashPreCodeTags')(text, options, globals);
text = showdown.subParser('githubCodeBlocks')(text, options, globals); text = showdown.subParser('githubCodeBlocks')(text, options, globals);
text = showdown.subParser('hashHTMLBlocks')(text, options, globals); text = showdown.subParser('hashHTMLBlocks')(text, options, globals);
text = showdown.subParser('hashHTMLSpans')(text, options, globals); text = showdown.subParser('hashHTMLSpans')(text, options, globals);

View File

@ -0,0 +1,15 @@
/**
* Hash span elements that should not be parsed as markdown
*/
showdown.subParser('hashPreCodeTags', function (text, config, globals) {
'use strict';
var repFunc = function (wholeMatch, match, left, right) {
// encode html entities
var codeblock = left + showdown.subParser('encodeCode')(match) + right;
return '\n\n~G' + (globals.ghCodeBlocks.push({text: wholeMatch, codeblock: codeblock}) - 1) + 'G\n\n';
};
text = showdown.helper.replaceRecursiveRegExp(text, repFunc, '^(?: |\\t){0,3}<pre\\b[^>]*>\\s*<code\\b[^>]*>', '^(?: |\\t){0,3}</code>\\s*</pre>', 'gim');
return text;
});

View File

@ -31,7 +31,6 @@ showdown.subParser('paragraphs', function (text, options, globals) {
for (i = 0; i < end; i++) { for (i = 0; i < end; i++) {
var blockText = '', var blockText = '',
grafsOutIt = grafsOut[i], grafsOutIt = grafsOut[i],
child = false,
codeFlag = false; codeFlag = false;
// if this is a marker for an html block... // if this is a marker for an html block...
while (grafsOutIt.search(/~(K|G)(\d+)\1/) >= 0) { while (grafsOutIt.search(/~(K|G)(\d+)\1/) >= 0) {
@ -42,7 +41,12 @@ showdown.subParser('paragraphs', function (text, options, globals) {
blockText = globals.gHtmlBlocks[num]; blockText = globals.gHtmlBlocks[num];
} else { } else {
// we need to check if ghBlock is a false positive // we need to check if ghBlock is a false positive
blockText = (codeFlag) ? globals.ghCodeBlocks[num].text : globals.ghCodeBlocks[num].codeblock; if (codeFlag) {
// use encoded version of all text
blockText = showdown.subParser('encodeCode')(globals.ghCodeBlocks[num].text);
} else {
blockText = globals.ghCodeBlocks[num].codeblock;
}
} }
blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs
@ -51,7 +55,6 @@ showdown.subParser('paragraphs', function (text, options, globals) {
if (/^<pre\b[^>]*>\s*<code\b[^>]*>/.test(grafsOutIt)) { if (/^<pre\b[^>]*>\s*<code\b[^>]*>/.test(grafsOutIt)) {
codeFlag = true; codeFlag = true;
} }
child = true;
} }
grafsOut[i] = grafsOutIt; grafsOut[i] = grafsOutIt;
} }

View File

@ -0,0 +1,7 @@
<p>code inception</p>
<pre><code>&lt;pre&gt;&lt;code&gt;
&lt;div&gt;some html code inside code html tags inside a fenced code block&lt;/div&gt;
&lt;/code&gt;&lt;/pre&gt;
</code></pre>

View File

@ -0,0 +1,8 @@
code inception
```
<pre><code>
<div>some html code inside code html tags inside a fenced code block</div>
</code></pre>
```

View File

@ -0,0 +1,16 @@
<pre>
<code>
foobar
</code>
</pre>
<p>blabla</p>
<pre nhaca="zulu"><code bla="bla">
foobar
</code>
</pre>
<pre><code>
&lt;div&gt;some html code&lt;/div&gt;
</code></pre>

View File

@ -0,0 +1,17 @@
<pre>
<code>
foobar
</code>
</pre>
blabla
<pre nhaca="zulu"><code bla="bla">
foobar
</code>
</pre>
<pre><code>
<div>some html code</div>
</code></pre>

View File

@ -11,6 +11,6 @@ print s
``` ```
No language indicated, so no syntax highlighting. No language indicated, so no syntax highlighting.
But let's throw in a <b>tag</b>. But let's throw in a &lt;b&gt;tag&lt;/b&gt;.
``` ```
</code></pre> </code></pre>

View File

@ -0,0 +1,12 @@
<pre lang="no-highlight"><code>```python
var s;
```
</code></pre>
<p>this is a long paragraph</p>
<pre lang="no-highlight"><code>
```javascript
var s;
```
</code></pre>

View File

@ -0,0 +1,12 @@
<pre lang="no-highlight"><code>```python
var s;
```
</code></pre>
this is a long paragraph
<pre lang="no-highlight"><code>
```javascript
var s;
```
</code></pre>