Commit ae95118a authored by Johan H's avatar Johan H Committed by henrik

Convert UTF-8 Emoji to Gitlab emoji

parent 969286fc
...@@ -34,6 +34,7 @@ v 8.13.0 (unreleased) ...@@ -34,6 +34,7 @@ v 8.13.0 (unreleased)
- Simplify Mentionable concern instance methods - Simplify Mentionable concern instance methods
- Fix permission for setting an issue's due date - Fix permission for setting an issue's due date
- API: Multi-file commit !6096 (mahcsig) - API: Multi-file commit !6096 (mahcsig)
- Unicode emoji are now converted to images
- Revert "Label list shows all issues (opened or closed) with that label" - Revert "Label list shows all issues (opened or closed) with that label"
- Expose expires_at field when sharing project on API - Expose expires_at field when sharing project on API
- Fix VueJS template tags being rendered in code comments - Fix VueJS template tags being rendered in code comments
......
...@@ -14,15 +14,16 @@ module Banzai ...@@ -14,15 +14,16 @@ module Banzai
search_text_nodes(doc).each do |node| search_text_nodes(doc).each do |node|
content = node.to_html content = node.to_html
next if has_ancestor?(node, IGNORED_ANCESTOR_TAGS) next if has_ancestor?(node, IGNORED_ANCESTOR_TAGS)
if content.include?(':') || node.text.match(emoji_unicode_pattern)
next unless content.include?(':') || node.text.match(emoji_unicode_pattern)
html = emoji_name_image_filter(content) html = emoji_name_image_filter(content)
html = emoji_unicode_image_filter(html) html = emoji_unicode_image_filter(html)
next if html == content next if html == content
node.replace(html)
end
node.replace(html)
end end
doc doc
end end
...@@ -34,31 +35,35 @@ module Banzai ...@@ -34,31 +35,35 @@ module Banzai
def emoji_name_image_filter(text) def emoji_name_image_filter(text)
text.gsub(emoji_pattern) do |match| text.gsub(emoji_pattern) do |match|
name = $1 name = $1
"<img class='emoji' title=':#{name}:' alt=':#{name}:' src='#{emoji_url(name)}' height='20' width='20' align='absmiddle' />" emoji_image_tag(name, emoji_url(name))
end end
end end
# Replace unicode emoji with corresponding images if they exist.
# Replace unicode emojis with corresponding images if they exist.
# #
# text - String text to replace unicode emojis in. # text - String text to replace unicode emoji in.
# #
# Returns a String with unicode emojis replaced with images. # Returns a String with unicode emoji replaced with images.
def emoji_unicode_image_filter(text) def emoji_unicode_image_filter(text)
text.gsub(emoji_unicode_pattern) do |moji| text.gsub(emoji_unicode_pattern) do |moji|
"<img class='emoji' title=':#{Gitlab::Emoji.emojis_by_moji[moji]['name']}:' alt=':#{Gitlab::Emoji.emojis_by_moji[moji]['name']}:' src='#{emoji_unicode_url(moji)}' height='20' width='20' align='absmiddle' />" emoji_image_tag(Gitlab::Emoji.emojis_by_moji[moji]['name'], emoji_unicode_url(moji))
end end
end end
def emoji_image_tag(emoji_name, emoji_url)
"<img class='emoji' title=':#{emoji_name}:' alt=':#{emoji_name}:' src='#{emoji_url}' height='20' width='20' align='absmiddle' />"
end
# Build a regexp that matches all valid :emoji: names. # Build a regexp that matches all valid :emoji: names.
def self.emoji_pattern def self.emoji_pattern
@emoji_pattern ||= /:(#{Gitlab::Emoji.emojis_names.map { |name| Regexp.escape(name) }.join('|')}):/ @emoji_pattern ||= /:(#{Gitlab::Emoji.emojis_names.map { |name| Regexp.escape(name) }.join('|')}):/
end end
# Build a regexp that matches all valid unicode emojis names. # Build a regexp that matches all valid unicode emojis names.
def self.emoji_unicode_pattern def self.emoji_unicode_pattern
@emoji_unicode_pattern ||= /(#{Gitlab::Emoji.emojis_unicodes.map { |moji| Regexp.escape(moji) }.join('|')})/ @emoji_unicode_pattern ||= /(#{Gitlab::Emoji.emojis_unicodes.map { |moji| Regexp.escape(moji) }.join('|')})/
end end
private private
def emoji_url(name) def emoji_url(name)
...@@ -80,13 +85,10 @@ module Banzai ...@@ -80,13 +85,10 @@ module Banzai
emoji_unicode_path = emoji_unicode_filename(moji) emoji_unicode_path = emoji_unicode_filename(moji)
if context[:asset_host] if context[:asset_host]
# Asset host is specified.
url_to_image(emoji_unicode_path) url_to_image(emoji_unicode_path)
elsif context[:asset_root] elsif context[:asset_root]
# Gitlab url is specified
File.join(context[:asset_root], url_to_image(emoji_unicode_path)) File.join(context[:asset_root], url_to_image(emoji_unicode_path))
else else
# All other cases
url_to_image(emoji_unicode_path) url_to_image(emoji_unicode_path)
end end
end end
...@@ -102,6 +104,7 @@ module Banzai ...@@ -102,6 +104,7 @@ module Banzai
def emoji_filename(name) def emoji_filename(name)
"#{Gitlab::Emoji.emoji_filename(name)}.png" "#{Gitlab::Emoji.emoji_filename(name)}.png"
end end
def emoji_unicode_pattern def emoji_unicode_pattern
self.class.emoji_unicode_pattern self.class.emoji_unicode_pattern
end end
......
...@@ -9,16 +9,19 @@ module Gitlab ...@@ -9,16 +9,19 @@ module Gitlab
def emojis_by_moji def emojis_by_moji
Gemojione.index.instance_variable_get(:@emoji_by_moji) Gemojione.index.instance_variable_get(:@emoji_by_moji)
end end
def emojis_unicodes def emojis_unicodes
emojis_by_moji.keys.sort emojis_by_moji.keys
end end
def emojis_names def emojis_names
emojis.keys.sort emojis.keys
end end
def emoji_filename(name) def emoji_filename(name)
emojis[name]["unicode"] emojis[name]["unicode"]
end end
def emoji_unicode_filename(moji) def emoji_unicode_filename(moji)
emojis_by_moji[moji]["unicode"] emojis_by_moji[moji]["unicode"]
end end
......
...@@ -16,10 +16,12 @@ describe Banzai::Filter::EmojiFilter, lib: true do ...@@ -16,10 +16,12 @@ describe Banzai::Filter::EmojiFilter, lib: true do
doc = filter('<p>:heart:</p>') doc = filter('<p>:heart:</p>')
expect(doc.css('img').first.attr('src')).to eq 'https://foo.com/assets/2764.png' expect(doc.css('img').first.attr('src')).to eq 'https://foo.com/assets/2764.png'
end end
it 'replaces supported unicode emoji' do it 'replaces supported unicode emoji' do
doc = filter('<p>❤️</p>') doc = filter('<p>❤️</p>')
expect(doc.css('img').first.attr('src')).to eq 'https://foo.com/assets/2764.png' expect(doc.css('img').first.attr('src')).to eq 'https://foo.com/assets/2764.png'
end end
it 'ignores unsupported emoji' do it 'ignores unsupported emoji' do
exp = act = '<p>:foo:</p>' exp = act = '<p>:foo:</p>'
doc = filter(act) doc = filter(act)
...@@ -162,4 +164,18 @@ describe Banzai::Filter::EmojiFilter, lib: true do ...@@ -162,4 +164,18 @@ describe Banzai::Filter::EmojiFilter, lib: true do
doc = filter(':frowning:', asset_host: 'https://this-is-ignored-i-guess?') doc = filter(':frowning:', asset_host: 'https://this-is-ignored-i-guess?')
expect(doc.css('img').first.attr('src')).to start_with('https://cdn.example.com') expect(doc.css('img').first.attr('src')).to start_with('https://cdn.example.com')
end end
it 'uses a custom asset_root context' do
root = Gitlab.config.gitlab.url + 'gitlab/root'
doc = filter("'🎱'", asset_root: root)
expect(doc.css('img').first.attr('src')).to start_with(root)
end
it 'uses a custom asset_host context' do
ActionController::Base.asset_host = 'https://cdn.example.com'
doc = filter("'🎱'", asset_host: 'https://this-is-ignored-i-guess?')
expect(doc.css('img').first.attr('src')).to start_with('https://cdn.example.com')
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment