jekyll-notion 0.1.3 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e49bed0932098925103231f77ac375ff0d86562ffb762c058c41b4db5e6ca091
4
- data.tar.gz: 22e172d0c20d1b4dd9bbd4c926010a06492be08f247abe18f99795182ce1f319
3
+ metadata.gz: '051289989a623c954ef76ca1299a79a4499649dea4b94dfe49ac75f9a67d4bbc'
4
+ data.tar.gz: 5de9f6860672c7eb8cdc355b5c5af3257fdc476cba3a5d20b4a420f330189f7e
5
5
  SHA512:
6
- metadata.gz: 8f9a68ae342fb7c23c24b9b744afc1c562a7f33bd270207bb8b4b0cd52306d393c95ed4b4e9571605437dd3c1d4bc2a44f0ec029889158dd54fbe4932aaea9d7
7
- data.tar.gz: eb5e8580bbd76add336f6fb0f2ee68efe3c4a807ddfbf3657054088288ebd787803babb3d7e00b0a392eb2feee14aca6629071924352a4e378672b52547409d6
6
+ metadata.gz: 211bcfcf58c3d5e0d1e9d684a7a8483e31882e9b2d81df5e8770a24a62ed7c82334d9481a9138dc6c3285fa3e0585facca6f2f68bcbb1b6d769595c5d9051774
7
+ data.tar.gz: 3d5d03c0223c1e891f5ba1102defb082638b67bd9e07cbfc56e2beeadc93e650ed8b2b758543a0a8afe6f8c36a2ad6ca33dd300955fbbc009b7260e509a7e21d
data/README.md CHANGED
@@ -32,12 +32,44 @@ 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 `id` in your `_config.yml` as follows.
35
+ Once your notion database 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: b91d5...
40
+ id: e42383cd-4975-4897-b967-ce453760499f
41
+ ```
42
+
43
+ ### Mutiple dabatases
44
+
45
+ You can also define multiple databases as follows.
46
+
47
+ ```yml
48
+ notion:
49
+ databases:
50
+ - id: b0e688e1-99af-4295-ae80-b67eb52f2e2f
51
+ - id: 2190450d-4cb3-4739-a5c8-340c4110fe21
52
+ collection: recipes
53
+ - id: e42383cd-4975-4897-b967-ce453760499f
54
+ collection: films
55
+ ```
56
+
57
+ When no collection is defined, the `posts` collection is used by default.
58
+
59
+ ### Database options
60
+
61
+ Each dabatase support the following options.
62
+
63
+ * `id`: the notion database unique identifier,
64
+ * `collection`: the collection each page belongs to (posts by default),
65
+ * `filter`: the database query filter,
66
+ * `sort`: the database query sort,
67
+ * `frontmatter`: additional front matter to append to each page in the collection.
68
+
69
+ ```yml
70
+ notion:
71
+ database:
72
+ id: e42383cd-4975-4897-b967-ce453760499f
41
73
  collection: posts
42
74
  filter: { "property": "Published", "checkbox": { "equals": true } }
43
75
  sort: { "property": "Last ordered", "direction": "ascending" }
@@ -45,44 +77,51 @@ notion:
45
77
  layout: post
46
78
  ```
47
79
 
48
- The other properties are:
49
- * `collection`: the collection each page belongs to (posts by default),
50
- * `filter`: the database query filter,
51
- * `sort`: the database query sort,
52
- * `frontmatter`: additional frontmatter to append to each page in the collection.
80
+ Note that you can also use [front matter defaults](https://jekyllrb.com/docs/configuration/front-matter-defaults/) to declare common key value pairs per collection.
81
+
82
+ ### Watch
53
83
 
54
- Note: Only one database is available.
84
+ By default, databases are only requested during the first build. Subsequent builds use the results from the cache.
85
+
86
+ Set `fetch_on_watch` to true to allow request on each rebuild.
87
+
88
+ ```yml
89
+ notion:
90
+ fetch_on_watch: true
91
+ database:
92
+ id: e42383cd-4975-4897-b967-ce453760499f
93
+ ```
55
94
 
56
95
  And that's all. Each page in the notion database will be included in the selected collection.
57
96
 
58
97
  ## Notion properties
59
98
 
60
- Below, page notion default properties are set in each page frontmatter.
99
+ Below, default properties per notion page are set for each document front matter.
61
100
 
62
- Default properties include `title`, created_time`, `last_edited_time`, `icon` and `cover.
101
+ Notion properties include page `id`, `title`, `created_time`, `last_edited_time`, `icon` and `cover`.
63
102
 
64
103
  ```
