2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-20 09:53:19 -04:00
|
|
|
module Gitlab
|
|
|
|
module HookData
|
|
|
|
class BaseBuilder
|
|
|
|
attr_accessor :object
|
|
|
|
|
2018-07-06 06:35:30 -04:00
|
|
|
MARKDOWN_SIMPLE_IMAGE = %r{
|
|
|
|
#{::Gitlab::Regex.markdown_code_or_html_blocks}
|
|
|
|
|
|
|
|
|
(?<image>
|
|
|
|
!
|
|
|
|
\[(?<title>[^\n]*?)\]
|
|
|
|
\((?<url>(?!(https?://|//))[^\n]+?)\)
|
|
|
|
)
|
|
|
|
}mx.freeze
|
|
|
|
|
2018-06-20 09:53:19 -04:00
|
|
|
def initialize(object)
|
|
|
|
@object = object
|
|
|
|
end
|
|
|
|
|
2018-07-17 09:58:14 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def absolute_image_urls(markdown_text)
|
2018-07-06 06:35:30 -04:00
|
|
|
return markdown_text unless markdown_text.present?
|
|
|
|
|
|
|
|
markdown_text.gsub(MARKDOWN_SIMPLE_IMAGE) do
|
|
|
|
if $~[:image]
|
2018-07-06 08:44:45 -04:00
|
|
|
url = $~[:url]
|
2018-10-15 06:42:15 -04:00
|
|
|
url = "#{uploads_prefix}#{url}" if url.start_with?('/uploads')
|
2018-07-06 08:44:45 -04:00
|
|
|
url = "/#{url}" unless url.start_with?('/')
|
|
|
|
|
|
|
|
"![#{$~[:title]}](#{Gitlab.config.gitlab.url}#{url})"
|
2018-07-06 06:35:30 -04:00
|
|
|
else
|
|
|
|
$~[0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-10-15 06:42:15 -04:00
|
|
|
|
|
|
|
def uploads_prefix
|
|
|
|
project&.full_path || ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
return unless object.respond_to?(:project)
|
|
|
|
|
|
|
|
object.project
|
|
|
|
end
|
2018-06-20 09:53:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|