How to show the full post unless there’s a user defined summary in Hugo

Hugo has two types of summaries: those automatically defined by Hugo and those manually defined by the user. What it doesn’t have is a way to know which type of summary each post has; something like an {{ .IsUserDefined }} boolean.

On the home page of this site I like to show the full contents of a post by default. If a post has a manually defined summary however, I show that instead with a “read more” link.

Accomplishing this in Hugo is a little more involved than it is in Jekyll, and as far as I can tell nobody has documented how to do it. Let’s fix that.

Configuring your content files

You need to start by choosing the summary separator you want to use in your content files. The only one that’s off limit is <!-​-more--> because it’s the one Hugo’s default summary functionality uses, and as such, it doesn’t get passed through in the .Content variable.

I went with <!-- readmore -->. Therefore, if I wanted to show a summary and a “read more” link for a post, I’d set it up like this:

---
title: "Example title"
date: 2017-08-09T12:45:27+02:00
categories: [Hugo]
---
This is the first paragraph in the post. It will be shown on the home page as it's before the summary separator.
<!-- readmore -->
On the homepage, this content won't be shown as it's after the summary separator.

Configuring your layout files

Open up the layout file you want to implement this functionality in. In my case, that was index.html.