2016-06-15 02:31:51 +08:00
|
|
|
import { cook, setup } from 'pretty-text/engines/discourse-markdown';
|
2017-06-09 06:02:30 +08:00
|
|
|
import { cook as cookIt, setup as setupIt } from 'pretty-text/engines/discourse-markdown-it';
|
2016-06-15 02:31:51 +08:00
|
|
|
import { sanitize } from 'pretty-text/sanitizer';
|
|
|
|
import WhiteLister from 'pretty-text/white-lister';
|
|
|
|
|
|
|
|
const _registerFns = [];
|
|
|
|
const identity = value => value;
|
|
|
|
|
|
|
|
export function registerOption(fn) {
|
|
|
|
_registerFns.push(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildOptions(state) {
|
2016-07-07 15:52:56 +08:00
|
|
|
const {
|
|
|
|
siteSettings,
|
|
|
|
getURL,
|
|
|
|
lookupAvatar,
|
|
|
|
getTopicInfo,
|
|
|
|
topicId,
|
|
|
|
categoryHashtagLookup,
|
|
|
|
userId,
|
|
|
|
getCurrentUser,
|
2017-06-09 06:02:30 +08:00
|
|
|
currentUser,
|
2017-06-29 01:47:22 +08:00
|
|
|
lookupAvatarByPostNumber,
|
|
|
|
emojiUnicodeReplacer
|
2016-07-07 15:52:56 +08:00
|
|
|
} = state;
|
2016-06-15 02:31:51 +08:00
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
if (!siteSettings.enable_experimental_markdown_it) {
|
|
|
|
setup();
|
|
|
|
}
|
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
const features = {
|
|
|
|
'bold-italics': true,
|
|
|
|
'auto-link': true,
|
|
|
|
'mentions': true,
|
|
|
|
'bbcode': true,
|
|
|
|
'quote': true,
|
|
|
|
'html': true,
|
|
|
|
'category-hashtag': true,
|
|
|
|
'onebox': true,
|
2017-06-09 06:02:30 +08:00
|
|
|
'newline': !siteSettings.traditional_markdown_linebreaks
|
2016-06-15 02:31:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
sanitize: true,
|
|
|
|
getURL,
|
|
|
|
features,
|
|
|
|
lookupAvatar,
|
|
|
|
getTopicInfo,
|
|
|
|
topicId,
|
|
|
|
categoryHashtagLookup,
|
2016-07-07 15:52:56 +08:00
|
|
|
userId,
|
|
|
|
getCurrentUser,
|
|
|
|
currentUser,
|
2017-06-09 06:02:30 +08:00
|
|
|
lookupAvatarByPostNumber,
|
2016-06-15 02:31:51 +08:00
|
|
|
mentionLookup: state.mentionLookup,
|
2017-06-29 01:47:22 +08:00
|
|
|
emojiUnicodeReplacer,
|
2017-06-09 06:02:30 +08:00
|
|
|
allowedHrefSchemes: siteSettings.allowed_href_schemes ? siteSettings.allowed_href_schemes.split('|') : null,
|
|
|
|
markdownIt: siteSettings.enable_experimental_markdown_it
|
2016-06-15 02:31:51 +08:00
|
|
|
};
|
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
if (siteSettings.enable_experimental_markdown_it) {
|
|
|
|
setupIt(options, siteSettings, state);
|
|
|
|
} else {
|
|
|
|
// TODO deprecate this
|
|
|
|
_registerFns.forEach(fn => fn(siteSettings, options, state));
|
|
|
|
}
|
2016-06-15 02:31:51 +08:00
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class {
|
|
|
|
constructor(opts) {
|
|
|
|
this.opts = opts || {};
|
|
|
|
this.opts.features = this.opts.features || {};
|
|
|
|
this.opts.sanitizer = (!!this.opts.sanitize) ? (this.opts.sanitizer || sanitize) : identity;
|
2017-06-09 06:02:30 +08:00
|
|
|
// We used to do a failsafe call to setup here
|
|
|
|
// under new engine we always expect setup to be called by buildOptions.
|
|
|
|
// setup();
|
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;
|
|
|
|
|
|
|
|
if (this.opts.markdownIt) {
|
|
|
|
result = cookIt(raw, this.opts);
|
|
|
|
} else {
|
|
|
|
result = cook(raw, this.opts);
|
|
|
|
}
|
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
return result ? result : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
sanitize(html) {
|
2016-10-21 23:39:48 +08:00
|
|
|
return this.opts.sanitizer(html, new WhiteLister(this.opts));
|
2016-06-15 02:31:51 +08:00
|
|
|
}
|
|
|
|
};
|