jekyll-notion 0.1.5 → 0.2.2

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: e46b38a04459bcc270b3dd99729eb34e4c7275393c3097cf92af526845e6f94e
4
- data.tar.gz: 100eb193c3ee037ca004a8e6b1a3b2bd2a52e2d727484f557edc2a2a0b1edd04
3
+ metadata.gz: 8ec8ddd6c2aceafbf5c872bcd4861f86dde3907ffbda6ec929a26c141a14bc7e
4
+ data.tar.gz: bbe31e585b82abd2df50c1ef7ec3811a37b3d84b0ca99d0ed97d26d887cb1392
5
5
  SHA512:
6
- metadata.gz: 25b24f800326c681fb40e28111f6b7662924679418b37d3e2113327061cddc2171a57d46abf2c3fbfaa42228a8f828106aa0ad2fa69f68df507a63c8f5d6cbbb
7
- data.tar.gz: d6fdce97c3cd46102b7e493787ee90b057f7c3ee254308f6cf97b9d7fe1e2f975c6969e73b1dd8426fb0d6436aa89446eb918f4b57ffeddcc4717074c9e95926
6
+ metadata.gz: f4949323862e1efc8ad2901260f83388a1ad443035f8748b1a4d365720773f59c7704ae8b64fcedd167cc86619b1414e6e25519591a1064bd5c1e79c31e5b751
7
+ data.tar.gz: 53f3d8a98060fbd64e9395327c79023fb75df778e3f52c1e0a6399e479cea7d7be133baac85c0622ea9d943afe32353c3707465f733e5a3e3f667fde37bf8ea7
data/README.md CHANGED
@@ -32,13 +32,48 @@ 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
+ collections:
49
+ - recipes
50
+ - films
51
+
52
+ notion:
53
+ databases:
54
+ - id: b0e688e1-99af-4295-ae80-b67eb52f2e2f
55
+ - id: 2190450d-4cb3-4739-a5c8-340c4110fe21
56
+ collection: recipes
57
+ - id: e42383cd-4975-4897-b967-ce453760499f
58
+ collection: films
59
+ ```
60
+
61
+ When no collection is defined, the `posts` collection is used by default.
62
+
63
+ ### Database options
64
+
65
+ Each dabatase support the following options.
66
+
67
+ * `id`: the notion database unique identifier,
68
+ * `collection`: the collection each page belongs to (posts by default),
69
+ * `filter`: the database query filter,
70
+ * `sort`: the database query sort,
71
+ * `frontmatter`: additional front matter to append to each page in the collection.
72
+
73
+ ```yml
74
+ notion:
75
+ database:
76
+ id: e42383cd-4975-4897-b967-ce453760499f
42
77
  collection: posts
43
78
  filter: { "property": "Published", "checkbox": { "equals": true } }
44
79
  sort: { "property": "Last ordered", "direction": "ascending" }
@@ -46,47 +81,51 @@ notion:
46
81
  layout: post
47
82
  ```
48
83
 
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.
84
+ 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
85
 
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.
86
+ ### Watch
57
87
 
58
- Note: Only one notion database is available.
88
+ By default, databases are only requested during the first build. Subsequent builds use the results from the cache.
89
+
90
+ Set `fetch_on_watch` to true to allow request on each rebuild.
91
+
92
+ ```yml
93
+ notion:
94
+ fetch_on_watch: true
95
+ database:
96
+ id: e42383cd-4975-4897-b967-ce453760499f
97
+ ```
59
98
 
60
99
  And that's all. Each page in the notion database will be included in the selected collection.
61
100
 
62
101
  ## Notion properties
63
102
 
64
- Below, page notion default properties are set in each page frontmatter.
103
+ Below, default properties per notion page are set for each document front matter.
65
104
 
66
- Default properties include `title`, created_time`, `last_edited_time`, `icon` and `cover.
105
+ Notion properties include page `id`, `title`, `created_time`, `last_edited_time`, `icon` and `cover`.
67
106
 
68
107
  ```
69
108
  ---
70
- id: b2998...
71
- title: A title
109
+ id: e42383cd-4975-4897-b967-ce453760499f
110
+ title: An amazing post
72
111
  cover: https://img.bank.sh/an_image.jpg
73
112
  date: 2022-01-23T12:31:00.000Z
74
113
  icon: \U0001F4A5
75
114
  ---
76
115
  ```
77
116
 
78
- Any property provided in the frontmatter config that matches a default property will be overwritten by the default value.
117
+ Default properties prevail over custom properties declared in the front matter config.
79
118
 
80
119
  ### Custom properties
81
120
 
82
121
  In addition to default properties, custom properties are also supported.
83
122
 
84
- Custom properties are appended to page frontmatter by default. Every property name is snake-cased.
123
+ Custom properties are appended to the page frontmatter by default. Every property name are downcased and snake-cased.
85
124
  For example, two properties named `Multiple Options` and `Tags` will be transformed to `multiple_options` and `tags`, respectively.
86
125
 
87
126
  ```
