Embedding youtube videos with Hakyll

A while ago a made a timelapse of my entry to Ludum Dare. This is how I parse and embed videos using Hakyll.

I don’t want to remember any special syntax for embedding, I don’t want to bother (remember?) to use something like {% youtube: <link> %} or whatever syntax we might find pretty. The simplest solution I could think of is simply to have a bare link separated by newlines:

Some text...

<link>

Some other text...

Would embed <link> as a video. A youtube link seems to always start like: https://www.youtube.com/watch?v= which we can parse with a simple regex. If we run this after compilation the link will also be surrounded by <p> tags.

Edit: The solution above does not handle unicode in the document before the video. See an update here.

The rendering part is not pretty and I’m sure one could move it to a template somehow.

We can use with:

And use it right after we compile:

I did not write this code originally, I found it on someone else’s blog, but I managed to loose the link and now I can’t find it.

The styling of the video is from another blog post:

Now in the end this:

https://www.youtube.com/watch?v=NIbr-mLi4DU

Will be transformed to:

The full source can be found on github.