2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Process Markdown `<pre><code>` blocks.
|
|
|
|
*/
|
|
|
|
showdown.subParser('codeBlocks', function (text, options, globals) {
|
2015-01-19 19:37:21 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('codeBlocks.before', text, options, globals);
|
2016-08-30 13:24:19 +08:00
|
|
|
|
|
|
|
// sentinel workarounds for lack of \A and \Z, safari\khtml bug
|
2015-01-19 19:37:21 +08:00
|
|
|
text += '~0';
|
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
var pattern = /(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g;
|
|
|
|
text = text.replace(pattern, function (wholeMatch, m1, m2) {
|
2015-05-27 02:57:36 +08:00
|
|
|
var codeblock = m1,
|
|
|
|
nextChar = m2,
|
|
|
|
end = '\n';
|
2015-01-19 19:37:21 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
codeblock = showdown.subParser('outdent')(codeblock);
|
|
|
|
codeblock = showdown.subParser('encodeCode')(codeblock);
|
|
|
|
codeblock = showdown.subParser('detab')(codeblock);
|
|
|
|
codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
|
2015-05-27 02:57:36 +08:00
|
|
|
codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing newlines
|
2015-01-19 19:37:21 +08:00
|
|
|
|
2015-05-27 02:57:36 +08:00
|
|
|
if (options.omitExtraWLInCodeBlocks) {
|
|
|
|
end = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
codeblock = '<pre><code>' + codeblock + end + '</code></pre>';
|
2015-01-19 19:37:21 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
return showdown.subParser('hashBlock')(codeblock, options, globals) + nextChar;
|
|
|
|
});
|
2015-01-19 19:37:21 +08:00
|
|
|
|
2016-08-30 13:24:19 +08:00
|
|
|
// strip sentinel
|
2015-01-19 19:37:21 +08:00
|
|
|
text = text.replace(/~0/, '');
|
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('codeBlocks.after', text, options, globals);
|
2015-01-19 19:37:21 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|