Read about our upcoming Code of Conduct on this issue

This instance was upgraded to Heptapod 0.28.0 today

commit_reference_filter.rb 2.33 KB
Newer Older
Robert Speicher's avatar
Robert Speicher committed
1
2
module Gitlab
  module Markdown
3
    # HTML filter that replaces commit references with links.
Robert Speicher's avatar
Robert Speicher committed
4
5
    #
    # This filter supports cross-project references.
6
    class CommitReferenceFilter < ReferenceFilter
Robert Speicher's avatar
Robert Speicher committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
      include CrossProjectReference

      # Public: Find commit references in text
      #
      #   CommitReferenceFilter.references_in(text) do |match, commit, project_ref|
      #     "<a href=...>#{commit}</a>"
      #   end
      #
      # text - String text to search.
      #
      # Yields the String match, the String commit identifier, and an optional
      # String of the external project reference.
      #
      # Returns a String replaced with the return of the block.
      def self.references_in(text)
22
        text.gsub(Commit.reference_pattern) do |match|
Robert Speicher's avatar
Robert Speicher committed
23
24
25
26
27
          yield match, $~[:commit], $~[:project]
        end
      end

      def call
28
        replace_text_nodes_matching(Commit.reference_pattern) do |content|
29
          commit_link_filter(content)
Robert Speicher's avatar
Robert Speicher committed
30
31
32
33
34
35
36
37
38
39
40
41
42
        end
      end

      # Replace commit references in text with links to the commit specified.
      #
      # text - String text to replace references in.
      #
      # Returns a String with commit references replaced with links. All links
      # have `gfm` and `gfm-commit` class names attached for styling.
      def commit_link_filter(text)
        self.class.references_in(text) do |match, commit_ref, project_ref|
          project = self.project_from_ref(project_ref)

43
          if commit = commit_from_ref(project, commit_ref)
44
45
            push_result(:commit, commit)

Robert Speicher's avatar
Robert Speicher committed
46
47
            url = url_for_commit(project, commit)

48
            title = escape_once(commit.link_title)
49
            klass = reference_class(:commit)
Robert Speicher's avatar
Robert Speicher committed
50
51
52
53
54

            project_ref += '@' if project_ref

            %(<a href="#{url}"
                 title="#{title}"
55
                 class="#{klass}">#{project_ref}#{commit.short_id}</a>)
Robert Speicher's avatar
Robert Speicher committed
56
57
58
59
60
61
          else
            match
          end
        end
      end

62
63
      def commit_from_ref(project, commit_ref)
        if project && project.valid_repo?
64
          project.commit(commit_ref)
65
66
67
        end
      end

Robert Speicher's avatar
Robert Speicher committed
68
69
70
71
72
73
74
75
      def url_for_commit(project, commit)
        h = Rails.application.routes.url_helpers
        h.namespace_project_commit_url(project.namespace, project, commit,
                                        only_path: context[:only_path])
      end
    end
  end
end