RSS

Custom hugo RSS template

Custom hugo RSS content. Add lastBuildDate to show the page last modification time. Make a custom description like adding image, use description field instead of summary or content.

Hugo ships with its own RSS 2.0 template, it generated the RSS feed file as index.xml and embedded into index.html header. e.g.

{{ range .AlternativeOutputFormats -}}
{{ printf `<link rel="%s" type="%s" href="%s" >` .Rel .MediaType.Type .Permalink | safeHTML }}
{{ end -}}

Generated html:

<link rel="alternate" type="application/rss+xml" href="https://djangocas.dev/blog/index.xml">

Default RSS template: https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/_default/rss.xml

Add <lastBuildDate> to <item> show the page last modification time

By default, the individual <item> only have <pubDate>. I want to add <lastBuildDate> as page’s last git commit. To do this. Change the RSS template file and use $.Page.Lastmod as <lastBuildDate>. e.g.

<lastBuildDate>{{ $.Page.Lastmod.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>

Change <description>

By default, <description> is very simple, just use .Summary:

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

To make more attractive, e.g. adding image in page, change .Summary to use .Description. Here is an example:

<description>
  {{ $img := (.Resources.ByType "image").GetMatch "*featured*" }}
  {{ with $img }}
  {{ $img := .Resize "640x" }}
  {{ printf "<![CDATA[<img src=\"%s\" width=\"%d\" height=\"%d\"/>]]>" $img.Permalink $img.Width $img.Height | safeHTML }}
  {{ end }}
  {{ .Description | html }}
</description>

The full <item> for a reference:

<item>
  <title>{{ .Section | title }}: {{ .Title }}</title>
  <link>{{ .Permalink }}</link>
  <lastBuildDate>{{ $.Page.Lastmod.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>
  <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>
    {{ $img := (.Resources.ByType "image").GetMatch "*featured*" }}
    {{ with $img }}
    {{ $img := .Resize "640x" }}
    {{ printf "<![CDATA[<img src=\"%s\" width=\"%d\" height=\"%d\"/>]]>" $img.Permalink $img.Width $img.Height | safeHTML }}
    {{ end }}
    {{ .Description | html }}
  </description>
</item>

Enjoy.

References