Tags

Tagging and browsing tags in Hexo

My blog is a worry stone sometimes. It鈥檚 nice to tinker with something that is fully under my control, even if it鈥檚 ultimately only really for me. I don鈥檛 write on this blog for anyone in particular. I don鈥檛 even have any analytics to I don鈥檛 know if anyone even reads these posts.

But it鈥檚 still mine and I want to make it great.

Something I鈥檝e done for a while is set up Hexo to ensure that my URLs make logical sense. Take my post about string compression and Blazor. Starting at that URL, you can keep chopping off parts and each step will still work.

https://moscardino.net/2020/09/string-compression-in-blazor/
https://moscardino.net/2020/09/
https://moscardino.net/2020/
https://moscardino.net/

It鈥檚 that kind of thing that make me happy with this blog. Though if I posted more, the month view might actually be useful.

There鈥檚 always been a bit more to these posts that I never exposed, though: tags. Each post is written in Markdown with some front matter that is used for metadata. This post鈥檚 front matter looks like this:

title: Tags
date: 2021-01-05
description: Tagging and browsing tags in Hexo
image: /images/organize.jpg
tags: [Hexo, Blog, Tags]

And since the beginning, each post has had appropriate tags assigned to it. They鈥檝e only really been used to populate some meta tags and in my own search results. About a week ago, I started showing the tags at the bottom of each post. Not only that, but you can find a link in the footer to browse all tags and view posts by tag.

The changes were simple, really, since Hexo does pretty much all of the work. Adding tag_dir: tags to my _config.yml file ensures that all tag pages live under /tags. This part gets added to my post layout file:

<% if (page.tags && page.tags.length) { %>
    <aside class="container">
        <span class="u-sr-only">Tags</span>
        
        <ul class="tag-list">
            <% page.tags.forEach(function (tag) { %>
                <li class="tag-list__item">
                    <a href="<%- url_for(tag.path) %>">
                        <%- tag.name %>
                    </a>
                </li>
            <% }); %>
        </ul>
    </aside>
<% } %>

And I created a new layout called tag-index.ejs which is used to create the page at /tags that lists all possible tags.

<div class="container">
    <h1 class="u-sr-only">
        All Tags
    </h1>

    <div class="post post--listitem post--archive-tag">
        <div class="post__inner">
            <p class="post__body">
                馃彿 Viewing all tags. Click on a tag to view posts.
            </p>
        </div>
    </div>

    <ul class="tag-list">
        <% site.tags.sort('-length').forEach(function (tag) { %>
            <li class="tag-list__item">
                <a href="<%- url_for(tag.path) %>">
                    <%- tag.name %> (<%- tag.length %>)
                </a>
            </li>
        <% }); %>
    </ul>
</div>

The main index layout which lists posts doesn鈥檛 need to be modified to filter by a selected tag (Hexo does that on it鈥檚 own) but does get a little header. There鈥檚 already a similar header for browsing by year and/or month, so this was basically reused code:

<% if (is_tag()) { %>
    <div class="post post--listitem post--archive-tag">
        <div class="post__inner">
            <p class="post__body">
                馃彿 Showing posts tagged with <%= page.tag %>. <a href="/tags">View all tags.</a>
            </p>
        </div>
    </div>
<% } %>

And that鈥檚 all. Add in some CSS to make them look nice, and now we have tags everywhere. It鈥檚 a nice change that I think works very well and makes the site that much nicer, especially for me.

Photo by Pop & Zebra on Unsplash.