zulip/static/js/alert_words.js

54 lines
1.7 KiB
JavaScript
Raw Normal View History

var alert_words = (function () {
var exports = {};
exports.words = page_params.alert_words;
// escape_user_regex taken from jquery-ui/autocomplete.js,
// licensed under MIT license.
function escape_user_regex(value) {
return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
}
var find_href_backwards = /href=['"][\w:\/\.]+$/;
var find_title_backwards = /title=['"][\w:\/\.]+$/;
exports.process_message = function (message) {
if (!exports.notifies(message)) {
return;
}
_.each(exports.words, function (word) {
var clean = escape_user_regex(word);
var before_punctuation = '\\s|^|>|[\\(\\".,\';\\[]';
var after_punctuation = '\\s|$|<|[\\)\\"\\?!:.,\';\\]!]';
var word_in_href = new RegExp(find_href_backwards + word, 'i');
var regex = new RegExp('(' + before_punctuation + ')' +
'(' + clean + ')' +
'(' + after_punctuation + ')' , 'i');
message.content = message.content.replace(regex, function (match, before, word, after, offset, content) {
// Don't munge URL hrefs
var pre_match = content.substring(0, offset);
if (find_href_backwards.exec(pre_match) || find_title_backwards.exec(pre_match)) {
return before + word + after;
} else {
return before + "<span class='alert-word'>" + word + "</span>" + after;
}
});
});
};
exports.notifies = function (message) {
return ((message.sender_email !== page_params.email) && message.alerted);
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = alert_words;
}