notion_to_md 0.2.0 → 1.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: d326983d99843871779f5331bab84d734bc01c21aa90b4da0cc76f1dd092c95e
4
- data.tar.gz: 4b6a34aebbe07df627d956e9918606385e5d3952c56522ee66f3e331f8522d24
3
+ metadata.gz: edaf453c97dcc2d166335f7179ea81845b44da0991e07b75f2e230288b141655
4
+ data.tar.gz: ff82f6e0a124195467797f10472bb3ce6e7460f43da2a5ff5f55cbdf0ae74cd6
5
5
  SHA512:
6
- metadata.gz: 5e4990d98adf9eaff1dafc48285430b788de0bf4088b95ecd5f6c970fc850e015e9f148c79f0603647902b96b26465b01f88e9d86133472cc154a581c6f2e907
7
- data.tar.gz: bb63c2a623265a153e7300ca9389bf1e75773373f8221eed41e4b7a11b33f801c26d8d033f3038ac93ead0586257e7874773c9a490de436a30ea79c4e5d5ca08
6
+ metadata.gz: 9f6c340abef5d82806618e3e4a30abaa320b1bc996760cc0f1a80211277a313009367b7f74c271ff27b668c2fc7402819a87c3c45408a70d41fc515db0afb47c
7
+ data.tar.gz: 764f05022696b17c425784ae8aa7ba7a1648b9fc4172914b21c0c7c7165bbded342596edad993e51ee78ed6e512985a9fa5c21e1b31d0746e5363896c86ee3e7
data/README.md CHANGED
@@ -88,8 +88,9 @@ The supported property types are:
88
88
  * `url`
89
89
  * `email`
90
90
  * `phone_number`
91
+ * `rich_text`, supported but in plain text
91
92
 
92
- `rich_text` as advanced types like `formula`, `relation` and `rollup` are not supported.
93
+ Advanced types like `formula`, `relation` and `rollup` are not supported.
93
94
 
94
95
  Check notion documentation about [property values](https://developers.notion.com/reference/property-value-object#all-property-values) to know more.
95
96
 
@@ -10,41 +10,15 @@ module NotionToMd
10
10
  end
11
11
 
12
12
  def convert(frontmatter: false)
13
+ md_page = Page.new(page: page, blocks: page_blocks)
13
14
  <<~MD
14
- #{parse_frontmatter if frontmatter}
15
- #{parse_content}
15
+ #{md_page.frontmatter if frontmatter}
16
+ #{md_page.body}
16
17
  MD
17
18
  end
18
19
 
19
20
  private
20
21
 
21
- def parse_content
22
- md = page_blocks[:results].map do |block|
23
- next Block.blank if block[:type] == 'paragraph' && block.dig(:paragraph, :text).empty?
24
-
25
- block_type = block[:type].to_sym
26
-
27
- begin
28
- Block.send(block_type, block[block_type])
29
- rescue StandardError
30
- Logger.info("Unsupported block type: #{block_type}")
31
- next nil
32
- end
33
- end
34
- Logger.info("Notion page #{page_id} converted to markdown")
35
- md.compact.join("\n\n")
36
- end
37
-
38
- def parse_frontmatter
39
- notion_page = Page.new(page: page)
40
- frontmatter = notion_page.props.to_a.map { |k, v| "#{k}: #{v}" }.join("\n")
41
- <<~CONTENT
42
- ---
43
- #{frontmatter}
44
- ---
45
- CONTENT
46
- end
47
-
48
22
  def page
49
23
  @page ||= @notion.page(id: page_id)
50
24
  end
@@ -2,10 +2,11 @@
2
2
 
3
3
  module NotionToMd
4
4
  class Page
5
- attr_reader :page
5
+ attr_reader :page, :blocks
6
6
 
7
- def initialize(page:)
7
+ def initialize(page:, blocks:)
8
8
  @page = page
9
+ @blocks = blocks
9
10
  end
10
11
 
11
12
  def title
@@ -42,6 +43,31 @@ module NotionToMd
42
43
  page[:archived]
43
44
  end
44
45
 
46
+ def body
47
+ @body ||= blocks[:results].map do |block|
48
+ next Block.blank if block[:type] == 'paragraph' && block.dig(:paragraph, :text).empty?
49
+
50
+ block_type = block[:type].to_sym
51
+
52
+ begin
53
+ Block.send(block_type, block[block_type])
54
+ rescue StandardError
55
+ Logger.info("Unsupported block type: #{block_type}")
56
+ next nil
57
+ end
58
+ end.compact.join("\n\n")
59
+ end
60
+
61
+ def frontmatter
62
+ @frontmatter ||= <<~CONTENT
63
+ ---
64
+ #{props.to_a.map do |k, v|
65
+ "#{k}: #{v}"
66
+ end.join("\n")}
67
+ ---
68
+ CONTENT
69
+ end
70
+
45
71
  def props
46
72
  @props ||= custom_props.deep_merge(default_props)
47
73
  end
@@ -73,8 +99,7 @@ module NotionToMd
73
99
  class CustomProperty
74
100
  class << self
75
101
  def multi_select(prop)
76
- multi_select = prop.multi_select.map(&:name).join(', ')
77
- "[#{multi_select}]"
102
+ prop.multi_select.map(&:name)
78
103
  end
79
104
 
80
105
  def select(prop)
@@ -82,13 +107,11 @@ module NotionToMd
82
107
  end
83
108
 
84
109
  def people(prop)
85
- people = prop.people.map(&:name).join(', ')
86
- "[#{people}]"
110
+ prop.people.map(&:name)
87
111
  end
88
112
 
89
113
  def files(prop)
90
- files = prop.files.map { |f| "\"#{f.file.url}\"" }.join(', ')
91
- "[#{files}]"
114
+ prop.files.map { |f| f.file.url }
92
115
  end
93
116
 
94
117
  def phone_number(prop)
@@ -117,6 +140,10 @@ module NotionToMd
117
140
  def url(prop)
118
141
  prop.url
119
142
  end
143
+
144
+ def rich_text(prop)
145
+ prop.rich_text.map(&:plain_text).join
146
+ end
120
147
  end
121
148
  end
122
149
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionToMd
4
- VERSION = '0.2.0'
4
+ VERSION = '1.1.0'
5
5
  end
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.2.0
4
+ version: 1.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: 2022-02-15 00:00:00.000000000 Z
11
+ date: 2022-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport