Art Kavanagh

Criticism, fiction and other writing

Adding a description to a post, using Hugo

Update, 12-Mar-2022: Having previously removed this post because I wasn’t sure that the method I describe in it was robust enough to work reliably, I’ve now satisfied myself that it does work, so I’m reinstating the post. I’ve also now added the archive page mentioned in the final paragraph below.

I’ve been wanting to add a description to my longer posts on I recently set up my newsletter preferences so that posts with titles are by default sent out to newsletter subscribers. I wanted a description (which would be different from the automatically generated Summary) to be used in Twitter cards — and ideally also in an automatically generated archive of newsletter posts (which I have still to work on). It could also be added to the head of each post in a meta “description” tag, which would show up in certain search results. is based on a static site generator named Hugo, which is powerful and complicated. I knew that Hugo is capable of doing what I want, but I wasn’t sure that I would be capable of getting it to do so. Until a few weeks ago, I had no experience of using Hugo, and I’ve been finding recently that, as I get older, I’m becoming increasingly resistant to learning new tech tricks. But this seemed like it might be worth pursuing, so long as it didn’t take up too much of my time.

In the end, it took longer than it should have, and than I’d have liked. Essentially, the idea was that, as I posted each issue of the newsletter, I’d add both the post title and a shortish description to a JSON file, "postlist.json", in the /data/ folder of my I’d add some lines to the "head" template which would cause Hugo to open the JSON file and loop though the various entries, looking for a title that matched that of the post. On finding a match, it would add a meta tag to the html file for the post, containing the corresponding description.

It sounds simple enough, because it is, but only for people who know what they’re doing. My problem was that, till it actually started to work, I wasn’t sure I was taking the right approach. I’m sure that Hugo’s error messages are highly informative to someone who understands the system but they’re opaque and intimidating to someone who doesn’t. Eventually, I worked out that I couldn’t simply compare the post title in my data file with the Title of the post: first, I had to convert the latter to a string. There were a few other little hitches of that nature.

As of now, my data file has just two entries. It looks like this:

{ "posts": [ { "posttitle": "You’ve got a right to be angry: Salman Rushdie, Fury", "postdescription": "Like some of his earlier fiction, notably Midnight’s Children, Salman Rushdie’s novel Fury (2001) is driven by anger. But it’s a more diffuse, less directed anger than in the earlier novel. Something has changed." }, { "posttitle": "Peter Abrahams’s impaired heroes", "postdescription": "Three novels from the mid 2000s by Stephen King’s “favorite American suspense novelist”, featuring protagonists who have the odds stacked against them, even more so than is usual in suspense fiction." } ] }

This file will have to be edited manually each time I want to add a new description. And the snippet I’ve included in the head template is this:

{{ $postlist := site.Data.postlist }} {{ $currentTitle := printf "%s" .Title }} {{ range $postlist.posts }} {{ if eq .posttitle $currentTitle }} <meta name="twitter:description" content="{{ .postdescription }}" /> {{ end }} {{ end }}

(There’s a separate snippet that adds the other Twitter card meta tags if the post has a title.) Rather to my amazement, it all seems to work. The next step will be to try to create a page that will automatically include the titles, dates and descriptions (with the permalink to the post) of all my newsletter posts. I’m feeling quite optimistic about that, having got this far.

Posted by Art, 10-Jan-2022; updated 23-Jan-2022 and again 12-Mar-2022.