Jekyll blog posts are simply text files written in Markdown. But, they need to be named a certain way and they need to include specific metadata, known as “front matter,” at the beginning of the file. I’ve been rekindling my love affair with Ruby lately and figured I’d write a silly little script to help me create new posts since I can never remember the file naming convention or how to craft the required front matter. I think there might already be a gem out there named Jekyll Compose that does something similar. But, since I’m looking for Ruby projects to work on I figured it would be more fun to write something from scratch.
For some work projects in the past I’ve used Python and Jinja templates for this kind of thing. So I figured the Ruby analog to Jinja – ERB – would work well. Here’s what I came up with.
blog_post_template.md.erb ERB template, which the above creator script uses:
--- layout: post title: <%= blog_post_title %> date: <%= timestamp %> author: twitter: mps75 tags: <%= tags %> ---
Here’s what it looks like to run the script:
Lastly, here’s what the outputted blog post file looks like (in the above example the file name is “2018-11-21-omg-new-post.md”):
--- layout: post title: OMG new post date: 2018-11-21 author: twitter: mps75 tags: ["blog", "test"] ---
Now the hard part – get some content in there….