Welcome to mirror list, hosted at ThFree Co, Russian Federation.

base_builder.rb « hook_data « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d54175bce811c166d65140c10233ce6cc9a338de (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
# frozen_string_literal: true

module Gitlab
  module HookData
    class BaseBuilder
      attr_accessor :object

      MARKDOWN_SIMPLE_IMAGE = %r{
          #{::Gitlab::Regex.markdown_code_or_html_blocks}
        |
          (?<image>
            !
            \[(?<title>[^\n]*?)\]
            \((?<url>(?!(https?://|//))[^\n]+?)\)
          )
      }mx.freeze

      def initialize(object)
        @object = object
      end

      private

      def absolute_image_urls(markdown_text)
        return markdown_text unless markdown_text.present?

        markdown_text.gsub(MARKDOWN_SIMPLE_IMAGE) do
          if $~[:image]
            url = $~[:url]
            url = "#{uploads_prefix}#{url}" if url.start_with?('/uploads')
            url = "/#{url}" unless url.start_with?('/')

            "![#{$~[:title]}](#{Gitlab.config.gitlab.url}#{url})"
          else
            $~[0]
          end
        end
      end

      def uploads_prefix
        project&.full_path || ''
      end

      def project
        return unless object.respond_to?(:project)

        object.project
      end
    end
  end
end