Customize RSS in Hugo Website Customize RSS in Hugo Website

Guide to disable, rename or customize RSS XML in hugo

Page content

In this tutorial, we’ll learn how to disable, rename, or customize RSS XML Feed for Hugo website.

Overview

When you generate a website using Hugo static site generator, Hugo out of the box generate RSS XML based on the RSS 2.0 Template

The RSS XML index.xml is auto generated for -

  • home for whole website e.g. https://example.com/index.xml
  • section for each section e.g. https://example.com/<SECTION>/index.html
  • taxonomyTerm for each taxonomy term e.g. https://example.com/categories/index.html
  • taxonomy for each taxonomy e.g. https://example.com/categories/<CATEGORY>/index.html

Following is a sample RSS XML index.xml for whole website:-

index.xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Coding N Concepts</title>
    <link>https://codingnconcepts.com/</link>
    <description>Recent content on Coding N Concepts</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Fri, 04 Sep 2020 00:00:00 +0000</lastBuildDate><atom:link href="https://codingnconcepts.com/index.xml" rel="self" type="application/rss+xml" />

    <item>
      <title>Customize RSS feed in Hugo Website</title>
      <link>https://codingnconcepts.com/hugo/custom-rss-feed-hugo/</link>
      <pubDate>Fri, 04 Sep 2020 00:00:00 +0000</pubDate>    
      <guid>https://codingnconcepts.com/hugo/custom-rss-feed-hugo/</guid>
      <description>&lt;p&gt;In this tutorial, we&amp;rsquo;ll learn how to disable or customize RSS Feed for Hugo website.&lt;/p&gt;</description>
    </item>
    
    <item>
        ...
    </item>

    ...
  </channel>
</rss>

Disable RSS Feed

There are two ways to disable generation of RSS Xml :-

Disable using config.toml

This approach is recommended, when you want to disable RSS permanently everytime you build your website.

config.toml (default)

First of all we’ll see what is the default RSS configuration in config.toml

[outputs]
    page = ["HTML"]
    home = ["HTML", "RSS"]
    section = ["HTML","RSS"]
    taxonomyTerm = ["HTML", "RSS"]
    taxonomy = ["HTML", "RSS"]

We see from above configuration that RSS pages are generated for home, section, taxonomyTerm and taxonomy out of the box.

config.toml (RSS disabled)

You can disable the generation of RSS Feed by configuration change in config.toml

[outputs]
    page = ["HTML"]
    home = ["HTML"]
    section = ["HTML"]
    taxonomyTerm = ["HTML"]
    taxonomy = ["HTML"]

Disable using command-line

This approach is recommended, when you want to disable RSS for a specific build of your website.

To generate a hugo website without RSS, execute following command from terminal:

hugo --disableKinds=RSS

To serve website in the localhost environment without a sitemap, execute following command from terminal:

hugo server --disableKinds=RSS

Rename RSS Feed

Hugo generates the RSS feed in index.xml. If you want to change the name from index.xml to say rss.xml then add following configuration in config.toml

config.toml
[outputFormats]
  [outputFormats.RSS]
    mediatype = "application/rss"
    baseName = "rss"

Customize RSS Feed

Hugo has a built-in RSS template, but if you want to customize RSS rss.xml then first of all copy and paste below hugo’s default RSS template in layouts/_default/rss.xml location:-

/layouts/_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">
  <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>

Now let’s do changes in above template to meet our requirement:-

Add Full Content in rss.xml

This is most common use case where you want to show full post content instead of just summary in RSS feed.

For this just replace this line in default RSS template

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

with this one

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

Exclude specific pages from rss.xml

This is also very common use case where you want to skip some pages from rss.xml.

Notice that we have added a condition {{ if ne .Params.rss_ignore true }} in default RSS template to exclude pages based on rss_ignore flag

/layouts/_default/sitemap.xml
...
    {{ range $pages }}{{ if ne .Params.rss_ignore true }}
    <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 }}{{ end }}
...

Now any page, you want to exclude from RSS Feed, add the rss_ignore: true property in front-matter of that page

blog-page.md
---
title: Customize RSS feed in Hugo Website

...

rss_ignore: true
---

Summary

In this tutorial, we’ve learned how to -

  1. Disable auto RSS feed generation
  2. Rename generated index.xml to some other name rss.xml
  3. Customize default RSS feed template for your hugo website.

If you have any other requirements of rss.xml customization, or you are facing issue following the tutorial. Please comment, I’ll try to solve your problem at earliest possible.