discourse/app/assets/javascripts/pretty-text/pretty-text.js.es6
Sam Saffron d0c5205a52 Feature: Change markdown engine to markdown it
This commit removes the old evilstreak markdownjs engine.

- Adds specs to WhiteLister and changes it to stop using globals
    (Fixes large memory leak)
- Fixes edge cases around bbcode handling
- Removes mdtest which is no longer valid (to be replaced with
    CommonMark)
- Updates MiniRacer to correct minor unmanaged memory leak
- Fixes plugin specs
2017-07-17 11:41:34 -04:00

90 lines
1.9 KiB
JavaScript

import { cook as cookIt, setup as setupIt } from 'pretty-text/engines/discourse-markdown-it';
export function registerOption() {
// TODO next major version deprecate this
// if (window.console) {
// window.console.log("registerOption is deprecated");
// }
}
export function buildOptions(state) {
const {
siteSettings,
getURL,
lookupAvatar,
getTopicInfo,
topicId,
categoryHashtagLookup,
userId,
getCurrentUser,
currentUser,
lookupAvatarByPostNumber,
emojiUnicodeReplacer
} = state;
let features = {
'bold-italics': true,
'auto-link': true,
'mentions': true,
'bbcode': true,
'quote': true,
'html': true,
'category-hashtag': true,
'onebox': true,
'newline': !siteSettings.traditional_markdown_linebreaks
};
if (state.features) {
features = _.merge(features, state.features);
}
const options = {
sanitize: true,
getURL,
features,
lookupAvatar,
getTopicInfo,
topicId,
categoryHashtagLookup,
userId,
getCurrentUser,
currentUser,
lookupAvatarByPostNumber,
mentionLookup: state.mentionLookup,
emojiUnicodeReplacer,
allowedHrefSchemes: siteSettings.allowed_href_schemes ? siteSettings.allowed_href_schemes.split('|') : null,
markdownIt: true
};
// 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();
}
};