2017-07-14 08:27:28 -04:00
|
|
|
import { default as WhiteLister } from 'pretty-text/white-lister';
|
2017-06-08 18:02:30 -04:00
|
|
|
import { sanitize } from 'pretty-text/sanitizer';
|
2017-06-26 13:09:02 -04:00
|
|
|
import guid from 'pretty-text/guid';
|
2017-06-08 18:02:30 -04:00
|
|
|
|
|
|
|
function deprecate(feature, name){
|
|
|
|
return function() {
|
2017-06-23 15:24:11 -04:00
|
|
|
if (window.console && window.console.log) {
|
|
|
|
window.console.log(feature + ': ' + name + ' is deprecated, please use the new markdown it APIs');
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
function createHelper(featureName, opts, optionCallbacks, pluginCallbacks, getOptions, whiteListed) {
|
2017-06-08 18:02:30 -04:00
|
|
|
let helper = {};
|
|
|
|
helper.markdownIt = true;
|
2017-07-14 08:27:28 -04:00
|
|
|
helper.whiteList = info => whiteListed.push([featureName, info]);
|
2017-06-08 18:02:30 -04:00
|
|
|
helper.registerInline = deprecate(featureName,'registerInline');
|
|
|
|
helper.replaceBlock = deprecate(featureName,'replaceBlock');
|
|
|
|
helper.addPreProcessor = deprecate(featureName,'addPreProcessor');
|
|
|
|
helper.inlineReplace = deprecate(featureName,'inlineReplace');
|
|
|
|
helper.postProcessTag = deprecate(featureName,'postProcessTag');
|
|
|
|
helper.inlineRegexp = deprecate(featureName,'inlineRegexp');
|
|
|
|
helper.inlineBetween = deprecate(featureName,'inlineBetween');
|
|
|
|
helper.postProcessText = deprecate(featureName,'postProcessText');
|
|
|
|
helper.onParseNode = deprecate(featureName,'onParseNode');
|
|
|
|
helper.registerBlock = deprecate(featureName,'registerBlock');
|
|
|
|
// hack to allow moving of getOptions
|
|
|
|
helper.getOptions = () => getOptions.f();
|
|
|
|
|
2017-06-23 11:36:45 -04:00
|
|
|
helper.registerOptions = (callback) => {
|
2017-06-08 18:02:30 -04:00
|
|
|
optionCallbacks.push([featureName, callback]);
|
|
|
|
};
|
|
|
|
|
2017-06-23 11:36:45 -04:00
|
|
|
helper.registerPlugin = (callback) => {
|
2017-06-08 18:02:30 -04:00
|
|
|
pluginCallbacks.push([featureName, callback]);
|
|
|
|
};
|
|
|
|
|
|
|
|
return helper;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO we may just use a proper ruler from markdown it... this is a basic proxy
|
|
|
|
class Ruler {
|
|
|
|
constructor() {
|
|
|
|
this.rules = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
getRules() {
|
|
|
|
return this.rules;
|
|
|
|
}
|
|
|
|
|
2017-06-30 15:19:07 -04:00
|
|
|
getRuleForTag(tag) {
|
|
|
|
this.ensureCache();
|
|
|
|
return this.cache[tag];
|
|
|
|
}
|
|
|
|
|
|
|
|
ensureCache() {
|
|
|
|
if (this.cache) { return; }
|
|
|
|
|
|
|
|
this.cache = {};
|
|
|
|
for(let i=this.rules.length-1;i>=0;i--) {
|
|
|
|
let info = this.rules[i];
|
|
|
|
this.cache[info.rule.tag] = info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 18:02:30 -04:00
|
|
|
push(name, rule) {
|
|
|
|
this.rules.push({name, rule});
|
2017-06-30 15:19:07 -04:00
|
|
|
this.cache = null;
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// block bb code ruler for parsing of quotes / code / polls
|
|
|
|
function setupBlockBBCode(md) {
|
2017-07-17 16:21:47 -04:00
|
|
|
md.block.bbcode = { ruler: new Ruler() };
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|
|
|
|
|
2017-06-23 11:36:45 -04:00
|
|
|
function setupInlineBBCode(md) {
|
2017-07-17 16:21:47 -04:00
|
|
|
md.inline.bbcode = { ruler: new Ruler() };
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupTextPostProcessRuler(md) {
|
|
|
|
const TextPostProcessRuler = requirejs('pretty-text/engines/discourse-markdown/text-post-process').TextPostProcessRuler;
|
|
|
|
md.core.textPostProcess = { ruler: new TextPostProcessRuler() };
|
2017-06-23 11:36:45 -04:00
|
|
|
}
|
|
|
|
|
2017-06-26 13:09:02 -04:00
|
|
|
function renderHoisted(tokens, idx, options) {
|
|
|
|
const content = tokens[idx].content;
|
|
|
|
if (content && content.length > 0) {
|
|
|
|
let id = guid();
|
|
|
|
options.discourse.hoisted[id] = tokens[idx].content;
|
|
|
|
return id;
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 16:32:53 -04:00
|
|
|
function setupUrlDecoding(md) {
|
|
|
|
// this fixed a subtle issue where %20 is decoded as space in
|
|
|
|
// automatic urls
|
|
|
|
md.utils.lib.mdurl.decode.defaultChars = ';/?:@&=+$,# ';
|
|
|
|
}
|
|
|
|
|
2017-06-26 13:09:02 -04:00
|
|
|
function setupHoister(md) {
|
|
|
|
md.renderer.rules.html_raw = renderHoisted;
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:54:20 -04:00
|
|
|
const IMG_SIZE_REGEX = /^([1-9]+[0-9]*)x([1-9]+[0-9]*)(\s*,\s*([1-9][0-9]?)%)?$/;
|
2017-07-11 12:13:03 -04:00
|
|
|
function renderImage(tokens, idx, options, env, slf) {
|
|
|
|
var token = tokens[idx];
|
|
|
|
|
|
|
|
let alt = slf.renderInlineAsText(token.children, options, env);
|
|
|
|
|
|
|
|
let split = alt.split('|');
|
|
|
|
if (split.length > 1) {
|
|
|
|
let match;
|
|
|
|
let info = split.splice(split.length-1)[0];
|
|
|
|
|
|
|
|
if (match = info.match(IMG_SIZE_REGEX)) {
|
|
|
|
if (match[1] && match[2]) {
|
|
|
|
alt = split.join('|');
|
|
|
|
|
|
|
|
let width = match[1];
|
|
|
|
let height = match[2];
|
|
|
|
|
|
|
|
if (match[4]) {
|
|
|
|
let percent = parseFloat(match[4]) / 100.0;
|
|
|
|
width = parseInt(width * percent);
|
|
|
|
height = parseInt(height * percent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token.attrIndex('width') === -1) {
|
|
|
|
token.attrs.push(['width', width]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token.attrIndex('height') === -1) {
|
|
|
|
token.attrs.push(['height', height]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
token.attrs[token.attrIndex('alt')][1] = alt;
|
|
|
|
return slf.renderToken(tokens, idx, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupImageDimensions(md) {
|
|
|
|
md.renderer.rules.image = renderImage;
|
|
|
|
}
|
|
|
|
|
2017-07-11 16:48:25 -04:00
|
|
|
let Helpers;
|
|
|
|
|
2017-06-08 18:02:30 -04:00
|
|
|
export function setup(opts, siteSettings, state) {
|
|
|
|
if (opts.setup) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-11 16:48:25 -04:00
|
|
|
// we got to require this late cause bundle is not loaded in pretty-text
|
2017-07-14 08:27:28 -04:00
|
|
|
Helpers = Helpers || requirejs('pretty-text/engines/discourse-markdown/helpers');
|
2017-07-11 16:48:25 -04:00
|
|
|
|
2017-06-08 18:02:30 -04:00
|
|
|
opts.markdownIt = true;
|
|
|
|
|
|
|
|
let optionCallbacks = [];
|
|
|
|
let pluginCallbacks = [];
|
|
|
|
|
|
|
|
// ideally I would like to change the top level API a bit, but in the mean time this will do
|
|
|
|
let getOptions = {
|
|
|
|
f: () => opts
|
|
|
|
};
|
|
|
|
|
|
|
|
const check = /discourse-markdown\/|markdown-it\//;
|
|
|
|
let features = [];
|
2017-07-14 08:27:28 -04:00
|
|
|
let whiteListed = [];
|
2017-06-08 18:02:30 -04:00
|
|
|
|
|
|
|
Object.keys(require._eak_seen).forEach(entry => {
|
|
|
|
if (check.test(entry)) {
|
2017-07-05 14:14:30 -04:00
|
|
|
const module = requirejs(entry);
|
2017-06-08 18:02:30 -04:00
|
|
|
if (module && module.setup) {
|
|
|
|
|
|
|
|
const featureName = entry.split('/').reverse()[0];
|
|
|
|
features.push(featureName);
|
2017-07-14 08:27:28 -04:00
|
|
|
module.setup(createHelper(featureName, opts, optionCallbacks, pluginCallbacks, getOptions, whiteListed));
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
optionCallbacks.forEach(([,callback])=>{
|
|
|
|
callback(opts, siteSettings, state);
|
|
|
|
});
|
|
|
|
|
|
|
|
// enable all features by default
|
|
|
|
features.forEach(feature => {
|
|
|
|
if (!opts.features.hasOwnProperty(feature)) {
|
|
|
|
opts.features[feature] = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let copy = {};
|
|
|
|
Object.keys(opts).forEach(entry => {
|
|
|
|
copy[entry] = opts[entry];
|
|
|
|
delete opts[entry];
|
|
|
|
});
|
|
|
|
|
2017-07-11 16:48:25 -04:00
|
|
|
copy.helpers = {
|
|
|
|
textReplace: Helpers.textReplace
|
|
|
|
};
|
|
|
|
|
2017-06-08 18:02:30 -04:00
|
|
|
opts.discourse = copy;
|
|
|
|
getOptions.f = () => opts.discourse;
|
|
|
|
|
|
|
|
opts.engine = window.markdownit({
|
|
|
|
discourse: opts.discourse,
|
|
|
|
html: true,
|
|
|
|
breaks: opts.discourse.features.newline,
|
|
|
|
xhtmlOut: false,
|
2017-07-21 13:20:45 -04:00
|
|
|
linkify: opts.discourse.features.linkify,
|
2017-06-27 16:50:13 -04:00
|
|
|
typographer: siteSettings.enable_markdown_typographer
|
2017-06-08 18:02:30 -04:00
|
|
|
});
|
|
|
|
|
2017-07-03 16:32:53 -04:00
|
|
|
setupUrlDecoding(opts.engine);
|
2017-06-26 13:09:02 -04:00
|
|
|
setupHoister(opts.engine);
|
2017-07-11 12:13:03 -04:00
|
|
|
setupImageDimensions(opts.engine);
|
2017-06-08 18:02:30 -04:00
|
|
|
setupBlockBBCode(opts.engine);
|
2017-06-23 11:36:45 -04:00
|
|
|
setupInlineBBCode(opts.engine);
|
2017-07-17 16:21:47 -04:00
|
|
|
setupTextPostProcessRuler(opts.engine);
|
2017-06-08 18:02:30 -04:00
|
|
|
|
|
|
|
pluginCallbacks.forEach(([feature, callback])=>{
|
|
|
|
if (opts.discourse.features[feature]) {
|
|
|
|
opts.engine.use(callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// top level markdown it notifier
|
|
|
|
opts.markdownIt = true;
|
|
|
|
opts.setup = true;
|
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
if (!opts.discourse.sanitizer || !opts.sanitizer) {
|
2017-06-26 15:21:27 -04:00
|
|
|
const whiteLister = new WhiteLister(opts.discourse);
|
2017-07-14 08:27:28 -04:00
|
|
|
|
|
|
|
whiteListed.forEach(([feature, info]) => {
|
|
|
|
whiteLister.whiteListFeature(feature, info);
|
|
|
|
});
|
|
|
|
|
2017-06-26 15:21:27 -04:00
|
|
|
opts.sanitizer = opts.discourse.sanitizer = (!!opts.discourse.sanitize) ? a=>sanitize(a, whiteLister) : a=>a;
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|
2017-07-14 08:27:28 -04:00
|
|
|
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function cook(raw, opts) {
|
2017-06-26 13:09:02 -04:00
|
|
|
// we still have to hoist html_raw nodes so they bypass the whitelister
|
|
|
|
// this is the case for oneboxes
|
|
|
|
let hoisted = {};
|
|
|
|
|
|
|
|
opts.discourse.hoisted = hoisted;
|
|
|
|
|
2017-06-26 13:24:55 -04:00
|
|
|
const rendered = opts.engine.render(raw);
|
2017-06-26 15:21:27 -04:00
|
|
|
let cooked = opts.discourse.sanitizer(rendered).trim();
|
2017-06-26 13:09:02 -04:00
|
|
|
|
|
|
|
const keys = Object.keys(hoisted);
|
|
|
|
if (keys.length) {
|
|
|
|
let found = true;
|
|
|
|
|
|
|
|
const unhoist = function(key) {
|
|
|
|
cooked = cooked.replace(new RegExp(key, "g"), function() {
|
|
|
|
found = true;
|
|
|
|
return hoisted[key];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
while (found) {
|
|
|
|
found = false;
|
|
|
|
keys.forEach(unhoist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete opts.discourse.hoisted;
|
|
|
|
return cooked;
|
|
|
|
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|