aboutsummaryrefslogtreecommitdiffhomepage
path: root/tpl
diff options
context:
space:
mode:
authorJoe Mooring <[email protected]>2024-04-01 15:30:03 -0700
committerBjørn Erik Pedersen <[email protected]>2024-04-02 11:21:03 +0200
commit6738a3e79dd545603d9851832bc3140fd184bfef (patch)
tree9faff4e6a3fed40c8c9f59df19fd296c48ad49ef /tpl
parent2f7df4b926fe20849e9030edfe9b1d94b8cadfa9 (diff)
downloadhugo-6738a3e79dd545603d9851832bc3140fd184bfef.tar.gz
hugo-6738a3e79dd545603d9851832bc3140fd184bfef.zip
tpl/tplimpl: Optionally exclude content from sitemap
Define global inclusion/exclusion in site configuration, and override via front matter. For example, to exclude a page from the sitemap: [sitemap] disable = true # default is false Closes #653 Closes #12282 Co-authored-by: kolappannathan <[email protected]> Co-authored-by: felicianotech <[email protected]>
Diffstat (limited to 'tpl')
-rw-r--r--tpl/tplimpl/embedded/templates/_default/sitemap.xml2
-rw-r--r--tpl/tplimpl/tplimpl_integration_test.go51
2 files changed, 52 insertions, 1 deletions
diff --git a/tpl/tplimpl/embedded/templates/_default/sitemap.xml b/tpl/tplimpl/embedded/templates/_default/sitemap.xml
index b1e4b2d2d..de1f467d1 100644
--- a/tpl/tplimpl/embedded/templates/_default/sitemap.xml
+++ b/tpl/tplimpl/embedded/templates/_default/sitemap.xml
@@ -1,7 +1,7 @@
{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
- {{ range .Data.Pages }}
+ {{ range where .Pages "Sitemap.Disable" "ne" true }}
{{- if .Permalink -}}
<url>
<loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
diff --git a/tpl/tplimpl/tplimpl_integration_test.go b/tpl/tplimpl/tplimpl_integration_test.go
index abda3af29..28d442e0d 100644
--- a/tpl/tplimpl/tplimpl_integration_test.go
+++ b/tpl/tplimpl/tplimpl_integration_test.go
@@ -254,3 +254,54 @@ disable = false
`s.src = '//' + "foo" + '.disqus.com/embed.js';`,
)
}
+
+func TestSitemap(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- hugo.toml --
+disableKinds = ['home','rss','section','taxonomy','term']
+[sitemap]
+disable = true
+-- content/p1.md --
+---
+title: p1
+sitemap:
+ p1_disable: foo
+---
+-- content/p2.md --
+---
+title: p2
+
+---
+-- layouts/_default/single.html --
+{{ .Title }}
+`
+
+ // Test A: Exclude all pages via site config.
+ b := hugolib.Test(t, files)
+ b.AssertFileContentExact("public/sitemap.xml",
+ "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">\n \n</urlset>\n",
+ )
+
+ // Test B: Include all pages via site config.
+ files_b := strings.ReplaceAll(files, "disable = true", "disable = false")
+ b = hugolib.Test(t, files_b)
+ b.AssertFileContentExact("public/sitemap.xml",
+ "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">\n <url>\n <loc>/p1/</loc>\n </url><url>\n <loc>/p2/</loc>\n </url>\n</urlset>\n",
+ )
+
+ // Test C: Exclude all pages via site config, but include p1 via front matter.
+ files_c := strings.ReplaceAll(files, "p1_disable: foo", "disable: false")
+ b = hugolib.Test(t, files_c)
+ b.AssertFileContentExact("public/sitemap.xml",
+ "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">\n <url>\n <loc>/p1/</loc>\n </url>\n</urlset>\n",
+ )
+
+ // Test D: Include all pages via site config, but exclude p1 via front matter.
+ files_d := strings.ReplaceAll(files_b, "p1_disable: foo", "disable: true")
+ b = hugolib.Test(t, files_d)
+ b.AssertFileContentExact("public/sitemap.xml",
+ "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">\n <url>\n <loc>/p2/</loc>\n </url>\n</urlset>\n",
+ )
+}