jekyll-notion 1.1.1 → 1.2.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: d06d0e87707f04d3125126bc7b801c38b6d25aa047ccea5074c567de5504c166
4
- data.tar.gz: cb581ec500f718c21156eacdffbd3b77368254573de4bfc891f5e074aea8d811
3
+ metadata.gz: ac8561afc4f42d433ae33d4baafb38e9eaf7a34167513bf628eef849c3e773b7
4
+ data.tar.gz: 7c55ac94b8490943d981f3c25d36d0b3993680614ba25cdb16be78f07b5e028a
5
5
  SHA512:
6
- metadata.gz: d4acb7a0293e3136b7ef128e3881c2683dc81c9daa56ce1f5659fdae146b117014a25660c394b692995d7b86bc0d06f2b2aeeccc1179c4571d035cbeb232c868
7
- data.tar.gz: fd462f54aaebbbf8fdfa925653b74b2443603ff10a7fb8c25b38dc63bea4f13fb261fcfb619c8e0f84d7974c1e2f5ae3a69ff0489bf817c2bacc7dd71a094381
6
+ metadata.gz: d402be5b38a3dc4cae78daf0e95140ecc0b593db1091ac325f262a68024236f50639c25f1bee42db6cee025122bfb82d2318f312909278509c66f2375062d4d1
7
+ data.tar.gz: c22e49f752ba7a848c6329e55c57707c6bc9d59e510f5407a112448335a9a36b877c9abe4e94cf30fd383db29defd5fafbba14558742417375c4df4990f91a90
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # jekyll-notion
2
2
 
3
- Import notion pages to a jekyll collection.
3
+ Import notion pages to a jekyll collection or data.
4
4
 
5
5
  ## Installation
6
6
 
@@ -32,16 +32,17 @@ Export the notion secret token in an environment variable named `NOTION_TOKEN`.
32
32
  $ export NOTION_TOKEN=<secret_...>
33
33
  ```
34
34
 
35
- Once your notion database has been shared, specify the database `id` in your `_config.yml` as follows.
35
+ Once your [notion database](https://www.notion.so/help/intro-to-databases) has been shared, specify the database `id` in your `_config.yml` as follows.
36
36
 
37
37
  ```yml
38
38
  notion:
39
39
  database:
40
- id: e42383cd-4975-4897-b967-ce453760499f
40
+ id: 5cfed4de3bdc4f43ae8ba653a7a2219b
41
41
  ```
42
42
 
43
43
  After running `jekyll build` (or `serve`) command, the `posts` collection is loaded with pages of the notion database specified in the configuration.
44
44
 
45
+
45
46
  ### Mutiple dabatases
46
47
 
47
48
  You can also define multiple databases as follows.
@@ -53,27 +54,51 @@ collections:
53
54
 
54
55
  notion:
55
56
  databases:
56
- - id: b0e688e1-99af-4295-ae80-b67eb52f2e2f
57
- - id: 2190450d-4cb3-4739-a5c8-340c4110fe21
57
+ - id: b0e688e199af4295ae80b67eb52f2e2f
58
+ - id: 2190450d4cb34739a5c8340c4110fe21
58
59
  collection: recipes
59
- - id: e42383cd-4975-4897-b967-ce453760499f
60
+ - id: e42383cd49754897b967ce453760499f
60
61
  collection: films
61
62
  ```
62
63
 
63
- In this example, the notion database `b0e688e1-99af-4295-ae80-b67eb52f2e2f` pages are mapped into the posts collection. `recipes` and `films` will contain the database pages `2190450d-4cb3-4739-a5c8-340c4110fe21` and `e42383cd-4975-4897-b967-ce453760499f`, respectively.
64
-
65
- ### data
64
+ ### Data
66
65
 
67
- Instead of storing notion pages in a collection, you can also map to the data object. Declare the data property as follows.
66
+ Instead of storing notion pages in a collection, you can also map to the data object. Use the `data` property instead of `collection`.
68
67
 
69
68
  ```yml
70
69
  notion:
71
70
  database:
72
- id: e42383cd-4975-4897-b967-ce453760499f
71
+ id: e42383cd49754897b967ce453760499f
73
72
  data: films
74
73
  ```
75
74
 
76
- Unlike collections, only the properties of the notion page are assigned to the each data item. The body of the notion page is omitted.
75
+ Page properties and body are stored as a hash data.
76
+
77
+ As a particular characteristic, the page is stored in a key named `content`.
78
+
79
+ ```html
80
+ <p>{{ site.data.films.content }}</p>
81
+ ```
82
+
83
+ The rest of properties as mapped as expected.
84
+
85
+ ### Pages
86
+
87
+ Individual Notion pages can also be mapped to data. Just define the `pages` or `page` property as follows.
88
+
89
+ ```yaml
90
+ notion:
91
+ pages:
92
+ - id: e42383cd49754897b967ce453760499f
93
+ data: about
94
+ - id: b0e688e199af4295ae80b67eb52f2e2f
95
+ data: contact
96
+ - id: 2190450d4cb34739a5c8340c4110fe21
97
+ data: footer
98
+
99
+ ```
100
+
101
+ _This feature is only available for data._
77
102
 
