diff --git a/dist/showdown.js b/dist/showdown.js index f91e710..cf35d4e 100644 Binary files a/dist/showdown.js and b/dist/showdown.js differ diff --git a/dist/showdown.js.map b/dist/showdown.js.map index 1e3cb20..ca3b22f 100644 Binary files a/dist/showdown.js.map and b/dist/showdown.js.map differ diff --git a/dist/showdown.min.js b/dist/showdown.min.js index 3ddb9d0..a17cf82 100644 Binary files a/dist/showdown.min.js and b/dist/showdown.min.js differ diff --git a/dist/showdown.min.js.map b/dist/showdown.min.js.map index d892f37..a0dcebf 100644 Binary files a/dist/showdown.min.js.map and b/dist/showdown.min.js.map differ diff --git a/src/converter.js b/src/converter.js index e2dfc27..4aead55 100644 --- a/src/converter.js +++ b/src/converter.js @@ -41,8 +41,10 @@ showdown.Converter = function (converterOptions) { parserOrder = [ 'githubCodeBlocks', 'hashHTMLBlocks', + 'hashHTMLSpans', 'stripLinkDefinitions', 'blockGamut', + 'unhashHTMLSpans', 'unescapeSpecialChars' ]; @@ -183,6 +185,7 @@ showdown.Converter = function (converterOptions) { var globals = { gHtmlBlocks: [], + gHtmlSpans: [], gUrls: {}, gTitles: {}, gDimensions: {}, diff --git a/src/helpers.js b/src/helpers.js index eb2a6ff..3b9d104 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -106,6 +106,68 @@ showdown.helper.escapeCharacters = function escapeCharacters(text, charsToEscape return text; }; +/** + * matchRecursiveRegExp + * + * (c) 2007 Steven Levithan + * MIT License + * + * Accepts a string to search, a left and right format delimiter + * as regex patterns, and optional regex flags. Returns an array + * of matches, allowing nested instances of left/right delimiters. + * Use the "g" flag to return all matches, otherwise only the + * first is returned. Be careful to ensure that the left and + * right format delimiters produce mutually exclusive matches. + * Backreferences are not supported within the right delimiter + * due to how it is internally combined with the left delimiter. + * When matching strings whose format delimiters are unbalanced + * to the left or right, the output is intentionally as a + * conventional regex library with recursion support would + * produce, e.g. "<" and ">" both produce ["x"] when using + * "<" and ">" as the delimiters (both strings contain a single, + * balanced instance of ""). + * + * examples: + * matchRecursiveRegExp("test", "\\(", "\\)") + * returns: [] + * matchRecursiveRegExp(">>t<>", "<", ">", "g") + * returns: ["t<>", ""] + * matchRecursiveRegExp("
test
", "]*>", "", "gi") + * returns: ["test"] + */ +showdown.helper.matchRecursiveRegExp = function (str, left, right, flags) { + 'use strict'; + var f = flags || '', + g = f.indexOf('g') > -1, + x = new RegExp(left + '|' + right, f), + l = new RegExp(left, f.replace(/g/g, '')), + a = [], + t, s, m, start, end; + + do { + t = 0; + while ((m = x.exec(str))) { + if (l.test(m[0])) { + if (!(t++)) { + start = m[0]; + s = x.lastIndex; + } + } else if (t) { + if (!--t) { + end = m[0]; + var match = str.slice(s, m.index); + a.push([start + match + end, match]); + if (!g) { + return a; + } + } + } + } + } while (t && (x.lastIndex = s)); + + return a; +}; + /** * POLYFILLS */ @@ -118,6 +180,10 @@ if (showdown.helper.isUndefined(console)) { log: function (msg) { 'use strict'; alert(msg); + }, + error: function (msg) { + 'use strict'; + throw msg; } }; } diff --git a/src/subParsers/codeSpans.js b/src/subParsers/codeSpans.js index b0ec1dd..132c989 100644 --- a/src/subParsers/codeSpans.js +++ b/src/subParsers/codeSpans.js @@ -26,13 +26,16 @@ showdown.subParser('codeSpans', function (text) { 'use strict'; + /* //special case -> literal html code tag + // Introduced in commit 5f043ca46d20eb88240c753ae7f7c7429f4ee27 + // Commented out due to issue #196 text = text.replace(/(<]*?>)([^]*?)<\/code>/g, function (wholeMatch, tag, c) { c = c.replace(/^([ \t]*)/g, ''); // leading whitespace c = c.replace(/[ \t]*$/g, ''); // trailing whitespace c = showdown.subParser('encodeCode')(c); return tag + c + ''; - }); + });*/ /* text = text.replace(/ diff --git a/src/subParsers/hashHTMLSpans.js b/src/subParsers/hashHTMLSpans.js new file mode 100644 index 0000000..588728d --- /dev/null +++ b/src/subParsers/hashHTMLSpans.js @@ -0,0 +1,26 @@ +/** + * Hash span elements that should not be parsed as markdown + */ +showdown.subParser('hashHTMLSpans', function (text, config, globals) { + 'use strict'; + + var matches = showdown.helper.matchRecursiveRegExp(text, ']*>', '', 'gi'); + + for (var i = 0; i < matches.length; ++i) { + text = text.replace(matches[i][0], '~L' + (globals.gHtmlSpans.push(matches[i][0]) - 1) + 'L'); + } + return text; +}); + +/** + * Unhash HTML spans + */ +showdown.subParser('unhashHTMLSpans', function (text, config, globals) { + 'use strict'; + + for (var i = 0; i < globals.gHtmlSpans.length; ++i) { + text = text.replace('~L' + i + 'L', globals.gHtmlSpans[i]); + } + + return text; +}); diff --git a/test/cases/literal-html-tags.html b/test/cases/literal-html-tags.html new file mode 100644 index 0000000..c9db48a --- /dev/null +++ b/test/cases/literal-html-tags.html @@ -0,0 +1,9 @@ +

some **code** yeah

+ +

some inline **code** block

+ +

some inline **code** block

+ +

yo dawg some code inception

+ +
some **div** yeah
\ No newline at end of file diff --git a/test/cases/literal-html-tags.md b/test/cases/literal-html-tags.md new file mode 100644 index 0000000..6e16a22 --- /dev/null +++ b/test/cases/literal-html-tags.md @@ -0,0 +1,9 @@ +some **code** yeah + +some inline **code** block + +some inline **code** block + +yo dawg some code inception + +
some **div** yeah
\ No newline at end of file