From 6a31a5d52b946358dc0be142c4119217f744d7ed Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 25 Aug 2015 22:25:37 +0800 Subject: [PATCH] Extract logic for censored-words so that it can be reused. --- .../discourse/dialects/censored_dialect.js | 23 +---------------- .../discourse/lib/censored-words.js | 25 +++++++++++++++++++ lib/pretty_text.rb | 1 + 3 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 app/assets/javascripts/discourse/lib/censored-words.js diff --git a/app/assets/javascripts/discourse/dialects/censored_dialect.js b/app/assets/javascripts/discourse/dialects/censored_dialect.js index 1cb38a9115a..c4faa574815 100644 --- a/app/assets/javascripts/discourse/dialects/censored_dialect.js +++ b/app/assets/javascripts/discourse/dialects/censored_dialect.js @@ -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); }); diff --git a/app/assets/javascripts/discourse/lib/censored-words.js b/app/assets/javascripts/discourse/lib/censored-words.js new file mode 100644 index 00000000000..840c3093b0d --- /dev/null +++ b/app/assets/javascripts/discourse/lib/censored-words.js @@ -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; + } +} diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index 116b83b6005..1b43959482c 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -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", )