A more complete Hugo RSS template
While developing the new website for the student council I found Hugo’s default RSS template lacking in two points:
The default template is built for blogs with only a single author. We will have many different authors on the student council website.
It only provides the content as text. Providing a text only representation in the description
tag is the way described by the RSS standard.
Many current RSS readers can also display a HTML representation of the content.
The advantage of a HTML representation is that elements like images and lists can be displayed.
HTML content can be shown by including a content:encoded
tag in the item
with the HTML wrapped in a <![CDATA[...]]>
.
The modified RSS template can be found below. Save it as layouts/rss.xml
to use this template.
The template assumes that the articles all have an image. The location of the image is stored as image
in the page’s frontmatter.
{{/* Based on Hugos default rss template: https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/_default/rss.xml */}}
{{- $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" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<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>
<docs>https://www.rssboard.org/rss-specification</docs>
<ttl>60</ttl>
<language>{{ site.Language.LanguageCode }}</language>
{{ with .Site.Copyright }}<copyright>{{ . }}</copyright>{{ end }}
<lastBuildDate>{{ now.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>
{{ with resources.Get (.Site.Params.Logo) }}<image><url>{{ .Permalink }}</url><title>{{ if eq page.Title page.Site.Title }}{{ page.Site.Title }}{{ else }}{{ with page.Title }}{{ . }} on {{ end }}{{ page.Site.Title }}{{ end }}</title><link>{{ page.Permalink }}</link></image>{{ 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 .Params.Author }}<author>{{ index $.Site.Params.Author . "name" }}</author>{{ end }}
<guid>{{ .Permalink }}</guid>
<description>{{ .Content | plainify }}</description>
{{ $page := . }}
{{- with .Params.image }}{{- with $page.Resources.Get . }}<content:encoded>{{ printf "<![CDATA[" | safeHTML }}<a href="{{ $page.Permalink }}"><img src="{{ .Permalink }}" /></a><br/><br/>{{ $page.Content | transform.HTMLUnescape | safe.HTML }}{{ printf "]]>" | safeHTML }}</content:encoded>{{ end }}{{ end }}
</item>
{{- end }}
</channel>
</rss>