fix(hashCodeTags): escape code tags

Previously, `<code>` tags were not escaped. This was counter intuitive since ´<pre><code>` tags
were being escaped. Now both pre code and code are escaped.

Closes #339
This commit is contained in:
Estevao Soares dos Santos 2017-02-06 05:09:52 +00:00
parent 7f43b79b33
commit 41cb3f6b7f
9 changed files with 29 additions and 14 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

@ -0,0 +1,18 @@
/**
* Hash and escape <code> elements that should not be parsed as markdown
*/
showdown.subParser('hashCodeTags', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('hashCodeTags.before', text, options, globals);
var repFunc = function (wholeMatch, match, left, right) {
var codeblock = left + showdown.subParser('encodeCode')(match, options, globals) + right;
return '¨C' + (globals.gHtmlSpans.push(codeblock) - 1) + 'C';
};
// Hash naked <code>
text = showdown.helper.replaceRecursiveRegExp(text, repFunc, '<code\\b[^>]*>', '</code>', 'gim');
text = globals.converter._dispatch('hashCodeTags.after', text, options, globals);
return text;
});

View File

@ -1,5 +1,5 @@
/**
* Hash span elements that should not be parsed as markdown
* Hash and escape <pre><code> elements that should not be parsed as markdown
*/
showdown.subParser('hashPreCodeTags', function (text, options, globals) {
'use strict';
@ -17,15 +17,3 @@ showdown.subParser('hashPreCodeTags', function (text, options, globals) {
text = globals.converter._dispatch('hashPreCodeTags.after', text, options, globals);
return text;
});
showdown.subParser('hashCodeTags', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('hashCodeTags.before', text, options, globals);
// Hash naked <code>
var matches = showdown.helper.matchRecursiveRegExp(text, '<code\\b[^>]*>', '</code>', 'gi');
for (var i = 0; i < matches.length; ++i) {
text = text.replace(matches[i][0], '¨C' + (globals.gHtmlSpans.push(matches[i][0]) - 1) + 'C');
}
text = globals.converter._dispatch('hashCodeTags.after', text, options, globals);
return text;
});

View File

@ -0,0 +1,4 @@
<p>this is code <code>some &lt;span&gt;text&lt;/span&gt;</code> yeah!</p>
<pre><code>
&lt;div&gt;foo&lt;/div&gt;
</code></pre>

View File

@ -0,0 +1,5 @@
this is code <code>some <span>text</span></code> yeah!
<pre><code>
<div>foo</div>
</code></pre>

View File

@ -1,5 +1,5 @@
<p><code>some **code** yeah</code></p>
<p>some <code>inline **code** block</code></p>
<p><code>some inline **code**</code> block</p>
<p>yo dawg <code start="true">some <code start="false">code</code> inception</code></p>
<p>yo dawg <code start="true">some &lt;code start="false"&gt;code&lt;/code&gt; inception</code></p>
<div>some **div** yeah</div>