discourse/app/assets/javascripts/pretty-text/engines/discourse-markdown/censored.js.es6

41 lines
953 B
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
import { censorFn } from "pretty-text/censored-words";
function recurse(tokens, apply) {
let i;
2018-06-15 23:03:24 +08:00
for (i = 0; i < tokens.length; i++) {
apply(tokens[i]);
if (tokens[i].children) {
recurse(tokens[i].children, apply);
}
}
}
function censorTree(state, censor) {
if (!state.tokens) {
return;
}
recurse(state.tokens, token => {
if (token.content) {
token.content = censor(token.content);
}
});
}
export function setup(helper) {
helper.registerOptions((opts, siteSettings) => {
2018-06-15 23:03:24 +08:00
opts.watchedWordsRegularExpressions =
siteSettings.watched_words_regular_expressions;
});
helper.registerPlugin(md => {
const censoredRegexp = md.options.discourse.censoredRegexp;
if (censoredRegexp) {
const replacement = String.fromCharCode(9632);
const censor = censorFn(censoredRegexp, replacement);
2018-06-15 23:03:24 +08:00
md.core.ruler.push("censored", state => censorTree(state, censor));
}
});
}