Class: MarkdownContentRewriterService

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
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, object, field, source_parent, target_parent) ⇒ MarkdownContentRewriterService

Returns a new instance of MarkdownContentRewriterService.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/markdown_content_rewriter_service.rb', line 11

def initialize(current_user, object, field, source_parent, target_parent)
  @current_user = current_user
  @source_parent = source_parent
  @target_parent = target_parent
  @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
end

Instance Method Details

#executeObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/services/markdown_content_rewriter_service.rb', line 35

def execute
  return result unless content

  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

#safe_to_copy_markdown?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'app/services/markdown_content_rewriter_service.rb', line 51

def safe_to_copy_markdown?
  strong_memoize(:safe_to_copy_markdown) do
    rewriters.none?(&:needs_rewrite?)
  end
end