Class: CollavreGithub::MarkdownSync::ContentProcessor

Inherits:
Object
  • Object
show all
Defined in:
app/services/collavre_github/markdown_sync/content_processor.rb

Constant Summary collapse

IMAGE_EXTENSIONS =
%w[.png .jpg .jpeg .gif .svg .webp .ico .bmp .tiff .avif].freeze

Instance Method Summary collapse

Constructor Details

#initialize(client:, repo:, branch:, path_to_creative_map:) ⇒ ContentProcessor

Returns a new instance of ContentProcessor.



6
7
8
9
10
11
12
# File 'app/services/collavre_github/markdown_sync/content_processor.rb', line 6

def initialize(client:, repo:, branch:, path_to_creative_map:)
  @client = client
  @repo = repo
  @branch = branch
  @path_to_creative_map = path_to_creative_map
  @image_cache = {}
end

Instance Method Details

#process(markdown_content, file_path) ⇒ Object

Returns [processed_content, array_of_blobs]



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/collavre_github/markdown_sync/content_processor.rb', line 15

def process(markdown_content, file_path)
  content = markdown_content.dup
  blobs = []
  dir = File.dirname(file_path)

  content = content.gsub(/(!?)\[([^\]]*)\]\(([^)]+)\)/) do |match|
    prefix = Regexp.last_match(1)
    label = Regexp.last_match(2)
    url = Regexp.last_match(3)

    next match if absolute_url?(url) || anchor_only?(url)

    if prefix == "!"
      process_image(match, label, url, dir, blobs)
    else
      process_link(match, label, url, dir)
    end
  end

  [ content, blobs ]
end