2022-08-02 16:06:03 +08:00
|
|
|
import {
|
|
|
|
createWatchedWordRegExp,
|
|
|
|
toWatchedWord,
|
|
|
|
} from "discourse-common/utils/watched-words";
|
|
|
|
|
|
|
|
export function censorFn(regexpList, replacementLetter) {
|
2022-08-02 18:09:51 +08:00
|
|
|
if (regexpList?.length) {
|
2019-08-01 01:33:49 +08:00
|
|
|
replacementLetter = replacementLetter || "■";
|
2022-08-02 16:06:03 +08:00
|
|
|
let censorRegexps = regexpList.map((regexp) => {
|
|
|
|
return createWatchedWordRegExp(toWatchedWord(regexp));
|
|
|
|
});
|
2019-08-01 01:33:49 +08:00
|
|
|
|
|
|
|
return function (text) {
|
2022-08-02 16:06:03 +08:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
});
|
2019-08-01 01:33:49 +08:00
|
|
|
});
|
2017-06-09 06:02:30 +08:00
|
|
|
|
2019-08-01 01:33:49 +08:00
|
|
|
return text;
|
|
|
|
};
|
2016-06-15 02:31:51 +08:00
|
|
|
}
|
2016-11-09 05:36:34 +08:00
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
return function (t) {
|
|
|
|
return t;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-01 01:33:49 +08:00
|
|
|
export function censor(text, censoredRegexp, replacementLetter) {
|
|
|
|
return censorFn(censoredRegexp, replacementLetter)(text);
|
2016-06-15 02:31:51 +08:00
|
|
|
}
|