notion_to_md 0.2.1 → 1.1.1

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: ce6b39ba9cc39e1428bc8a654532d16b9ea99054e3fbd96cf30edd56cbed28a8
4
- data.tar.gz: 6e38131568bb150a9ca7904f383fd8ade5721ccc5affe2949672b597b948ffe3
3
+ metadata.gz: b11e8415fbf22af0f2a346cdb5e5ed5fd79cf3736f37d2d047d8d11bf1b4e362
4
+ data.tar.gz: 57b5b92fb1a01faa5334c85926742e50682553d44c5ef3d96c5b7278ca27a70f
5
5
  SHA512:
6
- metadata.gz: 3ccfa65a6877fd1bcdc9a36f72094cc8374934a2713abfc10441b23348b919818539c286469afad699f69594dc854a12f884299d2c335ff6cd9bc17d920bbcb6
7
- data.tar.gz: fd638c9a53e8c6f6cf644e2e471fc7ecf8150bb1ef371d9f6f41215e4d1bd953d1929113c25dc703dd66647165adf84bbf9f7bddcb6494d8b0bcea5bd06f2741
6
+ metadata.gz: 501c58bec83d950a0f7a1c69da2ff542d00745704c3557aea0b159fd87a587702def108b174915ec6beabb5b67509fbc790491493644e4be9859079fcd5ffb99
7
+ data.tar.gz: fc5cf12e22528194b2a877333b73c0d28ed1d76f19a49e204a002bff0ceee60654935009d09155b57b391e463e792522cf29e027d5e565326ae46c3f8bb7f966
data/README.md CHANGED
@@ -40,8 +40,24 @@ md = notion_converter.convert
40
40
 
41
41
  And that's all. The `md` is a string variable containing the notion page formatted in markdown.
42
42
 
43
+ ## Blocks
43
44
 
44
- ### Front matter
45
+ Everything in a notion page body is a [block object](https://developers.notion.com/reference/block#block-object-keys). Therefore, not every type can be mapped to Markdown. Below there's a list of current supported blocks types.
46
+
47
+ * `paragraph`
48
+ * `heading_1`
49
+ * `heading_2`
50
+ * `heading_3`
51
+ * `bulleted_list_item`
52
+ * `numbered_list_item` as `bulleted_list_item`
53
+ * `to_do`
54
+ * `image`
55
+ * `bookmark`
56
+ * `callout`
57
+ * `quote`
58
+ * `divider`
59
+
60
+ ## Front matter
45
61
 
46
62
  From version 0.2.0, notion_to_md supports front matter in markdown files.
47
63
 
@@ -51,7 +67,7 @@ By default, the front matter section is not included to the document. To do so,
51
67
  NotionToMd::Converter.new(page_id: 'b91d5...').convert(frontmatter: tue)
52
68
  ```
53
69
 
54
- Default notion properties are page `id`, `title`, `created_time`, `last_edited_time`, `icon`, `archived` and `cover`.
70
+ Default notion [properties](https://developers.notion.com/reference/property-object) are page `id`, `title`, `created_time`, `last_edited_time`, `icon`, `archived` and `cover`.
55
71
 
56
72
  ```yml
57
73
  ---
@@ -88,8 +104,9 @@ The supported property types are:
88
104
  * `url`
89
105
  * `email`
90
106
  * `phone_number`
107
+ * `rich_text`, supported but in plain text
91
108
 
92
- `rich_text` as advanced types like `formula`, `relation` and `rollup` are not supported.
109
+ Advanced types like `formula`, `relation` and `rollup` are not supported.
93
110
 
94
111
  Check notion documentation about [property values](https://developers.notion.com/reference/property-value-object#all-property-values) to know more.
95
112
 
@@ -33,7 +33,6 @@ module NotionToMd
33
33
  "- #{convert_text(block)}"
34
34
  end
35
35
 
36
- # TODO: numbered_list_item
37
36
  def numbered_list_item(block)
38
37
  Logger.info('numbered_list_item type not supported. Shown as bulleted_list_item.')
39
38
  bulleted_list_item(block)
@@ -50,7 +49,7 @@ module NotionToMd
50
49
  language = block[:language]
51
50
  text = convert_text(block)
52
51
 
53
- "```#{language}\n\t#{text}\n```"
52
+ "```#{language}\n#{text}\n```"
54
53
  end
55
54
 
56
55
  def embed(block)
@@ -10,43 +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 do |k, v|
41
- "#{k}: #{v}"
42
- end.join("\n")
43
- <<~CONTENT
44
- ---
45
- #{frontmatter}
46
- ---
47
- CONTENT
48
- end
49
-
50
22
  def page
51
23
  @page ||= @notion.page(id: page_id)
52
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
@@ -114,6 +140,10 @@ module NotionToMd
114
140
  def url(prop)
115
141
  prop.url
116
142
  end
143
+
144
+ def rich_text(prop)
145
+ prop.rich_text.map(&:plain_text).join
146
+ end
117
147
  end
118
148
  end
119
149
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionToMd
4
- VERSION = '0.2.1'
4
+ VERSION = '1.1.1'
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.1
4
+ version: 1.1.1
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-26 00:00:00.000000000 Z
11
+ date: 2022-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport