diff --git a/lib/micropub/post.rb b/lib/micropub/post.rb index 90ad3ae..f4ecff8 100644 --- a/lib/micropub/post.rb +++ b/lib/micropub/post.rb @@ -1,4 +1,5 @@ require "date" +require "yaml" require "kramdown" module Micropub @@ -67,13 +68,18 @@ module Micropub end end + def post_frontmatter + { + "title" => title, + "date" => published.rfc3339, + "layout" => "micropost", + "categories" => categories, + }.compact.to_yaml + end + def post_content <<~POST - --- - date: '#{published.rfc3339}' - layout: micropost - categories: - #{categories.map { |category| "- #{category}\n" }.join.strip} + #{post_frontmatter.strip} --- #{content} diff --git a/test/lib/micropub/models/post_test.rb b/test/lib/micropub/models/post_test.rb index 39e57f8..1599602 100644 --- a/test/lib/micropub/models/post_test.rb +++ b/test/lib/micropub/models/post_test.rb @@ -205,5 +205,28 @@ describe Micropub::Post do Hallo, Earth! POST end + + it "returns a post formatted for hugo with a title" do + post = Micropub::Post.new( + "title" => "Welcome!", + "content" => "Hallo, Earth!", + "published" => "2019-11-12", + "category" => ["one", "two", "three"], + ) + + _(post.post_content).must_equal <<~POST + --- + title: Welcome! + date: '2019-11-12T00:00:00+00:00' + layout: micropost + categories: + - one + - two + - three + --- + + Hallo, Earth! + POST + end end end