Since I have converted my blog to Zola, I started using assets a little bit more in the posts. I am still not sure if it is the right thing, but for now it helps me convey information in addition to text.
To embed a photo into the post, there are at least two main options available in Zola: static assets and asset colocation. Using static assets is more suitable for icons and logos, generally for assets that are shared among multiple posts, so we explore the second option, the asset colocation.
The colocation roughly means that the page and assets it requires are
located in the same directory tree. Usually, the post is a .md
file
located somewhere near the top of the content/
directory. Does it mean we
should place photos there? Wouldn't it be a mess, so many markdown files
and images together? Well this is the reason to make a dedicated
directories. The exact same page can exist in the two locations (but not
both). Either as a standalone file:
content/is-emacs-better-than-vim.md
Or in the dedicated directory, as an index.md
:
content/is-emacs-better-than-vim/index.md
Colocating an asset simply means that the image is placed in the posts dedicated folder alongside it and referenced from the post:
content/is-emacs-better-than-vim/index.md
content/is-emacs-better-than-vim/raging-flamewar-photo.jpg
There is more going on under the hood, but Zola docs have it covered. What I want to focus on is the process of getting from the first option to the second one. This happens to me, when I want to add the picture into the existing older post when updating it, or when I start writing the post and then I realize I need to add an asset to it. Basically, it happens quite often.
What needs to be done then are these three steps in succession:
- Get the filename of the post without it's
.md
extension - Create a folder with the name of the file (if output paths are used for slugs)
- Move the post inside it as
index.md
I got annoyed the first time I had to do this by hand, so I wrote this script to do the "folderize" steps for me:
#!/bin/bash
fullname=$(basename -- "$1")
filename="${fullname%.*}"
mkdir "$filename"
mv "$fullname" "$filename/index.md"
Put this script into the content/
where all your .md
files are located
as folderize.sh
and make it executable via chmod +x folderize.sh
. It
can then be run as follows:
./folderize is-emacs-better-than-vim.md
There is one last thing that has to be done - making sure this script won't
get copied into the public/
folder when running zola build
. For this
purpose, the ignored_content
configuration option
is available for config.toml
:
ignored_content = ["*.sh"]
That's it!