Generate individual tag/category/author pages for your Jekyll website21 Mar 2021
I’ve been migrating more and more of my websites to Jekyll static sites, from WordPress. The advantages are clear:
- version management with
gitsuits me (it’s how I develop software).
- deployment/hosting with GitHub Pages is very easy and can be automated
- the security of having a static site (i.e. no visitor/hacker can change it) is a relief
- sure, WordPress has plugins and comments, but that has become more of a burden than a feature for me.
One thing that I still had to solve was tag and category indexes. WordPress does a great job of creating index pages for years, months, categories and tags. If your post uses the tag ‘apple’, it also shows up in the index page for the tag ‘apple’. All these pages help your SEO a lot.
Jekyll, on the other hand, only generates a web page for each .md or .html in the source. To get 100 different pages for 100 different tags, there need to be 100 source documents. No one wants to add/edit these by hand. Surely this could be automated, right? Thanks to my bashew bash script template, this was only one evening’s work.
jekyll_taxonomy generate tag do?
- it creates a (minimal) layout for tag indexes in
_layouts/with_tag.htmlthat can later be modified.
- it creates a (minimal) function tag listing page in
tag/index.htmlthat can later be modified.
- it cleans all
--cleanupwas specified (necessary when tags were removed/renamed)
- it looks up all tags used in
_posts/*.mdfront matter (as
tags:) and slugifies them (lower case, no spaces or funny characters). It then creates a
[tag].mdfile for each tag that uses the
- this works for tags and categories (obviously) but also for author, color, gender, country … or any other keyword you’ve defined in your posts’ front matter.
- it’s a bash script with no external dependencies, so it runs on MacOS, Linux (Ubuntu/Fedora/…), and Windows Subsytem for Linux (WSL1/2)
This is the script I’ve created to generate all my tags and categories for this blog in one go. The whole process (+- 600 posts, 50 categories, 500 tags) generates the pages in 15 seconds.
jekyll_taxonomy -c generate category ## recreate all category index pages git add category ## add any new category indexes to git jekyll_taxonomy -c generate tag ## recreate all tag index pages git add tag ## add any new tag indexes to git git status ## show me all added/removed tags and categories
Jekyll will now have to generate an extra number (in my case: +- 550) of pages, and since all of these go through all your posts, the Jekyll generation process takes a lot longer, in my case up to 80 seconds.