Skip to content

Commit

Permalink
fix(HTMLParser): fix code tags parsing
Browse files Browse the repository at this point in the history
Closes #231
  • Loading branch information
tivie committed Jan 25, 2016
1 parent 4c68452 commit 71a5873
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 16 deletions.
26 changes: 23 additions & 3 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.

2 changes: 1 addition & 1 deletion 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.

1 change: 1 addition & 0 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ showdown.Converter = function (converterOptions) {
});

// run the sub parsers
text = showdown.subParser('hashPreCodeTags')(text, options, globals);
text = showdown.subParser('githubCodeBlocks')(text, options, globals);
text = showdown.subParser('hashHTMLBlocks')(text, options, globals);
text = showdown.subParser('hashHTMLSpans')(text, options, globals);
Expand Down
15 changes: 15 additions & 0 deletions src/subParsers/hashPreCodeTags.js
Original file line number Diff line number Diff line change
@@ -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;
});
9 changes: 6 additions & 3 deletions src/subParsers/paragraphs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ showdown.subParser('paragraphs', function (text, options, globals) {
for (i = 0; i < end; i++) {
var blockText = '',
grafsOutIt = grafsOut[i],
child = false,
codeFlag = false;
// if this is a marker for an html block...
while (grafsOutIt.search(/~(K|G)(\d+)\1/) >= 0) {
Expand All @@ -42,7 +41,12 @@ showdown.subParser('paragraphs', function (text, options, globals) {
blockText = globals.gHtmlBlocks[num];
} else {
// 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

Expand All @@ -51,7 +55,6 @@ showdown.subParser('paragraphs', function (text, options, globals) {
if (/^<pre\b[^>]*>\s*<code\b[^>]*>/.test(grafsOutIt)) {
codeFlag = true;
}
child = true;
}
grafsOut[i] = grafsOutIt;
}
Expand Down
7 changes: 7 additions & 0 deletions test/cases/pre-code-tags-inside-code-block.html
Original file line number Diff line number Diff line change
@@ -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>

8 changes: 8 additions & 0 deletions test/cases/pre-code-tags-inside-code-block.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
code inception

```
<pre><code>
<div>some html code inside code html tags inside a fenced code block</div>
</code></pre>
```

16 changes: 16 additions & 0 deletions test/cases/pre-code-tags.html
Original file line number Diff line number Diff line change
@@ -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>
17 changes: 17 additions & 0 deletions test/cases/pre-code-tags.md
Original file line number Diff line number Diff line change
@@ -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>

12 changes: 6 additions & 6 deletions test/issues/#229.2.code-being-parsed-inside-HTML-code-tags.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<pre lang="no-highlight"><code>
foo
foo

```javascript
var s = "JavaScript syntax highlighting";
alert(s);
```
```javascript
var s = "JavaScript syntax highlighting";
alert(s);
```

bar
bar
</code></pre>

<p>this is a long paragraph</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

```
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>
12 changes: 12 additions & 0 deletions test/issues/#230.paragraphs-are-ignored-between-code-tags.html
Original file line number Diff line number Diff line change
@@ -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>
12 changes: 12 additions & 0 deletions test/issues/#230.paragraphs-are-ignored-between-code-tags.md
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 71a5873

Please sign in to comment.