discourse/app/assets/javascripts/pretty-text/engines/discourse-markdown/censored.js
Bianca Nenciu d7bd62d9cf
FIX: Replace censored watched word consistently (#12486)
Applying oneboxes and replacing censored watched words does not happen
in a strict order which often lead to inconsistencies. This commit
fixes the behavior and will never censor oneboxes.

To make it always censor oneboxes implies significant changes to the
PrettyText pipeline.
2021-03-23 13:09:24 +02:00

45 lines
1.0 KiB
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.registerOptions((opts, siteSettings) => {
opts.watchedWordsRegularExpressions =
siteSettings.watched_words_regular_expressions;
});
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));
}
});
}