mirror of
https://github.com/discourse/discourse.git
synced 2024-12-17 07:33:50 +08:00
234694b50f
This adds the markdown.it engine to Discourse. https://github.com/markdown-it/markdown-it As the migration is going to take a while the new engine is default disabled. To enable it you must change the hidden site setting: enable_experimental_markdown_it. This commit is a squash of many other commits, it also includes some improvements to autospec (ability to run plugins), and a dev dependency on the og gem for html normalization.
58 lines
1.5 KiB
JavaScript
58 lines
1.5 KiB
JavaScript
function escapeRegexp(text) {
|
|
return text.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
}
|
|
|
|
export function censorFn(censoredWords, censoredPattern, replacementLetter) {
|
|
|
|
let patterns = [];
|
|
|
|
replacementLetter = replacementLetter || "■";
|
|
|
|
if (censoredWords && censoredWords.length) {
|
|
patterns = censoredWords.split("|").map(t => `(${escapeRegexp(t)})`);
|
|
}
|
|
|
|
if (censoredPattern && censoredPattern.length > 0) {
|
|
patterns.push("(" + censoredPattern + ")");
|
|
}
|
|
|
|
if (patterns.length) {
|
|
let censorRegexp;
|
|
|
|
try {
|
|
censorRegexp = new RegExp("(\\b(?:" + patterns.join("|") + ")\\b)(?![^\\(]*\\))", "ig");
|
|
|
|
if (censorRegexp) {
|
|
|
|
return function(text) {
|
|
let original = text;
|
|
|
|
try {
|
|
let m = censorRegexp.exec(text);
|
|
|
|
while (m && m[0]) {
|
|
if (m[0].length > original.length) { return original; } // regex is dangerous
|
|
const replacement = new Array(m[0].length+1).join(replacementLetter);
|
|
text = text.replace(new RegExp(`(\\b${escapeRegexp(m[0])}\\b)(?![^\\(]*\\))`, "ig"), replacement);
|
|
m = censorRegexp.exec(text);
|
|
}
|
|
|
|
return text;
|
|
} catch (e) {
|
|
return original;
|
|
}
|
|
};
|
|
|
|
}
|
|
} catch(e) {
|
|
// fall through
|
|
}
|
|
}
|
|
|
|
return function(t){ return t;};
|
|
}
|
|
|
|
export function censor(text, censoredWords, censoredPattern, replacementLetter) {
|
|
return censorFn(censoredWords, censoredPattern, replacementLetter)(text);
|
|
}
|