Introduction
Today I will elaborate on RSS and how it works with Hugo. We will dive into what RSS feeds are and what benefits they deliver. Then I am going to show how you can create a RSS feed in Hugo and how to customize it.
Introduction to RSS feeds and their benefits
RSS is short for Really Simple Syndication. It is a format for distributing web content. RSS feeds allow users to subscribe to a website or blog and receive updates whenever new content is published. This means that you can keep up with your favorite websites without having to visit them every day.
There are many benefits to using RSS feeds. Here are a few of the most important ones:
Convenience: RSS feeds make it easy to stay up-to-date with your favorite websites. You can subscribe to a feed and then have all of the new content delivered to you in one place. This saves you time and effort.
Customization: You can customize your RSS feeds to show the content that you are most interested in. For example, you can subscribe to a feed for a specific category of articles, or you can subscribe to a feed for a specific author.
Efficiency: RSS feeds can help you to be more efficient with your time. Instead of having to visit each website individually, you can simply check your RSS feed to see what new content has been published.
If you are looking for a way to stay up-to-date with your favorite websites, then RSS feeds are a great option. They are convenient, customizable, and efficient.
To easily read and subscribe to a RSS feed you have to use a RSS feed reader
like Feedly or NewsBlur. There are many more out there but this are just some I find interesting.
How to create a RSS feed with Hugo
Hugo comes with a built-in RSS template that makes it easy to create an RSS feed for your Hugo site.
Running the hugo
command the RSS feed is directly created as index.xml
.
Running this with a freshly created new hugo site it should look like this:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>My New Hugo Site</title>
<link>http://example.org/</link>
<description>Recent content on My New Hugo Site</description>
<generator>Hugo -- gohugo.io</generator>
<language>en-us</language><atom:link href="http://example.org/index.xml" rel="self" type="application/rss+xml" />
</channel>
</rss>
The index.xml
file contains the following information:
title
: The title of your sitelink
: The link to your sitedescription
: A description of your sitelanguage
: specifies the language the feed is written ingenerator
: specifies the program used to generate the feedchannel
: describe the RSS feed
There are many more of the possible fields in a rss field which you can find here complete list.
Customizing your RSS feed
You can customize the RSS feed by adding additional information, such as the author’s name, the date of publication, or the tags associated with the post.
But first let’s take a look what the basic template looks like which is built into Hugo.
{{- $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>
<language>{{ site.Language.LanguageCode }}</language>{{ 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>
You can also find this file on GitHub.
As we can see here the template includes a lot and some of the optionial fields.
Now let’s move to customize this basic template. In my case I wanted to display the .Description
field instead of .Summary
but keep the ‘.Summary` as a fallback option.
Here are the steps I did to achieve the goal:
- Create a new
rss.xml
in the following path:layouts/_default/
As it is placed in the default folder it directly overwirtes the template from the built in template. - Copy & Paste the code from the template into the new file.
- Now I could change the delivered description into the following:
<description >{{ with .Description | html }}{{ . }}{{ else }}{{ .Summary | html }}{{ end }}</description >
To further configure the RSS feed you can write some stuff in the hugo.toml.
It is very easy to include the head
of the html file like this but take a look at the documentation:
{{ range .AlternativeOutputFormats -}}
{{ printf `<link rel="%s" type="%s" href="%s" title="%s" />` .Rel .MediaType.Type .Permalink $.Site.Title | safeHTML }}
{{ end -}}
Conclusion
In this blog post, we have discussed RSS feeds and how to create an RSS feed with Hugo. We have also talked about how to customize your RSS feed.
RSS feeds are a great way to stay up-to-date with your favorite websites. They are convenient, customizable, and efficient. If you are looking for a way to do this, then I encourage you to create an RSS feed for your Hugo site.