Class: Banzai::Filter::CustomEmojiFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
lib/banzai/filter/custom_emoji_filter.rb

Constant Summary collapse

IGNORED_ANCESTOR_TAGS =
%w[pre code tt].to_set

Instance Method Summary collapse

Instance Method Details

#callObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/banzai/filter/custom_emoji_filter.rb', line 10

def call
  return doc unless resource_parent

  doc.xpath('descendant-or-self::text()').each do |node|
    content = node.to_html

    next if has_ancestor?(node, IGNORED_ANCESTOR_TAGS)
    next unless content.include?(':')
    next unless has_custom_emoji?

    html = custom_emoji_name_element_filter(content)

    node.replace(html) unless html == content
  end

  doc
end

#custom_emoji_name_element_filter(text) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/banzai/filter/custom_emoji_filter.rb', line 35

def custom_emoji_name_element_filter(text)
  text.gsub(custom_emoji_pattern) do |match|
    name = Regexp.last_match[1]
    custom_emoji = all_custom_emoji[name]

    if custom_emoji
      Gitlab::Emoji.custom_emoji_tag(custom_emoji.name, custom_emoji.url)
    else
      match
    end
  end
end

#custom_emoji_patternObject



28
29
30
31
32
33
# File 'lib/banzai/filter/custom_emoji_filter.rb', line 28

def custom_emoji_pattern
  @emoji_pattern ||=
    /(?<=[^[:alnum:]:]|\n|^)
    :(#{CustomEmoji::NAME_REGEXP}):
    (?=[^[:alnum:]:]|$)/xo
end