notion_to_md 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: df84120caddccce07f99e140867a59ca176ee9c639a019785ef74faea03de32b
4
+ data.tar.gz: 5d95ff8014362018d505944b3b1afaf6593fac9f3f90cab28f6abd645843b39f
5
+ SHA512:
6
+ metadata.gz: 24a41732b091218c1c3b9b428714419a9ce993669d5edde0baa011cfb6953cb4db433aa8dc4536670577062b7a304b4d686361b756580800afcb72ebe2ab95b1
7
+ data.tar.gz: c2a9e707c602366f8f9e5d811c7010a65cdcf7c0a6fff17353b3a04a5cc28db7be452e453c5ee218b24a007c1c5d753aefb44c443c77e16cec1028097fcc71bf
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # notion_to_md
2
+ Notion Markdown Exporter in Ruby
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'notion'
4
+ require_relative './logger'
5
+ require_relative './text_annotation'
6
+
7
+ module NotionToMd
8
+ class Converter
9
+ attr_reader :page_id
10
+
11
+ def initialize(page_id:, token: nil)
12
+ @notion = Notion::Client.new(token: token || ENV['NOTION_TOKEN'])
13
+ @page_id = page_id
14
+ end
15
+
16
+ def convert
17
+ md = page_blocks[:results].map do |block|
18
+ next blank if block[:type] == 'paragraph' && block.dig(:paragraph, :text).empty?
19
+
20
+ block_type = block[:type].to_sym
21
+ send(block_type, block[block_type])
22
+ end
23
+ Logger.info("Notion page #{page_id} converted to markdown")
24
+ md.join("\n\n")
25
+ end
26
+
27
+ private
28
+
29
+ def page_blocks
30
+ @page_blocks ||= @notion.block_children(id: page_id)
31
+ end
32
+
33
+
34
+ def paragraph(block)
35
+ convert_text(block)
36
+ end
37
+
38
+ def heading_1(block)
39
+ "# #{convert_text(block)}"
40
+ end
41
+
42
+ def heading_2(block)
43
+ "## #{convert_text(block)}"
44
+ end
45
+
46
+ def heading_3(block)
47
+ "### #{convert_text(block)}"
48
+ end
49
+
50
+ def callout(block)
51
+ icon = get_icon(block[:icon])
52
+ text = convert_text(block)
53
+ "#{icon} #{text}"
54
+ end
55
+
56
+ def quote(block)
57
+ "> #{convert_text(block)}"
58
+ end
59
+
60
+ def bulleted_list_item(block)
61
+ "- #{convert_text(block)}"
62
+ end
63
+
64
+ # TODO: numbered_list_item
65
+ def numbered_list_item(block)
66
+ Logger.info('numbered_list_item type not supported. Shown as bulleted_list_item.')
67
+ bulleted_list_item(block)
68
+ end
69
+
70
+ def to_do(block)
71
+ checked = block[:checked]
72
+ text = convert_text(block)
73
+
74
+ "- #{checked ? '[x]' : '[ ]'} #{text}"
75
+ end
76
+
77
+ def code(block)
78
+ language = block[:language]
79
+ text = convert_text(block)
80
+
81
+ "```#{language}\n\t#{text}\n```"
82
+ end
83
+
84
+ def embed(block)
85
+ url = block[:url]
86
+
87
+ "[#{url}](#{url})"
88
+ end
89
+
90
+ def image(block)
91
+ type = block[:type].to_sym
92
+ url = block.dig(type, :url)
93
+ caption = convert_caption(block)
94
+
95
+ "![](#{url})\n\n#{caption}"
96
+ end
97
+
98
+ def bookmark(block)
99
+ url = block[:url]
100
+ "[#{url}](#{url})"
101
+ end
102
+
103
+ def divider(_block)
104
+ '---'
105
+ end
106
+
107
+ def equation(block)
108
+ equ = convert_text(block)
109
+ "$$ #{equ} $$"
110
+ end
111
+
112
+ def blank
113
+ '<br />'
114
+ end
115
+
116
+ def convert_text(block)
117
+ block[:text].map do |text|
118
+ content = text[:plain_text]
119
+ enrich_text_content(text, content)
120
+ end.join
121
+ end
122
+
123
+ def convert_caption(block)
124
+ convert_text(text: block[:caption])
125
+ end
126
+
127
+ def get_icon(block)
128
+ type = block[:type].to_sym
129
+ block[type]
130
+ end
131
+
132
+ def enrich_text_content(text, content)
133
+ enriched_content = add_link(text, content)
134
+ add_annotations(text, enriched_content)
135
+ end
136
+
137
+ def add_link(text, content)
138
+ href = text[:href]
139
+ return content if href.nil?
140
+
141
+ "[#{content}](#{href})"
142
+ end
143
+
144
+ def add_annotations(text, content)
145
+ annotations = text[:annotations].select { |key, value| !!value }
146
+ annotations.keys.inject(content) do |enriched_content, annotation|
147
+ TextAnnotation.send(annotation.to_sym, enriched_content)
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module NotionToMd
6
+ class Logger
7
+ @logger = ::Logger.new(STDOUT)
8
+
9
+ class << self
10
+ extend Forwardable
11
+ def_delegators :@logger, :debug, :info, :warn, :error, :fatal
12
+ end
13
+ end
14
+ end
15
+
16
+
@@ -0,0 +1,36 @@
1
+ module NotionToMd
2
+ # Append the text type:
3
+ # * italic: boolean,
4
+ # * bold: boolean,
5
+ # * striketrough: boolean,
6
+ # * underline: boolean,
7
+ # * code: boolean,
8
+ # * color: string NOT_SUPPORTED
9
+ class TextAnnotation
10
+ class << self
11
+ def italic(text)
12
+ "_#{text}_"
13
+ end
14
+
15
+ def bold(text)
16
+ "**#{text}**"
17
+ end
18
+
19
+ def striketrough(text)
20
+ "~~#{text}~~"
21
+ end
22
+
23
+ def underline(text)
24
+ text
25
+ end
26
+
27
+ def code(text)
28
+ "`#{text}`"
29
+ end
30
+
31
+ def color(text)
32
+ text
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NotionToMd
4
+ VERSION = '0.0.0'
5
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './notion_to_md/version'
4
+ require_relative './notion_to_md/converter'
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: notion_to_md
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Enrique Arias
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-12-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: notion-ruby-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.8
27
+ description: Notion Markdown Exporter in Ruby
28
+ email: emoriarty81@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - README.md
34
+ - lib/notion_to_md.rb
35
+ - lib/notion_to_md/converter.rb
36
+ - lib/notion_to_md/logger.rb
37
+ - lib/notion_to_md/text_annotation.rb
38
+ - lib/notion_to_md/version.rb
39
+ homepage: https://github.com/emoriarty/notion_to_md
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 2.7.3
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.1.6
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Notion Markdown Exporter
62
+ test_files: []