hugolib: Make RSS item limit configurable
authorCameron Moore <moorereason@gmail.com>
Mon, 13 Feb 2017 16:11:22 +0000 (10:11 -0600)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 17 Feb 2017 18:53:08 +0000 (19:53 +0100)
Add a new rssLimit site configuration option with default of 15.  Prior
to this fix, you could create your own RSS feed to override the default
limit of 15, but we still had a hardcoded limit of 50 items set in
`hugolib.renderRSS()`.

With this option in place, the `range first 15 .Data.Pages` logic is no
longer hardcoded into the embedded RSS template.

Because the size of the slice passed to the template is now limited to
rssLimit instead of 50, this commit is a breaking change for sites
with a custom RSS template that expects more than 15 items.

Fixes #3035

docs/content/overview/configuration.md
hugolib/config.go
hugolib/rss_test.go
hugolib/site_render.go
tpl/tplimpl/template_embedded.go

index 21b424ad619fac3fe38cbadb77da49b4412de5ac..b2ed068048e293bc1774b4cf51d137fbb5259974 100644 (file)
@@ -171,6 +171,8 @@ along with their current, default values:
     pygmentsStyle:              "monokai"
     # true: use pygments-css or false: color-codes directly
     pygmentsUseClasses:         false
+    # maximum number of items in the RSS feed
+    rssLimit:                   15
     # default sitemap configuration map
     sitemap:
     # filesystem path to read files relative from
index 552b19dacf5d12e3f3c3e47fc0ebdcdb2e23a44e..2d0cfe8ee9a62b7ddb28dc031c79efa1bfdd15a7 100644 (file)
@@ -102,6 +102,7 @@ func loadDefaultSettingsFor(v *viper.Viper) {
        v.SetDefault("paginatePath", "page")
        v.SetDefault("blackfriday", c.NewBlackfriday())
        v.SetDefault("rSSUri", "index.xml")
+       v.SetDefault("rssLimit", 15)
        v.SetDefault("sectionPagesMenu", "")
        v.SetDefault("disablePathToLower", false)
        v.SetDefault("hasCJKLanguage", false)
index 0a7f84a42a1867ae2a09c81d44dbf9a6ec3b8b34..0d14bf5d6f247ec15ed4465ab80c7903350b251d 100644 (file)
@@ -15,6 +15,7 @@ package hugolib
 
 import (
        "path/filepath"
+       "strings"
        "testing"
 
        "github.com/spf13/hugo/deps"
@@ -27,11 +28,14 @@ func TestRSSOutput(t *testing.T) {
                th      = testHelper{cfg}
        )
 
+       rssLimit := len(weightedSources) - 1
+
        rssURI := "customrss.xml"
 
        cfg.Set("baseURL", "http://auth/bub/")
        cfg.Set("rssURI", rssURI)
        cfg.Set("title", "RSSTest")
+       cfg.Set("rssLimit", rssLimit)
 
        for _, src := range weightedSources {
                writeSource(t, fs, filepath.Join("content", "sect", src.Name), string(src.Content))
@@ -46,4 +50,10 @@ func TestRSSOutput(t *testing.T) {
        // Taxonomy RSS
        th.assertFileContent(t, fs, filepath.Join("public", "categories", "hugo", rssURI), true, "<?xml", "rss version", "Hugo on RSSTest")
 
+       // RSS Item Limit
+       content := readDestination(t, fs, filepath.Join("public", rssURI))
+       c := strings.Count(content, "<item>")
+       if c != rssLimit {
+               t.Errorf("incorrect RSS item count: expected %d, got %d", rssLimit, c)
+       }
 }
index fa398aa043206651693d135f467c561ad4d7a999..23b5a11b45ad7d896ba15e737e995b4fe2e27866 100644 (file)
@@ -152,9 +152,9 @@ func (s *Site) renderRSS(p *Page) error {
                rssPage.Date = zeroDate
        }
 
-       high := 50
-       if len(rssPage.Pages) > high {
-               rssPage.Pages = rssPage.Pages[:high]
+       limit := s.Cfg.GetInt("rssLimit")
+       if len(rssPage.Pages) > limit {
+               rssPage.Pages = rssPage.Pages[:limit]
                rssPage.Data["Pages"] = rssPage.Pages
        }
        rssURI := s.Language.GetString("rssURI")
index abe5937bd5ff1aba43e1d8bda11f4b65d89830f1..f0b32bdd69a1111126fce0835d15f53543c23945 100644 (file)
@@ -77,7 +77,7 @@ func (t *GoHTMLTemplate) EmbedTemplates() {
     <copyright>{{.}}</copyright>{{end}}{{ if not .Date.IsZero }}
     <lastBuildDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>{{ end }}
     <atom:link href="{{.Permalink}}" rel="self" type="application/rss+xml" />
-    {{ range first 15 .Data.Pages }}
+    {{ range .Data.Pages }}
     <item>
       <title>{{ .Title }}</title>
       <link>{{ .Permalink }}</link>