Class: Banzai::Filter::CustomEmojiFilter

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

Constant Summary collapse

IGNORED_ANCESTOR_TAGS =
%w[pre code tt].to_set

Constants included from Banzai::Filter::Concerns::TimeoutFilterHandler

Banzai::Filter::Concerns::TimeoutFilterHandler::COMPLEX_MARKDOWN_MESSAGE, Banzai::Filter::Concerns::TimeoutFilterHandler::RENDER_TIMEOUT, Banzai::Filter::Concerns::TimeoutFilterHandler::SANITIZATION_RENDER_TIMEOUT

Constants included from Banzai::Filter::Concerns::PipelineTimingCheck

Banzai::Filter::Concerns::PipelineTimingCheck::MAX_PIPELINE_SECONDS

Instance Method Summary collapse

Methods included from Banzai::Filter::Concerns::PipelineTimingCheck

#exceeded_pipeline_max?

Instance Method Details

#callObject



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

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



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

def custom_emoji_name_element_filter(text)
  Gitlab::Utils::Gsub
    .gsub_with_limit(text, custom_emoji_pattern, limit: Banzai::Filter::FILTER_ITEM_LIMIT) do |match_data|
    name = match_data[1]
    custom_emoji = all_custom_emoji[name]

    if custom_emoji
      Gitlab::Emoji.custom_emoji_tag(custom_emoji.name, custom_emoji.url)
    else
      match_data[0]
    end
  end
end

#custom_emoji_patternObject



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

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