Revert "remove old markdown engine work-in-progress"

This reverts commit ee470b531788b71c22721562e8bbb846004a9bc7.
This commit is contained in:
Sam
2017-07-12 18:10:51 -04:00
parent bcbb9f208d
commit 79a084dd58
62 changed files with 3075 additions and 653 deletions

View File

@ -1,38 +1,27 @@
// we need a custom renderer for code blocks cause we have a slightly non compliant
// format with special handling for text and so on
import { escape } from 'pretty-text/sanitizer';
import { registerOption } from 'pretty-text/pretty-text';
// Support for various code blocks
const TEXT_CODE_CLASSES = ["text", "pre", "plain"];
function render(tokens, idx, options, env, slf, md) {
let token = tokens[idx],
info = token.info ? md.utils.unescapeAll(token.info) : '',
langName = md.options.discourse.defaultCodeLang,
className,
escapedContent = md.utils.escapeHtml(token.content);
if (info) {
// strip off any additional languages
info = info.split(/\s+/g)[0];
}
const acceptableCodeClasses = md.options.discourse.acceptableCodeClasses;
if (acceptableCodeClasses && info && acceptableCodeClasses.indexOf(info) !== -1) {
langName = info;
}
className = TEXT_CODE_CLASSES.indexOf(info) !== -1 ? 'lang-nohighlight' : 'lang-' + langName;
return `<pre><code class='${className}'>${escapedContent}</code></pre>\n`;
function codeFlattenBlocks(blocks) {
let result = "";
blocks.forEach(function(b) {
result += b;
if (b.trailing) { result += b.trailing; }
});
return result;
}
export function setup(helper) {
if (!helper.markdownIt) { return; }
registerOption((siteSettings, opts) => {
opts.features.code = true;
opts.defaultCodeLang = siteSettings.default_code_lang;
opts.acceptableCodeClasses = (siteSettings.highlighted_languages || "").split("|").concat(['auto', 'nohighlight']);
});
helper.registerOptions((opts, siteSettings) => {
opts.defaultCodeLang = siteSettings.default_code_lang;
opts.acceptableCodeClasses = (siteSettings.highlighted_languages || "").split("|").concat(['auto', 'nohighlight']);
});
export function setup(helper) {
if (helper.markdownIt) { return; }
helper.whiteList({
custom(tag, name, value) {
@ -45,7 +34,50 @@ export function setup(helper) {
}
});
helper.registerPlugin(md=>{
md.renderer.rules.fence = (tokens,idx,options,env,slf)=>render(tokens,idx,options,env,slf,md);
helper.replaceBlock({
start: /^`{3}([^\n\[\]]+)?\n?([\s\S]*)?/gm,
stop: /^```$/gm,
withoutLeading: /\[quote/gm, //if leading text contains a quote this should not match
emitter(blockContents, matches) {
const opts = helper.getOptions();
let codeLang = opts.defaultCodeLang;
const acceptableCodeClasses = opts.acceptableCodeClasses;
if (acceptableCodeClasses && matches[1] && acceptableCodeClasses.indexOf(matches[1]) !== -1) {
codeLang = matches[1];
}
if (TEXT_CODE_CLASSES.indexOf(matches[1]) !== -1) {
return ['p', ['pre', ['code', {'class': 'lang-nohighlight'}, codeFlattenBlocks(blockContents) ]]];
} else {
return ['p', ['pre', ['code', {'class': 'lang-' + codeLang}, codeFlattenBlocks(blockContents) ]]];
}
}
});
helper.replaceBlock({
start: /(<pre[^\>]*\>)([\s\S]*)/igm,
stop: /<\/pre>/igm,
rawContents: true,
skipIfTradtionalLinebreaks: true,
emitter(blockContents) {
return ['p', ['pre', codeFlattenBlocks(blockContents)]];
}
});
// Ensure that content in a code block is fully escaped. This way it's not white listed
// and we can use HTML and Javascript examples.
helper.onParseNode(function(event) {
const node = event.node,
path = event.path;
if (node[0] === 'code') {
const regexp = (path && path[path.length-1] && path[path.length-1][0] && path[path.length-1][0] === "pre") ?
/ +$/g : /^ +| +$/g;
const contents = node[node.length-1];
node[node.length-1] = escape(contents.replace(regexp,''));
}
});
}