Extract logic for censored-words so that it can be reused.

This commit is contained in:
Guo Xiang Tan 2015-08-25 22:25:37 +08:00
parent 294669c856
commit 6a31a5d52b
3 changed files with 27 additions and 22 deletions

View File

@ -1,24 +1,3 @@
var censorRegexp;
Discourse.Dialect.addPreProcessor(function(text) {
var censored = Discourse.SiteSettings.censored_words;
if (censored && censored.length) {
if (!censorRegexp) {
var split = censored.split("|");
if (split && split.length) {
censorRegexp = new RegExp("\\b(?:" + split.map(function (t) { return "(" + t.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') + ")"; }).join("|") + ")\\b", "ig");
}
}
if (censorRegexp) {
var m = censorRegexp.exec(text);
while (m && m[0]) {
var replacement = new Array(m[0].length+1).join('■');
text = text.replace(new RegExp("\\b" + m[0] + "\\b", "ig"), replacement);
m = censorRegexp.exec(text);
}
}
}
return text;
return Discourse.CensoredWords.censor(text);
});

View File

@ -0,0 +1,25 @@
Discourse.CensoredWords = {
censor: function(text) {
var censorRegexp,
censored = Discourse.SiteSettings.censored_words;
if (censored && censored.length) {
if (!censorRegexp) {
var split = censored.split("|");
if (split && split.length) {
censorRegexp = new RegExp("\\b(?:" + split.map(function (t) { return "(" + t.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') + ")"; }).join("|") + ")\\b", "ig");
}
}
if (censorRegexp) {
var m = censorRegexp.exec(text);
while (m && m[0]) {
var replacement = new Array(m[0].length+1).join('■');
text = text.replace(new RegExp("\\b" + m[0] + "\\b", "ig"), replacement);
m = censorRegexp.exec(text);
}
}
}
return text;
}
}

View File

@ -79,6 +79,7 @@ module PrettyText
"vendor/assets/javascripts/better_markdown.js",
"app/assets/javascripts/defer/html-sanitizer-bundle.js",
"app/assets/javascripts/discourse/dialects/dialect.js",
"app/assets/javascripts/discourse/lib/censored-words.js",
"app/assets/javascripts/discourse/lib/utilities.js",
"app/assets/javascripts/discourse/lib/markdown.js",
)