2020-10-28 10:22:06 +08:00
|
|
|
import AllowLister from "pretty-text/allow-lister";
|
|
|
|
import deprecated from "discourse-common/lib/deprecated";
|
2017-06-27 01:09:02 +08:00
|
|
|
import guid from "pretty-text/guid";
|
2017-06-09 06:02:30 +08:00
|
|
|
import { sanitize } from "pretty-text/sanitizer";
|
2019-11-01 00:54:46 +08:00
|
|
|
|
|
|
|
export const ATTACHMENT_CSS_CLASS = "attachment";
|
2017-06-09 06:02:30 +08:00
|
|
|
|
|
|
|
function deprecate(feature, name) {
|
|
|
|
return function () {
|
2017-06-24 03:24:11 +08:00
|
|
|
if (window.console && window.console.log) {
|
|
|
|
window.console.log(
|
|
|
|
feature +
|
|
|
|
": " +
|
|
|
|
name +
|
|
|
|
" is deprecated, please use the new markdown it APIs"
|
|
|
|
);
|
2017-06-09 06:02:30 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-14 20:27:28 +08:00
|
|
|
function createHelper(
|
|
|
|
featureName,
|
|
|
|
opts,
|
|
|
|
optionCallbacks,
|
|
|
|
pluginCallbacks,
|
|
|
|
getOptions,
|
2020-10-28 10:22:06 +08:00
|
|
|
allowListed
|
2017-07-14 20:27:28 +08:00
|
|
|
) {
|
2017-06-09 06:02:30 +08:00
|
|
|
let helper = {};
|
|
|
|
helper.markdownIt = true;
|
2020-10-28 10:22:06 +08:00
|
|
|
helper.allowList = (info) => allowListed.push([featureName, info]);
|
|
|
|
helper.whiteList = (info) => {
|
|
|
|
deprecated("`whiteList` has been replaced with `allowList`", {
|
|
|
|
since: "2.6.0.beta.4",
|
|
|
|
dropFrom: "2.7.0",
|
|
|
|
});
|
|
|
|
helper.allowList(info);
|
|
|
|
};
|
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
helper.registerInline = deprecate(featureName, "registerInline");
|
|
|
|
helper.replaceBlock = deprecate(featureName, "replaceBlock");
|
|
|
|
helper.addPreProcessor = deprecate(featureName, "addPreProcessor");
|
|
|
|
helper.inlineReplace = deprecate(featureName, "inlineReplace");
|
|
|
|
helper.postProcessTag = deprecate(featureName, "postProcessTag");
|
|
|
|
helper.inlineRegexp = deprecate(featureName, "inlineRegexp");
|
|
|
|
helper.inlineBetween = deprecate(featureName, "inlineBetween");
|
|
|
|
helper.postProcessText = deprecate(featureName, "postProcessText");
|
|
|
|
helper.onParseNode = deprecate(featureName, "onParseNode");
|
|
|
|
helper.registerBlock = deprecate(featureName, "registerBlock");
|
|
|
|
// hack to allow moving of getOptions
|
|
|
|
helper.getOptions = () => getOptions.f();
|
|
|
|
|
2017-06-23 23:36:45 +08:00
|
|
|
helper.registerOptions = (callback) => {
|
2017-06-09 06:02:30 +08:00
|
|
|
optionCallbacks.push([featureName, callback]);
|
|
|
|
};
|
|
|
|
|
2017-06-23 23:36:45 +08:00
|
|
|
helper.registerPlugin = (callback) => {
|
2017-06-09 06:02:30 +08:00
|
|
|
pluginCallbacks.push([featureName, callback]);
|
|
|
|
};
|
|
|
|
|
|
|
|
return helper;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO we may just use a proper ruler from markdown it... this is a basic proxy
|
|
|
|
class Ruler {
|
|
|
|
constructor() {
|
|
|
|
this.rules = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
getRules() {
|
|
|
|
return this.rules;
|
|
|
|
}
|
|
|
|
|
2017-07-01 03:19:07 +08:00
|
|
|
getRuleForTag(tag) {
|
|
|
|
this.ensureCache();
|
2017-12-27 13:11:30 +08:00
|
|
|
if (this.cache.hasOwnProperty(tag)) {
|
|
|
|
return this.cache[tag];
|
|
|
|
}
|
2017-07-01 03:19:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ensureCache() {
|
|
|
|
if (this.cache) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.cache = {};
|
|
|
|
for (let i = this.rules.length - 1; i >= 0; i--) {
|
|
|
|
let info = this.rules[i];
|
|
|
|
this.cache[info.rule.tag] = info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
push(name, rule) {
|
|
|
|
this.rules.push({ name, rule });
|
2017-07-01 03:19:07 +08:00
|
|
|
this.cache = null;
|
2017-06-09 06:02:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// block bb code ruler for parsing of quotes / code / polls
|
|
|
|
function setupBlockBBCode(md) {
|
2017-07-18 04:21:47 +08:00
|
|
|
md.block.bbcode = { ruler: new Ruler() };
|
2017-06-09 06:02:30 +08:00
|
|
|
}
|
|
|
|
|
2017-06-23 23:36:45 +08:00
|
|
|
function setupInlineBBCode(md) {
|
2017-07-18 04:21:47 +08:00
|
|
|
md.inline.bbcode = { ruler: new Ruler() };
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupTextPostProcessRuler(md) {
|
|
|
|
const TextPostProcessRuler = requirejs(
|
|
|
|
"pretty-text/engines/discourse-markdown/text-post-process"
|
|
|
|
).TextPostProcessRuler;
|
|
|
|
md.core.textPostProcess = { ruler: new TextPostProcessRuler() };
|
2017-06-23 23:36:45 +08:00
|
|
|
}
|
|
|
|
|
2017-06-27 01:09:02 +08:00
|
|
|
function renderHoisted(tokens, idx, options) {
|
|
|
|
const content = tokens[idx].content;
|
|
|
|
if (content && content.length > 0) {
|
|
|
|
let id = guid();
|
|
|
|
options.discourse.hoisted[id] = tokens[idx].content;
|
|
|
|
return id;
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 04:32:53 +08:00
|
|
|
function setupUrlDecoding(md) {
|
|
|
|
// this fixed a subtle issue where %20 is decoded as space in
|
|
|
|
// automatic urls
|
|
|
|
md.utils.lib.mdurl.decode.defaultChars = ";/?:@&=+$,# ";
|
|
|
|
}
|
|
|
|
|
2017-06-27 01:09:02 +08:00
|
|
|
function setupHoister(md) {
|
|
|
|
md.renderer.rules.html_raw = renderHoisted;
|
|
|
|
}
|
|
|
|
|
2019-12-09 22:20:03 +08:00
|
|
|
export function extractDataAttribute(str) {
|
|
|
|
let sep = str.indexOf("=");
|
|
|
|
if (sep === -1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const key = `data-${str.substr(0, sep)}`.toLowerCase();
|
|
|
|
if (!/^[A-Za-z]+[\w\-\:\.]*$/.test(key)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const value = str.substr(sep + 1);
|
|
|
|
return [key, value];
|
|
|
|
}
|
|
|
|
|
2020-01-23 07:41:39 +08:00
|
|
|
// videoHTML and audioHTML follow the same HTML syntax
|
|
|
|
// as oneboxer.rb when dealing with these formats
|
2020-07-18 05:31:01 +08:00
|
|
|
function videoHTML(token) {
|
2020-01-23 07:41:39 +08:00
|
|
|
const src = token.attrGet("src");
|
|
|
|
const origSrc = token.attrGet("data-orig-src");
|
2020-03-03 12:44:01 +08:00
|
|
|
const dataOrigSrcAttr = origSrc !== null ? `data-orig-src="${origSrc}"` : "";
|
2020-01-29 13:52:02 +08:00
|
|
|
return `<div class="video-container">
|
2020-07-16 06:36:51 +08:00
|
|
|
<video width="100%" height="100%" preload="metadata" controls>
|
2020-03-03 12:44:01 +08:00
|
|
|
<source src="${src}" ${dataOrigSrcAttr}>
|
2020-01-29 13:52:02 +08:00
|
|
|
<a href="${src}">${src}</a>
|
|
|
|
</video>
|
|
|
|
</div>`;
|
2020-01-23 07:41:39 +08:00
|
|
|
}
|
|
|
|
|
2020-07-16 06:36:51 +08:00
|
|
|
function audioHTML(token) {
|
2020-01-23 07:41:39 +08:00
|
|
|
const src = token.attrGet("src");
|
|
|
|
const origSrc = token.attrGet("data-orig-src");
|
2020-03-03 12:44:01 +08:00
|
|
|
const dataOrigSrcAttr = origSrc !== null ? `data-orig-src="${origSrc}"` : "";
|
2020-07-16 06:36:51 +08:00
|
|
|
return `<audio preload="metadata" controls>
|
2020-03-03 12:44:01 +08:00
|
|
|
<source src="${src}" ${dataOrigSrcAttr}>
|
2020-01-23 07:41:39 +08:00
|
|
|
<a href="${src}">${src}</a>
|
|
|
|
</audio>`;
|
|
|
|
}
|
|
|
|
|
2019-02-08 04:04:09 +08:00
|
|
|
const IMG_SIZE_REGEX = /^([1-9]+[0-9]*)x([1-9]+[0-9]*)(\s*,\s*(x?)([1-9][0-9]{0,2}?)([%x]?))?$/;
|
2020-01-23 07:41:39 +08:00
|
|
|
function renderImageOrPlayableMedia(tokens, idx, options, env, slf) {
|
2019-12-09 22:20:03 +08:00
|
|
|
const token = tokens[idx];
|
|
|
|
const alt = slf.renderInlineAsText(token.children, options, env);
|
|
|
|
const split = alt.split("|");
|
2021-02-11 23:44:41 +08:00
|
|
|
const altSplit = [split[0]];
|
2019-12-09 22:20:03 +08:00
|
|
|
|
2020-01-23 07:41:39 +08:00
|
|
|
// markdown-it supports returning HTML instead of continuing to render the current token
|
|
|
|
// see https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer
|
2020-01-29 13:52:02 +08:00
|
|
|
// handles |video and |audio alt transformations for image tags
|
2020-01-23 07:41:39 +08:00
|
|
|
if (split[1] === "video") {
|
2021-10-08 20:57:08 +08:00
|
|
|
if (
|
|
|
|
options.discourse.previewing &&
|
|
|
|
!options.discourse.limitedSiteSettings.enableDiffhtmlPreview
|
|
|
|
) {
|
2020-10-21 00:01:32 +08:00
|
|
|
return `<div class="onebox-placeholder-container">
|
|
|
|
<span class="placeholder-icon video"></span>
|
|
|
|
</div>`;
|
|
|
|
} else {
|
|
|
|
return videoHTML(token);
|
|
|
|
}
|
2020-01-23 07:41:39 +08:00
|
|
|
} else if (split[1] === "audio") {
|
2020-07-16 06:36:51 +08:00
|
|
|
return audioHTML(token);
|
2020-01-23 07:41:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// parsing ![myimage|500x300]() or ![myimage|75%]() or ![myimage|500x300, 75%]
|
2021-02-11 23:44:41 +08:00
|
|
|
for (let i = 1, match, data; i < split.length; ++i) {
|
2019-12-09 22:20:03 +08:00
|
|
|
if ((match = split[i].match(IMG_SIZE_REGEX)) && match[1] && match[2]) {
|
|
|
|
let width = match[1];
|
|
|
|
let height = match[2];
|
|
|
|
|
|
|
|
// calculate using percentage
|
|
|
|
if (match[5] && match[6] && match[6] === "%") {
|
|
|
|
let percent = parseFloat(match[5]) / 100.0;
|
|
|
|
width = parseInt(width * percent, 10);
|
|
|
|
height = parseInt(height * percent, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate using only given width
|
|
|
|
if (match[5] && match[6] && match[6] === "x") {
|
|
|
|
let wr = parseFloat(match[5]) / width;
|
|
|
|
width = parseInt(match[5], 10);
|
|
|
|
height = parseInt(height * wr, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate using only given height
|
|
|
|
if (match[5] && match[4] && match[4] === "x" && !match[6]) {
|
|
|
|
let hr = parseFloat(match[5]) / height;
|
|
|
|
height = parseInt(match[5], 10);
|
|
|
|
width = parseInt(width * hr, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token.attrIndex("width") === -1) {
|
|
|
|
token.attrs.push(["width", width]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token.attrIndex("height") === -1) {
|
|
|
|
token.attrs.push(["height", height]);
|
2017-07-12 00:13:03 +08:00
|
|
|
}
|
2019-12-09 22:20:03 +08:00
|
|
|
|
2020-09-22 22:28:28 +08:00
|
|
|
if (
|
|
|
|
options.discourse.previewing &&
|
|
|
|
match[6] !== "x" &&
|
|
|
|
match[4] !== "x"
|
|
|
|
) {
|
2019-12-09 22:20:03 +08:00
|
|
|
token.attrs.push(["class", "resizable"]);
|
2020-09-22 22:28:28 +08:00
|
|
|
}
|
2019-12-09 22:20:03 +08:00
|
|
|
} else if ((data = extractDataAttribute(split[i]))) {
|
|
|
|
token.attrs.push(data);
|
2021-02-11 23:44:41 +08:00
|
|
|
} else if (split[i] === "thumbnail") {
|
|
|
|
token.attrs.push(["data-thumbnail", "true"]);
|
2019-12-09 22:20:03 +08:00
|
|
|
} else {
|
|
|
|
altSplit.push(split[i]);
|
2017-07-12 00:13:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 22:20:03 +08:00
|
|
|
token.attrs[token.attrIndex("alt")][1] = altSplit.join("|");
|
2017-07-12 00:13:03 +08:00
|
|
|
return slf.renderToken(tokens, idx, options);
|
|
|
|
}
|
|
|
|
|
2020-01-23 07:41:39 +08:00
|
|
|
// we have taken over the ![]() syntax in markdown to
|
|
|
|
// be able to render a video or audio URL as well as the
|
|
|
|
// image using |video and |audio in the text inside []
|
|
|
|
function setupImageAndPlayableMediaRenderer(md) {
|
|
|
|
md.renderer.rules.image = renderImageOrPlayableMedia;
|
2017-07-12 00:13:03 +08:00
|
|
|
}
|
|
|
|
|
2019-05-29 09:00:25 +08:00
|
|
|
function renderAttachment(tokens, idx, options, env, slf) {
|
2019-12-09 22:20:03 +08:00
|
|
|
const linkToken = tokens[idx];
|
|
|
|
const textToken = tokens[idx + 1];
|
|
|
|
|
|
|
|
const split = textToken.content.split("|");
|
|
|
|
const contentSplit = [];
|
|
|
|
|
|
|
|
for (let i = 0, data; i < split.length; ++i) {
|
|
|
|
if (split[i] === ATTACHMENT_CSS_CLASS) {
|
|
|
|
linkToken.attrs.unshift(["class", split[i]]);
|
|
|
|
} else if ((data = extractDataAttribute(split[i]))) {
|
|
|
|
linkToken.attrs.push(data);
|
|
|
|
} else {
|
|
|
|
contentSplit.push(split[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (contentSplit.length > 0) {
|
|
|
|
textToken.content = contentSplit.join("|");
|
2019-05-29 09:00:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return slf.renderToken(tokens, idx, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupAttachments(md) {
|
|
|
|
md.renderer.rules.link_open = renderAttachment;
|
|
|
|
}
|
|
|
|
|
2017-07-12 04:48:25 +08:00
|
|
|
let Helpers;
|
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
export function setup(opts, siteSettings, state) {
|
|
|
|
if (opts.setup) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-12 04:48:25 +08:00
|
|
|
// we got to require this late cause bundle is not loaded in pretty-text
|
2017-07-14 20:27:28 +08:00
|
|
|
Helpers =
|
|
|
|
Helpers || requirejs("pretty-text/engines/discourse-markdown/helpers");
|
2017-07-12 04:48:25 +08:00
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
opts.markdownIt = true;
|
|
|
|
|
|
|
|
let optionCallbacks = [];
|
|
|
|
let pluginCallbacks = [];
|
|
|
|
|
|
|
|
// ideally I would like to change the top level API a bit, but in the mean time this will do
|
|
|
|
let getOptions = {
|
|
|
|
f: () => opts,
|
|
|
|
};
|
|
|
|
|
|
|
|
const check = /discourse-markdown\/|markdown-it\//;
|
|
|
|
let features = [];
|
2020-10-28 10:22:06 +08:00
|
|
|
let allowListed = [];
|
2017-06-09 06:02:30 +08:00
|
|
|
|
|
|
|
Object.keys(require._eak_seen).forEach((entry) => {
|
|
|
|
if (check.test(entry)) {
|
2017-07-06 02:14:30 +08:00
|
|
|
const module = requirejs(entry);
|
2017-06-09 06:02:30 +08:00
|
|
|
if (module && module.setup) {
|
2020-09-19 01:29:09 +08:00
|
|
|
const id = entry.split("/").reverse()[0];
|
|
|
|
let priority = module.priority || 0;
|
|
|
|
features.unshift({ id, setup: module.setup, priority });
|
2017-06-09 06:02:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-09-19 01:29:09 +08:00
|
|
|
features
|
|
|
|
.sort((a, b) => a.priority - b.priority)
|
|
|
|
.forEach((f) => {
|
|
|
|
f.setup(
|
|
|
|
createHelper(
|
|
|
|
f.id,
|
|
|
|
opts,
|
|
|
|
optionCallbacks,
|
|
|
|
pluginCallbacks,
|
|
|
|
getOptions,
|
2020-10-28 10:22:06 +08:00
|
|
|
allowListed
|
2020-09-19 01:29:09 +08:00
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-10-28 10:22:06 +08:00
|
|
|
Object.entries(state.allowListed || {}).forEach((entry) => {
|
|
|
|
allowListed.push(entry);
|
2018-07-12 12:13:52 +08:00
|
|
|
});
|
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
optionCallbacks.forEach(([, callback]) => {
|
|
|
|
callback(opts, siteSettings, state);
|
|
|
|
});
|
|
|
|
|
|
|
|
// enable all features by default
|
|
|
|
features.forEach((feature) => {
|
2020-09-19 01:29:09 +08:00
|
|
|
if (!opts.features.hasOwnProperty(feature.id)) {
|
|
|
|
opts.features[feature.id] = true;
|
2017-06-09 06:02:30 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let copy = {};
|
|
|
|
Object.keys(opts).forEach((entry) => {
|
|
|
|
copy[entry] = opts[entry];
|
|
|
|
delete opts[entry];
|
|
|
|
});
|
|
|
|
|
2017-07-12 04:48:25 +08:00
|
|
|
copy.helpers = {
|
|
|
|
textReplace: Helpers.textReplace,
|
|
|
|
};
|
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
opts.discourse = copy;
|
|
|
|
getOptions.f = () => opts.discourse;
|
|
|
|
|
2020-07-16 06:52:59 +08:00
|
|
|
opts.discourse.limitedSiteSettings = {
|
|
|
|
secureMedia: siteSettings.secure_media,
|
2021-10-08 20:57:08 +08:00
|
|
|
enableDiffhtmlPreview: siteSettings.enable_diffhtml_preview,
|
2020-07-16 06:52:59 +08:00
|
|
|
};
|
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
opts.engine = window.markdownit({
|
|
|
|
discourse: opts.discourse,
|
|
|
|
html: true,
|
|
|
|
breaks: opts.discourse.features.newline,
|
|
|
|
xhtmlOut: false,
|
2018-02-01 10:22:38 +08:00
|
|
|
linkify: siteSettings.enable_markdown_linkify,
|
2019-07-12 05:56:22 +08:00
|
|
|
typographer: siteSettings.enable_markdown_typographer,
|
2017-06-09 06:02:30 +08:00
|
|
|
});
|
|
|
|
|
2019-07-12 05:56:22 +08:00
|
|
|
const quotation_marks = siteSettings.markdown_typographer_quotation_marks;
|
|
|
|
if (quotation_marks) {
|
|
|
|
opts.engine.options.quotes = quotation_marks.split("|");
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:56:22 +08:00
|
|
|
opts.engine.linkify.tlds(
|
|
|
|
(siteSettings.markdown_linkify_tlds || "").split("|")
|
|
|
|
);
|
2018-02-01 10:22:38 +08:00
|
|
|
|
2017-07-04 04:32:53 +08:00
|
|
|
setupUrlDecoding(opts.engine);
|
2017-06-27 01:09:02 +08:00
|
|
|
setupHoister(opts.engine);
|
2020-01-23 07:41:39 +08:00
|
|
|
setupImageAndPlayableMediaRenderer(opts.engine);
|
2019-05-29 09:00:25 +08:00
|
|
|
setupAttachments(opts.engine);
|
2017-06-09 06:02:30 +08:00
|
|
|
setupBlockBBCode(opts.engine);
|
2017-06-23 23:36:45 +08:00
|
|
|
setupInlineBBCode(opts.engine);
|
2017-07-18 04:21:47 +08:00
|
|
|
setupTextPostProcessRuler(opts.engine);
|
2017-06-09 06:02:30 +08:00
|
|
|
|
|
|
|
pluginCallbacks.forEach(([feature, callback]) => {
|
|
|
|
if (opts.discourse.features[feature]) {
|
|
|
|
opts.engine.use(callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// top level markdown it notifier
|
|
|
|
opts.markdownIt = true;
|
|
|
|
opts.setup = true;
|
|
|
|
|
2017-07-14 20:27:28 +08:00
|
|
|
if (!opts.discourse.sanitizer || !opts.sanitizer) {
|
2020-10-28 10:22:06 +08:00
|
|
|
const allowLister = new AllowLister(opts.discourse);
|
2017-07-14 20:27:28 +08:00
|
|
|
|
2020-10-28 10:22:06 +08:00
|
|
|
allowListed.forEach(([feature, info]) => {
|
|
|
|
allowLister.allowListFeature(feature, info);
|
2017-07-14 20:27:28 +08:00
|
|
|
});
|
|
|
|
|
2017-06-27 03:21:27 +08:00
|
|
|
opts.sanitizer = opts.discourse.sanitizer = !!opts.discourse.sanitize
|
2020-10-28 10:22:06 +08:00
|
|
|
? (a) => sanitize(a, allowLister)
|
2017-06-27 03:21:27 +08:00
|
|
|
: (a) => a;
|
2017-06-09 06:02:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function cook(raw, opts) {
|
2020-07-27 08:23:54 +08:00
|
|
|
// we still have to hoist html_raw nodes so they bypass the allowlister
|
2017-06-27 01:09:02 +08:00
|
|
|
// this is the case for oneboxes
|
|
|
|
let hoisted = {};
|
|
|
|
|
|
|
|
opts.discourse.hoisted = hoisted;
|
|
|
|
|
2017-06-27 01:24:55 +08:00
|
|
|
const rendered = opts.engine.render(raw);
|
2017-06-27 03:21:27 +08:00
|
|
|
let cooked = opts.discourse.sanitizer(rendered).trim();
|
2017-06-27 01:09:02 +08:00
|
|
|
|
|
|
|
const keys = Object.keys(hoisted);
|
|
|
|
if (keys.length) {
|
|
|
|
let found = true;
|
|
|
|
|
|
|
|
const unhoist = function (key) {
|
|
|
|
cooked = cooked.replace(new RegExp(key, "g"), function () {
|
|
|
|
found = true;
|
|
|
|
return hoisted[key];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
while (found) {
|
|
|
|
found = false;
|
|
|
|
keys.forEach(unhoist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete opts.discourse.hoisted;
|
|
|
|
return cooked;
|
2017-06-09 06:02:30 +08:00
|
|
|
}
|