Class: Jekyll::NotionImport::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-notion-import/post.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection_name:, page:, default_front_matter:) ⇒ Post

Returns a new instance of Post.



6
7
8
9
10
# File 'lib/jekyll-notion-import/post.rb', line 6

def initialize(collection_name:, page:, default_front_matter:)
  @page = page
  @collection_name = collection_name
  @default_front_matter = default_front_matter
end

Instance Method Details

#collection_directoryObject



53
54
55
# File 'lib/jekyll-notion-import/post.rb', line 53

def collection_directory
  @collection_directory ||= "_#{collection_name}"
end

#file_exists?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/jekyll-notion-import/post.rb', line 37

def file_exists?
  File.exists?(path)
end

#file_nameObject



41
42
43
# File 'lib/jekyll-notion-import/post.rb', line 41

def file_name
  @filename ||= "#{publish_date.strftime("%Y-%m-%d")}-#{Jekyll::Utils.slugify(page.title)}.md"
end

#front_matterObject



57
58
59
60
61
# File 'lib/jekyll-notion-import/post.rb', line 57

def front_matter
  @front_matter ||= YAML.dump(
    Hash(default_front_matter).merge(page.props.transform_values {|v| v.is_a?(DateTime) ? v.iso8601 : v})
  ) + "---\n\n"
end

#importObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jekyll-notion-import/post.rb', line 12

def import
  unless Dir.exist?(collection_directory)
    Jekyll.logger.info("Jekyll Notion Import:", "Create #{collection_directory}")
    FileUtils.mkdir_p(collection_directory)
  end


  if file_exists?
    file_last_updated = DateTime.parse(YAML.load(File.read(path))["last_edited_time"])

    return if file_last_updated >= page.last_edited_time
  end

  Jekyll.logger.info("Jekyll Notion Import:", "Import #{file_name}")

  write_file
end

#pathObject



49
50
51
# File 'lib/jekyll-notion-import/post.rb', line 49

def path
  @path ||= File.join(collection_directory, file_name)
end

#publish_dateObject



45
46
47
# File 'lib/jekyll-notion-import/post.rb', line 45

def publish_date
  @publish_date ||= Date.parse(page.props["published"])
end

#write_fileObject



30
31
32
33
34
35
# File 'lib/jekyll-notion-import/post.rb', line 30

def write_file
  File.open(path, "w") do |f|
    f.puts(front_matter)
    f.puts(page.body)
  end
end