78
103
  ### Database options
79
104
 
@@ -87,7 +112,7 @@ Each dabatase support the following options.
87
112
  ```yml
88
113
  notion:
89
114
  database:
90
- id: e42383cd-4975-4897-b967-ce453760499f
115
+ id: e42383cd49754897b967ce453760499f
91
116
  collection: posts
92
117
  filter: { "property": "Published", "checkbox": { "equals": true } }
93
118
  sort: { "property": "Last ordered", "direction": "ascending" }
@@ -103,7 +128,7 @@ Set `fetch_on_watch` to true to allow request on each rebuild.
103
128
  notion:
104
129
  fetch_on_watch: true
105
130
  database:
106
- id: e42383cd-4975-4897-b967-ce453760499f
131
+ id: e42383cd49754897b967ce453760499f
107
132
  ```
108
133
 
109
134
  And that's all. Each page in the notion database will be included in the selected collection.
@@ -2,14 +2,14 @@
2
2
 
3
3
  module JekyllNotion
4
4
  class AbstractGenerator
5
- def initialize(db:, site:, plugin:)
6
- @db = db
5
+ def initialize(notion_resource:, site:, plugin:)
6
+ @notion_resource = notion_resource
7
7
  @site = site
8
8
  @plugin = plugin
9
9
  end
10
10
 
11
11
  def generate
12
- raise "Do not use the AbstractGenerator class directly. Implement the generate method in a subclass."
12
+ raise "Do not use the AbstractGenerator class. Implement the generate method in a subclass."
13
13
  end
14
14
  end
15
15
  end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JekyllNotion
4
+ class AbstractNotionResource
5
+ def initialize(config:)
6
+ @notion = Notion::Client.new
7
+ @config = config
8
+ end
9
+
10
+ def config
11
+ @config || {}
12
+ end
13
+
14
+ def id
15
+ config["id"]
16
+ end
17
+
18
+ def fetch
19
+ raise "Do not use the AbstractNotionResource class. Implement the fetch method in a subclass."
20
+ end
21
+
22
+ def collection_name
23
+ raise "Do not use the AbstractGenerator class. Implement the collection_name method in a subclass."
24
+ end
25
+
26
+ def data_name
27
+ raise "Do not use the AbstractGenerator class. Implement the data_name method in a subclass."
28
+ end
29
+
30
+ protected
31
+
32
+ def id?
33
+ if id.nil? || id.empty?
34
+ Jekyll.logger.warn("Jekyll Notion:",
35
+ "Database or page id is not provided. Cannot read from Notion.")
36
+ return false
37
+ end
38
+ true
39
+ end
40
+ end
41
+ end
@@ -3,18 +3,18 @@
3
3
  module JekyllNotion
4
4
  class CollectionGenerator < AbstractGenerator
5
5
  def generate
6
- @db.pages.each do |page|
6
+ @notion_resource.fetch.each do |page|
7
7
  next if file_exists?(make_path(page))
8
8
 
9
9
  collection.docs << make_doc(page)
10
10
  log_new_page(page)
11
11
  end
12
12
  # Caching current collection
13
- @plugin.collections[@db.collection] = collection
13
+ @plugin.collections[@notion_resource.collection_name] = collection
14
14
  end
15
15
 
16
16
  def collection
17
- @site.collections[@db.collection]
17
+ @site.collections[@notion_resource.collection_name]
18
18
  end
19
19
 
20
20
  private
@@ -35,11 +35,11 @@ module JekyllNotion
35
35
  end
36
36
 
37
37
  def make_path(page)
38
- "_#{@db.collection}/#{make_filename(page)}"
38
+ "_#{@notion_resource.collection_name}/#{make_filename(page)}"
39
39
  end
40
40
 
41
41
  def make_filename(page)
42
- if @db.collection == "posts"
42
+ if @notion_resource.collection_name == "posts"
43
43
  "#{page.created_time.to_date}-#{Jekyll::Utils.slugify(page.title,
44
44
  :mode => "latin")}.md"
45
45
  else
@@ -54,7 +54,7 @@ module JekyllNotion
54
54
  def log_new_page(page)
55
55
  Jekyll.logger.info("Jekyll Notion:", "Page => #{page.title}")
