Class: JekyllNotion::Generator
- Inherits:
-
Jekyll::Generator
- Object
- Jekyll::Generator
- JekyllNotion::Generator
- Defined in:
- lib/jekyll-notion/generator.rb
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
Instance Method Summary collapse
- #collection ⇒ Object
- #collection_name ⇒ Object
- #config ⇒ Object
- #config? ⇒ Boolean
- #config_frontmatter ⇒ Object
- #generate(site) ⇒ Object
- #make_filename ⇒ Object
- #make_frontmatter ⇒ Object
- #make_md ⇒ Object
- #make_page ⇒ Object
- #notion_token? ⇒ Boolean
- #page_frontmatter ⇒ Object
- #read_notion_database ⇒ Object
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
5 6 7 |
# File 'lib/jekyll-notion/generator.rb', line 5 def current_page @current_page end |
Instance Method Details
#collection ⇒ Object
68 69 70 |
# File 'lib/jekyll-notion/generator.rb', line 68 def collection @site.collections[collection_name] end |
#collection_name ⇒ Object
64 65 66 |
# File 'lib/jekyll-notion/generator.rb', line 64 def collection_name config.dig("database", "collection") || "posts" end |
#config ⇒ Object
72 73 74 |
# File 'lib/jekyll-notion/generator.rb', line 72 def config @config ||= @site.config["notion"] || {} end |
#config? ⇒ Boolean
84 85 86 87 88 89 90 |
# File 'lib/jekyll-notion/generator.rb', line 84 def config? if config.empty? Jekyll.logger.warn("Jekyll Notion:", "No config provided.") return false end true end |
#config_frontmatter ⇒ Object
52 53 54 |
# File 'lib/jekyll-notion/generator.rb', line 52 def config_frontmatter config.dig("database", "frontmatter") || {} end |
#generate(site) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/jekyll-notion/generator.rb', line 7 def generate(site) @site = site return unless notion_token? && config? read_notion_database end |
#make_filename ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/jekyll-notion/generator.rb', line 56 def make_filename if collection_name == "posts" "#{current_page.created_date}-#{current_page.title.downcase.parameterize}.md" else "#{current_page.title.downcase.parameterize}.md" end end |
#make_frontmatter ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/jekyll-notion/generator.rb', line 38 def make_frontmatter data = Jekyll::Utils.deep_merge_hashes(config_frontmatter, page_frontmatter) frontmatter = data.to_a.map { |k, v| "#{k}: #{v}" }.join("\n") <<~CONTENT --- #{frontmatter} --- CONTENT end |
#make_md ⇒ Object
34 35 36 |
# File 'lib/jekyll-notion/generator.rb', line 34 def make_md NotionToMd::Converter.new(:page_id => current_page.id).convert end |
#make_page ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/jekyll-notion/generator.rb', line 24 def make_page new_post = DocumentWithoutAFile.new( "#{Dir.pwd}/_#{collection_name}/#{make_filename}", { :site => @site, :collection => collection } ) new_post.content = "#{make_frontmatter}\n\n#{make_md}" new_post.read new_post end |
#notion_token? ⇒ Boolean
76 77 78 79 80 81 82 |
# File 'lib/jekyll-notion/generator.rb', line 76 def notion_token? if ENV["NOTION_TOKEN"].nil? || ENV["NOTION_TOKEN"].empty? Jekyll.logger.warn("Jekyll Notion:", "NOTION_TOKEN not provided. Cannot read from Notion.") return false end true end |
#page_frontmatter ⇒ Object
48 49 50 |
# File 'lib/jekyll-notion/generator.rb', line 48 def page_frontmatter Jekyll::Utils.deep_merge_hashes(current_page.custom_props, current_page.default_props) end |
#read_notion_database ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/jekyll-notion/generator.rb', line 15 def read_notion_database @db = NotionDatabase.new(:config => config) @db.pages.each do |page| @current_page = page collection.docs << make_page Jekyll.logger.info("Jekyll Notion:", "New notion page at #{collection.docs.last.path}") end end |