2017-06-08 17:02:30 -05:00
|
|
|
import { cook as cookIt, setup as setupIt } from 'pretty-text/engines/discourse-markdown-it';
|
2016-06-14 13:31:51 -05:00
|
|
|
|
2017-07-14 07:27:28 -05:00
|
|
|
export function registerOption() {
|
|
|
|
// TODO next major version deprecate this
|
|
|
|
// if (window.console) {
|
|
|
|
// window.console.log("registerOption is deprecated");
|
|
|
|
// }
|
2016-06-14 13:31:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function buildOptions(state) {
|
2016-07-07 02:52:56 -05:00
|
|
|
const {
|
|
|
|
siteSettings,
|
|
|
|
getURL,
|
|
|
|
lookupAvatar,
|
2017-11-03 08:51:40 -05:00
|
|
|
lookupPrimaryUserGroup,
|
2016-07-07 02:52:56 -05:00
|
|
|
getTopicInfo,
|
|
|
|
topicId,
|
|
|
|
categoryHashtagLookup,
|
|
|
|
userId,
|
|
|
|
getCurrentUser,
|
2017-06-08 17:02:30 -05:00
|
|
|
currentUser,
|
2017-06-28 12:47:22 -05:00
|
|
|
lookupAvatarByPostNumber,
|
2017-11-03 08:51:40 -05:00
|
|
|
lookupPrimaryUserGroupByPostNumber,
|
2017-11-20 15:28:03 -06:00
|
|
|
formatUsername,
|
2017-07-19 14:08:54 -05:00
|
|
|
emojiUnicodeReplacer,
|
|
|
|
lookupInlineOnebox,
|
2017-08-22 10:46:15 -05:00
|
|
|
lookupImageUrls,
|
2017-07-21 12:20:45 -05:00
|
|
|
previewing,
|
2017-06-28 15:56:44 -05:00
|
|
|
linkify,
|
2017-10-30 11:40:58 -05:00
|
|
|
censoredWords,
|
|
|
|
mentionLookup
|
2016-07-07 02:52:56 -05:00
|
|
|
} = state;
|
2016-06-14 13:31:51 -05:00
|
|
|
|
2017-07-14 07:27:28 -05:00
|
|
|
let features = {
|
2016-06-14 13:31:51 -05:00
|
|
|
'bold-italics': true,
|
|
|
|
'auto-link': true,
|
|
|
|
'mentions': true,
|
|
|
|
'bbcode': true,
|
|
|
|
'quote': true,
|
|
|
|
'html': true,
|
|
|
|
'category-hashtag': true,
|
|
|
|
'onebox': true,
|
2017-07-21 12:20:45 -05:00
|
|
|
'linkify': linkify !== false,
|
2017-06-08 17:02:30 -05:00
|
|
|
'newline': !siteSettings.traditional_markdown_linebreaks
|
2016-06-14 13:31:51 -05:00
|
|
|
};
|
|
|
|
|
2017-07-14 07:27:28 -05:00
|
|
|
if (state.features) {
|
|
|
|
features = _.merge(features, state.features);
|
|
|
|
}
|
|
|
|
|
2016-06-14 13:31:51 -05:00
|
|
|
const options = {
|
|
|
|
sanitize: true,
|
|
|
|
getURL,
|
|
|
|
features,
|
|
|
|
lookupAvatar,
|
2017-11-03 08:51:40 -05:00
|
|
|
lookupPrimaryUserGroup,
|
2016-06-14 13:31:51 -05:00
|
|
|
getTopicInfo,
|
|
|
|
topicId,
|
|
|
|
categoryHashtagLookup,
|
2016-07-07 02:52:56 -05:00
|
|
|
userId,
|
|
|
|
getCurrentUser,
|
|
|
|
currentUser,
|
2017-06-08 17:02:30 -05:00
|
|
|
lookupAvatarByPostNumber,
|
2017-11-03 08:51:40 -05:00
|
|
|
lookupPrimaryUserGroupByPostNumber,
|
2017-11-20 15:28:03 -06:00
|
|
|
formatUsername,
|
2017-10-30 11:40:58 -05:00
|
|
|
mentionLookup,
|
2017-06-28 12:47:22 -05:00
|
|
|
emojiUnicodeReplacer,
|
2017-07-19 14:08:54 -05:00
|
|
|
lookupInlineOnebox,
|
2017-08-22 10:46:15 -05:00
|
|
|
lookupImageUrls,
|
2017-06-28 15:56:44 -05:00
|
|
|
censoredWords,
|
2017-06-08 17:02:30 -05:00
|
|
|
allowedHrefSchemes: siteSettings.allowed_href_schemes ? siteSettings.allowed_href_schemes.split('|') : null,
|
2017-09-01 11:08:39 -05:00
|
|
|
allowedIframes: siteSettings.allowed_iframes ? siteSettings.allowed_iframes.split('|') : [],
|
2017-07-19 14:08:54 -05:00
|
|
|
markdownIt: true,
|
2017-12-05 05:30:13 -06:00
|
|
|
injectLineNumbersToPreview: siteSettings.enable_advanced_editor_preview_sync,
|
2017-07-19 14:08:54 -05:00
|
|
|
previewing
|
2016-06-14 13:31:51 -05:00
|
|
|
};
|
|
|
|
|
2017-07-14 07:27:28 -05:00
|
|
|
// note, this will mutate options due to the way the API is designed
|
|
|
|
// may need a refactor
|
|
|
|
setupIt(options, siteSettings, state);
|
2016-06-14 13:31:51 -05:00
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class {
|
|
|
|
constructor(opts) {
|
2017-07-14 07:27:28 -05:00
|
|
|
if (!opts) {
|
|
|
|
opts = buildOptions({ siteSettings: {}});
|
|
|
|
}
|
|
|
|
this.opts = opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
disableSanitizer() {
|
|
|
|
this.opts.sanitizer = this.opts.discourse.sanitizer = ident => ident;
|
2016-06-14 13:31:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
cook(raw) {
|
|
|
|
if (!raw || raw.length === 0) { return ""; }
|
|
|
|
|
2017-06-08 17:02:30 -05:00
|
|
|
let result;
|
2017-07-14 07:27:28 -05:00
|
|
|
result = cookIt(raw, this.opts);
|
2016-06-14 13:31:51 -05:00
|
|
|
return result ? result : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
sanitize(html) {
|
2017-07-14 07:27:28 -05:00
|
|
|
return this.opts.sanitizer(html).trim();
|
2016-06-14 13:31:51 -05:00
|
|
|
}
|
|
|
|
};
|