notion_to_md 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5e11590c90c4539c151d8c290f1175a76c331bcd4027832c055158ae11b3e396
4
- data.tar.gz: 410e1b78ddb8f287b9b4a92b7e583910cccf6474d612b42c4a4933dad0d154b5
3
+ metadata.gz: 77ebe548eec487347104eabbac6911836cef0ec6e5ea350cb9a376ef8e07ce50
4
+ data.tar.gz: b2fd0700b53683daa6e59a729418139bbdee41da8184dad57eff0f2c17578dc7
5
5
  SHA512:
6
- metadata.gz: deeb77d21a3be57cb713b1c765b008b502d8c100bb103be99a88b50311c3895d3557f8c2909db1d8334616e223862fd6bf6e0b21c233237ab99698f7d008c466
7
- data.tar.gz: 594bc21094683131274a3eb254ea9efad95a6d98839d219862bfd38b867aba43fb833ef3c9adb47dce55aa9a089ee16d806e049a356fedb1b5aee177746be8a1
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
+ "![](#{url})\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
- "![](#{url})\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
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'logger'
4
-
5
3
  module NotionToMd
6
4
  class Logger
7
5
  @logger = ::Logger.new($stdout)
@@ -0,0 +1,13 @@
1
+ module NotionToMd
2
+ class Text
3
+ class << self
4
+ def text(text)
5
+ text[:plain_text]
6
+ end
7
+
8
+ def equation(text)
9
+ "$$ #{text[:plain_text]} $$"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -18,12 +18,12 @@ module NotionToMd
18
18
  "**#{text}**"
19
19
  end
20
20
 
21
- def striketrough(text)
21
+ def strikethrough(text)
22
22
  "~~#{text}~~"
23
23
  end
24
24
 
25
25
  def underline(text)
26
- text
26
+ "<u>#{text}</u>"
27
27
  end
28
28
 
29
29
  def code(text)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionToMd
4
- VERSION = '0.0.3'
4
+ VERSION = '0.1.0'
5
5
  end
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.3
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: 2021-12-31 00:00:00.000000000 Z
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