56
56
  if @site.config.dig(
57
- "collections", @db.collection, "output"
57
+ "collections", @notion_resource.collection_name, "output"
58
58
  )
59
59
  Jekyll.logger.info("",
60
60
  "Path => #{collection.docs.last.path}")
@@ -3,22 +3,54 @@
3
3
  module JekyllNotion
4
4
  class DataGenerator < AbstractGenerator
5
5
  def generate
6
- @site.data[@db.data] = data
7
- # Caching current data
8
- @plugin.data[@db.data] = data
9
- log_pages
6
+ unless data.nil?
7
+ @site.data[@notion_resource.data_name] = data
8
+ # Caching current data in Generator instance (plugin)
9
+ @plugin.data[@notion_resource.data_name] = data
10
+ log_pages
11
+ end
10
12
  end
11
13
 
12
14
  private
13
15
 
14
16
  def data
15
- @data ||= @db.pages.map(&:props)
17
+ @data ||= if @notion_resource.is_a?(NotionDatabase)
18
+ pages = @notion_resource.fetch
19
+ pages.map { |page| page.props.merge({ "content" => convert(page) }) }
20
+ else
21
+ page = @notion_resource.fetch
22
+ page&.props&.merge({ "content" => convert(page) })
23
+ end
24
+ end
25
+
26
+ # Convert the notion page body using the site.converters.
27
+ #
28
+ # Returns String the converted content.
29
+ def convert(page)
30
+ converters.reduce(page.body) do |output, converter|
31
+ converter.convert output
32
+ rescue StandardError => e
33
+ Jekyll.logger.error "Conversion error:",
34
+ "#{converter.class} encountered an error while "\
35
+ "converting notion page '#{page.title}':"
36
+ Jekyll.logger.error("", e.to_s)
37
+ raise e
38
+ end
39
+ end
40
+
41
+ def converters
42
+ @converters ||= @site.converters.select { |c| c.matches(".md") }.tap(&:sort!)
16
43
  end
17
44
 
18
45
  def log_pages
19
- @db.pages.each do |page|
20
- Jekyll.logger.info("Jekyll Notion:", "Page => #{page.title}")
21
- Jekyll.logger.debug("", "Props => #{page.props.keys.inspect}")
46
+ if data.is_a?(Array)
47
+ data.each do |page|
48
+ Jekyll.logger.info("Jekyll Notion:", "Page => #{page["title"]}")
49
+ Jekyll.logger.debug("", "Props => #{page.keys.inspect}")
50
+ end
51
+ else
52
+ Jekyll.logger.info("Jekyll Notion:", "Page => #{data["title"]}")
53
+ Jekyll.logger.debug("", "Props => #{data.keys.inspect}")
22
54
  end
23
55
  end
24
56
  end
@@ -10,7 +10,8 @@ module JekyllNotion
10
10
  return unless notion_token? && config?
11
11
 
12
12
  if fetch_on_watch? || collections_and_data_empty?
13
- read_notion_database
13
+ read_notion_databases
14
+ read_notion_pages
14
15
  else
15
16
  collections.each_pair { |key, val| @site.collections[key] = val }
16
17
  data.each_pair { |key, val| @site.data[key] = val }
@@ -21,6 +22,10 @@ module JekyllNotion
21
22
  config["databases"] || [config["database"]]
22
23
  end
23
24
 
25
+ def pages
26
+ config["pages"] || [config["page"]]
27
+ end
28
+
24
29
  def collections
25
30
  @collections ||= {}
26
31
  end
@@ -35,10 +40,17 @@ module JekyllNotion
35
40
  collections.empty? && data.empty?
36
41
  end
37
42
 
38
- def read_notion_database
43
+ def read_notion_databases
39
44
  databases.each do |db_config|
40
45
  db = NotionDatabase.new(:config => db_config)
41
- GeneratorFactory.for(:db => db, :site => @site, :plugin => self).generate
46
+ GeneratorFactory.for(:notion_resource => db, :site => @site, :plugin => self).generate
47
+ end
48
+ end
49
+
50
+ def read_notion_pages
51
+ pages.each do |page_config|
52
+ page = NotionPage.new(:config => page_config)
53
+ GeneratorFactory.for(:notion_resource => page, :site => @site, :plugin => self).generate
42
54
  end
43
55
  end
44
56
 
@@ -2,11 +2,12 @@
2
2
 
3
3
  module JekyllNotion
4
4
  class GeneratorFactory
5
- def self.for(db:, site:, plugin:)
6
- if db.data.nil?
7
- CollectionGenerator.new(:db => db, :site => site, :plugin => plugin)
5
+ def self.for(notion_resource:, site:, plugin:)
6
+ if notion_resource.is_a?(NotionDatabase) && notion_resource.data_name.nil?
7
+ CollectionGenerator.new(:notion_resource => notion_resource, :site => site,
8
+ :plugin => plugin)
8
9
  else
