Update This post is from the old theme, so the screenshots will look different to what you see here.
Introduction
Hugo provides a powerful content management tool called taxonomies. In addition to default taxonomies such as categories and tags, you can define one on your own and customise as you like (see this repo for a cool example!).
Problem
One important feature of taxonomies is that you can show the list of terms (keywords) across the site, such as the list of tags. By default, the list looks like a regular branch bundle of a section because it follows the same layout:
Now, how can we improve this page? We can certainly reduce the spacing between terms using flexbox and sort them alphabetically.
It looks much better than before, but you can expect this list will grow to a messy clutter of words as the number of tags increases over time. It would look much better if we could divide them into smaller chunks based on the first letter.
note In Hugo, a taxonomy consists of many terms. So, the layout for the full list of tags should be tags/taxonomy.html
, and the layout for a single tag should be named as tags/term.html
. See below where Hugo pulls the layout from depending on the folder structure. For the full picture, check the documentation.
.
└── layouts/
├── _default/
│ ├── baseof.html
│ ├── list.html <- /posts
│ │ <- /tags, /tags/cool-tag
│ │ <- /categories, /categories/Hugo
│ └── single.html <- /posts/my-first-post
└── tags/
.
└── layouts/
├── _default/
│ ├── baseof.html
│ ├── list.html <- /posts
│ │ <- /categories, /categories/Hugo
│ └── single.html <- /posts/my-first-post
└── tags/
└── taxonomy.html <- /tags, /tags/cool-tag
.
└── layouts/
├── _default/
│ ├── baseof.html
│ ├── list.html <- /posts
│ │ <- /categories, /categories/Hugo
│ └── single.html <- /posts/my-first-post
└── tags/
├── taxonomy.html <- /tags
└── term.html <- /tags/cool-tag
Approach
I want to clarify that this is not the first solution to this problem. There was a discussion on this matter a while ago, and you can also find a different solution. These methods basically sort the titles alphabetically, track the first letter, and start the <ul>
element again whenever it detects a change. I wanted to try a more conventional approach, where you first build a dictionary of tags that look like this:
{
"A": [
"absolute value",
"algebra"
],
"D": [
"decimal",
"distributive law"
],
// ...
}
and build the document using this information.
Grouping the items
We will first build the dictionary, which we call $by_letter
, where the keys are the letters of the alphabet, and the values are the list of tags that start with that letter. Here is the full code:
|
|
Rendering
We can then make the list from the dictionary. Since we will make use of the CSS grid, the keys can just sit inside a <span>
.
|
|
Finally, we can put everything together using CSS.
|
|