Class: Banzai::Filter::ExternalIssueReferenceFilter

Inherits:
ReferenceFilter
  • Object
show all
Defined in:
lib/banzai/filter/external_issue_reference_filter.rb

Overview

HTML filter that replaces external issue tracker references with links. References are ignored if the project doesn't use an external issue tracker.

This filter does not support cross-project references.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ReferenceFilter

call, #call_and_update_nodes, #data_attribute, #each_node, #element_node?, #group, #ignore_ancestor_query, #initialize, #nodes, #reference_class, #replace_link_node_with_href, #replace_link_node_with_text, #replace_text_when_pattern_matches, #skip_project_check?, #text_node?, #user, #validate, #yield_valid_link

Methods included from OutputSafety

#escape_once

Methods included from RequestStoreReferenceCache

#cached_call, #get_or_set_cache

Constructor Details

This class inherits a constructor from Banzai::Filter::ReferenceFilter

Class Method Details

.references_in(text, pattern) ⇒ Object

Public: Find `JIRA-123` issue references in text

ExternalIssueReferenceFilter.references_in(text, pattern) do |match, issue|
  "<a href=...>##{issue}</a>"
end

text - String text to search.

Yields the String match and the String issue reference.

Returns a String replaced with the return of the block.


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

def self.references_in(text, pattern)
  text.gsub(pattern) do |match|
    yield match, $~[:issue]
  end
end

Instance Method Details

#callObject


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
# File 'lib/banzai/filter/external_issue_reference_filter.rb', line 30

def call
  # Early return if the project isn't using an external tracker
  return doc if project.nil? || default_issues_tracker?

  ref_pattern = issue_reference_pattern
  ref_start_pattern = /\A#{ref_pattern}\z/

  nodes.each_with_index do |node, index|
    if text_node?(node)
      replace_text_when_pattern_matches(node, index, ref_pattern) do |content|
        issue_link_filter(content)
      end

    elsif element_node?(node)
      yield_valid_link(node) do |link, inner_html|
        if link =~ ref_start_pattern
          replace_link_node_with_href(node, index, link) do
            issue_link_filter(link, link_content: inner_html)
          end
        end
      end
    end
  end

  doc
end