Class: Banzai::Filter::InlineDiffFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Includes:
Concerns::PipelineTimingCheck
Defined in:
lib/banzai/filter/inline_diff_filter.rb

Constant Summary collapse

IGNORED_ANCESTOR_TAGS =
%w[pre code tt].to_set
INLINE_DIFF_DELETION_UNTRUSTED =
'(?:\[\-(.*?)\-\]|\{\-(.*?)\-\})'
INLINE_DIFF_DELETION_UNTRUSTED_REGEX =
Gitlab::UntrustedRegexp.new(INLINE_DIFF_DELETION_UNTRUSTED, multiline: false).freeze
INLINE_DIFF_ADDITION_UNTRUSTED =
'(?:\[\+(.*?)\+\]|\{\+(.*?)\+\})'
INLINE_DIFF_ADDITION_UNTRUSTED_REGEX =
Gitlab::UntrustedRegexp.new(INLINE_DIFF_ADDITION_UNTRUSTED, multiline: false).freeze

Constants included from Concerns::PipelineTimingCheck

Concerns::PipelineTimingCheck::MAX_PIPELINE_SECONDS

Instance Method Summary collapse

Methods included from Concerns::PipelineTimingCheck

#exceeded_pipeline_max?

Instance Method Details

#callObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/banzai/filter/inline_diff_filter.rb', line 19

def call
  doc.xpath('descendant-or-self::text()').each do |node|
    next if has_ancestor?(node, IGNORED_ANCESTOR_TAGS)

    content = node.to_html
    html_content = inline_diff_filter(content)

    next if content == html_content

    node.replace(html_content)
  end
  doc
end

#inline_diff_filter(text) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/banzai/filter/inline_diff_filter.rb', line 33

def inline_diff_filter(text)
  html_content = INLINE_DIFF_DELETION_UNTRUSTED_REGEX
    .replace_gsub(text, limit: Banzai::Filter::FILTER_ITEM_LIMIT) do |match|
    %(<span class="idiff left right deletion">#{match[1]}#{match[2]}</span>)
  end

  INLINE_DIFF_ADDITION_UNTRUSTED_REGEX
    .replace_gsub(html_content, limit: Banzai::Filter::FILTER_ITEM_LIMIT) do |match|
    %(<span class="idiff left right addition">#{match[1]}#{match[2]}</span>)
  end
end