Class: Banzai::Filter::InlineEmbedsFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Defined in:
lib/banzai/filter/inline_embeds_filter.rb

Overview

HTML filter that inserts a node for each occurence of a given link format. To transform references to DB resources in place, prefer to inherit from AbstractReferenceFilter.

Instance Method Summary collapse

Instance Method Details

#callObject

Find every relevant link, create a new node based on the link, and insert this node after any html content surrounding the link.


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/banzai/filter/inline_embeds_filter.rb', line 12

def call
  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(params) ⇒ Object

Child class must provide the metrics_dashboard_url.

Return a Nokogiri::XML::Element to embed in the markdown which provides a url to the metric_dashboard endpoint where data can be requested through a prometheus proxy. InlineMetricsRedactorFilter is responsible for premissions to see this div (and relies on the class 'js-render-metrics' ).


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

def create_element(params)
  doc.document.create_element(
    'div',
    class: 'js-render-metrics',
    'data-dashboard-url': metrics_dashboard_url(params)
  )
end

#element_to_embed(node) ⇒ Object

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


55
56
57
58
59
# File 'lib/banzai/filter/inline_embeds_filter.rb', line 55

def element_to_embed(node)
  return unless params = embed_params(node)

  create_element(params)
end

#embed_params(node) ⇒ Object

Returns a hash of named parameters based on the provided regex with string keys.

Override to select nodes other than links.


65
66
67
68
69
# File 'lib/banzai/filter/inline_embeds_filter.rb', line 65

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

  link_pattern.match(url) { |m| m.named_captures }
end

#gitlab_domainObject


86
87
88
# File 'lib/banzai/filter/inline_embeds_filter.rb', line 86

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

Implement in child class unless overriding #embed_params

Returns the regex pattern used to filter to only matching urls.


42
43
# File 'lib/banzai/filter/inline_embeds_filter.rb', line 42

def link_pattern
end

#metrics_dashboard_urlObject

Implement in child class.

Provides a full url to request the relevant panels of metric data.

Raises:

  • (NotImplementedError)

82
83
84
# File 'lib/banzai/filter/inline_embeds_filter.rb', line 82

def metrics_dashboard_url
  raise NotImplementedError
end

#query_params(url) ⇒ Object

Parses query params out from full url string into hash.

Ex) '--> { title: 'Title', group: 'Group' }


75
76
77
# File 'lib/banzai/filter/inline_embeds_filter.rb', line 75

def query_params(url)
  Gitlab::Metrics::Dashboard::Url.parse_query(url)
end

#xpath_searchObject

Returns the xpath query string used to select nodes from the html document on which the embed is based.

Override to select nodes other than links.


49
50
51
# File 'lib/banzai/filter/inline_embeds_filter.rb', line 49

def xpath_search
  'descendant-or-self::a[@href]'
end