diff options
Diffstat (limited to 'app/services/markdown_content_rewriter_service.rb')
-rw-r--r-- | app/services/markdown_content_rewriter_service.rb | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/app/services/markdown_content_rewriter_service.rb b/app/services/markdown_content_rewriter_service.rb new file mode 100644 index 00000000000..f945990a1b4 --- /dev/null +++ b/app/services/markdown_content_rewriter_service.rb @@ -0,0 +1,28 @@ +# 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 + REWRITERS = [Gitlab::Gfm::ReferenceRewriter, Gitlab::Gfm::UploadsRewriter].freeze + + def initialize(current_user, content, source_parent, target_parent) + @current_user = current_user + @content = content.presence + @source_parent = source_parent + @target_parent = target_parent + end + + 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 + + private + + attr_reader :current_user, :content, :source_parent, :target_parent +end |