65
104
  ---
66
- id: b2998...
67
- title: A title
105
+ id: e42383cd-4975-4897-b967-ce453760499f
106
+ title: An amazing post
68
107
  cover: https://img.bank.sh/an_image.jpg
69
108
  date: 2022-01-23T12:31:00.000Z
70
109
  icon: \U0001F4A5
71
110
  ---
72
111
  ```
73
112
 
74
- Any property provided in the frontmatter config that matches a default property will be overwritten by the default value.
113
+ Default properties prevail over custom properties declared in the front matter config.
75
114
 
76
115
  ### Custom properties
77
116
 
78
117
  In addition to default properties, custom properties are also supported.
79
118
 
80
- Custom properties are appended to page frontmatter by default. Every property name is snake-cased.
119
+ Custom properties are appended to the page frontmatter by default. Every property name are downcased and snake-cased.
81
120
  For example, two properties named `Multiple Options` and `Tags` will be transformed to `multiple_options` and `tags`, respectively.
82
121
 
83
122
  ```
84
123
  ---
85
- id: b2998...
124
+ id: 2190450d-4cb3-4739-a5c8-340c4110fe21
86
125
  title: A title
87
126
  cover: https://img.bank.sh/an_image.jpg
88
127
  date: 2022-01-23T12:31:00.000Z
@@ -92,7 +131,7 @@ multiple_options: option1, option2
92
131
  ---
