2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Handle github codeblocks prior to running HashHTML so that
|
|
|
|
* HTML contained within the codeblock gets escaped properly
|
|
|
|
* Example:
|
|
|
|
* ```ruby
|
|
|
|
* def hello_world(x)
|
|
|
|
* puts "Hello, #{x}"
|
|
|
|
* end
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
showdown.subParser('githubCodeBlocks', function (text, options, globals) {
|
2015-01-19 19:37:21 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-07-12 03:33:11 +08:00
|
|
|
// early exit if option is not enabled
|
|
|
|
if (!options.ghCodeBlocks) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('githubCodeBlocks.before', text, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2017-01-29 08:07:19 +08:00
|
|
|
text += '¨0';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2017-11-11 20:29:36 +08:00
|
|
|
text = text.replace(/(?:^|\n)(```+|~~~+)([^\s`~]*)\n([\s\S]*?)\n\1/g, function (wholeMatch, delim, language, codeblock) {
|
2015-08-02 04:05:28 +08:00
|
|
|
var end = (options.omitExtraWLInCodeBlocks) ? '' : '\n';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-01-25 09:04:06 +08:00
|
|
|
// First parse the github code block
|
2017-01-30 03:28:30 +08:00
|
|
|
codeblock = showdown.subParser('encodeCode')(codeblock, options, globals);
|
|
|
|
codeblock = showdown.subParser('detab')(codeblock, options, globals);
|
2015-01-19 19:37:21 +08:00
|
|
|
codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
|
|
|
|
codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing whitespace
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-07-14 07:41:59 +08:00
|
|
|
codeblock = '<pre><code' + (language ? ' class="' + language + ' language-' + language + '"' : '') + '>' + codeblock + end + '</code></pre>';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-01-25 09:04:06 +08:00
|
|
|
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
|
2017-01-29 08:07:19 +08:00
|
|
|
return '\n\n¨G' + (globals.ghCodeBlocks.push({text: wholeMatch, codeblock: codeblock}) - 1) + 'G\n\n';
|
2015-01-19 19:37:21 +08:00
|
|
|
});
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
// attacklab: strip sentinel
|
2017-01-29 08:07:19 +08:00
|
|
|
text = text.replace(/¨0/, '');
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
return globals.converter._dispatch('githubCodeBlocks.after', text, options, globals);
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|