How to implement proper blog tagging with Jekyll24 Jun 2015
I can't imagine blog without tagging. Unfortunatelly, out-of-the box tagging support in Jekyll is very limited. But Jekyll is very extensible which makes it possible to implement tagging.
My goals for the tagging in blog:
- To be able to tag any post with any amount of tags which I can invent on-the-fly
- To have a list of all tagged posts grouped by tags
- To print a list of tags under each tagged post. Every tag shall lead to a page with a list of posts with the same tag (see this page as an example). The tags shall be displayed in the brief version of the post (in the blog index page) and under the post page.
Goal 1: tagging of the posts
It is really simple. Just add variable tags to every post (in the YAML frontmatter) you would like to tag:
Goal 2: List of all tagged posts
This is also pretty simple. The following template will generate list of tags:
Generated page can now be added to your site.
Goal 3: print list of hyperlinked tags under post
This is the most complicated part.
Now we need to print proper hyperlinks at the end of the post.
Add file tagsforpost.html to your _include folder:
At first stage, tag names are sorted. At the second stage they are printed as hyperlinks.
This file is used by two other templates.
First, post template:
Second, homepage template:
That's it. Congratulations! We are having working setup with tags.