May 3, 2014

Creating well-behaved Hakyll blog posts

Posts in a Hakyll-powered blog need to be created with care, if you want your feed to work well with clients and aggregators. There are many things to remember:

If you have clones of your site, decide which one to work in and make sure it’s up to date

Create the file in the right place

Name it consistently (I use YYYY-MM-DD-url-safe-title.md)

In my setup, prefix it with _ if it’s a draft (I render but don’t publish those)

Set title, tags, and author with a metadata block

Set published time with metadata to get a more precise timestamp than Hakyll can guess from the filename. Include a time zone. Use the right format.

When moving a post from draft to published: Update the published time Update the file name if the title or publish date has changed

If changing a post after it has been published: set updated time in the metadata

At some point, commit it to version control and sync it to other clones

I find this makes blogging feel tedious, especially after an absence when I’ve forgotten the details. Case in point, I managed to share an ugly template post with Planet Haskell readers while working on this one.

So I’m trying out this bash shell script, maybe it will help. Adjust to suit your setup.

(updated 2014/8/27)

# add to ~/.bashrc BLOGDIR= ~/src/MYSITE.com/blog # List recent blog posts. alias blog-ls= "ls $BLOGDIR | tail -10" # Create a new hakyll-compatible draft blog post. # blog-new ["The Title" ["tag1, tag2" ["Author Name"]]] function blog-new() { ( TITLE=${1:- Default Title } TAGS=${2:- defaulttag1, defaulttag2 } AUTHOR=${3:- Default Author Name } SLUG=${TITLE// / - } DATE= `date + "%Y-%m-%d" ` TIME= `date + "%Y-%m-%d %H:%M:%S%Z" ` FILE= _ $DATE - $SLUG .md echo creating $BLOGDIR / $FILE cat <<EOF >> $BLOGDIR / $FILE && emacsclient -n $BLOGDIR / $FILE --- title: $TITLE tags: $TAGS author: $AUTHOR published: $TIME --- EOF ) }

An example:

$ blog-new 'Scripted Hakyll blog post creation' 'hakyll, haskell' creating _2014-05-03-Scripted-Hakyll-blog-post-creation.md (file opens in emacs, edit & save) $ make ./site build Initialising... Creating store... Creating provider... Running rules... Checking for out-of-date items Compiling updated blog/_2014-05-03-Scripted-Hakyll-blog-post-creation.md Success

See also part 2.

JavaScript not enabled, comments will not be shown.