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



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

def collection
  @site.collections[collection_name]
end

#collection_nameObject



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

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

#configObject



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

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

#config?Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
# File 'lib/jekyll-notion/generator.rb', line 98

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

#config_frontmatterObject



61
62
63
# File 'lib/jekyll-notion/generator.rb', line 61

def config_frontmatter
  config.dig("database", "frontmatter") || {}
end

#docsObject



29
30
31
# File 'lib/jekyll-notion/generator.rb', line 29

def docs
  @docs ||= []
end

#fetch_on_watch?Boolean

Returns:

  • (Boolean)


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

def fetch_on_watch?
  config["fetch_on_watch"].present?
end

#generate(site) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/jekyll-notion/generator.rb', line 7

def generate(site)
  @site = site

  return unless notion_token? && config?

  if fetch_on_watch? || docs.empty?
    read_notion_database
  else
    collection.docs = docs
  end
end

#make_filenameObject



65
66
67
68
69
70
71
72
# File 'lib/jekyll-notion/generator.rb', line 65

def make_filename
  if collection_name == "posts"
    "#{current_page.created_date}-#{Jekyll::Utils.slugify(current_page.title,
                                                          :mode => "latin")}.md"
  else
    "#{current_page.title.downcase.parameterize}.md"
  end
end

#make_frontmatterObject



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

def make_frontmatter
  data = Jekyll::Utils.deep_merge_hashes(config_frontmatter, page_frontmatter)
  frontmatter = data.to_a.map { |k, v| "#{k}: #{v}" }.join("\n")
  "    ---\n    \#{frontmatter}\n    ---\n  CONTENT\nend\n"

#make_mdObject



43
44
45
# File 'lib/jekyll-notion/generator.rb', line 43

def make_md
  NotionToMd::Converter.new(:page_id => current_page.id).convert
end

#make_pageObject



33
34
35
36
37
38
39
40
41
# File 'lib/jekyll-notion/generator.rb', line 33

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)


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

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



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

def page_frontmatter
  Jekyll::Utils.deep_merge_hashes(current_page.custom_props, current_page.default_props)
end

#read_notion_databaseObject



19
20
21
22
23
24
25
26
27
# File 'lib/jekyll-notion/generator.rb', line 19

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
  @docs = collection.docs
end