discourse/app/assets/javascripts/pretty-text/engines/discourse-markdown/censored.js.es6
David Taylor 39e0442de9 FIX: Various watched words improvements
- Client-side censoring fixed for non-chrome browsers. (Regular expression rewritten to avoid lookback)
- Regex generation is now done on the server, to reduce repeated logic, and make it easier to extend in plugins
- Censor tests are moved to ruby, to ensure everything works end-to-end
- If "watched words regular expressions" is enabled, warn the admin when the generated regex is invalid
2019-08-02 15:29:12 +01:00

41 lines
953 B
JavaScript

import { censorFn } from "pretty-text/censored-words";
function recurse(tokens, apply) {
let i;
for (i = 0; i < tokens.length; i++) {
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));
}
});
}