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