summaryrefslogtreecommitdiff
path: root/lib/banzai/filter/suggestion_filter.rb
blob: 848aca10a20c349b8af93fd9625e9b4b45073bbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frozen_string_literal: true

# Generated HTML is transformed back to GFM by app/assets/javascripts/behaviors/markdown/nodes/code_block.js
module Banzai
  module Filter
    class SuggestionFilter < HTML::Pipeline::Filter
      # Class used for tagging elements that should be rendered
      TAG_CLASS = 'js-render-suggestion'.freeze
      SUGGESTION_REGEX = Gitlab::Diff::SuggestionsParser::SUGGESTION_CONTEXT

      def call
        return doc unless suggestions_filter_enabled?

        doc.search('pre.suggestion > code').each do |node|
          # TODO: Remove once multi-line suggestions FF get removed (#59178).
          remove_multi_line_params(node.parent)

          node.add_class(TAG_CLASS)
        end

        doc
      end

      def suggestions_filter_enabled?
        context[:suggestions_filter_enabled]
      end

      private

      def project
        context[:project]
      end

      def remove_multi_line_params(node)
        return if Feature.enabled?(:multi_line_suggestions, project)

        if node[SyntaxHighlightFilter::LANG_PARAMS_ATTR]&.match?(SUGGESTION_REGEX)
          node.remove_attribute(SyntaxHighlightFilter::LANG_PARAMS_ATTR)
        end
      end
    end
  end
end