Skip to content

Commit

Permalink
fix(HTMLParser): fix ghCodeBlocks being parsed inside code tags
Browse files Browse the repository at this point in the history
When using html pre/code tags to wrap github's fenced code block syntax,
showdown would parsed them instead of treating them like plain code.

Closes #229
  • Loading branch information
tivie committed Jan 25, 2016
1 parent e8852a8 commit 7d0436d
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 45 deletions.
57 changes: 41 additions & 16 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.

3 changes: 2 additions & 1 deletion src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ showdown.Converter = function (converterOptions) {
hashLinkCounts: {},
langExtensions: langExtensions,
outputModifiers: outputModifiers,
converter: this
converter: this,
ghCodeBlocks: []
};

// attacklab: Replace ~ with ~T
Expand Down
12 changes: 8 additions & 4 deletions src/subParsers/githubCodeBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ showdown.subParser('githubCodeBlocks', function (text, options, globals) {
text = text.replace(/(?:^|\n)```(.*)\n([\s\S]*?)\n```/g, function (wholeMatch, language, codeblock) {
var end = (options.omitExtraWLInCodeBlocks) ? '' : '\n';

// First parse the github code block
codeblock = showdown.subParser('encodeCode')(codeblock);
codeblock = showdown.subParser('detab')(codeblock);
codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing whitespace

codeblock = '<pre><code' + (language ? ' class="' + language + ' language-' + language + '"' : '') + '>' + codeblock + end + '</code></pre>';

return showdown.subParser('hashBlock')(codeblock, options, globals);
codeblock = showdown.subParser('hashBlock')(codeblock, options, globals);

// Since GHCodeblocks can be false positives, we need to
// store the primitive text and the parsed text in a global var,
// and then return a token
return '\n\n~G' + (globals.ghCodeBlocks.push({text: wholeMatch, codeblock: codeblock}) - 1) + 'G\n\n';
});

// attacklab: strip sentinel
text = text.replace(/~0/, '');

text = globals.converter._dispatch('githubCodeBlocks.after', text, options);

return text;
return globals.converter._dispatch('githubCodeBlocks.after', text, options);
});
40 changes: 30 additions & 10 deletions src/subParsers/paragraphs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ showdown.subParser('paragraphs', function (text, options, globals) {

for (var i = 0; i < end; i++) {
var str = grafs[i];

// if this is an HTML marker, copy it
if (str.search(/~K(\d+)K/g) >= 0) {
if (str.search(/~(K|G)(\d+)\1/g) >= 0) {
grafsOut.push(str);
} else if (str.search(/\S/) >= 0) {
} else {
str = showdown.subParser('spanGamut')(str, options, globals);
str = str.replace(/^([ \t]*)/g, '<p>');
str += '</p>';
Expand All @@ -29,16 +28,37 @@ showdown.subParser('paragraphs', function (text, options, globals) {

/** Unhashify HTML blocks */
end = grafsOut.length;
console.log(text);
for (i = 0; i < end; i++) {
var blockText = '';
var blockText = '',
grafsOutIt = grafsOut[i],
child = false,
codeFlag = false;
// if this is a marker for an html block...
while (grafsOut[i].search(/~K(\d+)K/) >= 0) {
blockText = globals.gHtmlBlocks[RegExp.$1];
while (grafsOutIt.search(/~(K|G)(\d+)\1/) >= 0) {
var delim = RegExp.$1,
num = RegExp.$2;

if (delim === 'K') {
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;
}
blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs
grafsOut[i] = grafsOut[i].replace(/~K\d+K/, blockText);

grafsOutIt = grafsOutIt.replace(/(\n\n)?~(K|G)\d+\2(\n\n)?/, blockText);
// Check if grafsOutIt is a pre->code
if (/^<pre\b[^>]*>\s*<code\b[^>]*>/.test(grafsOutIt)) {
codeFlag = true;
}
child = true;
}
grafsOut[i] = grafsOutIt;
}

text = globals.converter._dispatch('paragraphs.after', text, options);
return grafsOut.join('\n\n');
text = grafsOut.join('\n\n');
// Strip leading and trailing lines:
text = text.replace(/^\n+/g, '');
text = text.replace(/\n+$/g, '');
return globals.converter._dispatch('paragraphs.after', text, options);
});
3 changes: 1 addition & 2 deletions test/cases/list-with-code.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
<li><p>A list item with code:</p>

<pre><code>alert('Hello world!');
</code></pre>
</li>
</code></pre></li>
</ul>
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
<pre><code class="sh language-sh">$ git clone thing.git

dfgdfg
</code></pre>
</li>
</code></pre></li>
<li>
<p>I am another thing!</p>

<pre><code class="sh language-sh">$ git clone other-thing.git

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

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

bar
</code></pre>

<p>this is a long paragraph</p>

<p>this is another long paragraph</p>

<pre lang="no-highlight"><code>```javascript
var s = "JavaScript syntax highlighting";
alert(s);
```

```python
s = "Python syntax highlighting"
print s
```
</code></pre>
25 changes: 25 additions & 0 deletions test/issues/#229.2.code-being-parsed-inside-HTML-code-tags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<pre lang="no-highlight"><code>
foo

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

bar
</code></pre>

this is a long paragraph

this is another long paragraph

<pre lang="no-highlight"><code>```javascript
var s = "JavaScript syntax highlighting";
alert(s);
```

```python
s = "Python syntax highlighting"
print s
```
</code></pre>
16 changes: 16 additions & 0 deletions test/issues/#229.code-being-parsed-inside-HTML-code-tags.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<pre lang="no-highlight"><code>
```javascript
var s = "JavaScript syntax highlighting";
alert(s);
```

```python
s = "Python syntax highlighting"
print s
```

```
No language indicated, so no syntax highlighting.
But let's throw in a <b>tag</b>.
```
</code></pre>
16 changes: 16 additions & 0 deletions test/issues/#229.code-being-parsed-inside-HTML-code-tags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<pre lang="no-highlight"><code>
```javascript
var s = "JavaScript syntax highlighting";
alert(s);
```

```python
s = "Python syntax highlighting"
print s
```

```
No language indicated, so no syntax highlighting.
But let's throw in a <b>tag</b>.
```
</code></pre>
8 changes: 4 additions & 4 deletions test/karlcow/list-code.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<ul>
<li><p>a list containing a block of code</p>
<li>
<p>a list containing a block of code</p>

<pre><code>10 PRINT HELLO INFINITE
<pre><code>10 PRINT HELLO INFINITE
20 GOTO 10
</code></pre>
</li>
</code></pre></li>
</ul>

0 comments on commit 7d0436d

Please sign in to comment.