One feed with excluded sections

Hi, I have a current structure like:

[hendry@t480s foobar]$ cat config.yaml
baseURL: https://example.com
title: Example
disableKinds:
    - taxonomy
    - taxonomyTerm
    - sitemap
[hendry@t480s foobar]$ tree content/
content/
├── 2019
│   └── old.md
├── 2020
│   └── fresh.md
└── private-ish
    └── internal-but-public.md

3 directories, 3 files
[hendry@t480s foobar]$ tree public/
public/
├── 2019
│   └── index.xml
├── 2020
│   └── index.xml
├── index.xml
└── private-ish
    └── index.xml

3 directories, 4 files

First I don’t want RSS feeds in the sections like 2019/index.xml 2020/index.xml, I just want one top level /index.xml … how do I achieve that please?

Next I just want only sections to be included except “private-ish” in my feed.

I tried customizing my layout like so:

[hendry@t480s foobar]$ vim public/index.xml
<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>
    <lastBuildDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>
    {{ with .OutputFormats.Get "RSS" }}
    {{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }}
    {{ end }}
  {{ range .Data.Pages -}}
  {{- if or (ne .Section "private-ish") }}
    <item>le>{{ .Title }}</title>
      <link>{{ .Permalink }}</link>
      <pubDate>{{ .Lastmod.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
      <guid>{{ .Permalink }}</guid>
      <description>{{ .Summary | html }}</description>
    </item>
    {{ end }}
    {{ end }}
  </channel>
</rss>

But that bizaarely only shows indexes and not not the pages!?

[hendry@t480s foobar]$ cat public/index.xml
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Example</title>
    <link>https://example.com/</link>
    <lastBuildDate>Mon, 13 Jul 2020 13:34:38 +0800</lastBuildDate>

    <atom:link href="https://example.com/index.xml" rel="self" type="application/rss+xml" />


    <item>
      <title>2020s</title>
      <link>https://example.com/2020/</link>
      <pubDate>Mon, 13 Jul 2020 13:34:38 +0800</pubDate>
      <guid>https://example.com/2020/</guid>
      <description></description>
    </item>


    <item>
      <title>2019s</title>
      <link>https://example.com/2019/</link>
      <pubDate>Mon, 13 Jul 2020 13:33:50 +0800</pubDate>
      <guid>https://example.com/2019/</guid>
      <description></description>
    </item>



  </channel>
</rss>

What am I missing please? Sorry I am trying to simplify https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/_default/rss.xml since I found the first few lines too hard to fathom.

Thank you in advance!

config.toml:

[outputs]
home = ["HTML", "RSS"]
page = ["HTML"]
section = ["HTML"]
taxonomy = ["HTML"]
term = ["HTML"]

This is not a good reason to delete code. You are creating problems, not solving them.

This can be simplified to:

{{ if ne .Section "private-ish" }}

This is the default RSS template, with a conditional to exclude the “private-ish” section.

layouts/_default/rss.xml:

{{- $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\" 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 }}
      {{ if ne .Section "private-ish" }}
      <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>
        <description>{{ .Summary | html }}</description>
      </item>
      {{ end }}
    {{ end }}
  </channel>
</rss>

Thank you! Would still love to know why range .Data.Pages didn’t work to iterate through my content.

I delete code to better illustrate the problem and hopefully help me better understand hugo templates.

.Pages

Collection of regular pages and only first-level section pages under the current list page.

See https://gohugo.io/variables/site/#site-pages

1 Like

I use enableGitInfo: true since I don’t feel the need to set a Date frontmatter in my posts. That means I need IIUC:

<pubDate>{{ .Lastmod.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>

Next issue is that the last post doesn’t appear at the top. Aka the posts are not ordered by date.

Perhaps I should just give up on using enableGitInfo and resort to front matter instead? :rofl:

Furthermore I need to exclude several sections and I’m struggling to figure out how to do that.

[hendry@t480s foobar]$ rm -rf public/
[hendry@t480s foobar]$ grep exclude layouts/_default/rss.xml
      {{ if and (ne .Section "private-ish") (ne .Section "2020") (ne .Section "exclude-me-too")  }}
[hendry@t480s foobar]$ tree content/
content/
├── 2019
│   └── old.md
├── 2020
│   └── fresh.md
├── exclude-me-too
│   └── index.md
└── private-ish
    └── internal-but-public.md

4 directories, 4 files
[hendry@t480s foobar]$ hugo
Building sites … WARN 2020/07/14 10:33:09 found no layout file for "HTML" for kind "home": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
WARN 2020/07/14 10:33:09 found no layout file for "HTML" for kind "section": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
WARN 2020/07/14 10:33:09 found no layout file for "HTML" for kind "page": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
WARN 2020/07/14 10:33:09 found no layout file for "HTML" for kind "page": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
WARN 2020/07/14 10:33:09 found no layout file for "HTML" for kind "page": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
WARN 2020/07/14 10:33:09 found no layout file for "HTML" for kind "page": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
WARN 2020/07/14 10:33:09 found no layout file for "HTML" for kind "section": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
WARN 2020/07/14 10:33:09 found no layout file for "HTML" for kind "section": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.

                   | EN
-------------------+-----
  Pages            |  4
  Paginator pages  |  0
  Non-page files   |  0
  Static files     |  0
  Processed images |  0
  Aliases          |  0
  Sitemaps         |  0
  Cleaned          |  0

Total in 9 ms
[hendry@t480s foobar]$ grep exclude public/index.xml
        <link>https://example.com/exclude-me-too/</link>
        <guid>https://example.com/exclude-me-too/</guid>
[hendry@t480s foobar]$

Don’t mess with the date in the template. Instead, add this to config.toml:

[frontmatter]
date = ["lastmod", "date", "publishDate"]

See https://gohugo.io/getting-started/configuration/#configure-dates.

Your “exclude” code looks fine:

{{ if and (ne .Section "private-ish") (ne .Section "2020") (ne .Section "exclude-me-too")  }}

It will exclude those sections from being listed, but it will not prevent them from being built.

My exclude code doesn’t work since it appears in my

[hendry@t480s foobar]$ grep exclude public/index.xml
        <link>https://example.com/exclude-me-too/</link>
        <guid>https://example.com/exclude-me-too/</guid>

Thank you for the frontmatter date trick. That was driving me NUTS! enableGitInfo should ideally imply that.

Please repost the current version of your RSS template.

Here’s the test site files which includes the template: https://s.natalian.org/2020-07-14/exclude-me-too.zip

Thank you for sharing your code. This makes issue resolution much more efficient, and others are more likely to offer assistance when you do so.

The “exclude-me-too” directory is not a section. It contains “index.md”, which makes it a page.

Insert {{ .Section }} into your RSS template to troubleshoot.

If you want to exclude by directory name, you can do this:

{{ if and (ne .Section "private-ish") (ne .Section "2020") (not (strings.HasPrefix .File.Dir "exclude-me-too")) }}

Thank you again!

My next thought is that can i match all the section with \d\d\d\d yyyy structure?

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.