Mastodon hachyterm.io

Who wants to read an incomplete RSS feed that forces you to open the original website?

Hugo is the static site generator that powers my blog.
Hugo ships with a default RSS template which only shows a summary of your articles.

If you want to read the full content, you’ll need to visit the website. That sucks.

I want to create a full-text RSS feed for my blog.

Template Lookup Order

We’ll need to create a modified RSS template. Let’s make sure that Hugo will use the new one instead of the default template that comes with Hugo or the Hugo theme.

I created a new directory (layouts) in my Hugo folder (the root folder, which also contains my config.toml file).

Let’s copy the default Hugo RSS template into a file called index.xml (layouts/index.xml). That’s on position 5 in the template lookup order and higher than the default template.

Create New RSS Template

{{- $pctx := . -}}
{{- if .IsHome -}}{{ $pctx = .Site }}{{- end -}}
{{- $pages := slice -}}
{{- if or $.IsHome $.IsSection -}}
{{- $pages = $pctx.RegularPages -}}
{{- else -}}
{{- $pages = $pctx.Pages -}}
{{- end -}}
{{- $limit := .Site.Config.Services.RSS.Limit -}}
{{- if ge $limit 1 -}}
{{- $pages = $pages | first $limit -}}
{{- end -}}
{{- printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>{{ if eq  .Title  .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{.}} on {{ end }}{{ .Site.Title }}{{ end }}</title>
    <link>{{ .Permalink }}</link>
    <description>Recent content {{ if ne  .Title  .Site.Title }}{{ with .Title }}in {{.}} {{ end }}{{ end }}on {{ .Site.Title }}</description>
    <generator>Hugo -- gohugo.io</generator>{{ with .Site.LanguageCode }}
    <language>{{.}}</language>{{end}}{{ with .Site.Author.email }}
    <managingEditor>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</managingEditor>{{end}}{{ with .Site.Author.email }}
    <webMaster>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</webMaster>{{end}}{{ with .Site.Copyright }}
    <copyright>{{.}}</copyright>{{end}}{{ if not .Date.IsZero }}
    <lastBuildDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>{{ end }}
    {{- with .OutputFormats.Get "RSS" -}}
    {{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }}
    {{- end -}}
    {{ range $pages }}
    <item>
      <title>{{ .Title }}</title>
      <link>{{ .Permalink }}</link>
      <pubDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
      {{ with .Site.Author.email }}<author>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</author>{{end}}
      <guid>{{ .Permalink }}</guid>
      <description>{{ .Summary | html }}</description>
    </item>
    {{ end }}
  </channel>
</rss>

The trick lies in changing the content of the XML <description> tag.

The RSS 2.0 spec says that the description element can contain “the item’s full content or a summary of its contents, a decision entirely at the discretion of the publisher.”

Currently, we only show a short summary:

<description>{{ .Summary | html }}</description>

Change the line to the following:

<description>{{ .Content | html }}</description>

Bonus Tip: RSS Feed Limit

Hugo builds a RSS feed from the complete article backlog. You might want to restrict the number of RSS entries if you have many blog posts.

You need to add the rssLimit field.

Example config.toml:

baseurl = "https://www.rockyourcode.com"
languageCode = "en-us"
rssLimit = 25