discourse/frontend/pretty-text/addon/censored-words.js
David Taylor d1e1c02fcb DEV: Rename app/assets/javascripts/ -> frontend/
Commit created with `mv app/assets/javascripts frontend && git add .`
2025-10-22 16:24:11 +01:00

30 lines
895 B
JavaScript

export function censorFn(regexpList, replacementLetter = "■") {
if (regexpList?.length) {
const censorRegexps = regexpList.map((entry) => {
const [regexp, options] = Object.entries(entry)[0];
return new RegExp(regexp, options.case_sensitive ? "gu" : "gui");
});
return function (text) {
censorRegexps.forEach((censorRegexp) => {
text = text.replace(censorRegexp, (fullMatch, ...groupMatches) => {
const stringMatch = groupMatches.find((g) => typeof g === "string");
return fullMatch.replace(
stringMatch,
new Array(stringMatch.length + 1).join(replacementLetter)
);
});
});
return text;
};
}
return function (t) {
return t;
};
}
export function censor(text, censoredRegexp, replacementLetter) {
return censorFn(censoredRegexp, replacementLetter)(text);
}