Welcome to mirror list, hosted at ThFree Co, Russian Federation.

inline_observability_filter.rb « filter « banzai « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8e38f6899596345e1316dd4740501d326c122a97 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true

module Banzai
  module Filter
    class InlineObservabilityFilter < HTML::Pipeline::Filter
      include Gitlab::Utils::StrongMemoize

      def call
        return doc unless Gitlab::Observability.enabled?(group)

        doc.xpath(xpath_search).each do |node|
          next unless element = element_to_embed(node)

          # We want this to follow any surrounding content. For example,
          # if a link is inline in a paragraph.
          node.parent.children.last.add_next_sibling(element)
        end

        doc
      end

      # Placeholder element for the frontend to use as an
      # injection point for observability.
      def create_element(url)
        doc.document.create_element(
          'div',
          class: 'js-render-observability',
          'data-frame-url': url
        )
      end

      # Search params for selecting observability links.
      def xpath_search
        "descendant-or-self::a[starts-with(@href, '#{gitlab_domain}/groups/') and contains(@href,'/-/observability/')]"
      end

      # Creates a new element based on the parameters
      # obtained from the target link
      def element_to_embed(node)
        url = node['href']

        embeddable_url = extract_embeddable_url(url)
        create_element(embeddable_url) if embeddable_url
      end

      private

      def extract_embeddable_url(url)
        strong_memoize_with(:embeddable_url, url) do
          Gitlab::Observability.embeddable_url(url)
        end
      end

      def group
        context[:group] || context[:project]&.group
      end

      def gitlab_domain
        ::Gitlab.config.gitlab.url
      end
    end
  end
end