mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +08:00
REFACTOR: Move the discourse-markdown
engine out of the addon repo
The reasoning here is that we want to make a unique bundle for this folder and the default approach is to include everything in `addon`.
This commit is contained in:
parent
76477a1c8b
commit
f3156a6478
20 changed files with 19 additions and 19 deletions
|
@ -0,0 +1,58 @@
|
|||
// we need a custom renderer for code blocks cause we have a slightly non compliant
|
||||
// format with special handling for text and so on
|
||||
|
||||
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.trim().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`;
|
||||
}
|
||||
|
||||
export function setup(helper) {
|
||||
helper.registerOptions((opts, siteSettings) => {
|
||||
opts.defaultCodeLang = siteSettings.default_code_lang;
|
||||
opts.acceptableCodeClasses = (siteSettings.highlighted_languages || "")
|
||||
.split("|")
|
||||
.concat(["auto", "nohighlight"]);
|
||||
});
|
||||
|
||||
helper.whiteList({
|
||||
custom(tag, name, value) {
|
||||
if (tag === "code" && name === "class") {
|
||||
const m = /^lang\-(.+)$/.exec(value);
|
||||
if (m) {
|
||||
return helper.getOptions().acceptableCodeClasses.indexOf(m[1]) !== -1;
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
helper.registerPlugin((md) => {
|
||||
md.renderer.rules.fence = (tokens, idx, options, env, slf) =>
|
||||
render(tokens, idx, options, env, slf, md);
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue