summaryrefslogtreecommitdiff
path: root/lib/banzai/filter/project_reference_filter.rb
blob: 83cf45097edb1e15288ed1c492588803439cedb6 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true

module Banzai
  module Filter
    # HTML filter that replaces project references with links.
    class ProjectReferenceFilter < ReferenceFilter
      self.reference_type = :project

      # Public: Find `namespace/project>` project references in text
      #
      #   ProjectReferenceFilter.references_in(text) do |match, project|
      #     "<a href=...>#{project}></a>"
      #   end
      #
      # text - String text to search.
      #
      # Yields the String match, and the String project name.
      #
      # Returns a String replaced with the return of the block.
      def self.references_in(text)
        text.gsub(Project.markdown_reference_pattern) do |match|
          yield match, "#{$~[:namespace]}/#{$~[:project]}"
        end
      end

      def call
        ref_pattern = Project.markdown_reference_pattern
        ref_pattern_start = /\A#{ref_pattern}\z/

        nodes.each do |node|
          if text_node?(node)
            replace_text_when_pattern_matches(node, ref_pattern) do |content|
              project_link_filter(content)
            end
          elsif element_node?(node)
            yield_valid_link(node) do |link, inner_html|
              if link =~ ref_pattern_start
                replace_link_node_with_href(node, link) do
                  project_link_filter(link, link_content: inner_html)
                end
              end
            end
          end
        end

        doc
      end

      # Replace `namespace/project>` project references in text with links to the referenced
      # project page.
      #
      # text - String text to replace references in.
      # link_content - Original content of the link being replaced.
      #
      # Returns a String with `namespace/project>` references replaced with links. All links
      # have `gfm` and `gfm-project` class names attached for styling.
      def project_link_filter(text, link_content: nil)
        self.class.references_in(text) do |match, project_path|
          cached_call(:banzai_url_for_object, match, path: [Project, project_path.downcase]) do
            if project = projects_hash[project_path.downcase]
              link_to_project(project, link_content: link_content) || match
            else
              match
            end
          end
        end
      end

      # Returns a Hash containing all Project objects for the project
      # references in the current document.
      #
      # The keys of this Hash are the project paths, the values the
      # corresponding Project objects.
      def projects_hash
        @projects ||= Project.eager_load(:route, namespace: [:route])
                             .where_full_path_in(projects)
                             .index_by(&:full_path)
                             .transform_keys(&:downcase)
      end

      # Returns all projects referenced in the current document.
      def projects
        refs = Set.new

        nodes.each do |node|
          node.to_html.scan(Project.markdown_reference_pattern) do
            refs << "#{$~[:namespace]}/#{$~[:project]}"
          end
        end

        refs.to_a
      end

      private

      def urls
        Gitlab::Routing.url_helpers
      end

      def link_class
        reference_class(:project)
      end

      def link_to_project(project, link_content: nil)
        url = urls.project_url(project, only_path: context[:only_path])
        data = data_attribute(project: project.id)
        content = link_content || project.to_reference_with_postfix

        link_tag(url, data, content, project.name)
      end

      def link_tag(url, data, link_content, title)
        %(<a href="#{url}" #{data} class="#{link_class}" title="#{escape_once(title)}">#{link_content}</a>)
      end
    end
  end
end