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++) {
|
2021-03-23 19:09:24 +08:00
|
|
|
if (tokens[i].type === "html_raw" && tokens[i].onebox) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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.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);
|
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
|
|
|
});
|
|
|
|
}
|