Drafts in Hakyll

Posted on September 27, 2020
Tags: , ,

Creating Drafts in Hakyll

Migrating from Hugo to Hakyll, one thing that I missed was the out of box support for drafts. Lot of times I start to write a post forget and accidentally push it. Then in order to save myself from embarassment I will delete the draft post in the process forget about the draft post.

This weekend I have been spending quite sometime trying to setup a conduisive enviroment for me to write posts and save drafts.

Alot of the approaches on the internet for draft posts was to create a drafts folder and add logic to ignore the directory in site.hs. While this is a nice approach I was going more for a field in the metadata context. Reason I favored a new field was

  1. I don’t have to move posts, which knowing myself very likely I would forget.
  2. It is a lot easier to just add published field in the metadata of the post I am writing, if I am unable to complete it. Otherwise I would have to remember to move the draft post out to the drafts folder, that is just tooooo much work.

I found a SO1 suggestion about changing match to matchMetadata that lets you read the fields in the metadata.

So making the following change to the site.hs will filter out the posts where the draft field is set to true.

...
 matchMetadata "posts/*" (not . isDraft) $ do
 ...
 
---
isDraft :: Metadata -> Bool
isDraft = maybe False (=="true") . lookupString "draft"

After making the changes run stack build and then stack exec site rebuild


  1. StackExchange Post↩︎