Class: Jekyll::NotionImport::Post
- Inherits:
-
Object
- Object
- Jekyll::NotionImport::Post
- Defined in:
- lib/jekyll-notion-import/post.rb
Instance Method Summary collapse
- #collection_directory ⇒ Object
- #file_exists? ⇒ Boolean
- #file_name ⇒ Object
- #front_matter ⇒ Object
- #import ⇒ Object
-
#initialize(collection_name:, page:, default_front_matter:) ⇒ Post
constructor
A new instance of Post.
- #path ⇒ Object
- #publish_date ⇒ Object
- #write_file ⇒ Object
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_directory ⇒ Object
53 54 55 |
# File 'lib/jekyll-notion-import/post.rb', line 53 def collection_directory @collection_directory ||= "_#{collection_name}" end |
#file_exists? ⇒ Boolean
37 38 39 |
# File 'lib/jekyll-notion-import/post.rb', line 37 def file_exists? File.exists?(path) end |
#file_name ⇒ Object
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_matter ⇒ Object
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 |
#import ⇒ Object
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 |
#path ⇒ Object
49 50 51 |
# File 'lib/jekyll-notion-import/post.rb', line 49 def path @path ||= File.join(collection_directory, file_name) end |
#publish_date ⇒ Object
45 46 47 |
# File 'lib/jekyll-notion-import/post.rb', line 45 def publish_date @publish_date ||= Date.parse(page.props["published"]) end |
#write_file ⇒ Object
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 |