reference_filter.rb 2.7 KB
Newer Older
1
require 'active_support/core_ext/string/output_safety'
2
require 'html/pipeline/filter'
3 4 5 6 7 8 9 10

module Gitlab
  module Markdown
    # Base class for GitLab Flavored Markdown reference filters.
    #
    # References within <pre>, <code>, <a>, and <style> elements are ignored.
    #
    # Context options:
11
    #   :project (required) - Current project, ignored if reference is cross-project.
12 13 14
    #   :reference_class    - Custom CSS class added to reference links.
    #   :only_path          - Generate path-only links.
    #
15 16
    # Results:
    #   :references - A Hash of references that were found and replaced.
17
    class ReferenceFilter < HTML::Pipeline::Filter
18 19 20 21 22 23
      def initialize(*args)
        super

        result[:references] = Hash.new { |hash, type| hash[type] = [] }
      end

24 25 26 27
      def escape_once(html)
        ERB::Util.html_escape_once(html)
      end

28 29 30 31 32 33 34 35 36 37 38 39
      # Don't look for references in text nodes that are children of these
      # elements.
      IGNORE_PARENTS = %w(pre code a style).to_set

      def ignored_ancestry?(node)
        has_ancestor?(node, IGNORE_PARENTS)
      end

      def project
        context[:project]
      end

40 41
      # Add a reference to the pipeline's result Hash
      #
R
Robert Speicher 已提交
42 43
      # type   - Singular Symbol reference type (e.g., :issue, :user, etc.)
      # values - One or more Objects to add
R
Robert Speicher 已提交
44 45
      def push_result(type, *values)
        return if values.empty?
46

R
Robert Speicher 已提交
47
        result[:references][type].push(*values)
48 49
      end

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
      def reference_class(type)
        "gfm gfm-#{type} #{context[:reference_class]}".strip
      end

      # Iterate through the document's text nodes, yielding the current node's
      # content if:
      #
      # * The `project` context value is present AND
      # * The node's content matches `pattern` AND
      # * The node is not an ancestor of an ignored node type
      #
      # pattern - Regex pattern against which to match the node's content
      #
      # Yields the current node's String contents. The result of the block will
      # replace the node's existing content and update the current document.
      #
R
Robert Speicher 已提交
66
      # Returns the updated Nokogiri::XML::Document object.
67
      def replace_text_nodes_matching(pattern)
68 69
        return doc if project.nil?

70
        search_text_nodes(doc).each do |node|
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
          content = node.to_html

          next unless content.match(pattern)
          next if ignored_ancestry?(node)

          html = yield content

          next if html == content

          node.replace(html)
        end

        doc
      end

86 87 88
      # Ensure that a :project key exists in context
      #
      # Note that while the key might exist, its value could be nil!
89 90 91 92 93 94
      def validate
        needs :project
      end
    end
  end
end