2020-10-27 22:22:06 -04:00
|
|
|
import AllowLister from "pretty-text/allow-lister";
|
2022-02-03 18:00:40 -05:00
|
|
|
import { cloneJSON } from "discourse-common/lib/object";
|
2020-10-27 22:22:06 -04:00
|
|
|
import deprecated from "discourse-common/lib/deprecated";
|
2017-06-26 13:09:02 -04:00
|
|
|
import guid from "pretty-text/guid";
|
2017-06-08 18:02:30 -04:00
|
|
|
import { sanitize } from "pretty-text/sanitizer";
|
2019-10-31 12:54:46 -04:00
|
|
|
|
|
|
|
export const ATTACHMENT_CSS_CLASS = "attachment";
|
2017-06-08 18:02:30 -04:00
|
|
|
|
|
|
|
function deprecate(feature, name) {
|
|
|
|
return function () {
|
2017-06-23 15:24:11 -04:00
|
|
|
if (window.console && window.console.log) {
|
|
|
|
window.console.log(
|
|
|
|
feature +
|
|
|
|
": " +
|
|
|
|
name +
|
|
|
|
" is deprecated, please use the new markdown it APIs"
|
|
|
|
);
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-03 18:00:40 -05:00
|
|
|
// We have some custom extensions and extension points for markdown-it, including
|
|
|
|
// the helper (passed in via setup) that has registerOptions, registerPlugin etc.
|
|
|
|
// as well as our postProcessText rule to replace text with a regex.
|
|
|
|
//
|
|
|
|
// Take a look at https://meta.discourse.org/t/developers-guide-to-markdown-extensions/66023
|
|
|
|
// for more detailed information.
|
2017-07-14 08:27:28 -04:00
|
|
|
function createHelper(
|
|
|
|
featureName,
|
|
|
|
opts,
|
|
|
|
optionCallbacks,
|
|
|
|
pluginCallbacks,
|
2022-02-03 18:00:40 -05:00
|
|
|
customMarkdownCookFnCallbacks,
|
2017-07-14 08:27:28 -04:00
|
|
|
getOptions,
|
2020-10-27 22:22:06 -04:00
|
|
|
allowListed
|
2017-07-14 08:27:28 -04:00
|
|
|
) {
|
2017-06-08 18:02:30 -04:00
|
|
|
let helper = {};
|
|
|
|
helper.markdownIt = true;
|
2020-10-27 22:22:06 -04: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",
|
2022-11-16 05:00:39 -05:00
|
|
|
id: "discourse.markdown-it.whitelist",
|
2020-10-27 22:22:06 -04:00
|
|
|
});
|
|
|
|
helper.allowList(info);
|
|
|
|
};
|
|
|
|
|
2017-06-08 18:02:30 -04: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 11:36:45 -04:00
|
|
|
helper.registerOptions = (callback) => {
|
2017-06-08 18:02:30 -04:00
|
|
|
optionCallbacks.push([featureName, callback]);
|
|
|
|
};
|
|
|
|
|
2017-06-23 11:36:45 -04:00
|
|
|
helper.registerPlugin = (callback) => {
|
2017-06-08 18:02:30 -04:00
|
|
|
pluginCallbacks.push([featureName, callback]);
|
|
|
|
};
|
|
|
|
|
2022-02-03 18:00:40 -05:00
|
|
|
helper.buildCookFunction = (callback) => {
|
|
|
|
customMarkdownCookFnCallbacks.push([featureName, callback]);
|
|
|
|
};
|
|
|
|
|
2017-06-08 18:02:30 -04:00
|
|
|
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-06-30 15:19:07 -04:00
|
|
|
getRuleForTag(tag) {
|
|
|
|
this.ensureCache();
|
2017-12-27 00:11:30 -05:00
|
|
|
if (this.cache.hasOwnProperty(tag)) {
|
|
|
|
return this.cache[tag];
|
|
|
|
}
|
2017-06-30 15:19:07 -04: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-08 18:02:30 -04:00
|
|
|
push(name, rule) {
|
|
|
|
this.rules.push({ name, rule });
|
2017-06-30 15:19:07 -04:00
|
|
|
this.cache = null;
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// block bb code ruler for parsing of quotes / code / polls
|
|
|
|
function setupBlockBBCode(md) {
|
2017-07-17 16:21:47 -04:00
|
|
|
md.block.bbcode = { ruler: new Ruler() };
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|
|
|
|
|
2022-02-03 18:00:40 -05:00
|
|
|
// inline bbcode ruler for parsing of spoiler tags, discourse-chart etc
|
2017-06-23 11:36:45 -04:00
|
|
|
function setupInlineBBCode(md) {
|
2017-07-17 16:21:47 -04:00
|
|
|
md.inline.bbcode = { ruler: new Ruler() };
|
|
|
|
}
|
|
|
|
|
2022-02-03 18:00:40 -05:00
|
|
|
// rule for text replacement via regex, used for @mentions, category hashtags, etc.
|
2017-07-17 16:21:47 -04:00
|
|
|
function setupTextPostProcessRuler(md) {
|
|
|
|
const TextPostProcessRuler = requirejs(
|
|
|
|
"pretty-text/engines/discourse-markdown/text-post-process"
|
|
|
|
).TextPostProcessRuler;
|
|
|
|
md.core.textPostProcess = { ruler: new TextPostProcessRuler() };
|
2017-06-23 11:36:45 -04:00
|
|
|
}
|
|
|
|
|
2022-02-03 18:00:40 -05:00
|
|
|
// hoists html_raw tokens out of the render flow and replaces them
|
|
|
|
// with a GUID. this GUID is then replaced with the final raw HTML
|
|
|
|
// content in unhoistForCooked
|
2017-06-26 13:09:02 -04:00
|
|
|
function renderHoisted(tokens, idx, options) {
|
|
|
|
const content = tokens[idx].content;
|
|
|
|
if (content && content.length > 0) {
|
|
|
|
let id = guid();
|
2022-02-03 18:00:40 -05:00
|
|
|
options.discourse.hoisted[id] = content;
|
2017-06-26 13:09:02 -04:00
|
|
|
return id;
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 16:32:53 -04:00
|
|
|
function setupUrlDecoding(md) {
|
|
|
|
// this fixed a subtle issue where %20 is decoded as space in
|
|
|
|
// automatic urls
|
|
|
|
md.utils.lib.mdurl.decode.defaultChars = ";/?:@&=+$,# ";
|
|
|
|
}
|
|
|
|
|
2022-02-03 18:00:40 -05:00
|
|
|
// html_raw tokens, funnily enough, render raw HTML via renderHoisted and
|
|
|
|
// unhoistForCooked
|
2017-06-26 13:09:02 -04:00
|
|
|
function setupHoister(md) {
|
|
|
|
md.renderer.rules.html_raw = renderHoisted;
|
|
|
|
}
|
|
|
|
|
2020-01-22 18:41:39 -05:00
|
|
|
// videoHTML and audioHTML follow the same HTML syntax
|
|
|
|
// as oneboxer.rb when dealing with these formats
|
2020-07-17 17:31:01 -04:00
|
|
|
function videoHTML(token) {
|
2020-01-22 18:41:39 -05:00
|
|
|
const src = token.attrGet("src");
|
|
|
|
const origSrc = token.attrGet("data-orig-src");
|
2020-03-02 23:44:01 -05:00
|
|
|
const dataOrigSrcAttr = origSrc !== null ? `data-orig-src="${origSrc}"` : "";
|
2020-01-29 00:52:02 -05:00
|
|
|
return `<div class="video-container">
|
2023-03-09 15:19:19 -05:00
|
|
|
<video width="100%" height="100%" preload="metadata" crossOrigin="anonymous" controls>
|
2020-03-02 23:44:01 -05:00
|
|
|
<source src="${src}" ${dataOrigSrcAttr}>
|
2020-01-29 00:52:02 -05:00
|
|
|
<a href="${src}">${src}</a>
|
|
|
|
</video>
|
|
|
|
</div>`;
|
2020-01-22 18:41:39 -05:00
|
|
|
}
|
|
|
|
|
2020-07-15 18:36:51 -04:00
|
|
|
function audioHTML(token) {
|
2020-01-22 18:41:39 -05:00
|
|
|
const src = token.attrGet("src");
|
|
|
|
const origSrc = token.attrGet("data-orig-src");
|
2020-03-02 23:44:01 -05:00
|
|
|
const dataOrigSrcAttr = origSrc !== null ? `data-orig-src="${origSrc}"` : "";
|
2020-07-15 18:36:51 -04:00
|
|
|
return `<audio preload="metadata" controls>
|
2020-03-02 23:44:01 -05:00
|
|
|
<source src="${src}" ${dataOrigSrcAttr}>
|
2020-01-22 18:41:39 -05:00
|
|
|
<a href="${src}">${src}</a>
|
|
|
|
</audio>`;
|
|
|
|
}
|
|
|
|
|
2022-07-06 04:37:54 -04: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-22 18:41:39 -05:00
|
|
|
function renderImageOrPlayableMedia(tokens, idx, options, env, slf) {
|
2019-12-09 09:20:03 -05:00
|
|
|
const token = tokens[idx];
|
|
|
|
const alt = slf.renderInlineAsText(token.children, options, env);
|
|
|
|
const split = alt.split("|");
|
2021-02-11 10:44:41 -05:00
|
|
|
const altSplit = [split[0]];
|
2019-12-09 09:20:03 -05:00
|
|
|
|
2020-01-22 18:41:39 -05: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 00:52:02 -05:00
|
|
|
// handles |video and |audio alt transformations for image tags
|
2020-01-22 18:41:39 -05:00
|
|
|
if (split[1] === "video") {
|
2021-10-08 08:57:08 -04:00
|
|
|
if (
|
|
|
|
options.discourse.previewing &&
|
|
|
|
!options.discourse.limitedSiteSettings.enableDiffhtmlPreview
|
|
|
|
) {
|
2020-10-20 12:01:32 -04:00
|
|
|
return `<div class="onebox-placeholder-container">
|
|
|
|
<span class="placeholder-icon video"></span>
|
|
|
|
</div>`;
|
|
|
|
} else {
|
|
|
|
return videoHTML(token);
|
|
|
|
}
|
2020-01-22 18:41:39 -05:00
|
|
|
} else if (split[1] === "audio") {
|
2020-07-15 18:36:51 -04:00
|
|
|
return audioHTML(token);
|
2020-01-22 18:41:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// parsing ![myimage|500x300]() or ![myimage|75%]() or ![myimage|500x300, 75%]
|
2021-02-11 10:44:41 -05:00
|
|
|
for (let i = 1, match, data; i < split.length; ++i) {
|
2019-12-09 09:20:03 -05: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-11 12:13:03 -04:00
|
|
|
}
|
2019-12-09 09:20:03 -05:00
|
|
|
|
2020-09-22 10:28:28 -04:00
|
|
|
if (
|
|
|
|
options.discourse.previewing &&
|
|
|
|
match[6] !== "x" &&
|
|
|
|
match[4] !== "x"
|
|
|
|
) {
|
2019-12-09 09:20:03 -05:00
|
|
|
token.attrs.push(["class", "resizable"]);
|
2020-09-22 10:28:28 -04:00
|
|
|
}
|
2019-12-09 09:20:03 -05:00
|
|
|
} else if ((data = extractDataAttribute(split[i]))) {
|
|
|
|
token.attrs.push(data);
|
2021-02-11 10:44:41 -05:00
|
|
|
} else if (split[i] === "thumbnail") {
|
|
|
|
token.attrs.push(["data-thumbnail", "true"]);
|
2019-12-09 09:20:03 -05:00
|
|
|
} else {
|
|
|
|
altSplit.push(split[i]);
|
2017-07-11 12:13:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 07:07:37 -04:00
|
|
|
const altValue = altSplit.join("|").trim();
|
|
|
|
if (altValue === "") {
|
|
|
|
token.attrSet("role", "presentation");
|
|
|
|
} else {
|
|
|
|
token.attrSet("alt", altValue);
|
|
|
|
}
|
|
|
|
|
2017-07-11 12:13:03 -04:00
|
|
|
return slf.renderToken(tokens, idx, options);
|
|
|
|
}
|
|
|
|
|
2020-01-22 18:41:39 -05: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-11 12:13:03 -04:00
|
|
|
}
|
|
|
|
|
2019-05-28 21:00:25 -04:00
|
|
|
function renderAttachment(tokens, idx, options, env, slf) {
|
2019-12-09 09:20:03 -05: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-28 21:00:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return slf.renderToken(tokens, idx, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupAttachments(md) {
|
|
|
|
md.renderer.rules.link_open = renderAttachment;
|
|
|
|
}
|
|
|
|
|
2022-02-03 18:00:40 -05:00
|
|
|
function buildCustomMarkdownCookFunction(engineOpts, defaultEngineOpts) {
|
|
|
|
// everything except the engine for opts can just point to the other
|
|
|
|
// opts references, they do not change and we don't need to worry about
|
|
|
|
// mutating them. note that this may need to be updated when additional
|
|
|
|
// opts are added to the pipeline
|
|
|
|
const newOpts = {};
|
|
|
|
newOpts.allowListed = defaultEngineOpts.allowListed;
|
|
|
|
newOpts.pluginCallbacks = defaultEngineOpts.pluginCallbacks;
|
|
|
|
newOpts.sanitizer = defaultEngineOpts.sanitizer;
|
|
|
|
newOpts.discourse = {};
|
|
|
|
const featureConfig = cloneJSON(defaultEngineOpts.discourse.features);
|
|
|
|
|
|
|
|
// everything from the discourse part of defaultEngineOpts can be cloned except
|
|
|
|
// the features, because these can be a limited subset and we don't want to
|
|
|
|
// change the original object reference
|
|
|
|
for (const [key, value] of Object.entries(defaultEngineOpts.discourse)) {
|
|
|
|
if (key !== "features") {
|
|
|
|
newOpts.discourse[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (engineOpts.featuresOverride !== undefined) {
|
|
|
|
overrideMarkdownFeatures(featureConfig, engineOpts.featuresOverride);
|
|
|
|
}
|
|
|
|
newOpts.discourse.features = featureConfig;
|
|
|
|
|
|
|
|
const markdownitOpts = {
|
|
|
|
discourse: newOpts.discourse,
|
|
|
|
html: defaultEngineOpts.engine.options.html,
|
|
|
|
breaks: defaultEngineOpts.engine.options.breaks,
|
|
|
|
xhtmlOut: defaultEngineOpts.engine.options.xhtmlOut,
|
|
|
|
linkify: defaultEngineOpts.engine.options.linkify,
|
|
|
|
typographer: defaultEngineOpts.engine.options.typographer,
|
|
|
|
};
|
|
|
|
newOpts.engine = createMarkdownItEngineWithOpts(
|
|
|
|
markdownitOpts,
|
|
|
|
engineOpts.markdownItRules
|
|
|
|
);
|
|
|
|
|
|
|
|
// we have to do this to make sure plugin callbacks, allow list, and helper
|
|
|
|
// functions are all set up correctly for the new engine
|
|
|
|
setupMarkdownEngine(newOpts, featureConfig);
|
|
|
|
|
|
|
|
// we don't need the whole engine as a consumer, just a cook function
|
|
|
|
// will do
|
|
|
|
return function customRenderFn(contentToRender) {
|
2022-02-27 16:54:55 -05:00
|
|
|
return cook(contentToRender, newOpts);
|
2022-02-03 18:00:40 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function createMarkdownItEngineWithOpts(markdownitOpts, ruleOverrides) {
|
|
|
|
if (ruleOverrides !== undefined) {
|
|
|
|
// Preset for "zero", https://github.com/markdown-it/markdown-it/blob/master/lib/presets/zero.js
|
2022-06-20 09:25:13 -04:00
|
|
|
return globalThis.markdownit("zero", markdownitOpts).enable(ruleOverrides);
|
2022-02-03 18:00:40 -05:00
|
|
|
}
|
2022-06-20 09:25:13 -04:00
|
|
|
return globalThis.markdownit(markdownitOpts);
|
2022-02-03 18:00:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function overrideMarkdownFeatures(features, featureOverrides) {
|
|
|
|
if (featureOverrides !== undefined) {
|
|
|
|
Object.keys(features).forEach((feature) => {
|
|
|
|
features[feature] = featureOverrides.includes(feature);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupMarkdownEngine(opts, featureConfig) {
|
|
|
|
const quotation_marks =
|
|
|
|
opts.discourse.limitedSiteSettings.markdownTypographerQuotationMarks;
|
|
|
|
if (quotation_marks) {
|
|
|
|
opts.engine.options.quotes = quotation_marks.split("|");
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.engine.linkify.tlds(
|
|
|
|
(opts.discourse.limitedSiteSettings.markdownLinkifyTlds || "").split("|")
|
|
|
|
);
|
|
|
|
|
|
|
|
setupUrlDecoding(opts.engine);
|
|
|
|
setupHoister(opts.engine);
|
|
|
|
setupImageAndPlayableMediaRenderer(opts.engine);
|
|
|
|
setupAttachments(opts.engine);
|
|
|
|
setupBlockBBCode(opts.engine);
|
|
|
|
setupInlineBBCode(opts.engine);
|
|
|
|
setupTextPostProcessRuler(opts.engine);
|
|
|
|
|
|
|
|
opts.pluginCallbacks.forEach(([feature, callback]) => {
|
|
|
|
if (featureConfig[feature]) {
|
2022-11-07 18:17:43 -05:00
|
|
|
if (callback === null || callback === undefined) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log("BAD MARKDOWN CALLBACK FOUND");
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(`FEATURE IS: ${feature}`);
|
|
|
|
}
|
2022-02-03 18:00:40 -05:00
|
|
|
opts.engine.use(callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// top level markdown it notifier
|
|
|
|
opts.markdownIt = true;
|
|
|
|
opts.setup = true;
|
|
|
|
|
|
|
|
if (!opts.discourse.sanitizer || !opts.sanitizer) {
|
|
|
|
const allowLister = new AllowLister(opts.discourse);
|
|
|
|
|
|
|
|
opts.allowListed.forEach(([feature, info]) => {
|
|
|
|
allowLister.allowListFeature(feature, info);
|
|
|
|
});
|
|
|
|
|
|
|
|
opts.sanitizer = opts.discourse.sanitizer = !!opts.discourse.sanitize
|
|
|
|
? (a) => sanitize(a, allowLister)
|
|
|
|
: (a) => a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function unhoistForCooked(hoisted, cooked) {
|
|
|
|
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];
|
|
|
|
});
|
2022-12-22 18:56:30 -05:00
|
|
|
delete hoisted[key];
|
2022-02-03 18:00:40 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
while (found) {
|
|
|
|
found = false;
|
|
|
|
keys.forEach(unhoist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cooked;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function extractDataAttribute(str) {
|
|
|
|
let sep = str.indexOf("=");
|
|
|
|
if (sep === -1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-04-01 11:35:17 -04:00
|
|
|
const key = `data-${str.slice(0, sep)}`.toLowerCase();
|
2022-02-03 18:00:40 -05:00
|
|
|
if (!/^[A-Za-z]+[\w\-\:\.]*$/.test(key)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-04-01 11:35:17 -04:00
|
|
|
const value = str.slice(sep + 1);
|
2022-02-03 18:00:40 -05:00
|
|
|
return [key, value];
|
|
|
|
}
|
|
|
|
|
2017-07-11 16:48:25 -04:00
|
|
|
let Helpers;
|
|
|
|
|
2017-06-08 18:02:30 -04:00
|
|
|
export function setup(opts, siteSettings, state) {
|
|
|
|
if (opts.setup) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-11 16:48:25 -04:00
|
|
|
// we got to require this late cause bundle is not loaded in pretty-text
|
2017-07-14 08:27:28 -04:00
|
|
|
Helpers =
|
|
|
|
Helpers || requirejs("pretty-text/engines/discourse-markdown/helpers");
|
2017-07-11 16:48:25 -04:00
|
|
|
|
2017-06-08 18:02:30 -04:00
|
|
|
opts.markdownIt = true;
|
|
|
|
|
|
|
|
let optionCallbacks = [];
|
|
|
|
let pluginCallbacks = [];
|
2022-02-03 18:00:40 -05:00
|
|
|
let customMarkdownCookFnCallbacks = [];
|
2017-06-08 18:02:30 -04:00
|
|
|
|
|
|
|
// 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-27 22:22:06 -04:00
|
|
|
let allowListed = [];
|
2017-06-08 18:02:30 -04:00
|
|
|
|
2022-02-03 18:00:40 -05:00
|
|
|
// all of the modules under discourse-markdown or markdown-it
|
|
|
|
// directories are considered additional markdown "features" which
|
|
|
|
// may define their own rules
|
2017-06-08 18:02:30 -04:00
|
|
|
Object.keys(require._eak_seen).forEach((entry) => {
|
|
|
|
if (check.test(entry)) {
|
2017-07-05 14:14:30 -04:00
|
|
|
const module = requirejs(entry);
|
2017-06-08 18:02:30 -04:00
|
|
|
if (module && module.setup) {
|
2020-09-18 13:29:09 -04:00
|
|
|
const id = entry.split("/").reverse()[0];
|
|
|
|
let priority = module.priority || 0;
|
|
|
|
features.unshift({ id, setup: module.setup, priority });
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-09-18 13:29:09 -04:00
|
|
|
features
|
|
|
|
.sort((a, b) => a.priority - b.priority)
|
2022-02-03 18:00:40 -05:00
|
|
|
.forEach((markdownFeature) => {
|
|
|
|
markdownFeature.setup(
|
2020-09-18 13:29:09 -04:00
|
|
|
createHelper(
|
2022-02-03 18:00:40 -05:00
|
|
|
markdownFeature.id,
|
2020-09-18 13:29:09 -04:00
|
|
|
opts,
|
|
|
|
optionCallbacks,
|
|
|
|
pluginCallbacks,
|
2022-02-03 18:00:40 -05:00
|
|
|
customMarkdownCookFnCallbacks,
|
2020-09-18 13:29:09 -04:00
|
|
|
getOptions,
|
2020-10-27 22:22:06 -04:00
|
|
|
allowListed
|
2020-09-18 13:29:09 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-10-27 22:22:06 -04:00
|
|
|
Object.entries(state.allowListed || {}).forEach((entry) => {
|
|
|
|
allowListed.push(entry);
|
2018-07-12 00:13:52 -04:00
|
|
|
});
|
|
|
|
|
2017-06-08 18:02:30 -04:00
|
|
|
optionCallbacks.forEach(([, callback]) => {
|
|
|
|
callback(opts, siteSettings, state);
|
|
|
|
});
|
|
|
|
|
|
|
|
// enable all features by default
|
|
|
|
features.forEach((feature) => {
|
2020-09-18 13:29:09 -04:00
|
|
|
if (!opts.features.hasOwnProperty(feature.id)) {
|
|
|
|
opts.features[feature.id] = true;
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-02-03 18:00:40 -05:00
|
|
|
if (opts.featuresOverride !== undefined) {
|
|
|
|
overrideMarkdownFeatures(opts.features, opts.featuresOverride);
|
2022-01-06 02:27:12 -05:00
|
|
|
}
|
|
|
|
|
2017-06-08 18:02:30 -04:00
|
|
|
let copy = {};
|
|
|
|
Object.keys(opts).forEach((entry) => {
|
|
|
|
copy[entry] = opts[entry];
|
|
|
|
delete opts[entry];
|
|
|
|
});
|
|
|
|
|
2017-07-11 16:48:25 -04:00
|
|
|
copy.helpers = {
|
|
|
|
textReplace: Helpers.textReplace,
|
|
|
|
};
|
|
|
|
|
2017-06-08 18:02:30 -04:00
|
|
|
opts.discourse = copy;
|
|
|
|
getOptions.f = () => opts.discourse;
|
|
|
|
|
2020-07-15 18:52:59 -04:00
|
|
|
opts.discourse.limitedSiteSettings = {
|
2022-09-28 19:24:33 -04:00
|
|
|
secureUploads: siteSettings.secure_uploads,
|
2021-10-08 08:57:08 -04:00
|
|
|
enableDiffhtmlPreview: siteSettings.enable_diffhtml_preview,
|
2022-02-03 18:00:40 -05:00
|
|
|
traditionalMarkdownLinebreaks: siteSettings.traditional_markdown_linebreaks,
|
|
|
|
enableMarkdownLinkify: siteSettings.enable_markdown_linkify,
|
|
|
|
enableMarkdownTypographer: siteSettings.enable_markdown_typographer,
|
|
|
|
markdownTypographerQuotationMarks:
|
|
|
|
siteSettings.markdown_typographer_quotation_marks,
|
|
|
|
markdownLinkifyTlds: siteSettings.markdown_linkify_tlds,
|
FEATURE: Generic hashtag autocomplete lookup and markdown cooking (#18937)
This commit fleshes out and adds functionality for the new `#hashtag` search and
lookup system, still hidden behind the `enable_experimental_hashtag_autocomplete`
feature flag.
**Serverside**
We have two plugin API registration methods that are used to define data sources
(`register_hashtag_data_source`) and hashtag result type priorities depending on
the context (`register_hashtag_type_in_context`). Reading the comments in plugin.rb
should make it clear what these are doing. Reading the `HashtagAutocompleteService`
in full will likely help a lot as well.
Each data source is responsible for providing its own **lookup** and **search**
method that returns hashtag results based on the arguments provided. For example,
the category hashtag data source has to take into account parent categories and
how they relate, and each data source has to define their own icon to use for the
hashtag, and so on.
The `Site` serializer has two new attributes that source data from `HashtagAutocompleteService`.
There is `hashtag_icons` that is just a simple array of all the different icons that
can be used for allowlisting in our markdown pipeline, and there is `hashtag_context_configurations`
that is used to store the type priority orders for each registered context.
When sending emails, we cannot render the SVG icons for hashtags, so
we need to change the HTML hashtags to the normal `#hashtag` text.
**Markdown**
The `hashtag-autocomplete.js` file is where I have added the new `hashtag-autocomplete`
markdown rule, and like all of our rules this is used to cook the raw text on both the clientside
and on the serverside using MiniRacer. Only on the server side do we actually reach out to
the database with the `hashtagLookup` function, on the clientside we just render a plainer
version of the hashtag HTML. Only in the composer preview do we do further lookups based
on this.
This rule is the first one (that I can find) that uses the `currentUser` based on a passed
in `user_id` for guardian checks in markdown rendering code. This is the `last_editor_id`
for both the post and chat message. In some cases we need to cook without a user present,
so the `Discourse.system_user` is used in this case.
**Chat Channels**
This also contains the changes required for chat so that chat channels can be used
as a data source for hashtag searches and lookups. This data source will only be
used when `enable_experimental_hashtag_autocomplete` is `true`, so we don't have
to worry about channel results suddenly turning up.
------
**Known Rough Edges**
- Onebox excerpts will not render the icon svg/use tags, I plan to address that in a follow up PR
- Selecting a hashtag + pressing the Quote button will result in weird behaviour, I plan to address that in a follow up PR
- Mixed hashtag contexts for hashtags without a type suffix will not work correctly, e.g. #ux which is both a category and a channel slug will resolve to a category when used inside a post or within a [chat] transcript in that post. Users can get around this manually by adding the correct suffix, for example ::channel. We may get to this at some point in future
- Icons will not show for the hashtags in emails since SVG support is so terrible in email (this is not likely to be resolved, but still noting for posterity)
- Additional refinements and review fixes wil
2022-11-20 17:37:06 -05:00
|
|
|
enableExperimentalHashtagAutocomplete:
|
|
|
|
siteSettings.enable_experimental_hashtag_autocomplete,
|
2020-07-15 18:52:59 -04:00
|
|
|
};
|
|
|
|
|
2022-01-06 02:27:12 -05:00
|
|
|
const markdownitOpts = {
|
2017-06-08 18:02:30 -04:00
|
|
|
discourse: opts.discourse,
|
|
|
|
html: true,
|
2022-02-03 18:00:40 -05:00
|
|
|
breaks: !opts.discourse.limitedSiteSettings.traditionalMarkdownLinebreaks,
|
2017-06-08 18:02:30 -04:00
|
|
|
xhtmlOut: false,
|
2022-02-03 18:00:40 -05:00
|
|
|
linkify: opts.discourse.limitedSiteSettings.enableMarkdownLinkify,
|
|
|
|
typographer: opts.discourse.limitedSiteSettings.enableMarkdownTypographer,
|
2022-01-06 02:27:12 -05:00
|
|
|
};
|
|
|
|
|
2022-02-03 18:00:40 -05:00
|
|
|
opts.engine = createMarkdownItEngineWithOpts(
|
|
|
|
markdownitOpts,
|
|
|
|
opts.discourse.markdownItRules
|
2018-01-31 23:56:22 -05:00
|
|
|
);
|
2018-01-31 21:22:38 -05:00
|
|
|
|
2022-02-03 18:00:40 -05:00
|
|
|
opts.pluginCallbacks = pluginCallbacks;
|
|
|
|
opts.allowListed = allowListed;
|
2017-07-14 08:27:28 -04:00
|
|
|
|
2022-02-03 18:00:40 -05:00
|
|
|
setupMarkdownEngine(opts, opts.discourse.features);
|
2017-07-14 08:27:28 -04:00
|
|
|
|
2022-02-03 18:00:40 -05:00
|
|
|
customMarkdownCookFnCallbacks.forEach(([, callback]) => {
|
|
|
|
callback(opts, (engineOpts, afterBuild) =>
|
|
|
|
afterBuild(buildCustomMarkdownCookFunction(engineOpts, opts))
|
|
|
|
);
|
|
|
|
});
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function cook(raw, opts) {
|
2020-07-26 20:23:54 -04:00
|
|
|
// we still have to hoist html_raw nodes so they bypass the allowlister
|
2022-12-22 18:56:30 -05:00
|
|
|
// this is the case for oneboxes and also certain plugins that require
|
|
|
|
// raw HTML rendering within markdown bbcode rules
|
|
|
|
opts.discourse.hoisted ??= {};
|
2017-06-26 13:09:02 -04:00
|
|
|
|
2017-06-26 13:24:55 -04:00
|
|
|
const rendered = opts.engine.render(raw);
|
2017-06-26 15:21:27 -04:00
|
|
|
let cooked = opts.discourse.sanitizer(rendered).trim();
|
2022-12-22 18:56:30 -05:00
|
|
|
|
|
|
|
// opts.discourse.hoisted guid keys will be deleted within here to
|
|
|
|
// keep the object empty
|
|
|
|
cooked = unhoistForCooked(opts.discourse.hoisted, cooked);
|
2022-02-03 18:00:40 -05:00
|
|
|
|
2017-06-26 13:09:02 -04:00
|
|
|
return cooked;
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|