2020-08-11 11:10:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# This service passes Markdown content through our GFM rewriter classes
|
|
|
|
# which rewrite references to GitLab objects and uploads within the content
|
|
|
|
# based on their visibility by the `target_parent`.
|
|
|
|
class MarkdownContentRewriterService
|
2022-06-02 17:09:25 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2020-08-11 11:10:08 -04:00
|
|
|
|
2022-06-02 17:09:25 -04:00
|
|
|
REWRITERS = [Gitlab::Gfm::ReferenceRewriter, Gitlab::Gfm::UploadsRewriter].freeze
|
2020-08-19 23:10:04 -04:00
|
|
|
|
2022-06-02 17:09:25 -04:00
|
|
|
def initialize(current_user, object, field, source_parent, target_parent)
|
2020-08-11 11:10:08 -04:00
|
|
|
@current_user = current_user
|
|
|
|
@source_parent = source_parent
|
|
|
|
@target_parent = target_parent
|
2022-06-02 17:09:25 -04:00
|
|
|
@object = object
|
|
|
|
@field = field
|
|
|
|
|
|
|
|
validate_parameters!
|
|
|
|
|
|
|
|
@content = object[field].dup.presence
|
|
|
|
@html_field = object.cached_markdown_fields.html_field(field)
|
|
|
|
@content_html = object.cached_html_for(field)
|
|
|
|
|
|
|
|
@rewriters =
|
|
|
|
REWRITERS.map do |rewriter_class|
|
|
|
|
rewriter_class.new(@content, content_html, source_parent, current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
@result = {
|
|
|
|
field => nil,
|
|
|
|
html_field => nil
|
|
|
|
}.with_indifferent_access
|
2020-08-11 11:10:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2022-06-02 17:09:25 -04:00
|
|
|
return result unless content
|
2020-08-11 11:10:08 -04:00
|
|
|
|
2022-06-02 17:09:25 -04:00
|
|
|
unless safe_to_copy_markdown?
|
|
|
|
rewriters.each do |rewriter|
|
|
|
|
rewriter.rewrite(target_parent)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result[field] = content
|
|
|
|
result[html_field] = content_html if safe_to_copy_markdown?
|
|
|
|
result[:skip_markdown_cache_validation] = safe_to_copy_markdown?
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def safe_to_copy_markdown?
|
|
|
|
strong_memoize(:safe_to_copy_markdown) do
|
|
|
|
rewriters.none?(&:needs_rewrite?)
|
2020-08-11 11:10:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-06-02 17:09:25 -04:00
|
|
|
def validate_parameters!
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/39654#note_399095117
|
|
|
|
raise ArgumentError, 'The rewriter classes require that `source_parent` is a `Project`' \
|
|
|
|
unless source_parent.is_a?(Project)
|
|
|
|
|
|
|
|
if object.cached_markdown_fields[field].nil?
|
|
|
|
raise ArgumentError, 'The `field` attribute does not contain cached markdown'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :current_user, :content, :source_parent,
|
|
|
|
:target_parent, :rewriters, :content_html,
|
|
|
|
:field, :html_field, :object, :result
|
2020-08-11 11:10:08 -04:00
|
|
|
end
|