Skip to content

Instantly share code, notes, and snippets.

@jonsagara
Created March 27, 2022 22:00
Show Gist options
  • Save jonsagara/d1a4dadfb01587ce6af561ffb81545f4 to your computer and use it in GitHub Desktop.
Save jonsagara/d1a4dadfb01587ce6af561ffb81545f4 to your computer and use it in GitHub Desktop.
Adding RSS to my hugo site

In my config.toml file, I have this configuration that mentions RSS:

[outputs]
home = ["HTML", "RSS"]
page = ["HTML", "RSS"]

[outputFormats]
[outputFormats.RSS]
mediatype = "application/rss+xml"
baseName = "feed"

Unfortunately, it has been too long, and I don't remember exactly what the above means. I think it means that both my home page and my blog pages can render either HTML or RSS.

I believe the baseName setting means my feed will be hosted at feed.xml.

In my template, I reference a head partial:

<!DOCTYPE html>
<html>
    {{- partial "head.html" . -}}
    <body>

In head.html, which corresponds to the HTML <head> tag, I have this declaration that renders the RSS link:

<!-- RSS. Only ever link to /feed.xml. Render home, section, and pages. -->
{{- with .OutputFormats.Get "rss" -}}
    {{ printf `<link rel="%s" type="%s" href="%s" title="%s" />` .Rel .MediaType.Type $.Site.RSSLink $.Site.Title | safeHTML }}
{{- end }}

And finally, in themes/{theme name}/layouts/_default, I have an rss.xml file that contains the following:

{{/*
    Default RSS template copied from https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/_default/rss.xml,
    as suggested here: https://gohugo.io/templates/rss/#the-embedded-rssxml

    Modified so that the main feed only displays pages from the blog section, as shown here:
    https://benjamincongdon.me/blog/2020/01/14/Tips-for-Customizing-Hugo-RSS-Feeds/

    Also show the page author instead of the site author.
*/}}
{{- $pages := where (where .Site.Pages ".Section" "blog") "Kind" "page" -}}
{{- $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 .Params.author }}<author>{{ . | html }}</author>{{end}}
      <guid>{{ .Permalink }}</guid>
      <description>{{ .Summary | html }}{{ printf "<p><a href=%q>Continue reading...</a></p>" .Permalink | html }}</description>
    </item>
    {{ end }}
  </channel>
</rss>

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment