2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
# Generated HTML is transformed back to GFM by app/assets/javascripts/behaviors/markdown/nodes/emoji.js
|
2015-12-23 02:04:40 +05:30
|
|
|
module Banzai
|
|
|
|
module Filter
|
2016-11-03 12:29:30 +05:30
|
|
|
# HTML filter that replaces :emoji: and unicode with images.
|
2015-09-11 14:41:01 +05:30
|
|
|
#
|
|
|
|
# Based on HTML::Pipeline::EmojiFilter
|
|
|
|
class EmojiFilter < HTML::Pipeline::Filter
|
|
|
|
IGNORED_ANCESTOR_TAGS = %w(pre code tt).to_set
|
|
|
|
|
|
|
|
def call
|
2021-06-02 17:11:27 +05:30
|
|
|
doc.xpath('descendant-or-self::text()').each do |node|
|
2015-09-11 14:41:01 +05:30
|
|
|
content = node.to_html
|
|
|
|
next if has_ancestor?(node, IGNORED_ANCESTOR_TAGS)
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
next unless content.include?(':') || node.text.match(emoji_unicode_pattern)
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
html = emoji_unicode_element_unicode_filter(content)
|
|
|
|
html = emoji_name_element_unicode_filter(html)
|
2015-09-11 14:41:01 +05:30
|
|
|
|
|
|
|
next if html == content
|
|
|
|
|
|
|
|
node.replace(html)
|
|
|
|
end
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Replace :emoji: with corresponding gl-emoji unicode.
|
2015-09-11 14:41:01 +05:30
|
|
|
#
|
|
|
|
# text - String text to replace :emoji: in.
|
|
|
|
#
|
2017-08-17 22:00:37 +05:30
|
|
|
# Returns a String with :emoji: replaced with gl-emoji unicode.
|
|
|
|
def emoji_name_element_unicode_filter(text)
|
2015-09-11 14:41:01 +05:30
|
|
|
text.gsub(emoji_pattern) do |match|
|
2020-11-24 15:15:51 +05:30
|
|
|
name = Regexp.last_match(1)
|
2021-12-11 22:18:48 +05:30
|
|
|
emoji = TanukiEmoji.find_by_alpha_code(name)
|
|
|
|
Gitlab::Emoji.gl_emoji_tag(emoji)
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Replace unicode emoji with corresponding gl-emoji unicode.
|
2016-11-03 12:29:30 +05:30
|
|
|
#
|
|
|
|
# text - String text to replace unicode emoji in.
|
|
|
|
#
|
2017-08-17 22:00:37 +05:30
|
|
|
# Returns a String with unicode emoji replaced with gl-emoji unicode.
|
|
|
|
def emoji_unicode_element_unicode_filter(text)
|
2016-11-03 12:29:30 +05:30
|
|
|
text.gsub(emoji_unicode_pattern) do |moji|
|
2021-12-11 22:18:48 +05:30
|
|
|
emoji = TanukiEmoji.find_by_codepoints(moji)
|
|
|
|
Gitlab::Emoji.gl_emoji_tag(emoji)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
# Build a regexp that matches all valid :emoji: names.
|
|
|
|
def self.emoji_pattern
|
2021-12-11 22:18:48 +05:30
|
|
|
@emoji_pattern ||= TanukiEmoji.index.alpha_code_pattern
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
# Build a regexp that matches all valid unicode emojis names.
|
|
|
|
def self.emoji_unicode_pattern
|
2021-12-11 22:18:48 +05:30
|
|
|
@emoji_unicode_pattern ||= TanukiEmoji.index.codepoints_pattern
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def emoji_pattern
|
|
|
|
self.class.emoji_pattern
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def emoji_unicode_pattern
|
|
|
|
self.class.emoji_unicode_pattern
|
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|