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



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

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


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

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

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

.blankObject



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

def blank
  '<br />'
end

.bookmark(block) ⇒ Object



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

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



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

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

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

.convert_caption(block) ⇒ Object



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

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

.convert_text(block) ⇒ Object



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

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

.divider(_block) ⇒ Object



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

def divider(_block)
  '---'
end

.embed(block) ⇒ Object



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

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

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

.enrich_text_content(text, content) ⇒ Object



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

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

.get_icon(block) ⇒ Object



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

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



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

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

TODO: numbered_list_item



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

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



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

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

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