Class: NotionToMd::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/notion_to_md/block.rb

Class Method Summary collapse

Class Method Details

.add_annotations(text, content) ⇒ Object



110
111
112
113
114
115
# File 'lib/notion_to_md/block.rb', line 110

def add_annotations(text, content)
  annotations = text[:annotations].select { |_key, value| !!value }
  annotations.keys.inject(content) do |enriched_content, annotation|
    TextAnnotation.send(annotation.to_sym, enriched_content)
  end
end


103
104
105
106
107
108
# File 'lib/notion_to_md/block.rb', line 103

def add_link(text, content)
  href = text[:href]
  return content if href.nil?

  "[#{content}](#{href})"
end

.blankObject



78
79
80
# File 'lib/notion_to_md/block.rb', line 78

def blank
  '<br />'
end

.bookmark(block) ⇒ Object



69
70
71
72
# File 'lib/notion_to_md/block.rb', line 69

def bookmark(block)
  url = block[:url]
  "[#{url}](#{url})"
end

.bulleted_list_item(block) ⇒ Object



32
33
34
# File 'lib/notion_to_md/block.rb', line 32

def bulleted_list_item(block)
  "- #{convert_text(block)}"
end

.callout(block) ⇒ Object



22
23
24
25
26
# File 'lib/notion_to_md/block.rb', line 22

def callout(block)
  icon = get_icon(block[:icon])
  text = convert_text(block)
  "#{icon} #{text}"
end

.code(block) ⇒ Object



48
49
50
51
52
53
# File 'lib/notion_to_md/block.rb', line 48

def code(block)
  language = block[:language]
  text = convert_text(block)

  "```#{language}\n#{text}\n```"
end

.convert_caption(block) ⇒ Object



89
90
91
# File 'lib/notion_to_md/block.rb', line 89

def convert_caption(block)
  convert_text(rich_text: block[:caption])
end

.convert_text(block) ⇒ Object



82
83
84
85
86
87
# File 'lib/notion_to_md/block.rb', line 82

def convert_text(block)
  block[:rich_text].map do |text|
    content = Text.send(text[:type], text)
    enrich_text_content(text, content)
  end.join
end

.divider(_block) ⇒ Object



74
75
76
# File 'lib/notion_to_md/block.rb', line 74

def divider(_block)
  '---'
end

.embed(block) ⇒ Object



55
56
57
58
59
# File 'lib/notion_to_md/block.rb', line 55

def embed(block)
  url = block[:url]

  "[#{url}](#{url})"
end

.enrich_text_content(text, content) ⇒ Object



98
99
100
101
# File 'lib/notion_to_md/block.rb', line 98

def enrich_text_content(text, content)
  enriched_content = add_link(text, content)
  add_annotations(text, enriched_content)
end

.get_icon(block) ⇒ Object



93
94
95
96
# File 'lib/notion_to_md/block.rb', line 93

def get_icon(block)
  type = block[:type].to_sym
  block[type]
end

.heading_1(block) ⇒ Object



10
11
12
# File 'lib/notion_to_md/block.rb', line 10

def heading_1(block)
  "# #{convert_text(block)}"
end

.heading_2(block) ⇒ Object



14
15
16
# File 'lib/notion_to_md/block.rb', line 14

def heading_2(block)
  "## #{convert_text(block)}"
end

.heading_3(block) ⇒ Object



18
19
20
# File 'lib/notion_to_md/block.rb', line 18

def heading_3(block)
  "### #{convert_text(block)}"
end

.image(block) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/notion_to_md/block.rb', line 61

def image(block)
  type = block[:type].to_sym
  url = block.dig(type, :url)
  caption = convert_caption(block)

  "![](#{url})\n\n#{caption}"
end

.numbered_list_item(block) ⇒ Object



36
37
38
39
# File 'lib/notion_to_md/block.rb', line 36

def numbered_list_item(block)
  Logger.info('numbered_list_item type not supported. Shown as bulleted_list_item.')
  bulleted_list_item(block)
end

.paragraph(block) ⇒ Object



6
7
8
# File 'lib/notion_to_md/block.rb', line 6

def paragraph(block)
  convert_text(block)
end

.quote(block) ⇒ Object



28
29
30
# File 'lib/notion_to_md/block.rb', line 28

def quote(block)
  "> #{convert_text(block)}"
end

.to_do(block) ⇒ Object



41
42
43
44
45
46
# File 'lib/notion_to_md/block.rb', line 41

def to_do(block)
  checked = block[:checked]
  text = convert_text(block)

  "- #{checked ? '[x]' : '[ ]'} #{text}"
end