mirror of
https://github.com/discourse/discourse.git
synced 2024-11-29 21:45:16 +08:00
42a529f9ae
Inline the helper functions, avoid creating and then immediately destructuring arrays, use complete strings instead of string interpolation, Map instead of a pojo.
31 lines
895 B
JavaScript
31 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);
|
|
}
|