Skip to content

Commit 5f043ca

Browse files
committed
fix(subParsers/codeSpans): Fix issue with code html tags not being correctly escaped
1 parent 220b85d commit 5f043ca

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

src/subParsers/codeSpans.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
showdown.subParser('codeSpans', function (text) {
2727
'use strict';
2828

29+
//special case -> literal html code tag
30+
text = text.replace(/(<code[^><]*?>)([^]*?)<\/code>/g, function (wholeMatch, tag, c) {
31+
c = c.replace(/^([ \t]*)/g, ''); // leading whitespace
32+
c = c.replace(/[ \t]*$/g, ''); // trailing whitespace
33+
c = showdown.subParser('encodeCode')(c);
34+
return tag + c + '</code>';
35+
});
36+
2937
/*
3038
text = text.replace(/
3139
(^|[^\\]) // Character before opening ` can't be a backslash
@@ -38,15 +46,14 @@ showdown.subParser('codeSpans', function (text) {
3846
(?!`)
3947
/gm, function(){...});
4048
*/
41-
42-
text = text.replace(/(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm, function (wholeMatch, m1, m2, m3) {
43-
var c = m3;
44-
c = c.replace(/^([ \t]*)/g, ''); // leading whitespace
45-
c = c.replace(/[ \t]*$/g, ''); // trailing whitespace
46-
c = showdown.subParser('encodeCode')(c);
47-
return m1 + '<code>' + c + '</code>';
49+
text = text.replace(/(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm,
50+
function (wholeMatch, m1, m2, m3) {
51+
var c = m3;
52+
c = c.replace(/^([ \t]*)/g, ''); // leading whitespace
53+
c = c.replace(/[ \t]*$/g, ''); // trailing whitespace
54+
c = showdown.subParser('encodeCode')(c);
55+
return m1 + '<code>' + c + '</code>';
4856
});
4957

5058
return text;
51-
5259
});

0 commit comments

Comments
 (0)