Nithitsuki's Website

posts abt-me others


by default hugo dosen’t support generating atom rss feeds, instead defaulting to rss version 2.0, but you can easily add support for atom rss feed by following the steps below:

  1. create a new file named atom.xml (or list.atom.xml) in the layouts/_default directory of your hugo project root (or the theme folder).

  2. add the following code to the atom.xml file. This template handles the logic for generating the feed and automatically converts relative image links to absolute links so they show up correctly in RSS readers:

{{- $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\"?>" | safeHTML }}
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>{{ if eq  .Title  .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{.}} on {{ end }}{{ .Site.Title }}{{ end }}</title>
  {{/* Dynamic Self Link: Matches the filename defined in hugo.toml */}}
  {{- with .OutputFormats.Get "Atom" -}}
  <link href="{{ .Permalink }}" rel="self"/>
  {{- end -}}
  <link href="{{ .Permalink }}"/>
  {{- $lastmod := .Date -}}
  {{- if .Date.IsZero -}}
    {{- if gt (len $pages) 0 -}}
      {{- $lastmod = (index $pages 0).Lastmod -}}
    {{- else -}}
      {{- $lastmod = now -}}
    {{- end -}}
  {{- end -}}
  <updated>{{ $lastmod.Format "2006-01-02T15:04:05-07:00" | safeHTML }}</updated>
  <id>{{ .Permalink }}</id>
  {{- with site.Params.author.name -}}
  <author>
    <name>{{.}}</name>
    {{- with site.Params.author.email -}}<email>{{.}}</email>{{- end -}}
  </author>
  {{- end -}}
  <generator>Hugo -- gohugo.io</generator>
  {{- range $pages }}
  <entry>
    <title>{{ .Title }}</title>
    <link href="{{ .Permalink }}"/>
    <id>{{ .Permalink }}</id>
    {{- with site.Params.author.name -}}
    <author>
      <name>{{.}}</name>
    </author>
    {{- end -}}
    <published>{{ .Date.Format "2006-01-02T15:04:05-07:00" | safeHTML }}</published>
    <updated>{{ .Lastmod.Format "2006-01-02T15:04:05-07:00" | safeHTML }}</updated>
    {{- if .Content -}}
    {{- $content := .Content | replaceRE " style=\"[^\"]*\"" "" -}}
    {{- $base := .Site.BaseURL -}}
    {{- $perm := .Permalink -}}
    {{- /* Handle absolute URLs for images and links */ -}}
    {{- $content = $content | replaceRE " src=\"/([^\"]*)\"" (printf " src=\"%s$1\"" $base) -}}
    {{- $content = $content | replaceRE " src=\"([^/:\"][^:\"]*)\"" (printf " src=\"%s$1\"" $perm) -}}
    {{- $content = $content | replaceRE " href=\"/([^\"]*)\"" (printf " href=\"%s$1\"" $base) -}}
    {{- $content = $content | replaceRE " href=\"([^/:\"][^:\"]*)\"" (printf " href=\"%s$1\"" $perm) -}}
    <content type="html">{{ $content | html }}</content>
    {{- end -}}
  </entry>
  {{- end }}
</feed>
  1. append the following code to your hugo.toml (or config.toml if you use an older theme) file to define the Atom output format:
[mediaTypes]
  [mediaTypes."application/atom+xml"]
    suffixes = ["xml"]

[outputFormats]
  [outputFormats.Atom]
    mediaType = "application/atom+xml"
    baseName = "atom" 
    isPlainText = false
    isHTML = false

[outputs]
  home = ["HTML", "RSS", "Atom"]
  section = ["HTML", "RSS", "Atom"]

for hugo.yaml or hugo.json, the configuration, refer to https://gohugo.io/configuration/output-formats/ to add the output format configuration above in the respective format.

  1. Make sure to link the atom feed in your website’s header by adding the following code to your head partial or base_of.html file
    {{ range .AlternativeOutputFormats -}}
    <link rel="{{ .Rel }}" type="{{ .MediaType.Type }}" href="{{ .Permalink | safeURL }}" title="{{ site.Title }}">
    {{ end -}}
  1. and that’s it! now when you build your hugo site, it will generate an Atom feed at the specified location, for example: https://nithitsuki.com/atom.xml