hugo-rocinante icon indicating copy to clipboard operation
hugo-rocinante copied to clipboard

Photos in RSS feed?

Open drewdiver opened this issue 3 years ago • 1 comments

Hello! I love this theme.. I was curious if there's a way to get even a single photo post to show in an RSS feed? As an example, I test via NetNewsWire and I can see the post but there's no content delivered until I view in a browser.

drewdiver avatar Jun 22 '21 07:06 drewdiver

I also had this issue. I created a new RSS template in the root "/layouts/_default/" folder called "rss.xml." I have mine in the root folder of my source code, but I assume you could put that in the theme "/layouts/_default/" folder as well.

I based this off of the default RSS file that Hugo uses.

You can read more about the RSS handling of Hugo here.

I have modified the Rocinante theme to build my own site, so not all of my rss.xml code might be necessary for you.

In my rss.xml file, I have added code to include the Photos posts in my RSS, to exclude pages or posts that have front matter flag to not show up in the RSS feed, and handling of links as I was having issues with relative url structure for images, PDFs, etc.

This is what I ended up creating and it seems to work for me:

My rss.xml file:

{{- $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 -}}
{{- $pages = (where $pages ".Params.norss" "!=" "true") -}}
{{- 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>
      {{- $content := replaceRE "a href=\"(#.*?)\"" (printf "%s%s%s" "a href=\"" .Permalink "$1\"") .Content -}}
      {{- $content := replaceRE "a href=\"(.*?.jpg)\"" (printf "%s%s%s" "a href=\"" .Permalink "$1\"") $content -}}
      {{- $content := replaceRE "a href=\"(.*?.pdf)\"" (printf "%s%s%s" "a href=\"" .Permalink "$1\"") $content -}}
	  {{- $content = replaceRE "img src=\"(.*?)\"" (printf "%s%s%s" "img src=\"" .Permalink "$1\"") $content -}}
	  <description>
	  {{ $content | html }}
	  {{ if eq .Type "photos" }}
	  {{ range .Resources }}
        {{ printf "%s%s%s" "<p><img src=\"" .Permalink "\"></p>" }}   
	  {{ end }}
	  {{ end }}
	  </description>
    </item>
    {{ end }}
  </channel>
</rss>

andsplat avatar May 10 '22 17:05 andsplat