Module: Bookflow::AppleBooks::RenderMarkdown

Defined in:
lib/bookflow/apple_books/render_markdown.rb

Class Method Summary collapse

Class Method Details

.format_location(annotation) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/bookflow/apple_books/render_markdown.rb', line 39

def format_location(annotation)
  parts = []
  parts << "absolute=#{annotation.absolute_location.inspect}" unless annotation.absolute_location.nil?
  parts << "start=#{annotation.range_start.inspect}" unless annotation.range_start.nil?
  parts << "end=#{annotation.range_end.inspect}" unless annotation.range_end.nil?
  return nil if parts.empty?

  parts.join(", ")
end

.render(book_export, exported_at:) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bookflow/apple_books/render_markdown.rb', line 6

def render(book_export, exported_at:)
  lines = []
  lines << "# #{book_export.book.title}"
  lines << ""
  lines << "- Author: #{book_export.book.author}"
  lines << "- Asset ID: #{book_export.book.asset_id}"
  lines << "- Source Path: #{book_export.book.source_path}"
  lines << "- Exported At: #{Utils.format_timestamp(exported_at)}"
  lines << "- Annotation Count: #{book_export.annotations.length}"
  lines << ""
  lines << "## Highlights"

  book_export.annotations.each_with_index do |annotation, index|
    lines << ""
    lines << "### #{index + 1}"
    lines << ""
    lines << "> #{annotation.highlight_text.to_s.rstrip}"
    lines << ""
    lines << "- Created At: #{Utils.format_timestamp(annotation.created_at)}" if annotation.created_at
    location = format_location(annotation)
    lines << "- Location: #{location}" unless location.nil?

    note = annotation.note_text.to_s.rstrip
    next if note.empty?

    lines << ""
    lines << "Note:"
    lines << note
  end

  "#{lines.join("\n")}\n"
end