summaryrefslogtreecommitdiff
path: root/lib/gitlab/gfm/reference_rewriter.rb
blob: 08d7db49ad778f936d74574c756536a251bf223f (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
# frozen_string_literal: true

module Gitlab
  module Gfm
    ##
    # Class that unfolds local references in text.
    #
    # The initializer takes text in Markdown and project this text is valid
    # in context of.
    #
    # `unfold` method tries to find all local references and unfold each of
    # those local references to cross reference format, assuming that the
    # argument passed to this method is a project that references will be
    # viewed from (see `Referable#to_reference method).
    #
    # Examples:
    #
    # 'Hello, this issue is related to #123 and
    #  other issues labeled with ~"label"', will be converted to:
    #
    # 'Hello, this issue is related to gitlab-org/gitlab-ce#123 and
    #  other issue labeled with gitlab-org/gitlab-ce~"label"'.
    #
    # It does respect markdown lexical rules, so text in code block will not be
    # replaced, see another example:
    #
    # 'Merge request for issue #1234, see also link:
    #  http://gitlab.com/some/link/#1234, and code `puts #1234`' =>
    #
    # 'Merge request for issue gitlab-org/gitlab-ce#1234, se also link:
    #  http://gitlab.com/some/link/#1234, and code `puts #1234`'
    #
    class ReferenceRewriter
      RewriteError = Class.new(StandardError)

      def initialize(text, source_parent, current_user)
        @text = text
        @source_parent = source_parent
        @current_user = current_user
        @original_html = markdown(text)
        @pattern = Gitlab::ReferenceExtractor.references_pattern
      end

      def rewrite(target_parent)
        return @text unless needs_rewrite?

        @text.gsub(@pattern) do |reference|
          unfold_reference(reference, Regexp.last_match, target_parent)
        end
      end

      def needs_rewrite?
        @text =~ @pattern
      end

      private

      def unfold_reference(reference, match, target_parent)
        before = @text[0...match.begin(0)]
        after = @text[match.end(0)..-1]

        referable = find_referable(reference)
        return reference unless referable

        cross_reference = build_cross_reference(referable, target_parent)
        return reference if reference == cross_reference

        if cross_reference.nil?
          raise RewriteError, "Unspecified reference detected for #{referable.class.name}"
        end

        new_text = before + cross_reference + after
        substitution_valid?(new_text) ? cross_reference : reference
      end

      def find_referable(reference)
        extractor = Gitlab::ReferenceExtractor.new(@source_parent,
                                                   @current_user)
        extractor.analyze(reference)
        extractor.all.first
      end

      def build_cross_reference(referable, target_parent)
        if referable.respond_to?(:project)
          referable.to_reference(target_parent)
        else
          referable.to_reference(@source_parent, target_project: target_parent)
        end
      end

      def substitution_valid?(substituted)
        @original_html == markdown(substituted)
      end

      def markdown(text)
        Banzai.render(text, project: @source_parent, no_original_data: true)
      end
    end
  end
end