2017-07-14 08:27:28 -04:00
|
|
|
import { censorFn } from 'pretty-text/censored-words';
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
function recurse(tokens, apply) {
|
|
|
|
let i;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
|
|
|
|
export function setup(helper) {
|
2017-07-14 08:27:28 -04:00
|
|
|
helper.registerOptions((opts, siteSettings) => {
|
2018-01-10 14:11:14 -05:00
|
|
|
opts.watchedWordsRegularExpressions = siteSettings.watched_words_regular_expressions;
|
2017-07-14 08:27:28 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
helper.registerPlugin(md => {
|
|
|
|
const words = md.options.discourse.censoredWords;
|
2017-06-08 18:02:30 -04:00
|
|
|
|
2018-02-26 15:48:59 -05:00
|
|
|
if ((words && words.length > 0)) {
|
2017-07-14 08:27:28 -04:00
|
|
|
const replacement = String.fromCharCode(9632);
|
2018-02-26 15:48:59 -05:00
|
|
|
const censor = censorFn(words, replacement, md.options.discourse.watchedWordsRegularExpressions);
|
2017-07-14 08:27:28 -04:00
|
|
|
md.core.ruler.push('censored', state => censorTree(state, censor));
|
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
});
|
|
|
|
}
|