jekyll-notion 0.1.6 → 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: db8a62e0d2b12f5cedd1f405e289e95a974a22a80e8c4ffd9e0798a75806773a
4
- data.tar.gz: 4668f71e64ff3604223b664a251b22931ce23d889c535b06e39f735891252074
3
+ metadata.gz: '051289989a623c954ef76ca1299a79a4499649dea4b94dfe49ac75f9a67d4bbc'
4
+ data.tar.gz: 5de9f6860672c7eb8cdc355b5c5af3257fdc476cba3a5d20b4a420f330189f7e
5
5
  SHA512:
6
- metadata.gz: ae19056b6cd4692b327d37803bf2c4a7afda07f6736cf2af660c12908694249a34c5e813191843d6ee2aa230f211a4044fbdca1fb203a827f6629aa3707e9e5c
7
- data.tar.gz: a7dd6dea825e2124032ec6900638a913fcc4e05de009798e24b365aa19c41f80fd399316bc6b9f811ea4c8a96e659d9cab11371dff60650752a7bd3aff847df8
6
+ metadata.gz: 211bcfcf58c3d5e0d1e9d684a7a8483e31882e9b2d81df5e8770a24a62ed7c82334d9481a9138dc6c3285fa3e0585facca6f2f68bcbb1b6d769595c5d9051774
7
+ data.tar.gz: 3d5d03c0223c1e891f5ba1102defb082638b67bd9e07cbfc56e2beeadc93e650ed8b2b758543a0a8afe6f8c36a2ad6ca33dd300955fbbc009b7260e509a7e21d
data/README.md CHANGED
@@ -32,13 +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
- fetch_on_watch: false
40
39
  database:
41
- 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
42
73
  collection: posts
43
74
  filter: { "property": "Published", "checkbox": { "equals": true } }
44
75
  sort: { "property": "Last ordered", "direction": "ascending" }
@@ -46,47 +77,51 @@ notion:
46
77
  layout: post
47
78
  ```
48
79
 
49
- `fetch_on_watch` when set to `true` it allows fetching notion pages on each rebuild. By default is off, pages are only retrieved in the first build.
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.
50
81
 
51
- `database` properties are:
52
- * `id`: the notion database unique identifier,
53
- * `collection`: the collection each page belongs to (posts by default),
54
- * `filter`: the database query filter,
55
- * `sort`: the database query sort,
56
- * `frontmatter`: additional frontmatter to append to each page in the collection.
82
+ ### Watch
57
83
 
58
- Note: Only one notion 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
+ ```
59
94
 
60
95
  And that's all. Each page in the notion database will be included in the selected collection.
61
96
 
62
97
  ## Notion properties
63
98
 
64
- 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.
65
100
 
66
- 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`.
67
102
 
68
103
  ```
69
104
  ---
70
- id: b2998...
71
- title: A title
105
+ id: e42383cd-4975-4897-b967-ce453760499f
106
+ title: An amazing post
72
107
  cover: https://img.bank.sh/an_image.jpg
73
108
  date: 2022-01-23T12:31:00.000Z
74
109
  icon: \U0001F4A5
75
110
  ---
76
111
  ```
77
112
 
78
- 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.
79
114
 
80
115
  ### Custom properties
81
116
 
82
117
  In addition to default properties, custom properties are also supported.
83
118
 
84
- 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.
85
120
  For example, two properties named `Multiple Options` and `Tags` will be transformed to `multiple_options` and `tags`, respectively.
86
121
 
87
122
  ```
88
123
  ---
89
- id: b2998...
124
+ id: 2190450d-4cb3-4739-a5c8-340c4110fe21
90
125
  title: A title
91
126
  cover: https://img.bank.sh/an_image.jpg
92
127
  date: 2022-01-23T12:31:00.000Z
@@ -96,7 +131,7 @@ multiple_options: option1, option2
96
131
  ---
97
132
  ```
98
133
 
99
- The supported properties are:
134
+ The supported property types are:
100
135
 
101
136
  * `number`
102
137
  * `select`
@@ -2,38 +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
- if fetch_on_watch? || docs.empty?
12
+ if fetch_on_watch? || collections.empty?
13
13
  read_notion_database
14
14
  else
15
- collection.docs = docs
15
+ collections.each_pair { |key, val| @site.collections[key] = val }
16
16
  end
17
17
  end
18
18
 
19
+ protected
20
+
19
21
  def read_notion_database
20
- @db = NotionDatabase.new(:config => config)
21
- @db.pages.each do |page|
22
- @current_page = page
23
- collection.docs << make_page
24
- 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
25
31
  end
26
- @docs = collection.docs
27
32
  end
28
33
 
29
- def docs
30
- @docs ||= []
34
+ def databases
35
+ config["databases"] || [config["database"]]
36
+ end
37
+
38
+ def collections
39
+ @collections ||= {}
31
40
  end
32
41
 
33
42
  def make_page
34
43
  new_post = DocumentWithoutAFile.new(
35
- "#{Dir.pwd}/_#{collection_name}/#{make_filename}",
36
- { :site => @site, :collection => collection }
44
+ "#{Dir.pwd}/_#{current_db.collection}/#{make_filename}",
45
+ { :site => @site, :collection => current_collection }
37
46
  )
38
47
  new_post.content = "#{make_frontmatter}\n\n#{make_md}"
39
48
  new_post.read
@@ -45,7 +54,7 @@ module JekyllNotion
45
54
  end
46
55
 
47
56
  def make_frontmatter
48
- data = Jekyll::Utils.deep_merge_hashes(config_frontmatter, page_frontmatter)
57
+ data = Jekyll::Utils.deep_merge_hashes(current_db.frontmatter, page_frontmatter)
49
58
  frontmatter = data.to_a.map { |k, v| "#{k}: #{v}" }.join("\n")
50
59
  <<~CONTENT
51
60
  ---
@@ -58,12 +67,8 @@ module JekyllNotion
58
67
  Jekyll::Utils.deep_merge_hashes(current_page.custom_props, current_page.default_props)
59
68
  end
60
69
 
61
- def config_frontmatter
62
- config.dig("database", "frontmatter") || {}
63
- end
64
-
65
70
  def make_filename
66
- if collection_name == "posts"
71
+ if current_db.collection == "posts"
67
72
  "#{current_page.created_date}-#{Jekyll::Utils.slugify(current_page.title,
68
73
  :mode => "latin")}.md"
69
74
  else
@@ -71,12 +76,8 @@ module JekyllNotion
71
76
  end
72
77
  end
73
78
 
74
- def collection_name
75
- config.dig("database", "collection") || "posts"
76
- end
77
-
78
- def collection
79
- @site.collections[collection_name]
79
+ def current_collection
80
+ @site.collections[current_db.collection]
80
81
  end
81
82
 
82
83
  def config
@@ -102,5 +103,16 @@ module JekyllNotion
102
103
  end
103
104
  true
104
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
105
117
  end
106
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:",
@@ -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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllNotion
4
- VERSION = "0.1.6"
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.6
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-02-05 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