diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index d50326d4e..ebe1503c9 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -246,13 +246,24 @@ module PrettyText end end + def self.emoji_unicode(text) + return text unless SiteSetting.enable_emoji? + + Emoji.db.each do |e| + text.gsub!(e['emoji'], ":#{e['aliases'][0]}:") + end + + text + end + def self.cook(text, opts={}) options = opts.dup # we have a minor inconsistency options[:topicId] = opts[:topic_id] - sanitized = markdown(text.dup, options) + working_text = emoji_unicode(text.dup) + sanitized = markdown(working_text, options) doc = Nokogiri::HTML.fragment(sanitized) diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 64e5851c7..4fd92a845 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -378,7 +378,17 @@ HTML table = "
test
a
" expect(PrettyText.cook(table)).to match_html("") end + end + describe "emoji" do + it "replaces unicode emoji with our emoji sets if emoji is enabled" do + expect(PrettyText.cook("💣")).to match(/\:bomb\:/) + end + + it "doesn't replace unicode emoji if emoji is disabled" do + SiteSetting.enable_emoji = false + expect(PrettyText.cook("💣")).not_to match(/\:bomb\:/) + end end end