mirror of
https://github.com/discourse/discourse.git
synced 2024-12-01 17:04:54 +08:00
4bf9b73296
This fixes the test suite failures introduced by 862007fb18
36 lines
970 B
JavaScript
36 lines
970 B
JavaScript
import {
|
|
createWatchedWordRegExp,
|
|
toWatchedWord,
|
|
} from "discourse-common/utils/watched-words";
|
|
|
|
export function censorFn(regexpList, replacementLetter) {
|
|
if (regexpList?.length) {
|
|
replacementLetter = replacementLetter || "■";
|
|
let censorRegexps = regexpList.map((regexp) => {
|
|
return createWatchedWordRegExp(toWatchedWord(regexp));
|
|
});
|
|
|
|
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);
|
|
}
|