Class: Banzai::Filter::InlineObservabilityFilter

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

Instance Method Summary collapse

Instance Method Details

#callObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/banzai/filter/inline_observability_filter.rb', line 8

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

#create_element(url) ⇒ Object

Placeholder element for the frontend to use as an injection point for observability.



24
25
26
27
28
29
30
# File 'lib/banzai/filter/inline_observability_filter.rb', line 24

def create_element(url)
  doc.document.create_element(
    'div',
    class: 'js-render-observability',
    'data-frame-url': url
  )
end

#element_to_embed(node) ⇒ Object

Creates a new element based on the parameters obtained from the target link



39
40
41
42
43
44
# File 'lib/banzai/filter/inline_observability_filter.rb', line 39

def element_to_embed(node)
  url = node['href']

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

#xpath_searchObject

Search params for selecting observability links.



33
34
35
# File 'lib/banzai/filter/inline_observability_filter.rb', line 33

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