88
127
  ---
89
- id: b2998...
128
+ id: 2190450d-4cb3-4739-a5c8-340c4110fe21
90
129
  title: A title
91
130
  cover: https://img.bank.sh/an_image.jpg
92
131
  date: 2022-01-23T12:31:00.000Z
@@ -96,7 +135,7 @@ multiple_options: option1, option2
96
135
  ---
97
136
  ```
98
137
 
99
- The supported properties are:
138
+ The supported property types are:
100
139
 
101
140
  * `number`
102
141
  * `select`
@@ -2,50 +2,79 @@
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
+ next if file_exists?(make_path)
27
+
28
+ current_collection.docs << make_page
29
+ log_new_page
30
+ end
31
+ # Store current collection
32
+ collections[current_db.collection] = current_collection
25
33
  end
26
- @docs = collection.docs
27
34
  end
28
35
 
29
- def docs
30
- @docs ||= []
36
+ def databases
37
+ config["databases"] || [config["database"]]
38
+ end
39
+
40
+ def collections
41
+ @collections ||= {}
42
+ end
43
+
44
+ # Checks if a file already exists in the site source
45
+ def file_exists?(file_path)
46
+ File.exist? @site.in_source_dir(file_path)
31
47
  end
32
48
 
33
49
  def make_page
34
50
  new_post = DocumentWithoutAFile.new(
35
- "#{Dir.pwd}/_#{collection_name}/#{make_filename}",
36
- { :site => @site, :collection => collection }
51
+ make_path,
52
+ { :site => @site, :collection => current_collection }
37
53
  )
38
54
  new_post.content = "#{make_frontmatter}\n\n#{make_md}"
39
55
  new_post.read
40
56
  new_post
41
57
  end
42
58
 
59
+ def make_path
60
+ "_#{current_db.collection}/#{make_filename}"
61
+ end
62
+
63
+ def make_filename
64
+ if current_db.collection == "posts"
65
+ "#{current_page.created_date}-#{Jekyll::Utils.slugify(current_page.title,
66
+ :mode => "latin")}.md"
67
+ else
68
+ "#{current_page.title.downcase.parameterize}.md"
69
+ end
70
+ end
71
+
43
72
  def make_md
44
73
  NotionToMd::Converter.new(:page_id => current_page.id).convert
45
74
  end
46
75
 
47
76
  def make_frontmatter
48
- data = Jekyll::Utils.deep_merge_hashes(config_frontmatter, page_frontmatter)
77
+ data = Jekyll::Utils.deep_merge_hashes(current_db.frontmatter, page_frontmatter)
49
78
  frontmatter = data.to_a.map { |k, v| "#{k}: #{v}" }.join("\n")
50
79
  <<~CONTENT
51
80
  ---
@@ -58,25 +87,8 @@ module JekyllNotion
58
87
  Jekyll::Utils.deep_merge_hashes(current_page.custom_props, current_page.default_props)
59
88
  end
60
89
 
61
- def config_frontmatter
62
- config.dig("database", "frontmatter") || {}
63
- end
64
-
65
- def make_filename
66
- if collection_name == "posts"
67
- "#{current_page.created_date}-#{Jekyll::Utils.slugify(current_page.title,
68
- :mode => "latin")}.md"
69
- else
70
- "#{current_page.title.downcase.parameterize}.md"
71
- end
72
- end
73
-
74
- def collection_name
75
- config.dig("database", "collection") || "posts"
76
- end
77
-
78
- def collection
79
- @site.collections[collection_name]
90
+ def current_collection
91
+ @site.collections[current_db.collection]
80
92
  end
81
93
 
82
94
  def config
@@ -102,5 +114,16 @@ module JekyllNotion
102
114
  end
103
115
  true
104
116
  end
117
+
118
+ def log_new_page
119
+ Jekyll.logger.info("Jekyll Notion:", "Page => #{current_page.title}")
120
+ if @site.config.dig(
121
+ "collections", current_db.collection, "output"
122
+ )
123
+ Jekyll.logger.info("",
124
+ "Path => #{current_collection.docs.last.path}")
125
+ end
126
+ Jekyll.logger.debug("", "Props => #{page_frontmatter.keys.inspect}")
127
+ end
105
128
  end
106
129
  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
 
@@ -87,7 +87,7 @@ module JekyllNotion
87
87
  end
88
88
 
89
89
  def files(prop)
90
- files = prop.files.map { |f| f.file.url }.join(", ")
90
+ files = prop.files.map { |f| "\"#{f.file.url}\"" }.join(", ")
91
91
  "[#{files}]"
92
92
  end
93
93
 
@@ -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.5"
4
+ VERSION = "0.2.2"
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.5
4
+ version: 0.2.2
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-03 00:00:00.000000000 Z
11
+ date: 2022-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport