Adding `published` to Hakyll Posts

riccardoodone

Riccardo Odone

Posted on May 18, 2020

Adding `published` to Hakyll Posts

You can keep reading here or jump to my blog to get the full experience, including the wonderful pink, blue and white palette.


Last week I deployed the blog and published by mistake a post that I was keeping for the future:

I guess that was just a matter of time cause I always keep some unpublished posts around. Up until now, I was doing some hacky manual git stuff to schedule them. Not anymore!

I added support for an additional published field in the metadata of each post. The scaffolding script now adds published: false by default.

The core of the change resides in site.hs:

-  match "posts/*" $ do
+  matchMetadata "posts/*" isPublished $ do
Enter fullscreen mode Exit fullscreen mode

In particular, instead of compiling all posts, it only takes the "published" ones. A post is considered published if

  1. it does not have a published metadata field or
  2. published is true.

I could have enforced all posts to have a published field and skip 1. but I was too lazy to retrofit that 😅

Here's the implementation:

isPublished :: Metadata -> Bool
isPublished = maybe True (== "true") . lookupString "published"
Enter fullscreen mode Exit fullscreen mode

I actually did one step more and decided to consider all posts published when a HAKYLL_ENV env variable is set to development:

env <- getEnvironment
hakyll $ do
  matchMetadata "posts/*" (isDevelopmentOrPublished env) $ do
-- ...

isDevelopmentOrPublished :: [(String, String)] -> Metadata -> Bool
isDevelopmentOrPublished env metadata = isDevelopmentEnv || isPublished
  where
    isDevelopmentEnv = lookup "HAKYLL_ENV" env == Just "development"
    isPublished = maybe True (== "true") . lookupString "published" $ metadata
Enter fullscreen mode Exit fullscreen mode

That way, as long as I preview the blog with HAKYLL_ENV=development stack exec site watch, I can keep published: false while working on a new post.

Please feel free to copy the code described above and use it for your Hakyll blog!


Get the latest content via email from me personally. Reply with your thoughts. Let's learn from each other. Subscribe to my PinkLetter!

💖 💪 🙅 🚩
riccardoodone
Riccardo Odone

Posted on May 18, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Custom Markdown in Pandoc
functional Custom Markdown in Pandoc

June 8, 2020

Production Drafts for Hakyll Posts
functional Production Drafts for Hakyll Posts

May 28, 2020

Adding `published` to Hakyll Posts
functional Adding `published` to Hakyll Posts

May 18, 2020

Crossposting to DevTo via command line
functional Crossposting to DevTo via command line

January 13, 2020

Tweeting a Blog Post via command line
functional Tweeting a Blog Post via command line

January 6, 2020