A reader reached out to me the other day to let me know that my RSS feed for this blog didn’t include post content, just descriptions, and that this hindered their ability to read my posts offline. This obviously isn’t ideal, so I set about adding the full post content.
Customizing the RSS Feed
Hugo comes with a builtin RSS feed template, but it’s easy enough to override.
Simply copy the contents of this
file
to the themes/[theme name]/layouts/_default
directory, and then edit to your
heart’s content.
By default, the post-related part of this feed looks something like this:
<item>
<title>{{ .Title }}</title>
<link>{{ .Permalink }}</link>
<pubDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
{{- with $authorEmail }}<author>{{ . }}{{ with $authorName }} ({{ . }}){{ end }}</author>{{ end }}
<guid>{{ .Permalink }}</guid>
<description>{{ .Summary | transform.XMLEscape | safeHTML }}</description>
</item>
In Hugo, post content is available in .Content
.
It’s tempting to simply change the <description>
tag:
- <description>{{ .Summary | transform.XMLEscape | safeHTML }}</description>
+ <description>{{ .Content | transform.XMLEscape | safeHTML }}</description>
but there is a better way: <content:encoded>
.
The content: Namespace
The default Atom XML namespace doesn’t provide a way to have both a post
description and the full post content, but there is a namespace that does:
the Content Namespace Extension.
Adding this name space is easy; simply change the opening rss
tag to include
it:
- <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
+ <rss version="2.0"
+ xmlns:atom="http://www.w3.org/2005/Atom"
+ xmlns:content="http://purl.org/rss/1.0/modules/content/">
and then, within the <item>
, add a tag that contains the URL-encoded content:
<item>
<title>{{ .Title }}</title>
<link>{{ .Permalink }}</link>
<pubDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
{{- with $authorEmail }}<author>{{ . }}{{ with $authorName }} ({{ . }}){{ end }}</author>{{ end }}
<guid>{{ .Permalink }}</guid>
<description>{{ .Summary | transform.XMLEscape | safeHTML }}</description>
+ <content:encoded>{{ .Content | transform.XMLEscape | safeHTML }}</content:encoded>
</item>
This way, RSS readers can display the post description in their feeds/lists, but can still get access to the full post content and make it available to readers offline.