93
132
  ```
94
133
 
95
- The supported properties are:
134
+ The supported property types are:
96
135
 
97
136
  * `number`
98
137
  * `select`
@@ -2,29 +2,47 @@
2
2
 
3
3
  module JekyllNotion
4
4
  class Generator < Jekyll::Generator
5
- attr_reader :current_page
5
+ attr_reader :current_page, :current_db
6
6
 
7
7
  def generate(site)
8
8
  @site = site
9
9
 
10
10
  return unless notion_token? && config?
11
11
 
12
- read_notion_database
12
+ if fetch_on_watch? || collections.empty?
13
+ read_notion_database
14
+ else
15
+ collections.each_pair { |key, val| @site.collections[key] = val }
16
+ end
13
17
  end
14
18
 
19
+ protected
20
+
15
21
  def read_notion_database
16
- @db = NotionDatabase.new(:config => config)
17
- @db.pages.each do |page|
18
- @current_page = page
19
- collection.docs << make_page
20
- Jekyll.logger.info("Jekyll Notion:", "New notion page at #{collection.docs.last.path}")
22
+ databases.each do |db_config|
23
+ @current_db = NotionDatabase.new(:config => db_config)
24
+ @current_db.pages.each do |page|
25
+ @current_page = page
26
+ current_collection.docs << make_page
27
+ log_new_page
28
+ end
29
+ # Store current collection
30
+ collections[current_db.collection] = current_collection
21
31
  end
22
32
  end
23
33
 
34
+ def databases
35
+ config["databases"] || [config["database"]]
36
+ end
37
+
38
+ def collections
39
+ @collections ||= {}
40
+ end
41
+
24
42
  def make_page
25
43
  new_post = DocumentWithoutAFile.new(
26
- "#{Dir.pwd}/_#{collection_name}/#{make_filename}",
27
- { :site => @site, :collection => collection }
44
+ "#{Dir.pwd}/_#{current_db.collection}/#{make_filename}",
45
+ { :site => @site, :collection => current_collection }
28
46
  )
29
47
  new_post.content = "#{make_frontmatter}\n\n#{make_md}"
30
48
  new_post.read
@@ -36,7 +54,7 @@ module JekyllNotion
36
54
  end
37
55
 
38
56
  def make_frontmatter
39
- data = Jekyll::Utils.deep_merge_hashes(config_frontmatter, page_frontmatter)
57
+ data = Jekyll::Utils.deep_merge_hashes(current_db.frontmatter, page_frontmatter)
40
58
  frontmatter = data.to_a.map { |k, v| "#{k}: #{v}" }.join("\n")
41
59
  <<~CONTENT
42
60
  ---
@@ -49,30 +67,27 @@ module JekyllNotion
49
67
  Jekyll::Utils.deep_merge_hashes(current_page.custom_props, current_page.default_props)
50
68
  end
51
69
 
52
- def config_frontmatter
53
- config.dig("database", "frontmatter") || {}
54
- end
55
-
56
70
  def make_filename
57
- if collection_name == "posts"
58
- "#{current_page.created_date}-#{current_page.title.downcase.parameterize}.md"
71
+ if current_db.collection == "posts"
72
+ "#{current_page.created_date}-#{Jekyll::Utils.slugify(current_page.title,
73
+ :mode => "latin")}.md"
59
74
  else
60
75
  "#{current_page.title.downcase.parameterize}.md"
61
76
  end
62
77
  end
63
78
 
64
- def collection_name
65
- config.dig("database", "collection") || "posts"
66
- end
67
-
68
- def collection
69
- @site.collections[collection_name]
79
+ def current_collection
80
+ @site.collections[current_db.collection]
70
81
  end
71
82
 
72
83
  def config
73
84
  @config ||= @site.config["notion"] || {}
74
85
  end
75
86
 
87
+ def fetch_on_watch?
88
+ config["fetch_on_watch"].present?
89
+ end
90
+
76
91
  def notion_token?
77
92
  if ENV["NOTION_TOKEN"].nil? || ENV["NOTION_TOKEN"].empty?
78
93
  Jekyll.logger.warn("Jekyll Notion:", "NOTION_TOKEN not provided. Cannot read from Notion.")
@@ -88,5 +103,16 @@ module JekyllNotion
88
103
  end
89
104
  true
90
105
  end
106
+
107
+ def log_new_page
108
+ Jekyll.logger.info("Jekyll Notion:", "Page => #{current_page.title}")
109
+ if @site.config.dig(
110
+ "collections", current_db.collection, "output"
111
+ )
112
+ Jekyll.logger.info("",
113
+ "Path => #{current_collection.docs.last.path}")
114
+ end
115
+ Jekyll.logger.debug("", "Props => #{page_frontmatter.keys.inspect}")
116
+ end
91
117
  end
92
118
  end
@@ -11,28 +11,40 @@ module JekyllNotion
11
11
  return [] unless id?
12
12
 
13
13
  @pages ||= @notion.database_query(query)[:results].map do |page|
14
- NotionPage.new(:page => page, :layout => config["layout"])
14
+ NotionPage.new(:page => page, :layout => layout)
15
15
  end
16
16
  end
17
17
 
18
- private
19
-
20
18
  def config
21
- @config["database"]
19
+ @config || {}
22
20
  end
23
21
 
24
22
  def filter
25
- @config.dig("database", "filter")
23
+ config["filter"]
26
24
  end
27
25
 
28
26
  def sort
29
- @config.dig("database", "sort")
27
+ config["sort"]
30
28
  end
31
29
 
32
30
  def id
33
- @config.dig("database", "id")
31
+ config["id"]
32
+ end
33
+
34
+ def frontmatter
35
+ config["frontmatter"] || {}
36
+ end
37
+
38
+ def collection
39
+ config["collection"] || "posts"
40
+ end
41
+
42
+ def layout
43
+ config["layout"]
34
44
  end
35
45
 
46
+ private
47
+
36
48
  def id?
37
49
  if id.nil? || id.empty?
38
50
  Jekyll.logger.warn("Jekyll Notion:",
@@ -43,7 +55,7 @@ module JekyllNotion
43
55
  end
44
56
 
45
57
  def query
46
- { :id => id, :filter => filter, :sort => sort }
58
+ { :id => id, :filter => filter, :sort => sort }.compact
47
59
  end
48
60
  end
49
61
  end
@@ -61,12 +61,12 @@ module JekyllNotion
61
61
 
62
62
  def default_props
63
63
  @default_props ||= {
64
- :id => id,
65
- :title => title,
66
- :date => created_datetime,
67
- :cover => cover,
68
- :icon => icon,
69
- :updated_date => updated_datetime,
64
+ "id" => id,
65
+ "title" => title,
66
+ "date" => created_datetime,
67
+ "cover" => cover,
68
+ "icon" => icon,
69
+ "updated_date" => updated_datetime,
70
70
  }
71
71
  end
72
72
 
@@ -113,6 +113,10 @@ module JekyllNotion
113
113
  def date(prop)
114
114
  prop.date.start
115
115
  end
116
+
117
+ def url(prop)
118
+ prop.url
119
+ end
116
120
  end
117
121
  end
118
122
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllNotion
4
- VERSION = "0.1.3"
4
+ VERSION = "0.2.0"
5
5
  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: 0.1.3
4
+ version: 0.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-01-30 00:00:00.000000000 Z
11
+ date: 2022-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport