jekyll-notion 2.3.2 → 2.4.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: dadf92a4c00ce112ad5ca51e136b676a380b54fedfa75a9488ce5437d56a71d1
4
- data.tar.gz: df4431eab3308f7502082196c9d8bb8518b340c94d7c76950f0430664769afa1
3
+ metadata.gz: 823cd1e40e6efc0003a0829807361ef7e9aef85d13e7a5a83236be1ea48312b0
4
+ data.tar.gz: 729882137bd7597ce87f2ba66d8f79df57b794262cd19950b5288f952a8f6c13
5
5
  SHA512:
6
- metadata.gz: 6d4aba49009378a8ca3e298d1de97d5285854bc5282c1aedb6e43ad7d6406a60baa0cfae614f33d027fa7a94353f2d03bd93405d6c8b60ac0ea72639a7f50f26
7
- data.tar.gz: 79bc2b133cd57a4da85216433564963ac56a49120ac36ed15aa8bb585fce64b54751f8a635816c1c9fc6afd968ad6289e4cc1dea854cee85576753cfd22e1397
6
+ metadata.gz: a32dc6d722f0692658969df2d7ebfd195e865503c47fda06ed2556f8b7c005968a2c08f96dfe4a5188926fc192094494d53423d107b67ca93a9717952a05f2e4
7
+ data.tar.gz: 3b8b5b61aa56e48e2823f58c00ddc67af8a9bdc5b892aedd7d13863aea25ecf14cf2def9b64cc1259170974527d00d250b8468b053d38a14e1957037d13f1fd3
data/README.md CHANGED
@@ -24,9 +24,9 @@ plugins:
24
24
 
25
25
  ## Usage
26
26
 
