ATOM and JSON feeds

I will share my configuration to build ATOM and JSON feeds.

config-file:

disableKinds = ["RSS"]  # kick out RSS

[mediaTypes]
  [mediaTypes."application/atom+xml"]
    suffixes = ["xml"]


[outputFormats.HTML]
baseName              = "index"
NoUgly                = true


[outputFormats.ATOM]
MediaType             = "application/atom+xml"
BaseName              = "feed"
suffix                = "xml"
IsHTML                = false
IsPlainText           = false
noUgly                = true
Rel                   = "alternate"

[outputFormats.JSON]
MediaType             = "application/json"
BaseName              = "feed"
suffix                = "json"
IsHTML                = false
IsPlainText           = true
noUgly                = false
Rel                   = "alternate"

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

layout/_default/home.atom.xml Changed! Updated!

{{ print "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>"| safeHTML }}
{{ print "<?xml-stylesheet type=\"text/css\" href=\"/css/atom.css\" ?>"| safeHTML }}
{{ print "<!-- home.atom.xml -->"| safeHTML }}
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="text">{{ if .IsHome }}{{ .Site.Title | plainify }}{{ else }}{{ with .Title | plainify }}{{.}} auf {{ end }}{{ .Site.Title | plainify }}{{ end }}</title>
	<subtitle type="text">Alle Beiträge der letzten 24 Monate</subtitle>
	{{ with  .OutputFormats.Get "ATOM" -}}
	<link rel="self"  type="application/atom+xml" href="{{.Permalink}}" />
	{{- end }}
	<link rel="alternate" type="type/html" href="{{.Permalink}}" />
	{{ with .Site.Author.name }}<author>
		<name>{{.}}</name>
		{{ with $.Site.Author.email }}<email>{{.}}</email>{{end}}
	</author>{{end}}
	{{ with .Site.Copyright }}<rights type="text">{{.}}</rights>{{end}}
	<generator url="https://gohugo.io" version="{{.Hugo.Version}}">Hugo</generator>
	{{ if not .Site.LastChange.IsZero }}<updated>{{.Site.LastChange.UTC.Format .Site.Params.dateFormatFeed }}</updated>{{ end }} 
	{{ $uuid := sha1 $.Site.BaseURL }}<id>urn:uuid:{{substr $uuid 0 8}}-{{substr $uuid 8 4}}-5{{substr $uuid 13 3}}-{{substr $uuid 16 1}}9{{substr $uuid 17 2}}-{{substr $uuid 21 12}}</id>
	{{ $start := now.AddDate -1 0 0}}
	{{ range where .Site.Pages "PublishDate" "gt" $start -}}
	{{ if ne .Section ""}}
	<entry> {{ $url := .Permalink }}{{ $uuid := sha1 .Permalink }}{{ $page := .}}
      <title type="text">{{ .Title | plainify }}</title> 
	  <category term="{{$page.Section}}" />
      <link rel='alternate' type='type/html' href='{{ $url }}' />
	  <published>{{ .PublishDate.UTC.Format .Site.Params.dateFormatFeed }}</published>
      <updated>{{ .Lastmod.UTC.Format .Site.Params.dateFormatFeed  }}</updated>
      <id>urn:uuid:{{substr $uuid 0 8}}-{{substr $uuid 8 4}}-5{{substr $uuid 13 3}}-{{substr $uuid 16 1}}9{{substr $uuid 17 2}}-{{substr $uuid 21 12}}</id>
      <summary type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml">{{ range first 1 (.Resources.Match "**/*.*") -}}{{ $original := . }}{{ $.Scratch.Set "image" ($original.Fit "480x360") }}{{ $image := $.Scratch.Get "image" }}<a href="{{ $url }}"><img src="{{ $image.RelPermalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}" /></a><br/><br/>{{- end }}
{{ .Summary | plainify |chomp }}
	  <br/>{{ if not .Truncated}}<a href="{{ $url }}">[read more]</a>{{ end }}
	  </div></summary>
    </entry>
	
    {{- end }}
	{{- end }}
</feed>

it uses page bundles to add an image to the feed. $start takes alle pages out of the last year. change it to range first .Param.RSSlimit for your need.

layout/index.json

{
    "version": "https://jsonfeed.org/version/1",
    "title": "{{ .Site.Title }}",
    "home_page_url": "{{ .Site.BaseURL }}",
	{{ with  .OutputFormats.Get "JSON" -}}
	"feed_url": "{{.Permalink}}",
	{{- end }}	
	{{ if isset .Site.Params "description" -}}
	"description": "{{ .Site.Params.description }}",
	{{- end }}
	{{ if isset .Site.Params "author" -}}
	"author": { "name": "{{ .Site.Params.author }}" },
	{{- end }}
    "items": [{{ $start := now.AddDate -1 0 0}}
	{{ range where .Site.Pages "PublishDate" "gt" $start -}}
		{ {{ $page := .}}
			"id": "{{ .Permalink }}",
			"title": "{{ .Title | plainify}}",
			"content_text": {{ .Summary | plainify | jsonify }},
			"url": "{{ .Permalink }}",
			"date_published": "{{ $page.PublishDate.UTC.Format .Site.Params.dateFormatFeed  }}",
			"date_modified" : "{{ $page.Lastmod.UTC.Format .Site.Params.dateFormatFeed  }}",
			"tags": ["{{ delimit .Params.tags ", " }}"]
		},{{- end }}
		{ }
    ]
}

works like the ATOM feed.

change it for your needs!

Hello,

I happen to maintain two Hugo theme components: for ATOM feed and JF2 format JSON feeds for single and list pages.

  • If you like, you can colloborate with me on the ATOM feed theme component to make it better.
  • As for the JSON feed, see if you can convert it to a theme component like mine. That way it will be easy to share with people. See the instructions for using the ATOM feed theme component as an example.

Tanks,
I’m retired now and I use Hugo for my brain and finger training :slight_smile:
Take what you like.

Feed templates updated …

Just a typo: languge in JSON feed to version 1.1 · gj52/HugoSample@2272774 · GitHub

Thanks for your great job :heart:

Danke