Class: MarkdownContentRewriterService

Inherits:
Object
  • Object
show all
Defined in:
app/services/markdown_content_rewriter_service.rb

Overview

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`.

Constant Summary collapse

REWRITERS =
[Gitlab::Gfm::ReferenceRewriter, Gitlab::Gfm::UploadsRewriter].freeze

Instance Method Summary collapse

Constructor Details

#initialize(current_user, content, source_parent, target_parent) ⇒ MarkdownContentRewriterService

Returns a new instance of MarkdownContentRewriterService.

Raises:

  • (ArgumentError)

9
10
11
12
13
14
15
16
17
18
# File 'app/services/markdown_content_rewriter_service.rb', line 9

def initialize(current_user, content, source_parent, target_parent)
  # 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)

  @current_user = current_user
  @content = content.presence
  @source_parent = source_parent
  @target_parent = target_parent
end

Instance Method Details

#executeObject


20
21
22
23
24
25
26
27
# File 'app/services/markdown_content_rewriter_service.rb', line 20

def execute
  return unless content

  REWRITERS.inject(content) do |text, klass|
    rewriter = klass.new(text, source_parent, current_user)
    rewriter.rewrite(target_parent)
  end
end