Adding Twitter Cards to Hakyll Posts

Posted on April 15, 2019 by Riccardo

Intro

By default Hakyll does not add the proper meta tags for Twitter Cards. For that reason, if we try to tweet a link to a Hakyll post, no card will be rendered. We can confirm that by using Twitter’s Card Validator:

That can be fixed with a few lines of code:

The Few Lines of Code

As explained in the docs, it’s enough to add to the head of the HTML document the following stuff:

In Hakyll we can add the meta tags to templates/default.html. We just need to make sure to have them only in the post pages. To make that happen, we just check if the description key is present in the context:

And then in each post metadata we need to add description and cover_image: