Created
March 27, 2022 22:00
-
-
Save jonsagara/d1a4dadfb01587ce6af561ffb81545f4 to your computer and use it in GitHub Desktop.
Revisions
-
jonsagara created this gist
Mar 27, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,83 @@ In my `config.toml` file, I have this configuration that mentions RSS: ```toml [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: ```html <!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: ```html <!-- 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: ```xml {{/* 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!