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



74
75
76
# File 'lib/jekyll-notion/generator.rb', line 74

def collection
  @collection ||= @site.collections[collection_name]
end

#collection_nameObject



70
71
72
# File 'lib/jekyll-notion/generator.rb', line 70

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

#configObject



78
79
80
# File 'lib/jekyll-notion/generator.rb', line 78

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

#config?Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
# File 'lib/jekyll-notion/generator.rb', line 90

def config?
  if config.empty?
    Jekyll.logger.warn("Jekyll Notion:", "No config provided.")
    return false
  end
  true
end

#config_frontmatterObject



58
59
60
# File 'lib/jekyll-notion/generator.rb', line 58

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



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

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, notion_frontmatter)
  page_frontmatter = data.to_a.map { |k, v| "#{k}: #{v}" }.join("\n")
  <<~CONTENT
    ---
    #{page_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_frontmatterObject



48
49
50
51
52
53
54
55
56
# File 'lib/jekyll-notion/generator.rb', line 48

def notion_frontmatter
  {
    :id    => current_page.id,
    :title => current_page.title,
    :date  => current_page.created_datetime,
    :cover => current_page.cover,
    :icon  => current_page.icon,
  }
end

#notion_token?Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
# File 'lib/jekyll-notion/generator.rb', line 82

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

#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