Class: Marlens::GithubToJiraComment::GithubMarkdownResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/marlens/github_to_jira_comment/github_markdown_resolver.rb

Defined Under Namespace

Classes: Image, Result

Constant Summary collapse

MARKDOWN_IMAGE =
/!\[[^\]]*\]\(([^)\s]+)(\s+"[^"]*")?\)/
HTML_IMAGE =
/<img\b(?<attributes>[^>]+)>/i

Instance Method Summary collapse

Constructor Details

#initialize(github_client: nil, renderer: nil) ⇒ GithubMarkdownResolver

Returns a new instance of GithubMarkdownResolver.



15
16
17
18
# File 'lib/marlens/github_to_jira_comment/github_markdown_resolver.rb', line 15

def initialize(github_client: nil, renderer: nil)
  @github_client = github_client
  @renderer = renderer
end

Instance Method Details

#resolve(markdown, context: nil) ⇒ Object



20
21
22
# File 'lib/marlens/github_to_jira_comment/github_markdown_resolver.rb', line 20

def resolve(markdown, context: nil)
  (markdown, context:).markdown
end

#resolve_with_metadata(markdown, context: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/marlens/github_to_jira_comment/github_markdown_resolver.rb', line 24

def (markdown, context: nil)
  images = image_occurrences(markdown)
  attachment_images = images.select { |image| github_user_attachment?(image.url) }
  return Result.new(markdown:, allowed_image_hosts: []) if attachment_images.empty?

  rendered_images = rendered_image_urls(render_markdown(markdown, context:)).select { |url| rendered_attachment?(url) }
  mappings = {}
  attachment_images.each do |image|
    rendered = mappings[image.url] || rendered_images.find { |url| url.include?(github_attachment_id(image.url)) }
    raise "GitHub Markdown render did not return an image for #{image.url}" unless rendered

    mappings[image.url] ||= rendered
  end

  resolved = rewrite_image_urls(markdown, mappings)
  Result.new(markdown: resolved, allowed_image_hosts: mappings.values.filter_map { |url| host(url) }.uniq)
end