notion_to_md 2.4.0 → 2.5.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/README.md +21 -5
- data/lib/notion_to_md/blocks/block.rb +1 -1
- data/lib/notion_to_md/blocks/builder.rb +1 -1
- data/lib/notion_to_md/blocks/bulleted_list_block.rb +1 -1
- data/lib/notion_to_md/blocks/bulleted_list_item_block.rb +1 -1
- data/lib/notion_to_md/blocks/factory.rb +1 -1
- data/lib/notion_to_md/blocks/normalizer.rb +1 -1
- data/lib/notion_to_md/blocks/numbered_list_block.rb +1 -1
- data/lib/notion_to_md/blocks/numbered_list_item_block.rb +1 -1
- data/lib/notion_to_md/blocks/table_block.rb +1 -1
- data/lib/notion_to_md/blocks/table_row_block.rb +1 -1
- data/lib/notion_to_md/blocks/to_do_list_block.rb +1 -1
- data/lib/notion_to_md/blocks/to_do_list_item_block.rb +1 -1
- data/lib/notion_to_md/blocks/types.rb +23 -1
- data/lib/notion_to_md/blocks.rb +1 -1
- data/lib/notion_to_md/converter.rb +14 -5
- data/lib/notion_to_md/helpers/yaml_sanitizer.rb +17 -0
- data/lib/notion_to_md/helpers.rb +1 -0
- data/lib/notion_to_md/logger.rb +1 -1
- data/lib/notion_to_md/page.rb +4 -2
- data/lib/notion_to_md/page_property.rb +5 -3
- data/lib/notion_to_md/text.rb +1 -1
- data/lib/notion_to_md/text_annotation.rb +1 -1
- data/lib/notion_to_md/version.rb +2 -2
- data/lib/notion_to_md.rb +22 -1
- 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: 4137f1edccac5aceec8f5195beb513019ec2ad84c52c5495a1d9842098dd2b4d
|
4
|
+
data.tar.gz: 51a57a1bf294d1c2cf3d0be1e24c693aa633c3c873fe864a9f2864fec6194ecb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32e4a48a21d956bba91ba88d3aa3db0fdb6f7b0f54f55fcd44fdb07dd15842247ab6150b06f0f82a828ef8f0f632d158e746f54007afdd85357c41e75167b203
|
7
|
+
data.tar.gz: b9aa39eeb9b5cb105a8ebf48ca78fe4e88c0a9a7c013f63dc0057dc3adf0ffcfd0cbd2c72df87b83d5e5eafaaa9605e26a0c44f63852af4be9e87e37f752d6cc
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# notion_to_md
|
2
2
|
Notion Markdown Exporter in Ruby.
|
3
3
|
|
4
|
+
The generated document is compliant with the [GitHub Flavored Markdown specification](https://github.github.com/gfm/).
|
5
|
+
|
4
6
|
## Installation
|
5
7
|
Use gem to install.
|
6
8
|
```bash
|
@@ -28,8 +30,6 @@ md = notion_converter.convert
|
|
28
30
|
Since v2.3 you can also use the convenient `convert` method from the root module.
|
29
31
|
|
30
32
|
```ruby
|
31
|
-
require 'notion_to_md'
|
32
|
-
|
33
33
|
md = NotionToMd.convert(page_id: 'b91d5...', token: 'secret_...')
|
34
34
|
```
|
35
35
|
|
@@ -40,15 +40,27 @@ $ export NOTION_TOKEN=<secret_...>
|
|
40
40
|
```
|
41
41
|
|
42
42
|
```ruby
|
43
|
-
require 'notion_to_md'
|
44
|
-
|
45
43
|
notion_converter = NotionToMd::Converter.new(page_id: 'b91d5...')
|
46
44
|
md = notion_converter.convert
|
47
45
|
# or
|
48
46
|
md = NotionToMd.convert(page_id: 'b91d5...')
|
49
47
|
```
|
50
48
|
|
51
|
-
And that's all. The `md` is a string variable containing the notion
|
49
|
+
And that's all. The `md` is a string variable containing the notion document formatted in markdown.
|
50
|
+
|
51
|
+
### Callable
|
52
|
+
|
53
|
+
From version 2.5.0, the `call` method is also available. It's an alias for `convert`.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
md = NotionToMd.call(page_id: 'b91d5...')
|
57
|
+
```
|
58
|
+
|
59
|
+
The `call` method also supports the passing of a block.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
NotionToMd.call(page_id: 'b91d5...') { puts _1 }
|
63
|
+
```
|
52
64
|
|
53
65
|
## Blocks
|
54
66
|
|
@@ -69,6 +81,10 @@ Everything in a notion page body is a [block object](https://developers.notion.c
|
|
69
81
|
* `table`
|
70
82
|
* `embed`
|
71
83
|
* `code`
|
84
|
+
* `link_preview`
|
85
|
+
* `file`
|
86
|
+
* `pdf`
|
87
|
+
* `video`
|
72
88
|
|
73
89
|
### Nested blocks
|
74
90
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
class NotionToMd
|
4
4
|
module Blocks
|
5
5
|
class Types
|
6
6
|
class << self
|
@@ -89,6 +89,28 @@ module NotionToMd
|
|
89
89
|
"|#{block[:cells].map(&method(:convert_table_row)).join('|')}|"
|
90
90
|
end
|
91
91
|
|
92
|
+
def link_preview(block)
|
93
|
+
url = block[:url]
|
94
|
+
|
95
|
+
"[#{url}](#{url})"
|
96
|
+
end
|
97
|
+
|
98
|
+
def file(block)
|
99
|
+
type = block[:type].to_sym
|
100
|
+
url = block.dig(type, :url)
|
101
|
+
caption = convert_caption(block)
|
102
|
+
|
103
|
+
"[#{url}](#{url})\n\n#{caption}"
|
104
|
+
end
|
105
|
+
|
106
|
+
def pdf(block)
|
107
|
+
file(block)
|
108
|
+
end
|
109
|
+
|
110
|
+
def video(block)
|
111
|
+
file(block)
|
112
|
+
end
|
113
|
+
|
92
114
|
private
|
93
115
|
|
94
116
|
def convert_table_row(cells)
|
data/lib/notion_to_md/blocks.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
##
|
3
|
+
class NotionToMd
|
5
4
|
# The Converter class allows to transform notion pages to markdown documents.
|
6
5
|
# Just create a new Converter instance by providing the page_id:
|
7
6
|
# page_converter = NotionToMd::Converter.new(page_id: '9dc17c9c9d2e469dbbf0f9648f3288d3')
|
8
7
|
# Then, call for convert to obtain the markdown document:
|
9
8
|
# page_converter.convert
|
10
|
-
|
11
9
|
class Converter
|
12
|
-
|
10
|
+
include Callee
|
11
|
+
|
12
|
+
attr_reader :page_id, :frontmatter
|
13
13
|
|
14
14
|
# === Parameters
|
15
15
|
# page_id::
|
@@ -20,9 +20,10 @@ module NotionToMd
|
|
20
20
|
# === Returns
|
21
21
|
# A NotionToMd::Converter object.
|
22
22
|
#
|
23
|
-
def initialize(page_id:, token: nil)
|
23
|
+
def initialize(page_id:, token: nil, frontmatter: false)
|
24
24
|
@notion = Notion::Client.new(token: token || ENV['NOTION_TOKEN'])
|
25
25
|
@page_id = page_id
|
26
|
+
@frontmatter = frontmatter
|
26
27
|
end
|
27
28
|
|
28
29
|
# === Parameters
|
@@ -40,6 +41,14 @@ module NotionToMd
|
|
40
41
|
MD
|
41
42
|
end
|
42
43
|
|
44
|
+
def call
|
45
|
+
md = convert frontmatter: frontmatter
|
46
|
+
|
47
|
+
yield md if block_given?
|
48
|
+
|
49
|
+
md
|
50
|
+
end
|
51
|
+
|
43
52
|
private
|
44
53
|
|
45
54
|
def page
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class NotionToMd
|
2
|
+
module Helpers
|
3
|
+
module YamlSanitizer
|
4
|
+
# Escape the frontmatter value if it contains a colon or a dash followed by a space
|
5
|
+
# @param value [String] the value to escape
|
6
|
+
# @return [String] the escaped value
|
7
|
+
def escape_frontmatter_value(value)
|
8
|
+
if value.match?(/: |-\s/)
|
9
|
+
# Escape the double quotes inside the string
|
10
|
+
"\"#{value.gsub('"', '\"')}\""
|
11
|
+
else
|
12
|
+
value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative './helpers/yaml_sanitizer'
|
data/lib/notion_to_md/logger.rb
CHANGED
data/lib/notion_to_md/page.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
class NotionToMd
|
4
4
|
class Page
|
5
|
+
include Helpers::YamlSanitizer
|
6
|
+
|
5
7
|
attr_reader :page, :blocks
|
6
8
|
|
7
9
|
def initialize(page:, blocks:)
|
@@ -93,7 +95,7 @@ module NotionToMd
|
|
93
95
|
def default_props
|
94
96
|
@default_props ||= {
|
95
97
|
'id' => id,
|
96
|
-
'title' => title
|
98
|
+
'title' => escape_frontmatter_value(title),
|
97
99
|
'created_time' => created_time,
|
98
100
|
'cover' => cover,
|
99
101
|
'icon' => icon,
|
@@ -1,8 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
class NotionToMd
|
4
4
|
class PageProperty
|
5
5
|
class << self
|
6
|
+
include Helpers::YamlSanitizer
|
7
|
+
|
6
8
|
def file(prop)
|
7
9
|
prop.dig(:file, :url)
|
8
10
|
rescue NoMethodError
|
@@ -28,7 +30,7 @@ module NotionToMd
|
|
28
30
|
end
|
29
31
|
|
30
32
|
def select(prop)
|
31
|
-
prop.dig(:select, :name)
|
33
|
+
escape_frontmatter_value(prop.dig(:select, :name))
|
32
34
|
rescue NoMethodError
|
33
35
|
nil
|
34
36
|
end
|
@@ -95,7 +97,7 @@ module NotionToMd
|
|
95
97
|
|
96
98
|
def rich_text(prop)
|
97
99
|
text = prop[:rich_text].map { |text| text[:plain_text] }.join
|
98
|
-
text.blank? ? nil : text
|
100
|
+
text.blank? ? nil : escape_frontmatter_value(text)
|
99
101
|
rescue NoMethodError
|
100
102
|
nil
|
101
103
|
end
|
data/lib/notion_to_md/text.rb
CHANGED
data/lib/notion_to_md/version.rb
CHANGED
data/lib/notion_to_md.rb
CHANGED
@@ -4,6 +4,8 @@ require 'notion'
|
|
4
4
|
require 'logger'
|
5
5
|
require 'active_support/inflector'
|
6
6
|
require 'active_support/core_ext/object/blank'
|
7
|
+
require 'callee'
|
8
|
+
require_relative './notion_to_md/helpers'
|
7
9
|
require_relative './notion_to_md/version'
|
8
10
|
require_relative './notion_to_md/converter'
|
9
11
|
require_relative './notion_to_md/logger'
|
@@ -13,7 +15,18 @@ require_relative './notion_to_md/blocks'
|
|
13
15
|
require_relative './notion_to_md/text'
|
14
16
|
require_relative './notion_to_md/text_annotation'
|
15
17
|
|
16
|
-
|
18
|
+
# The NotionToMd class allows to transform notion pages to markdown documents.
|
19
|
+
class NotionToMd
|
20
|
+
include Callee
|
21
|
+
|
22
|
+
attr_reader :page_id, :token, :frontmatter
|
23
|
+
|
24
|
+
def initialize(page_id:, token: nil, frontmatter: false)
|
25
|
+
@page_id = page_id
|
26
|
+
@token = token || ENV['NOTION_TOKEN']
|
27
|
+
@frontmatter = frontmatter
|
28
|
+
end
|
29
|
+
|
17
30
|
# === Parameters
|
18
31
|
# page_id::
|
19
32
|
# A string representing the notion page id.
|
@@ -28,4 +41,12 @@ module NotionToMd
|
|
28
41
|
def self.convert(page_id:, token: nil, frontmatter: false)
|
29
42
|
Converter.new(page_id: page_id, token: token).convert(frontmatter: frontmatter)
|
30
43
|
end
|
44
|
+
|
45
|
+
def call
|
46
|
+
md = self.class.convert(page_id: page_id, token: token, frontmatter: frontmatter)
|
47
|
+
|
48
|
+
yield md if block_given?
|
49
|
+
|
50
|
+
md
|
51
|
+
end
|
31
52
|
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: 2.
|
4
|
+
version: 2.5.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: 2024-04-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: callee
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.3.6
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.3.6
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: notion-ruby-client
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -145,6 +159,8 @@ files:
|
|
145
159
|
- lib/notion_to_md/blocks/to_do_list_item_block.rb
|
146
160
|
- lib/notion_to_md/blocks/types.rb
|
147
161
|
- lib/notion_to_md/converter.rb
|
162
|
+
- lib/notion_to_md/helpers.rb
|
163
|
+
- lib/notion_to_md/helpers/yaml_sanitizer.rb
|
148
164
|
- lib/notion_to_md/logger.rb
|
149
165
|
- lib/notion_to_md/page.rb
|
150
166
|
- lib/notion_to_md/page_property.rb
|