One thing that I instantly loved about Jekyll is that it encourages you to keep a strict and tidy folder structure in your project: you have your posts, layouts, styles, includes, collections and pages, and each one of these will go in a different folder to keep concerns separate. But what about the assets, namely images, that you want to include with your posts? Will you keep piling them in your assets folder? I have a better idea.
This post attemps to solve two problems. Firstly, I wanted to display a caption for every image I included on a post. While displaying an image is very straightforward with Markdown, I would have to do some fiddling to throw a caption in the mix, especially because I wanted to use HTML5’s
<figcaption>, so I didn’t have much choice but to include plain HTML code every time I wanted to show an image. That would mean a combination of two things I hate:
- writing ugly code, since I would be polluting my Markdown posts with HTML code which kind of defeats the point of using Markdown in the first place, and
- I would be repeating A LOT of code, A LOT of times, and let me tell you that I like my code how I like my feet: DRY - boy, I should be quoted on this!
The second problem we attempt to solve is file organisation. Where to store the images? It looks ugly to use a folder for all post images, or even worse, have them in the same folder where you have your layout images. File names will get messy at some point, and you’ll eventually try to save a picture of a nice calendar (date.jpg) on the same folder you have that photo you took on your first day with Josephine (date.jpg). You see my point.
I would prefer a
posts folder with a sub-folder for every post. Something like this:
So how to keep the files organised in a structure like this while maintaining the code clean, short and maintainable? I created a file called
image (I didn’t use the .html extension, but maybe I should) inside my
_includes folder that looks like this:
This file expects the image name, the caption and the alt text (the last two are optional), figures out the image path name (line 1) by slugifying the concatenation of the post date, the title and the image name, and based on the arguments passed write either a
<figcaption> or a plain old
So now when I want to include an image in a post I just add:
And then the magic happens. This could be extended for other types of media, like videos. ∎