Class: LowLoad::MarkdownAdapter

Inherits:
Adapter
  • Object
show all
Defined in:
lib/adapters/markdown_adapter.rb

Constant Summary collapse

EXTENSIONS =
['md', 'rd', 'markdown', 'raindown']

Instance Method Summary collapse

Methods inherited from Adapter

#evaluate, #preload

Instance Method Details

#metadata(file_path:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/adapters/markdown_adapter.rb', line 11

def (file_path:)
  line_numbers = []
  yaml_lines = []

  File.foreach(file_path).with_index do |line, index|
    if line.strip == '---'
      line_numbers << index + 1
      break if line_numbers.count > 1
    elsif line_numbers.count > 0
      yaml_lines << line
    end
  end

  # TODO: Test that this returns an empty hash when no yaml lines found.
  YAML.safe_load(yaml_lines.join, symbolize_names: true)
end

#url_path(file_path:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/adapters/markdown_adapter.rb', line 28

def url_path(file_path:)
  # Remove "_number-" and segments beginning with "_".
  url_path = file_path.split('/').map do |segment|
    next segment.sub(/^_\d\W/, '') if segment.sub(/^_\d\W/, '') != segment
    next nil if segment.sub(/^_/, '') != segment

    segment
  end.compact.join('/')
  
  EXTENSIONS.each do |extension|
    url_path.delete_suffix!(".#{extension}")
  end

  url_path
end