Cleansing Jekyll YAML front-matter dates

15 Feb 2016

When updating one of my static websites to the Heroku Cedar stack I took the opportunity to update Jekyll to the latest version, currently 1.3.4.

After upgrading, I regenerated my site from scratch to make sure everything was working. Unfortunately, there seems to have been some changes that break the generation of posts because of a YAML date parsing oddity. This causes your posts to be given the wrong date: +11 hours for me, as my local timezone is GMT+11 (Sydney, Australia), so in some cases my posts appeared to have been authored a day later than reality.

While the Jekyll maintainers debate how to rectify the problem, I decided to take matters into my own hands and just quote all dates in my site’s YAML front matter.

To achieve this feat, I wrote a quick bash script to modify all the posts in place:

#!/bin/bash

all_markdown_posts() {
  find _posts/ -iname '*.md'
}

fix_date_yaml() {
  sed -i '' -e "s/^date\: \(.*\)$/date: \'\1\'/" $1
}

main() {
  for md_file in $(all_markdown_posts); do
    fix_date_yaml $md_file
  done
}

main

Run this from inside your static site’s root directory, (or modify the all_markdown_posts() function to return a list of the markdown files you want altered).

Your files will end up looking like this:

...
date: '2013-02-01 16:54:17'
...

And Jekyll will generate your posts with the correct date.