Class: JekyllNotion::Generator

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll-notion/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_pageObject (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

#collectionObject



68
69
70
# File 'lib/jekyll-notion/generator.rb', line 68

def collection
  @site.collections[collection_name]
end

#collection_nameObject



64
65
66
# File 'lib/jekyll-notion/generator.rb', line 64

def collection_name
  config.dig("database", "collection") || "posts"
end

#configObject



72
73
74
# File 'lib/jekyll-notion/generator.rb', line 72

def config
  @config ||= @site.config["notion"] || {}
end

#config?Boolean

Returns:

  • (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_frontmatterObject



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_filenameObject



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_frontmatterObject



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_mdObject



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_pageObject



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

Returns:

  • (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_frontmatterObject



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_databaseObject



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