2017-06-09 06:02:30 +08:00
|
|
|
import {
|
|
|
|
cook as cookIt,
|
|
|
|
setup as setupIt,
|
|
|
|
} from "pretty-text/engines/discourse-markdown-it";
|
2020-09-03 00:04:14 +08:00
|
|
|
import { deepMerge } from "discourse-common/lib/object";
|
2021-12-08 01:33:45 +08:00
|
|
|
import deprecated from "discourse-common/lib/deprecated";
|
2016-06-15 02:31:51 +08:00
|
|
|
|
2017-07-14 20:27:28 +08:00
|
|
|
export function registerOption() {
|
2021-12-08 01:33:45 +08:00
|
|
|
deprecated(
|
|
|
|
"`registerOption() from `pretty-text` is deprecated. Use `helper.registerOptions()` instead.",
|
|
|
|
{
|
|
|
|
since: "2.8.0.beta9",
|
|
|
|
dropFrom: "2.9.0.beta1",
|
|
|
|
}
|
|
|
|
);
|
2016-06-15 02:31:51 +08:00
|
|
|
}
|
|
|
|
|
2022-02-23 14:13:46 +08:00
|
|
|
// see also: __optInput in PrettyText#cook and PrettyText#markdown,
|
|
|
|
// the options are passed here and must be explicitly allowed with
|
|
|
|
// the const options & state below
|
2016-06-15 02:31:51 +08:00
|
|
|
export function buildOptions(state) {
|
2016-07-07 15:52:56 +08:00
|
|
|
const {
|
|
|
|
siteSettings,
|
|
|
|
getURL,
|
|
|
|
lookupAvatar,
|
2017-11-03 21:51:40 +08:00
|
|
|
lookupPrimaryUserGroup,
|
2016-07-07 15:52:56 +08:00
|
|
|
getTopicInfo,
|
|
|
|
topicId,
|
2022-02-23 14:13:46 +08:00
|
|
|
forceQuoteLink,
|
2016-07-07 15:52:56 +08:00
|
|
|
categoryHashtagLookup,
|
|
|
|
userId,
|
|
|
|
getCurrentUser,
|
2017-06-09 06:02:30 +08:00
|
|
|
currentUser,
|
2017-06-29 01:47:22 +08:00
|
|
|
lookupAvatarByPostNumber,
|
2017-11-03 21:51:40 +08:00
|
|
|
lookupPrimaryUserGroupByPostNumber,
|
2017-11-21 05:28:03 +08:00
|
|
|
formatUsername,
|
2017-07-20 03:08:54 +08:00
|
|
|
emojiUnicodeReplacer,
|
2019-05-29 09:00:25 +08:00
|
|
|
lookupUploadUrls,
|
2017-07-22 01:20:45 +08:00
|
|
|
previewing,
|
2019-08-01 01:33:49 +08:00
|
|
|
censoredRegexp,
|
2020-05-28 02:11:52 +08:00
|
|
|
disableEmojis,
|
|
|
|
customEmojiTranslation,
|
2021-06-02 13:36:49 +08:00
|
|
|
watchedWordsReplace,
|
|
|
|
watchedWordsLink,
|
2022-01-06 15:27:12 +08:00
|
|
|
featuresOverride,
|
|
|
|
markdownItRules,
|
2022-01-28 11:02:02 +08:00
|
|
|
additionalOptions,
|
2016-07-07 15:52:56 +08:00
|
|
|
} = state;
|
2016-06-15 02:31:51 +08:00
|
|
|
|
2022-01-06 15:28:32 +08:00
|
|
|
let features = {};
|
2016-06-15 02:31:51 +08:00
|
|
|
|
2017-07-14 20:27:28 +08:00
|
|
|
if (state.features) {
|
2020-09-03 00:04:14 +08:00
|
|
|
features = deepMerge(features, state.features);
|
2017-07-14 20:27:28 +08:00
|
|
|
}
|
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
const options = {
|
|
|
|
sanitize: true,
|
|
|
|
getURL,
|
|
|
|
features,
|
|
|
|
lookupAvatar,
|
2017-11-03 21:51:40 +08:00
|
|
|
lookupPrimaryUserGroup,
|
2016-06-15 02:31:51 +08:00
|
|
|
getTopicInfo,
|
|
|
|
topicId,
|
2022-02-23 14:13:46 +08:00
|
|
|
forceQuoteLink,
|
2016-06-15 02:31:51 +08:00
|
|
|
categoryHashtagLookup,
|
2016-07-07 15:52:56 +08:00
|
|
|
userId,
|
|
|
|
getCurrentUser,
|
|
|
|
currentUser,
|
2017-06-09 06:02:30 +08:00
|
|
|
lookupAvatarByPostNumber,
|
2017-11-03 21:51:40 +08:00
|
|
|
lookupPrimaryUserGroupByPostNumber,
|
2017-11-21 05:28:03 +08:00
|
|
|
formatUsername,
|
2017-06-29 01:47:22 +08:00
|
|
|
emojiUnicodeReplacer,
|
2019-05-29 09:00:25 +08:00
|
|
|
lookupUploadUrls,
|
2019-08-01 01:33:49 +08:00
|
|
|
censoredRegexp,
|
2020-05-28 02:11:52 +08:00
|
|
|
customEmojiTranslation,
|
2017-06-09 06:02:30 +08:00
|
|
|
allowedHrefSchemes: siteSettings.allowed_href_schemes
|
|
|
|
? siteSettings.allowed_href_schemes.split("|")
|
|
|
|
: null,
|
2017-09-02 00:08:39 +08:00
|
|
|
allowedIframes: siteSettings.allowed_iframes
|
|
|
|
? siteSettings.allowed_iframes.split("|")
|
|
|
|
: [],
|
2017-07-20 03:08:54 +08:00
|
|
|
markdownIt: true,
|
2019-06-03 15:41:26 +08:00
|
|
|
previewing,
|
|
|
|
disableEmojis,
|
2021-06-02 13:36:49 +08:00
|
|
|
watchedWordsReplace,
|
|
|
|
watchedWordsLink,
|
2022-01-06 15:27:12 +08:00
|
|
|
featuresOverride,
|
|
|
|
markdownItRules,
|
2022-01-28 11:02:02 +08:00
|
|
|
additionalOptions,
|
2016-06-15 02:31:51 +08:00
|
|
|
};
|
|
|
|
|
2017-07-14 20:27:28 +08:00
|
|
|
// note, this will mutate options due to the way the API is designed
|
|
|
|
// may need a refactor
|
|
|
|
setupIt(options, siteSettings, state);
|
2016-06-15 02:31:51 +08:00
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class {
|
|
|
|
constructor(opts) {
|
2017-07-14 20:27:28 +08:00
|
|
|
if (!opts) {
|
|
|
|
opts = buildOptions({ siteSettings: {} });
|
|
|
|
}
|
|
|
|
this.opts = opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
disableSanitizer() {
|
|
|
|
this.opts.sanitizer = this.opts.discourse.sanitizer = (ident) => ident;
|
2016-06-15 02:31:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cook(raw) {
|
|
|
|
if (!raw || raw.length === 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
let result;
|
2017-07-14 20:27:28 +08:00
|
|
|
result = cookIt(raw, this.opts);
|
2016-06-15 02:31:51 +08:00
|
|
|
return result ? result : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
sanitize(html) {
|
2017-07-14 20:27:28 +08:00
|
|
|
return this.opts.sanitizer(html).trim();
|
2016-06-15 02:31:51 +08:00
|
|
|
}
|
|
|
|
}
|