jekyll-notion 2.4.4 → 3.0.0.beta1

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.
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module JekyllNotion
4
- class AbstractGenerator
5
- def initialize(notion_resource:, site:, plugin:)
6
- @notion_resource = notion_resource
7
- @site = site
8
- @plugin = plugin
9
- end
10
-
11
- def generate
12
- raise "Do not use the AbstractGenerator class. Implement the generate method in a subclass."
13
- end
14
-
15
- def resource_id
16
- @notion_resource.id
17
- end
18
- end
19
- end
@@ -1,74 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module JekyllNotion
4
- class CollectionGenerator < AbstractGenerator
5
- def generate
6
- @notion_resource.fetch.each do |page|
7
- next if file_exists?(make_path(page))
8
-
9
- collection.docs << make_doc(page)
10
- log_new_page(page)
11
- end
12
- # Caching current collection
13
- @plugin.collections[@notion_resource.collection_name] = collection
14
- end
15
-
16
- def collection
17
- @site.collections[@notion_resource.collection_name]
18
- end
19
-
20
- private
21
-
22
- # Checks if a file already exists in the site source
23
- def file_exists?(file_path)
24
- File.exist? @site.in_source_dir(file_path)
25
- end
26
-
27
- def make_doc(page)
28
- new_post = DocumentWithoutAFile.new(
29
- make_path(page),
30
- { :site => @site, :collection => collection }
31
- )
32
- new_post.content = make_md(page)
33
- new_post.read
34
- new_post
35
- end
36
-
37
- def make_path(page)
38
- "_#{@notion_resource.collection_name}/#{make_filename(page)}"
39
- end
40
-
41
- def make_filename(page)
42
- if @notion_resource.collection_name == "posts"
43
- "#{date_for(page)}-#{Jekyll::Utils.slugify(page.title, :mode => "latin")}.md"
44
- else
45
- "#{Jekyll::Utils.slugify(page.title, :mode => "latin")}.md"
46
- end
47
- end
48
-
49
- def make_md(page)
50
- NotionToMd::Converter.new(:page_id => page.id).convert(:frontmatter => true)
51
- end
52
-
53
- def log_new_page(page)
54
- Jekyll.logger.info("Jekyll Notion:", "Page => #{page.title}")
55
- if @site.config.dig(
56
- "collections", @notion_resource.collection_name, "output"
57
- )
58
- Jekyll.logger.info("",
59
- "URL => #{collection.docs.last.url}")
60
- end
61
- Jekyll.logger.debug("", "Props => #{collection.docs.last.data.keys.inspect}")
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
- Time.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
- Time.parse(page.created_time).to_date
72
- end
73
- end
74
- end
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module JekyllNotion
4
- class DataGenerator < AbstractGenerator
5
- def generate
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
12
- end
13
-
14
- private
15
-
16
- def data
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!)
43
- end
44
-
45
- def log_pages
46
- if data.is_a?(Array)
47
- data.each { |page| log_page(page, Array.to_s) }
48
- else
49
- log_page(data, Hash.to_s)
50
- end
51
- end
52
-
53
- def log_page(page, type)
54
- Jekyll.logger.info("Jekyll Notion:", "Page => #{page["title"]}")
55
- Jekyll.logger.info("", "#{type} => site.data.#{@notion_resource.data_name}")
56
- Jekyll.logger.debug("", "Props => #{page.keys.inspect}")
57
- end
58
- end
59
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module JekyllNotion
4
- class PageGenerator < AbstractGenerator
5
- def generate
6
- notion_page = @notion_resource.fetch
7
- unless notion_page.nil?
8
- page = make_page(notion_page)
9
- @site.pages << page
10
- log_page(notion_page)
11
- @plugin.pages << page
12
- end
13
- end
14
-
15
- def make_page(notion_page)
16
- JekyllNotion::PageWithoutAFile.new(@site, @site.source, "", "#{notion_page.title}.md",
17
- make_md)
18
- end
19
-
20
- def log_page(notion_page)
21
- Jekyll.logger.info("Jekyll Notion:", "Page => #{notion_page.title}")
22
- Jekyll.logger.info("", "URL => #{@site.pages.last.url}")
23
- Jekyll.logger.debug("", "Props => #{notion_page.props.keys.inspect}")
24
- end
25
-
26
- def make_md
27
- NotionToMd::Converter.new(:page_id => @notion_resource.id).convert(:frontmatter => true)
28
- end
29
- end
30
- end
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module JekyllNotion
4
- class NotionDatabase < AbstractNotionResource
5
- # Returns an empty array or a NotionToMd:Page array
6
- def fetch
7
- return [] unless id?
8
-
9
- @fetch ||= @notion.database_query(query)[:results].map do |page|
10
- NotionToMd::Page.new(:page => page, :blocks => build_blocks(page.id))
11
- end
12
- end
13
-
14
- def filter
15
- config["filter"]
16
- end
17
-
18
- def sorts
19
- if config["sort"]
20
- Jekyll.logger.warn("Jekyll Notion:", "sort property is deprecated, use sorts instead")
21
- end
22
- config["sorts"]
23
- end
24
-
25
- def collection_name
26
- config["collection"] || "posts"
27
- end
28
-
29
- def data_name
30
- config["data"]
31
- end
32
-
33
- private
34
-
35
- def query
36
- { :database_id => id, :filter => filter, :sorts => sorts }.compact
37
- end
38
- end
39
- end
@@ -1,21 +0,0 @@
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({ :page_id => id }),
10
- :blocks => build_blocks(id))
11
- end
12
-
13
- def data_name
14
- config["data"]
15
- end
16
-
17
- def collection_name
18
- nil
19
- end
20
- end
21
- end