2018-06-15 23:03:24 +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;
|
2018-06-15 23:03:24 +08:00
|
|
|
for (i = 0; i < tokens.length; i++) {
|
2017-07-14 20:27:28 +08:00
|
|
|
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-06-15 23:03:24 +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-06-15 23:03:24 +08:00
|
|
|
if (words && words.length > 0) {
|
2017-07-14 20:27:28 +08:00
|
|
|
const replacement = String.fromCharCode(9632);
|
2018-06-15 23:03:24 +08:00
|
|
|
const censor = censorFn(
|
|
|
|
words,
|
|
|
|
replacement,
|
|
|
|
md.options.discourse.watchedWordsRegularExpressions
|
|
|
|
);
|
|
|
|
md.core.ruler.push("censored", state => censorTree(state, censor));
|
2017-07-14 20:27:28 +08:00
|
|
|
}
|
2016-06-15 02:31:51 +08:00
|
|
|
});
|
|
|
|
}
|