mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: Don't substitute emojis within code blocks
This commit is contained in:
parent
bfaa4cdb37
commit
0167f6bb57
@ -12,6 +12,15 @@ Discourse.Dialect.registerEmoji = function(code, url) {
|
|||||||
extendedEmoji[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
|
// This method is used by PrettyText to reset custom emojis in multisites
|
||||||
Discourse.Dialect.resetEmojis = function() {
|
Discourse.Dialect.resetEmojis = function() {
|
||||||
extendedEmoji = {};
|
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) {
|
function escapeRegExp(s) {
|
||||||
return s.replace(/[-/\\^$*+?.()|[\]{}]/gi, '\\$&');
|
return s.replace(/[-/\\^$*+?.()|[\]{}]/gi, '\\$&');
|
||||||
}
|
}
|
||||||
|
@ -131,12 +131,8 @@ class Emoji
|
|||||||
@unicode_replacements
|
@unicode_replacements
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.unicode_regexp
|
def self.unicode_replacements_json
|
||||||
@unicode_regexp ||= Regexp.union(unicode_replacements.keys)
|
@unicode_replacements_json ||= unicode_replacements.to_json
|
||||||
end
|
|
||||||
|
|
||||||
def self.sub_unicode!(text)
|
|
||||||
text.gsub!(unicode_regexp) {|m| ":#{unicode_replacements[m]}:"}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -201,6 +201,12 @@ module PrettyText
|
|||||||
end
|
end
|
||||||
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)
|
# reset emojis (v8 context is shared amongst multisites)
|
||||||
context.eval("Discourse.Dialect.resetEmojis();")
|
context.eval("Discourse.Dialect.resetEmojis();")
|
||||||
# custom emojis
|
# custom emojis
|
||||||
@ -259,7 +265,6 @@ module PrettyText
|
|||||||
options[:topicId] = opts[:topic_id]
|
options[:topicId] = opts[:topic_id]
|
||||||
|
|
||||||
working_text = text.dup
|
working_text = text.dup
|
||||||
Emoji.sub_unicode!(working_text) if SiteSetting.enable_emoji?
|
|
||||||
sanitized = markdown(working_text, options)
|
sanitized = markdown(working_text, options)
|
||||||
|
|
||||||
doc = Nokogiri::HTML.fragment(sanitized)
|
doc = Nokogiri::HTML.fragment(sanitized)
|
||||||
|
@ -395,6 +395,14 @@ HTML
|
|||||||
expect(PrettyText.cook("💣")).to match(/\:bomb\:/)
|
expect(PrettyText.cook("💣")).to match(/\:bomb\:/)
|
||||||
end
|
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
|
it "replaces some glyphs that are not in the emoji range" do
|
||||||
expect(PrettyText.cook("☺")).to match(/\:slightly_smiling\:/)
|
expect(PrettyText.cook("☺")).to match(/\:slightly_smiling\:/)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user