9
- DataGenerator.new(:db => db, :site => site, :plugin => plugin)
10
+ DataGenerator.new(:notion_resource => notion_resource, :site => site, :plugin => plugin)
10
11
  end
11
12
  end
12
13
  end
@@ -1,24 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllNotion
4
- class NotionDatabase
5
- def initialize(config:)
6
- @notion = Notion::Client.new
7
- @config = config
8
- end
9
-
10
- def pages
4
+ class NotionDatabase < AbstractNotionResource
5
+ # Returns an empty array or a NotionToMd:Page array
6
+ def fetch
11
7
  return [] unless id?
12
8
 
13
- @pages ||= @notion.database_query(query)[:results].map do |page|
14
- NotionToMd::Page.new(:page => page)
9
+ @fetch ||= @notion.database_query(query)[:results].map do |page|
10
+ NotionToMd::Page.new(:page => page, :blocks => @notion.block_children(:id => page.id))
15
11
  end
16
12
  end
17
13
 
18
- def config
19
- @config || {}
20
- end
21
-
22
14
  def filter
23
15
  config["filter"]
24
16
  end
@@ -27,29 +19,16 @@ module JekyllNotion
27
19
  config["sort"]
28
20
  end
29
21
 
30
- def id
31
- config["id"]
32
- end
33
-
34
- def collection
22
+ def collection_name
35
23
  config["collection"] || "posts"
36
24
  end
37
25
 
38
- def data
26
+ def data_name
39
27
  config["data"]
40
28
  end
41
29
 
42
30
  private
43
31
 
44
- def id?
45
- if id.nil? || id.empty?
46
- Jekyll.logger.warn("Jekyll Notion:",
47
- "database id is not provided. Cannot read from Notion.")
48
- return false
49
- end
50
- true
51
- end
52
-
53
32
  def query
54
33
  { :id => id, :filter => filter, :sort => sort }.compact
55
34
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JekyllNotion
4
+ class NotionPage < AbstractNotionResource
5
+ # Returns the nil or a NotionToMd::Page instance
6
+ def fetch
7
+ return nil unless id?
8
+
9
+ @fetch ||= NotionToMd::Page.new(:page => @notion.page(query),
10
+ :blocks => @notion.block_children(query))
11
+ end
12
+
13
+ def data_name
14
+ config["data"]
15
+ end
16
+
17
+ def collection_name
18
+ nil
19
+ end
20
+
21
+ private
22
+
23
+ def query
24
+ { :id => id }
25
+ end
26
+ end
27
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllNotion
4
- VERSION = "1.1.1"
4
+ VERSION = "1.2.0"
5
5
  end
data/lib/jekyll-notion.rb CHANGED
@@ -15,8 +15,10 @@ end
15
15
  module JekyllNotion
16
16
  autoload :GeneratorFactory, "jekyll-notion/generator_factory"
17
17
  autoload :AbstractGenerator, "jekyll-notion/abstract_generator"
18
+ autoload :AbstractNotionResource, "jekyll-notion/abstract_notion_resource"
18
19
  autoload :CollectionGenerator, "jekyll-notion/collection_generator"
19
20
  autoload :DataGenerator, "jekyll-notion/data_generator"
20
21
  autoload :DocumentWithoutAFile, "jekyll-notion/document_without_a_file"
21
22
  autoload :NotionDatabase, "jekyll-notion/notion_database"
23
+ autoload :NotionPage, "jekyll-notion/notion_page"
22
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-notion
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.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-28 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: jekyll
@@ -50,14 +50,14 @@ dependencies:
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '0'
53
+ version: '1.0'
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '0'
60
+ version: '1.0'
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: bundler
63
63
  requirement: !ruby/object:Gem::Requirement
@@ -111,12 +111,14 @@ files:
111
111
  - README.md
112
112
  - lib/jekyll-notion.rb
113
113
  - lib/jekyll-notion/abstract_generator.rb
114
+ - lib/jekyll-notion/abstract_notion_resource.rb
114
115
  - lib/jekyll-notion/collection_generator.rb
115
116
  - lib/jekyll-notion/data_generator.rb
116
117
  - lib/jekyll-notion/document_without_a_file.rb
117
118
  - lib/jekyll-notion/generator.rb
118
119
  - lib/jekyll-notion/generator_factory.rb
119
120
  - lib/jekyll-notion/notion_database.rb
121
+ - lib/jekyll-notion/notion_page.rb
120
122
  - lib/jekyll-notion/version.rb
121
123
  homepage: https://github.com/emoriarty/jekyll-notion
122
124
  licenses: