FIX: Don't substitute emojis within code blocks

This commit is contained in:
Robin Ward 2016-03-02 14:31:32 -05:00
parent bfaa4cdb37
commit 0167f6bb57
4 changed files with 38 additions and 7 deletions

View File

@ -12,6 +12,15 @@ Discourse.Dialect.registerEmoji = function(code, url) {
extendedEmoji[code] = url;
};
var _unicodeReplacements;
var _unicodeRegexp;
Discourse.Dialect.setUnicodeReplacements = function(replacements) {
_unicodeReplacements = replacements;
if (replacements) {
_unicodeRegexp = new RegExp(Object.keys(replacements).join("|"), "g");
}
}
// This method is used by PrettyText to reset custom emojis in multisites
Discourse.Dialect.resetEmojis = function() {
extendedEmoji = {};
@ -151,6 +160,19 @@ Object.keys(translations).forEach(function (t) {
}
});
Discourse.Dialect.addPreProcessor(function(text) {
if (_unicodeReplacements) {
_unicodeRegexp.lastIndex = 0;
var m;
while ((m = _unicodeRegexp.exec(text)) !== null) {
text = text.replace(m[0], ":" + _unicodeReplacements[m[0]] + ":");
}
}
return text;
});
function escapeRegExp(s) {
return s.replace(/[-/\\^$*+?.()|[\]{}]/gi, '\\$&');
}

View File

@ -131,12 +131,8 @@ class Emoji
@unicode_replacements
end
def self.unicode_regexp
@unicode_regexp ||= Regexp.union(unicode_replacements.keys)
end
def self.sub_unicode!(text)
text.gsub!(unicode_regexp) {|m| ":#{unicode_replacements[m]}:"}
def self.unicode_replacements_json
@unicode_replacements_json ||= unicode_replacements.to_json
end
end

View File

@ -201,6 +201,12 @@ module PrettyText
end
end
if SiteSetting.enable_emoji?
context.eval("Discourse.Dialect.setUnicodeReplacements(#{Emoji.unicode_replacements_json})");
else
context.eval("Discourse.Dialect.setUnicodeReplacements(null)");
end
# reset emojis (v8 context is shared amongst multisites)
context.eval("Discourse.Dialect.resetEmojis();")
# custom emojis
@ -259,7 +265,6 @@ module PrettyText
options[:topicId] = opts[:topic_id]
working_text = text.dup
Emoji.sub_unicode!(working_text) if SiteSetting.enable_emoji?
sanitized = markdown(working_text, options)
doc = Nokogiri::HTML.fragment(sanitized)

View File

@ -395,6 +395,14 @@ HTML
expect(PrettyText.cook("💣")).to match(/\:bomb\:/)
end
it "doesn't replace emoji in inline code blocks with our emoji sets if emoji is enabled" do
expect(PrettyText.cook("`💣`")).not_to match(/\:bomb\:/)
end
it "doesn't replace emoji in code blocks with our emoji sets if emoji is enabled" do
expect(PrettyText.cook("```\n💣`\n```\n")).not_to match(/\:bomb\:/)
end
it "replaces some glyphs that are not in the emoji range" do
expect(PrettyText.cook("")).to match(/\:slightly_smiling\:/)
end