Art Kavanagh

Criticism, fiction and other writing

Adding a description to a post, using Hugo

Update, 21-Jan-2022: Though I’m pleased it worked, I’m not going to be using the method described below for the time being. Instead, I’m going to add a simple Twitter card description that merely refers to the date of the post, and I’ve created an archive page that I’ll update manually whenever I post a new issue (usually every two weeks. 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.

Update, later on 10-Jan-2022: The newsletter archive page didn’t give me any problems and is working. I may need to refine it a bit but there’s no urgency about that.

Well, that turned out to be easier than I expected. I’ve now got an automatically generated newsletter archive (based on applying the category “Newsletter” to all posts with titles). I haven’t yet got pagination or separation by date but all in good time.

— Art Kavanagh (@artkavana) January 10, 2022

Posted by Art, 10-Jan-2022.