feat(events): add events to all subparsers

This commit adds events to all subparsers (that were previously not being watched).
This commit is contained in:
Estevao Soares dos Santos 2017-01-29 19:28:30 +00:00
parent fd014747a1
commit 7d63a3e635
25 changed files with 62 additions and 36 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

@ -285,8 +285,13 @@ showdown.Converter = function (converterOptions) {
// detab
text = showdown.subParser('detab')(text, options, globals);
// stripBlankLines
text = showdown.subParser('stripBlankLines')(text, options, globals);
/**
* Strip any lines consisting only of spaces and tabs.
* This makes subsequent regexs easier to write, because we can
* match consecutive blank lines with /\n+/ instead of something
* contorted like /[ \t]*\n+/
*/
text = text.replace(/^[ \t]+$/mg, '');
//run languageExtensions
showdown.helper.forEach(langExtensions, function (ext) {

View File

@ -38,7 +38,7 @@ showdown.subParser('autoLinks', function (text, options, globals) {
function replaceMail(wholeMatch, b, mail) {
var href = 'mailto:';
b = b || '';
mail = showdown.subParser('unescapeSpecialChars')(mail);
mail = showdown.subParser('unescapeSpecialChars')(mail, options, globals);
if (options.encodeEmails) {
mail = showdown.helper.encodeEmailAddress(mail);
href = showdown.helper.encodeEmailAddress(href + mail);

View File

@ -15,9 +15,9 @@ showdown.subParser('codeBlocks', function (text, options, globals) {
nextChar = m2,
end = '\n';
codeblock = showdown.subParser('outdent')(codeblock);
codeblock = showdown.subParser('encodeCode')(codeblock);
codeblock = showdown.subParser('detab')(codeblock);
codeblock = showdown.subParser('outdent')(codeblock, options, globals);
codeblock = showdown.subParser('encodeCode')(codeblock, options, globals);
codeblock = showdown.subParser('detab')(codeblock, options, globals);
codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing newlines

View File

@ -49,7 +49,7 @@ showdown.subParser('codeSpans', function (text, options, globals) {
var c = m3;
c = c.replace(/^([ \t]*)/g, ''); // leading whitespace
c = c.replace(/[ \t]*$/g, ''); // trailing whitespace
c = showdown.subParser('encodeCode')(c);
c = showdown.subParser('encodeCode')(c, options, globals);
return m1 + '<code>' + c + '</code>';
}
);

View File

@ -1,8 +1,9 @@
/**
* Convert all tabs to spaces
*/
showdown.subParser('detab', function (text) {
showdown.subParser('detab', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('detab.before', text, options, globals);
// expand first n-1 tabs
text = text.replace(/\t(?=\t)/g, ' '); // g_tab_width
@ -27,6 +28,6 @@ showdown.subParser('detab', function (text) {
text = text.replace(/¨A/g, ' '); // g_tab_width
text = text.replace(/¨B/g, '');
text = globals.converter._dispatch('detab.after', text, options, globals);
return text;
});

View File

@ -1,8 +1,10 @@
/**
* Smart processing for ampersands and angle brackets that need to be encoded.
*/
showdown.subParser('encodeAmpsAndAngles', function (text) {
showdown.subParser('encodeAmpsAndAngles', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('encodeAmpsAndAngles.before', text, options, globals);
// Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin:
// http://bumppo.net/projects/amputator/
text = text.replace(/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/g, '&amp;');
@ -10,5 +12,6 @@ showdown.subParser('encodeAmpsAndAngles', function (text) {
// Encode naked <'s
text = text.replace(/<(?![a-z\/?\$!])/gi, '&lt;');
text = globals.converter._dispatch('encodeAmpsAndAngles.after', text, options, globals);
return text;
});

View File

@ -9,9 +9,13 @@
* ...but we're sidestepping its use of the (slow) RegExp constructor
* as an optimization for Firefox. This function gets called a LOT.
*/
showdown.subParser('encodeBackslashEscapes', function (text) {
showdown.subParser('encodeBackslashEscapes', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('encodeBackslashEscapes.before', text, options, globals);
text = text.replace(/\\(\\)/g, showdown.helper.escapeCharactersCallback);
text = text.replace(/\\([`*_{}\[\]()>#+-.!~])/g, showdown.helper.escapeCharactersCallback);
text = globals.converter._dispatch('encodeBackslashEscapes.after', text, options, globals);
return text;
});

View File

@ -3,9 +3,11 @@
* The point is that in code, these characters are literals,
* and lose their special Markdown meanings.
*/
showdown.subParser('encodeCode', function (text) {
showdown.subParser('encodeCode', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('encodeCode.before', text, options, globals);
// Encode all ampersands; HTML entities are not
// entities within a Markdown code span.
text = text
@ -13,9 +15,7 @@ showdown.subParser('encodeCode', function (text) {
// Do the angle bracket song and dance:
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
// Now, escape characters that are magic in Markdown:
//text = showdown.helper.escapeCharacters(text, '*_{}[]\\', false); // replaced line to improve performance
.replace(/([*_{}\[\]\\])/g, showdown.helper.escapeCharactersCallback);
// jj the line above breaks this:
@ -25,5 +25,6 @@ showdown.subParser('encodeCode', function (text) {
// special char: *
// ---
text = globals.converter._dispatch('encodeCode.after', text, options, globals);
return text;
});

View File

@ -2,8 +2,9 @@
* Within tags -- meaning between < and > -- encode [\ ` * _] so they
* don't conflict with their use in Markdown for code, italics and strong.
*/
showdown.subParser('escapeSpecialCharsWithinTagAttributes', function (text) {
showdown.subParser('escapeSpecialCharsWithinTagAttributes', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('escapeSpecialCharsWithinTagAttributes.before', text, options, globals);
// Build a regex to find HTML tags and comments. See Friedl's
// "Mastering Regular Expressions", 2nd Ed., pp. 200-201.
@ -16,5 +17,6 @@ showdown.subParser('escapeSpecialCharsWithinTagAttributes', function (text) {
.replace(/([\\`*_ ~=])/g, showdown.helper.escapeCharactersCallback);
});
text = globals.converter._dispatch('escapeSpecialCharsWithinTagAttributes.after', text, options, globals);
return text;
});

View File

@ -24,8 +24,8 @@ showdown.subParser('githubCodeBlocks', function (text, options, globals) {
var end = (options.omitExtraWLInCodeBlocks) ? '' : '\n';
// First parse the github code block
codeblock = showdown.subParser('encodeCode')(codeblock);
codeblock = showdown.subParser('detab')(codeblock);
codeblock = showdown.subParser('encodeCode')(codeblock, options, globals);
codeblock = showdown.subParser('detab')(codeblock, options, globals);
codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing whitespace

View File

@ -1,5 +1,8 @@
showdown.subParser('hashBlock', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('hashBlock.before', text, options, globals);
text = text.replace(/(^\n+|\n+$)/g, '');
return '\n\n¨K' + (globals.gHtmlBlocks.push(text) - 1) + 'K\n\n';
text = '\n\n¨K' + (globals.gHtmlBlocks.push(text) - 1) + 'K\n\n';
text = globals.converter._dispatch('hashBlock.after', text, options, globals);
return text;
});

View File

@ -1,5 +1,6 @@
showdown.subParser('hashHTMLBlocks', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('hashHTMLBlocks.before', text, options, globals);
var blockTags = [
'pre',
@ -64,5 +65,6 @@ showdown.subParser('hashHTMLBlocks', function (text, options, globals) {
text = text.replace(/(?:\n\n)( {0,3}(?:<([?%])[^\r]*?\2>)[ \t]*(?=\n{2,}))/g,
showdown.subParser('hashElement')(text, options, globals));
text = globals.converter._dispatch('hashHTMLBlocks.after', text, options, globals);
return text;
});

View File

@ -1,26 +1,31 @@
/**
* Hash span elements that should not be parsed as markdown
*/
showdown.subParser('hashHTMLSpans', function (text, config, globals) {
showdown.subParser('hashHTMLSpans', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('hashHTMLSpans.before', text, options, globals);
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('hashHTMLSpans.after', text, options, globals);
return text;
});
/**
* Unhash HTML spans
*/
showdown.subParser('unhashHTMLSpans', function (text, config, globals) {
showdown.subParser('unhashHTMLSpans', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('unhashHTMLSpans.before', text, options, globals);
for (var i = 0; i < globals.gHtmlSpans.length; ++i) {
text = text.replace('¨C' + i + 'C', globals.gHtmlSpans[i]);
}
text = globals.converter._dispatch('unhashHTMLSpans.after', text, options, globals);
return text;
});

View File

@ -1,15 +1,18 @@
/**
* Hash span elements that should not be parsed as markdown
*/
showdown.subParser('hashPreCodeTags', function (text, config, globals) {
showdown.subParser('hashPreCodeTags', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('hashPreCodeTags.before', text, options, globals);
var repFunc = function (wholeMatch, match, left, right) {
// encode html entities
var codeblock = left + showdown.subParser('encodeCode')(match) + right;
var codeblock = left + showdown.subParser('encodeCode')(match, options, globals) + right;
return '\n\n¨G' + (globals.ghCodeBlocks.push({text: wholeMatch, codeblock: codeblock}) - 1) + 'G\n\n';
};
text = showdown.helper.replaceRecursiveRegExp(text, repFunc, '^ {0,3}<pre\\b[^>]*>\\s*<code\\b[^>]*>', '^ {0,3}</code>\\s*</pre>', 'gim');
text = globals.converter._dispatch('hashPreCodeTags.after', text, options, globals);
return text;
});

View File

@ -1,8 +1,9 @@
/**
* Remove one level of line-leading tabs or spaces
*/
showdown.subParser('outdent', function (text) {
showdown.subParser('outdent', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('outdent.before', text, options, globals);
// attacklab: hack around Konqueror 3.5.4 bug:
// "----------bug".replace(/^-/g,"") == "bug"
@ -11,5 +12,6 @@ showdown.subParser('outdent', function (text) {
// attacklab: clean up hack
text = text.replace(/¨0/g, '');
text = globals.converter._dispatch('outdent.after', text, options, globals);
return text;
});

View File

@ -43,7 +43,7 @@ showdown.subParser('paragraphs', function (text, options, globals) {
// we need to check if ghBlock is a false positive
if (codeFlag) {
// use encoded version of all text
blockText = showdown.subParser('encodeCode')(globals.ghCodeBlocks[num].text);
blockText = showdown.subParser('encodeCode')(globals.ghCodeBlocks[num].text, options, globals);
} else {
blockText = globals.ghCodeBlocks[num].codeblock;
}

View File

@ -1,10 +0,0 @@
/**
* Strip any lines consisting only of spaces and tabs.
* This makes subsequent regexs easier to write, because we can
* match consecutive blank lines with /\n+/ instead of something
* contorted like /[ \t]*\n+/
*/
showdown.subParser('stripBlankLines', function (text) {
'use strict';
return text.replace(/^[ \t]+$/mg, '');
});

View File

@ -13,7 +13,7 @@ showdown.subParser('stripLinkDefinitions', function (text, options, globals) {
text = text.replace(regex, function (wholeMatch, linkId, url, width, height, blankLines, title) {
linkId = linkId.toLowerCase();
globals.gUrls[linkId] = showdown.subParser('encodeAmpsAndAngles')(url); // Link IDs are case-insensitive
globals.gUrls[linkId] = showdown.subParser('encodeAmpsAndAngles')(url, options, globals); // Link IDs are case-insensitive
if (blankLines) {
// Oops, found blank lines, so it's not a title.

View File

@ -1,12 +1,15 @@
/**
* Swap back in all the special characters we've hidden.
*/
showdown.subParser('unescapeSpecialChars', function (text) {
showdown.subParser('unescapeSpecialChars', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('unescapeSpecialChars.before', text, options, globals);
text = text.replace(/¨E(\d+)E/g, function (wholeMatch, m1) {
var charCodeToReplace = parseInt(m1);
return String.fromCharCode(charCodeToReplace);
});
text = globals.converter._dispatch('unescapeSpecialChars.after', text, options, globals);
return text;
});

View File

@ -0,0 +1 @@
<p>some text <code>**foo**</code></p>

View File

@ -0,0 +1 @@
some text `**foo**`