27
- Before using the gem create an integration and generate a secret token. Check [notion getting started guide](https://developers.notion.com/docs/getting-started) to learn more.
27
+ Before using the gem, create an integration and generate a secret token. For more in-depth instructions, refer to the Notion "Getting Started" [guide](https://developers.notion.com/docs/getting-started).
28
28
 
29
- Once you have youe secret, export it in an environment variable named `NOTION_TOKEN`.
29
+ Once you have your secret token, make sure to export it into an environment variable named `NOTION_TOKEN`.
30
30
 
31
31
  ```bash
32
32
  $ export NOTION_TOKEN=<secret_...>
@@ -34,7 +34,7 @@ $ export NOTION_TOKEN=<secret_...>
34
34
 
35
35
  ### Databases
36
36
 
37
- Once the [notion database](https://www.notion.so/help/intro-to-databases) has been shared, specify the database `id` in the `_config.yml` file as follows.
37
+ Once the [notion database](https://developers.notion.com/docs/working-with-databases) has been shared, specify the database `id` in the `_config.yml` file as follows.
38
38
 
39
39
  ```yml
40
40
  notion:
@@ -42,7 +42,7 @@ notion:
42
42
  - id: 5cfed4de3bdc4f43ae8ba653a7a2219b
43
43
  ```
44
44
 
45
- By default, the notion pages contained in the database will be loaded into the `posts` collection.
45
+ By default, the notion pages in the database will be loaded into the `posts` collection.
46
46
 
47
47
  We can also define __multiple databases__ as follows.
48
48
 
@@ -80,6 +80,12 @@ notion:
80
80
  sorts: [{ "timestamp": "created_time", "direction": "ascending" }]
81
81
  ```
82
82
 
83
+ #### Posts date
84
+
85
+ The `created_time` property of a notion page is used to set the date in the post filename. This is the date used for the `date` variable of the [predefined variables for posts](https://jekyllrb.com/docs/front-matter/#predefined-variables-for-posts).
86
+
87
+ It's important to note that the `created_time` cannot be modifed. However, if you wish to change the date of a post, you can create a new page property named "date" (or "Date"). This way, the posts collection will use the `date` property for the post date variable instead of the `created_time`.
88
+
83
89
  ### Pages
84
90
 
85
91
  Individual Notion pages can also be loaded into Jekyll. Just define the `page` property as follows.
@@ -155,6 +161,42 @@ notion:
155
161
 
156
162
  And that's all. Each page in the notion database will be included in the selected collection.
157
163
 
164
+ ### Cache
165
+
166
+ Starting from version 2.4.0, every request to Notion is cached locally. The cache enables the retrieval of Notion resources only during the first request. Subsequent requests are fetched from the cache, which can significantly reduce build times.
167
+
168
+ The cache mechanism is based on the [vcr](https://github.com/vcr/vcr) gem, which records HTTP requests. Every Notion resource, whether it is a database or page, is stored in an independent file using the document ID as the filename. For example, a database ID e42383cd49754897b967ce453760499f will be stored in the following path:
169
+
170
+ ```bash
171
+ .cache/jekyll-notion/vcr_cassetes/e42383cd49754897b967ce453760499f.yml
172
+ ```
173
+
174
+ **Note: The `cache` option invalidates the fetch_on_watch feature.**
175
+
176
+ #### Cache folder
177
+
178
+ By default, the cache folder is `.cache/jekyll-notion/vcr_cassetes`, but you can change this folder by setting the `cache_dir` property in the `_config.yml` file as follows.
179
+
180
+ ```yaml
181
+ notion:
182
+ cache_dir: another/folder
183
+ ```
184
+
185
+ The path must be relative to the working folder.
186
+
187
+ #### Cleaning cache
188
+
189
+ To clear the cache, delete the cache folder. If you want to remove a specific cache file, locate the file that matches the Notion resource ID and delete it.
190
+
191
+ #### Disabling cache
192
+
193
+ If you're not interested in the cache or you just want to disable it, set the ˋcache` option to false.
194
+
195
+ ```yaml
196
+ notion:
197
+ cache: false
198
+ ```
199
+
158
200
  ## Notion properties
159
201
 
160
202
  Notion page properties are set for each document in the front matter.
@@ -39,7 +39,7 @@ module JekyllNotion
39
39
  end
40
40
 
41
41
  def build_blocks(block_id)
42
- NotionToMd::Blocks.build(block_id: block_id) do |nested_id|
42
+ NotionToMd::Blocks.build(:block_id => block_id) do |nested_id|
43
43
  @notion.block_children({ :block_id => nested_id })
44
44
  end
45
45
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+
5
+ module JekyllNotion
6
+ module Cacheable
7
+ def self.setup(cache_dir)
8
+ # Using VCR to record and playback Notion API responses for caching
9
+ VCR.configure do |config|
10
+ config.cassette_library_dir = cache_path(cache_dir)
11
+ config.hook_into :faraday # Faraday is used by notion-ruby-client gem
12
+ config.filter_sensitive_data("<NOTION_TOKEN>") { ENV["NOTION_TOKEN"] }
13
+ config.allow_http_connections_when_no_cassette = true
14
+ config.default_cassette_options = {
15
+ :allow_playback_repeats => true,
16
+ :record => :new_episodes,
17
+ }
18
+ end
19
+ end
20
+
21
+ def self.cache_path(path = nil)
22
+ if path.nil?
23
+ File.join(Dir.getwd, ".cache", "jekyll-notion", "vcr_cassettes")
24
+ else
25
+ File.join(Dir.getwd, path)
26
+ end
27
+ end
28
+
29
+ def generate(*args)
30
+ VCR.use_cassette(resource_id) { super(*args) }
31
+ end
32
+ end
33
+ end
@@ -9,6 +9,8 @@ module JekyllNotion
9
9
 
10
10
  return unless notion_token? && config?
11
11
 
12
+ setup
13
+
12
14
  if fetch_on_watch? || cache_empty?
13
15
  read_notion_databases
14
16
  read_notion_pages
@@ -78,7 +80,8 @@ module JekyllNotion
78
80
 
79
81
  def notion_token?
80
82
  if ENV["NOTION_TOKEN"].nil? || ENV["NOTION_TOKEN"].empty?
81
- Jekyll.logger.warn("Jekyll Notion:", "NOTION_TOKEN not provided. Cannot read from Notion.")
83
+ Jekyll.logger.warn("Jekyll Notion:",
84
+ "Cannot read from Notion becuase NOTION_TOKEN was not provided")
82
85
  return false
83
86
  end
84
87
  true
@@ -86,10 +89,26 @@ module JekyllNotion
86
89
 
87
90
  def config?
88
91
  if config.empty?
89
- Jekyll.logger.warn("Jekyll Notion:", "No config provided.")
92
+ Jekyll.logger.warn("Jekyll Notion:", "No configuration provided")
90
93
  return false
91
94
  end
92
95
  true
93
96
  end
97
+
98
+ def setup
99
+ # Cache Notion API responses
100
+ if ENV["JEKYLL_ENV"] != "test" && cache?
101
+ JekyllNotion::Cacheable.setup(config["cache_dir"])
102
+ JekyllNotion::CollectionGenerator.prepend(JekyllNotion::Cacheable)
103
+ JekyllNotion::PageGenerator.prepend(JekyllNotion::Cacheable)
104
+ JekyllNotion::DataGenerator.prepend(JekyllNotion::Cacheable)
105
+ end
106
+ end
107
+
108
+ def cache?
109
+ return true if config["cache"].nil?
110
+
111
+ config["cache"] == true.to_s
112
+ end
94
113
  end
95
114
  end
@@ -11,5 +11,9 @@ module JekyllNotion
11
11
  def generate
12
12
  raise "Do not use the AbstractGenerator class. Implement the generate method in a subclass."
13
13
  end
14
+
15
+ def resource_id
16
+ @notion_resource.id
17
+ end
14
18
  end
15
19
  end
@@ -40,10 +40,9 @@ module JekyllNotion
40
40
 
41
41
  def make_filename(page)
42
42
  if @notion_resource.collection_name == "posts"
43
- "#{page.created_time.to_date}-#{Jekyll::Utils.slugify(page.title,
44
- :mode => "latin")}.md"
43
+ "#{date_for(page)}-#{Jekyll::Utils.slugify(page.title, :mode => "latin")}.md"
45
44
  else
46
- "#{page.title.downcase.parameterize}.md"
45
+ "#{Jekyll::Utils.slugify(page.title, :mode => "latin")}.md"
47
46
  end
48
47
  end
49
48
 
@@ -61,5 +60,15 @@ module JekyllNotion
61
60
  end
62
61
  Jekyll.logger.debug("", "Props => #{collection.docs.last.data.keys.inspect}")
63
62
  end
63
+
64
+ def date_for(page)
65
+ # The "date" property overwrites the Jekyll::Document#data["date"] key
66
+ # which is the date used by Jekyll to set the post date.
67
+ DateTime.parse(page.props["date"]).to_date
68
+ rescue TypeError, NoMethodError
69
+ # Because the "date" property is not required,
70
+ # it fallbacks to the created_time which is always present.
71
+ page.created_time.to_date
72
+ end
64
73
  end
65
74
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllNotion
4
- VERSION = "2.3.2"
4
+ VERSION = "2.4.1"
5
5
  end
data/lib/jekyll-notion.rb CHANGED
@@ -5,6 +5,7 @@ require "notion"
5
5
  require "notion_to_md"
6
6
  require "logger"
7
7
  require "jekyll-notion/generator"
8
+ require "vcr"
8
9
 
9
10
  NotionToMd::Logger.level = Logger::ERROR
10
11
 
@@ -24,4 +25,5 @@ module JekyllNotion
24
25
  autoload :AbstractNotionResource, "jekyll-notion/abstract_notion_resource"
25
26
  autoload :NotionDatabase, "jekyll-notion/notion_database"
26
27
  autoload :NotionPage, "jekyll-notion/notion_page"
28
+ autoload :Cacheable, "jekyll-notion/cacheable"
27
29
  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: 2.3.2
4
+ version: 2.4.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-10-05 00:00:00.000000000 Z
11
+ date: 2023-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -58,6 +58,20 @@ dependencies:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
60
  version: '2.2'
61
+ - !ruby/object:Gem::Dependency
62
+ name: vcr
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '6.2'
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '6.2'
61
75
  - !ruby/object:Gem::Dependency
62
76
  name: bundler
63
77
  requirement: !ruby/object:Gem::Requirement
@@ -125,6 +139,7 @@ files:
125
139
  - README.md
126
140
  - lib/jekyll-notion.rb
127
141
  - lib/jekyll-notion/abstract_notion_resource.rb
142
+ - lib/jekyll-notion/cacheable.rb
128
143
  - lib/jekyll-notion/document_without_a_file.rb
129
144
  - lib/jekyll-notion/factories/database_factory.rb
130
145
  - lib/jekyll-notion/factories/page_factory.rb