generate jekyll front-matter with bash
Creating a post in a jekyll-powered blog is pretty straightforward. Just create a file in markdown format and put your post in. Every post has a special YAML Front Matter block with some variables. Using a bash script we can easily generate this file with proper values for the YAML variables. For example, for this post, the filename contains the post date and title.
The same values (date, title) are contained in the YAML block inside the file, but a bit more formatted. A Bash script can take care the formatting for us by only supplying the post title as the script argument. This assumes that the date (and optionally time) are set to the current time we run the bash script.
First we need to grab the date and optionaly the time.
_date will be used for the filename and _datetime for the date variable.
We also need to replace whitespace with dashes in the post title, for use in the filename.
The filename is supplied a an argument, so we might want to bail if the user does not supply one
Also, another safe switch would be to check if the file already exists to prevent accidental overwrites.
To do that we need the absolute path for our post file. Putting all these together we have:
Finally, the magic code that creates our post file uses here documents
Make sure to avoid quoting of the EOF as bash variable subsitution won’t work. Also, note that I used the >| notation because I have the bash noclobber variable set.
The jekyll-post script is on github, also added some code to prompt user for launching a text editor.