discourse/app/assets/javascripts/pretty-text/censored-words.js.es6

27 lines
722 B
Plaintext
Raw Normal View History

export function censorFn(regexpString, replacementLetter) {
if (regexpString) {
let censorRegexp = new RegExp(regexpString, "ig");
replacementLetter = replacementLetter || "■";
return function(text) {
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)
2018-06-15 23:03:24 +08:00
);
});
return text;
};
}
2018-06-15 23:03:24 +08:00
return function(t) {
return t;
};
}
export function censor(text, censoredRegexp, replacementLetter) {
return censorFn(censoredRegexp, replacementLetter)(text);
}