Both RSS and Atom feeds?

Is this possible? Porting from Jekyll and need an ATOM feed and RSS feed said that it isn’t, but that was in 2015 so I’m hoping that something has changed since then.

I’m assuming that I would put multiple templates in the layouts directory, but I’m not sure what to name them and what to put in config.toml to tell Hugo to generate them all. (I only want them generated for the blog part of my hugo site, not the whole thing.)

Thanks,

Bob

Yes.

Hugo has inbuilt support for RSS and Atom support can be added using a Hugo theme component (mine).

3 Likes

3rd version is a JSON feed. I include my template here - you must(!) it change for your needs!

{
  "version"      : "https://jsonfeed.org/version/1",
  "title"        : {{ .Site.Title | htmlUnescape | jsonify }},
  "home_page_url": {{ .Site.BaseURL  | absURL | jsonify }},
  {{ with .OutputFormats.Get "JSON" }}"feed_url"     : {{.Permalink | absURL | jsonify }},{{ end }} 
  {{ $icon := resources.Get "img/yy512.png" }}"icon"         : "{{$icon.Permalink}}",
  {{ $favicon := resources.Get "img/yy64.png" }}"favicon"      : "{{$favicon.Permalink}}",
  {{ with .Site.Author.name         }}"author"       : 
  {
    "name"       : {{ . | jsonify }},
    "avatar"     : {{ $.Site.Params.sidebar.avatar | absURL | jsonify }}
  },  {{ end }}
  {{ $r1 := where .Site.Pages "PublishDate" "gt" (now.AddDate -1 0 0) }}{{ $r2 := where .Data.Pages.ByDate.Reverse "Section" "not in" (split .Site.Params.invisibleSections ",") }}{{ $list := $r1 | intersect $r2 }}{{ $len := len $list }}
  "items": 
  [{{ range $index, $item := $list }}
    {
      "id"            : {{ $uuid := sha1 (.Permalink  | absURL)}}"{{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}}",
      "url"           : {{ $item.Permalink | jsonify  }},
      "title"         : {{ $item.Title   | plainify | chomp | jsonify }},
      "summary"       : {{ $item.Summary | plainify | chomp | jsonify }},
      "content_text"  : {{ $item.Plain              | chomp | jsonify }},
{{ range first 1 (.Resources.ByType "image") }}{{ $original := . }}{{ $.Scratch.Set "image" ($original.Fit "480x360") }}{{ $image := $.Scratch.Get "image" }}      "banner_image"  : "{{ $image.Permalink }}",{{ end }}
      "date_published": {{ $item.PublishDate.UTC.Format .Site.Params.dateFormatFeed | jsonify  }},
      "date_modified" : {{ $item.Lastmod.UTC.Format .Site.Params.dateFormatFeed | jsonify  }},
      "tags"          : [{{ range $index, $director := .Params.tags }}{{ if gt $index 0 }},{{ end }}"{{ . }}"{{ end }}]
    }{{ if lt (add $index 1) $len }},{{ end }}{{ end }}
  ]
}

config:

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

Thanks! I put your template in a layouts/feed.json file and your [outputFormats.JSON] section in my config.toml. After running just “hugo” I don’t see any new files in my public directory. Did I miss a step?

I also tried putting feed.json in layouts/_default.

dont forget to config the output

[outputs]
    home                  = [ "HTML", "RSS", "ATOM", "JSON"]

I named it home.json.json, the _default folder is OK.

Sometimes is it good to give hugo the -debug option. In the log you can see what hugo is looking for.

1 Like

It works great, but it’s not picking up the favicon. I’m not sure whether this is something I can address.

My http://bobdc.com/favicon.ico image shows up in browser tabs when viewing my bobdc.com website. (I put the image in the static directory and it worked fine.)

When I add the http://bobdc.com/blog/atom.xml feed generated by hugo-atom-feed/ to the feed reader inoreader.com, it does not show the favicon. I did set loadFavicon = true in config.toml. Any suggestions?

Thanks,
Bob

yes, you have to change

  {{ $icon := resources.Get "img/yy512.png" }}"icon"         : "{{$icon.Permalink}}",
  {{ $favicon := resources.Get "img/yy64.png" }}"favicon"      : "{{$favicon.Permalink}}",

it looks for static/img/yy64.png and static/img/yy64.png
try

  {{ "icon"         : "favicon.ico",
  {{ "favicon"      : "favicon.ico",

icon should be bigger, favicon can be small. DOn’t know, if feed reader understand .ico files. Better to use gif or png.

good luck :wink:

Thanks! But, I was actually replying to kaushalmodi about his hugo-atom-theme component and didn’t realize that the discussion software would put my comment under your last one. While my ultimate goal was an atom feed, I did still learn a lot from your code.

Now I am confused :slight_smile:

The Atom theme component has nothing to do with favicons, at least it’s not yet encoding the favicon path anywhere in the feed as far as I know.

yes, ATOM has no favicon,

ATOM feeds can have a and element. There should be a png, jpg or gif picture.
My response was to @bobdc part, the feed would not show any icon.

1 Like

my udated feed templates are now online