mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 05:47:31 +08:00
b56e9ad656
* DEV: Use site setting instead * DEV: Use .length instead of a different property * DEV: Simplify watched word code
40 lines
889 B
JavaScript
40 lines
889 B
JavaScript
import { censorFn } from "pretty-text/censored-words";
|
|
|
|
function recurse(tokens, apply) {
|
|
let i;
|
|
for (i = 0; i < tokens.length; i++) {
|
|
if (tokens[i].type === "html_raw" && tokens[i].onebox) {
|
|
continue;
|
|
}
|
|
|
|
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);
|
|
}
|
|
});
|
|
}
|
|
|
|
export function setup(helper) {
|
|
helper.registerPlugin((md) => {
|
|
const censoredRegexp = md.options.discourse.censoredRegexp;
|
|
|
|
if (censoredRegexp) {
|
|
const replacement = String.fromCharCode(9632);
|
|
const censor = censorFn(censoredRegexp, replacement);
|
|
md.core.ruler.push("censored", (state) => censorTree(state, censor));
|
|
}
|
|
});
|
|
}
|