mirror of
https://github.com/discourse/discourse.git
synced 2024-12-01 12:24:37 +08:00
0b8d0a14d7
Sometimes plugins need to have additional data or options available when rendering custom markdown features/rules that are not available on the default opts.discourse object. These additional options should be namespaced to the plugin adding them. ``` Site.markdown_additional_options["chat"] = { limited_pretty_text_markdown_rules: [] } ``` These are passed down to markdown rules on opts.discourse.additionalOptions. The main motivation for adding this is the chat plugin, which currently stores chat_pretty_text_features and chat_pretty_text_markdown_rules on the Site object via additions to the serializer, and the Site object is not accessible to import via markdown rules (either through Site.current() or through container.lookup). So, to have this working for both front + backend code, we need to attach these additional options from the Site object onto the markdown options object.
120 lines
2.5 KiB
JavaScript
120 lines
2.5 KiB
JavaScript
import {
|
|
cook as cookIt,
|
|
setup as setupIt,
|
|
} from "pretty-text/engines/discourse-markdown-it";
|
|
import { deepMerge } from "discourse-common/lib/object";
|
|
import deprecated from "discourse-common/lib/deprecated";
|
|
|
|
export function registerOption() {
|
|
deprecated(
|
|
"`registerOption() from `pretty-text` is deprecated. Use `helper.registerOptions()` instead.",
|
|
{
|
|
since: "2.8.0.beta9",
|
|
dropFrom: "2.9.0.beta1",
|
|
}
|
|
);
|
|
}
|
|
|
|
export function buildOptions(state) {
|
|
const {
|
|
siteSettings,
|
|
getURL,
|
|
lookupAvatar,
|
|
lookupPrimaryUserGroup,
|
|
getTopicInfo,
|
|
topicId,
|
|
categoryHashtagLookup,
|
|
userId,
|
|
getCurrentUser,
|
|
currentUser,
|
|
lookupAvatarByPostNumber,
|
|
lookupPrimaryUserGroupByPostNumber,
|
|
formatUsername,
|
|
emojiUnicodeReplacer,
|
|
lookupUploadUrls,
|
|
previewing,
|
|
censoredRegexp,
|
|
disableEmojis,
|
|
customEmojiTranslation,
|
|
watchedWordsReplace,
|
|
watchedWordsLink,
|
|
featuresOverride,
|
|
markdownItRules,
|
|
additionalOptions,
|
|
} = state;
|
|
|
|
let features = {};
|
|
|
|
if (state.features) {
|
|
features = deepMerge(features, state.features);
|
|
}
|
|
|
|
const options = {
|
|
sanitize: true,
|
|
getURL,
|
|
features,
|
|
lookupAvatar,
|
|
lookupPrimaryUserGroup,
|
|
getTopicInfo,
|
|
topicId,
|
|
categoryHashtagLookup,
|
|
userId,
|
|
getCurrentUser,
|
|
currentUser,
|
|
lookupAvatarByPostNumber,
|
|
lookupPrimaryUserGroupByPostNumber,
|
|
formatUsername,
|
|
emojiUnicodeReplacer,
|
|
lookupUploadUrls,
|
|
censoredRegexp,
|
|
customEmojiTranslation,
|
|
allowedHrefSchemes: siteSettings.allowed_href_schemes
|
|
? siteSettings.allowed_href_schemes.split("|")
|
|
: null,
|
|
allowedIframes: siteSettings.allowed_iframes
|
|
? siteSettings.allowed_iframes.split("|")
|
|
: [],
|
|
markdownIt: true,
|
|
previewing,
|
|
disableEmojis,
|
|
watchedWordsReplace,
|
|
watchedWordsLink,
|
|
featuresOverride,
|
|
markdownItRules,
|
|
additionalOptions,
|
|
};
|
|
|
|
// note, this will mutate options due to the way the API is designed
|
|
// may need a refactor
|
|
setupIt(options, siteSettings, state);
|
|
|
|
return options;
|
|
}
|
|
|
|
export default class {
|
|
constructor(opts) {
|
|
if (!opts) {
|
|
opts = buildOptions({ siteSettings: {} });
|
|
}
|
|
this.opts = opts;
|
|
}
|
|
|
|
disableSanitizer() {
|
|
this.opts.sanitizer = this.opts.discourse.sanitizer = (ident) => ident;
|
|
}
|
|
|
|
cook(raw) {
|
|
if (!raw || raw.length === 0) {
|
|
return "";
|
|
}
|
|
|
|
let result;
|
|
result = cookIt(raw, this.opts);
|
|
return result ? result : "";
|
|
}
|
|
|
|
sanitize(html) {
|
|
return this.opts.sanitizer(html).trim();
|
|
}
|
|
}
|