notion_to_md 0.0.3 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/notion_to_md/block.rb +119 -0
- data/lib/notion_to_md/converter.rb +2 -123
- data/lib/notion_to_md/logger.rb +0 -2
- data/lib/notion_to_md/text.rb +13 -0
- data/lib/notion_to_md/text_annotation.rb +2 -2
- data/lib/notion_to_md/version.rb +1 -1
- data/lib/notion_to_md.rb +6 -0
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77ebe548eec487347104eabbac6911836cef0ec6e5ea350cb9a376ef8e07ce50
|
4
|
+
data.tar.gz: b2fd0700b53683daa6e59a729418139bbdee41da8184dad57eff0f2c17578dc7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7f09feec62553d76c72e670aa4fa05d02e98f19e74352237c8c42a255960570f77ae530b3196be24a054fa8a55f744f0b0b96dfbfb1c5e7d06f71b1423bcdf6
|
7
|
+
data.tar.gz: '09a041dab71e1fea3796ca9193c6a5fdf385de1d7fc212373f2e7cec086568935c72171ba900c90001907fdd4e1667e819b69c28740c2e5fa18a4b17821facb3'
|
@@ -0,0 +1,119 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NotionToMd
|
4
|
+
class Block
|
5
|
+
class << self
|
6
|
+
def paragraph(block)
|
7
|
+
convert_text(block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def heading_1(block)
|
11
|
+
"# #{convert_text(block)}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def heading_2(block)
|
15
|
+
"## #{convert_text(block)}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def heading_3(block)
|
19
|
+
"### #{convert_text(block)}"
|
20
|
+
end
|
21
|
+
|
22
|
+
def callout(block)
|
23
|
+
icon = get_icon(block[:icon])
|
24
|
+
text = convert_text(block)
|
25
|
+
"#{icon} #{text}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def quote(block)
|
29
|
+
"> #{convert_text(block)}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def bulleted_list_item(block)
|
33
|
+
"- #{convert_text(block)}"
|
34
|
+
end
|
35
|
+
|
36
|
+
# TODO: numbered_list_item
|
37
|
+
def numbered_list_item(block)
|
38
|
+
Logger.info('numbered_list_item type not supported. Shown as bulleted_list_item.')
|
39
|
+
bulleted_list_item(block)
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_do(block)
|
43
|
+
checked = block[:checked]
|
44
|
+
text = convert_text(block)
|
45
|
+
|
46
|
+
"- #{checked ? '[x]' : '[ ]'} #{text}"
|
47
|
+
end
|
48
|
+
|
49
|
+
def code(block)
|
50
|
+
language = block[:language]
|
51
|
+
text = convert_text(block)
|
52
|
+
|
53
|
+
"```#{language}\n\t#{text}\n```"
|
54
|
+
end
|
55
|
+
|
56
|
+
def embed(block)
|
57
|
+
url = block[:url]
|
58
|
+
|
59
|
+
"[#{url}](#{url})"
|
60
|
+
end
|
61
|
+
|
62
|
+
def image(block)
|
63
|
+
type = block[:type].to_sym
|
64
|
+
url = block.dig(type, :url)
|
65
|
+
caption = convert_caption(block)
|
66
|
+
|
67
|
+
"\n\n#{caption}"
|
68
|
+
end
|
69
|
+
|
70
|
+
def bookmark(block)
|
71
|
+
url = block[:url]
|
72
|
+
"[#{url}](#{url})"
|
73
|
+
end
|
74
|
+
|
75
|
+
def divider(_block)
|
76
|
+
'---'
|
77
|
+
end
|
78
|
+
|
79
|
+
def blank
|
80
|
+
'<br />'
|
81
|
+
end
|
82
|
+
|
83
|
+
def convert_text(block)
|
84
|
+
block[:text].map do |text|
|
85
|
+
content = Text.send(text[:type], text)
|
86
|
+
enrich_text_content(text, content)
|
87
|
+
end.join
|
88
|
+
end
|
89
|
+
|
90
|
+
def convert_caption(block)
|
91
|
+
convert_text(text: block[:caption])
|
92
|
+
end
|
93
|
+
|
94
|
+
def get_icon(block)
|
95
|
+
type = block[:type].to_sym
|
96
|
+
block[type]
|
97
|
+
end
|
98
|
+
|
99
|
+
def enrich_text_content(text, content)
|
100
|
+
enriched_content = add_link(text, content)
|
101
|
+
add_annotations(text, enriched_content)
|
102
|
+
end
|
103
|
+
|
104
|
+
def add_link(text, content)
|
105
|
+
href = text[:href]
|
106
|
+
return content if href.nil?
|
107
|
+
|
108
|
+
"[#{content}](#{href})"
|
109
|
+
end
|
110
|
+
|
111
|
+
def add_annotations(text, content)
|
112
|
+
annotations = text[:annotations].select { |_key, value| !!value }
|
113
|
+
annotations.keys.inject(content) do |enriched_content, annotation|
|
114
|
+
TextAnnotation.send(annotation.to_sym, enriched_content)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -1,9 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'notion'
|
4
|
-
require_relative './logger'
|
5
|
-
require_relative './text_annotation'
|
6
|
-
|
7
3
|
module NotionToMd
|
8
4
|
class Converter
|
9
5
|
attr_reader :page_id
|
@@ -15,12 +11,12 @@ module NotionToMd
|
|
15
11
|
|
16
12
|
def convert
|
17
13
|
md = page_blocks[:results].map do |block|
|
18
|
-
next blank if block[:type] == 'paragraph' && block.dig(:paragraph, :text).empty?
|
14
|
+
next Block.blank if block[:type] == 'paragraph' && block.dig(:paragraph, :text).empty?
|
19
15
|
|
20
16
|
block_type = block[:type].to_sym
|
21
17
|
|
22
18
|
begin
|
23
|
-
send(block_type, block[block_type])
|
19
|
+
Block.send(block_type, block[block_type])
|
24
20
|
rescue StandardError
|
25
21
|
Logger.info("Unsupported block type: #{block_type}")
|
26
22
|
next nil
|
@@ -35,122 +31,5 @@ module NotionToMd
|
|
35
31
|
def page_blocks
|
36
32
|
@page_blocks ||= @notion.block_children(id: page_id)
|
37
33
|
end
|
38
|
-
|
39
|
-
def paragraph(block)
|
40
|
-
convert_text(block)
|
41
|
-
end
|
42
|
-
|
43
|
-
def heading_1(block)
|
44
|
-
"# #{convert_text(block)}"
|
45
|
-
end
|
46
|
-
|
47
|
-
def heading_2(block)
|
48
|
-
"## #{convert_text(block)}"
|
49
|
-
end
|
50
|
-
|
51
|
-
def heading_3(block)
|
52
|
-
"### #{convert_text(block)}"
|
53
|
-
end
|
54
|
-
|
55
|
-
def callout(block)
|
56
|
-
icon = get_icon(block[:icon])
|
57
|
-
text = convert_text(block)
|
58
|
-
"#{icon} #{text}"
|
59
|
-
end
|
60
|
-
|
61
|
-
def quote(block)
|
62
|
-
"> #{convert_text(block)}"
|
63
|
-
end
|
64
|
-
|
65
|
-
def bulleted_list_item(block)
|
66
|
-
"- #{convert_text(block)}"
|
67
|
-
end
|
68
|
-
|
69
|
-
# TODO: numbered_list_item
|
70
|
-
def numbered_list_item(block)
|
71
|
-
Logger.info('numbered_list_item type not supported. Shown as bulleted_list_item.')
|
72
|
-
bulleted_list_item(block)
|
73
|
-
end
|
74
|
-
|
75
|
-
def to_do(block)
|
76
|
-
checked = block[:checked]
|
77
|
-
text = convert_text(block)
|
78
|
-
|
79
|
-
"- #{checked ? '[x]' : '[ ]'} #{text}"
|
80
|
-
end
|
81
|
-
|
82
|
-
def code(block)
|
83
|
-
language = block[:language]
|
84
|
-
text = convert_text(block)
|
85
|
-
|
86
|
-
"```#{language}\n\t#{text}\n```"
|
87
|
-
end
|
88
|
-
|
89
|
-
def embed(block)
|
90
|
-
url = block[:url]
|
91
|
-
|
92
|
-
"[#{url}](#{url})"
|
93
|
-
end
|
94
|
-
|
95
|
-
def image(block)
|
96
|
-
type = block[:type].to_sym
|
97
|
-
url = block.dig(type, :url)
|
98
|
-
caption = convert_caption(block)
|
99
|
-
|
100
|
-
"\n\n#{caption}"
|
101
|
-
end
|
102
|
-
|
103
|
-
def bookmark(block)
|
104
|
-
url = block[:url]
|
105
|
-
"[#{url}](#{url})"
|
106
|
-
end
|
107
|
-
|
108
|
-
def divider(_block)
|
109
|
-
'---'
|
110
|
-
end
|
111
|
-
|
112
|
-
def equation(block)
|
113
|
-
equ = convert_text(block)
|
114
|
-
"$$ #{equ} $$"
|
115
|
-
end
|
116
|
-
|
117
|
-
def blank
|
118
|
-
'<br />'
|
119
|
-
end
|
120
|
-
|
121
|
-
def convert_text(block)
|
122
|
-
block[:text].map do |text|
|
123
|
-
content = text[:plain_text]
|
124
|
-
enrich_text_content(text, content)
|
125
|
-
end.join
|
126
|
-
end
|
127
|
-
|
128
|
-
def convert_caption(block)
|
129
|
-
convert_text(text: block[:caption])
|
130
|
-
end
|
131
|
-
|
132
|
-
def get_icon(block)
|
133
|
-
type = block[:type].to_sym
|
134
|
-
block[type]
|
135
|
-
end
|
136
|
-
|
137
|
-
def enrich_text_content(text, content)
|
138
|
-
enriched_content = add_link(text, content)
|
139
|
-
add_annotations(text, enriched_content)
|
140
|
-
end
|
141
|
-
|
142
|
-
def add_link(text, content)
|
143
|
-
href = text[:href]
|
144
|
-
return content if href.nil?
|
145
|
-
|
146
|
-
"[#{content}](#{href})"
|
147
|
-
end
|
148
|
-
|
149
|
-
def add_annotations(text, content)
|
150
|
-
annotations = text[:annotations].select { |_key, value| !!value }
|
151
|
-
annotations.keys.inject(content) do |enriched_content, annotation|
|
152
|
-
TextAnnotation.send(annotation.to_sym, enriched_content)
|
153
|
-
end
|
154
|
-
end
|
155
34
|
end
|
156
35
|
end
|
data/lib/notion_to_md/logger.rb
CHANGED
data/lib/notion_to_md/version.rb
CHANGED
data/lib/notion_to_md.rb
CHANGED
@@ -1,4 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'notion'
|
4
|
+
require 'logger'
|
3
5
|
require_relative './notion_to_md/version'
|
4
6
|
require_relative './notion_to_md/converter'
|
7
|
+
require_relative './notion_to_md/logger'
|
8
|
+
require_relative './notion_to_md/block'
|
9
|
+
require_relative './notion_to_md/text'
|
10
|
+
require_relative './notion_to_md/text_annotation'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: notion_to_md
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Enrique Arias
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: notion-ruby-client
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rubocop
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -60,8 +74,10 @@ extra_rdoc_files: []
|
|
60
74
|
files:
|
61
75
|
- README.md
|
62
76
|
- lib/notion_to_md.rb
|
77
|
+
- lib/notion_to_md/block.rb
|
63
78
|
- lib/notion_to_md/converter.rb
|
64
79
|
- lib/notion_to_md/logger.rb
|
80
|
+
- lib/notion_to_md/text.rb
|
65
81
|
- lib/notion_to_md/text_annotation.rb
|
66
82
|
- lib/notion_to_md/version.rb
|
67
83
|
homepage: https://github.com/emoriarty/notion_to_md
|