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

83 lines
2.1 KiB
Plaintext
Raw Normal View History

2017-01-31 10:39:45 +08:00
function escapeRegexp(text) {
2018-06-15 23:03:24 +08:00
return text.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&").replace(/\*/g, "S*");
2017-01-31 10:39:45 +08:00
}
function createCensorRegexp(patterns) {
return new RegExp(`((?<!\\w)(?:${patterns.join("|")}))(?!\\w)`, "ig");
}
2018-06-15 23:03:24 +08:00
export function censorFn(
censoredWords,
replacementLetter,
watchedWordsRegularExpressions
) {
let patterns = [];
replacementLetter = replacementLetter || "&#9632;";
if (censoredWords && censoredWords.length) {
patterns = censoredWords.split("|");
if (!watchedWordsRegularExpressions) {
patterns = patterns.map(t => `(${escapeRegexp(t)})`);
}
}
if (patterns.length) {
let censorRegexp;
try {
if (watchedWordsRegularExpressions) {
2018-06-15 23:03:24 +08:00
censorRegexp = new RegExp(
"((?:" + patterns.join("|") + "))(?![^\\(]*\\))",
"ig"
);
} else {
censorRegexp = createCensorRegexp(patterns);
}
if (censorRegexp) {
return function(text) {
let original = text;
try {
let m = censorRegexp.exec(text);
const fourCharReplacement = new Array(5).join(replacementLetter);
while (m && m[0]) {
2018-06-15 23:03:24 +08:00
if (m[0].length > original.length) {
return original;
} // regex is dangerous
if (watchedWordsRegularExpressions) {
text = text.replace(censorRegexp, fourCharReplacement);
} else {
2018-06-15 23:03:24 +08:00
const replacement = new Array(m[0].length + 1).join(
replacementLetter
);
text = text.replace(
createCensorRegexp([escapeRegexp(m[0])]),
2018-06-15 23:03:24 +08:00
replacement
);
}
m = censorRegexp.exec(text);
}
return text;
} catch (e) {
return original;
}
};
}
2018-06-15 23:03:24 +08:00
} catch (e) {
// fall through
}
}
2018-06-15 23:03:24 +08:00
return function(t) {
return t;
};
}
export function censor(text, censoredWords, replacementLetter) {
return censorFn(censoredWords, replacementLetter)(text);
}