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 => {
|
2019-08-01 01:33:49 +08:00
|
|
|
const censoredRegexp = md.options.discourse.censoredRegexp;
|
2017-06-09 06:02:30 +08:00
|
|
|
|
2019-08-01 01:33:49 +08:00
|
|
|
if (censoredRegexp) {
|
2017-07-14 20:27:28 +08:00
|
|
|
const replacement = String.fromCharCode(9632);
|
2019-08-01 01:33:49 +08:00
|
|
|
const censor = censorFn(censoredRegexp, replacement);
|
2018-06-15 23:03:24 +08:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|