Skip to content

Commit 7f30617

Browse files
jmooringbep
authored andcommitted
hugolib: Allow override of sitemap file name
Closes gohugoio#12525
1 parent 931e096 commit 7f30617

File tree

2 files changed

+61
-14
lines changed

2 files changed

+61
-14
lines changed

‎hugolib/content_map_page.go‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,11 +1881,19 @@ func (sa *sitePagesAssembler) addStandalonePages() error {
18811881
}
18821882

18831883
if sitemapEnabled {
1884-
addStandalone("/_sitemap", kinds.KindSitemap, output.SitemapFormat)
1885-
skipSitemapIndex := s.Conf.IsMultihost() || !(s.Conf.DefaultContentLanguageInSubdir() || s.Conf.IsMultilingual())
1884+
of := output.SitemapFormat
1885+
if s.conf.Sitemap.Filename != "" {
1886+
of.BaseName = paths.Filename(s.conf.Sitemap.Filename)
1887+
}
1888+
addStandalone("/_sitemap", kinds.KindSitemap, of)
18861889

1890+
skipSitemapIndex := s.Conf.IsMultihost() || !(s.Conf.DefaultContentLanguageInSubdir() || s.Conf.IsMultilingual())
18871891
if !skipSitemapIndex {
1888-
addStandalone("/_sitemapindex", kinds.KindSitemapIndex, output.SitemapIndexFormat)
1892+
of = output.SitemapIndexFormat
1893+
if s.conf.Sitemap.Filename != "" {
1894+
of.BaseName = paths.Filename(s.conf.Sitemap.Filename)
1895+
}
1896+
addStandalone("/_sitemapindex", kinds.KindSitemapIndex, of)
18891897
}
18901898
}
18911899

‎hugolib/content_map_test.go‎

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package hugolib
1616
import (
1717
"fmt"
1818
"path/filepath"
19+
"strings"
1920
"testing"
2021

2122
qt "github.com/frankban/quicktest"
@@ -27,7 +28,7 @@ func TestContentMapSite(t *testing.T) {
2728
pageTempl := `
2829
---
2930
title: "Page %d"
30-
date: "2019-06-0%d"
31+
date: "2019-06-0%d"
3132
lastMod: "2019-06-0%d"
3233
categories: [%q]
3334
---
@@ -54,7 +55,7 @@ draft: true
5455
title: "Hugo Home"
5556
cascade:
5657
description: "Common Description"
57-
58+
5859
---
5960
6061
Home Content.
@@ -110,7 +111,7 @@ IsDescendant overlap2: false: {{ $overlap2.IsDescendant $overlap1 }}
110111
IsAncestor overlap1: false: {{ $overlap1.IsAncestor $overlap2 }}
111112
IsAncestor overlap2: false: {{ $overlap2.IsAncestor $overlap1 }}
112113
FirstSection: {{ $blogSub.FirstSection.RelPermalink }} {{ $blog.FirstSection.RelPermalink }} {{ $home.FirstSection.RelPermalink }} {{ $page.FirstSection.RelPermalink }}
113-
InSection: true: {{ $page.InSection $blog }} false: {{ $page.InSection $blogSub }}
114+
InSection: true: {{ $page.InSection $blog }} false: {{ $page.InSection $blogSub }}
114115
Next: {{ $page2.Next.RelPermalink }}
115116
NextInSection: {{ $page2.NextInSection.RelPermalink }}
116117
Pages: {{ range $blog.Pages }}{{ .RelPermalink }}|{{ end }}
@@ -140,11 +141,11 @@ Draft5: {{ if (.Site.GetPage "blog/draftsection/sub/page") }}FOUND{{ end }}|
140141
Num Regular: 9
141142
Main Sections: [blog]
142143
Pag Num Pages: 9
143-
144-
Home: Hugo Home|/|2019-06-08|Current Section: /|Resources:
145-
Blog Section: Blogs|/blog/|2019-06-08|Current Section: /blog|Resources:
144+
145+
Home: Hugo Home|/|2019-06-08|Current Section: /|Resources:
146+
Blog Section: Blogs|/blog/|2019-06-08|Current Section: /blog|Resources:
146147
Blog Sub Section: Page 3|/blog/subsection/|2019-06-03|Current Section: /blog/subsection|Resources: application: /blog/subsection/subdata.json|
147-
Page: Page 1|/blog/page1/|2019-06-01|Current Section: /blog|Resources:
148+
Page: Page 1|/blog/page1/|2019-06-01|Current Section: /blog|Resources:
148149
Bundle: Page 12|/blog/bundle/|0001-01-01|Current Section: /blog|Resources: application: /blog/bundle/data.json|page: |
149150
IsDescendant: true: true true: true true: true true: true true: true false: false false: false
150151
IsAncestor: true: true true: true true: true true: true true: true true: true false: false false: false false: false false: false
@@ -153,7 +154,7 @@ Draft5: {{ if (.Site.GetPage "blog/draftsection/sub/page") }}FOUND{{ end }}|
153154
IsAncestor overlap1: false: false
154155
IsAncestor overlap2: false: false
155156
FirstSection: /blog/ /blog/ / /blog/
156-
InSection: true: true false: false
157+
InSection: true: true false: false
157158
Next: /blog/page3/
158159
NextInSection: /blog/page3/
159160
Pages: /blog/page3/|/blog/subsection/|/blog/page2/|/blog/page1/|/blog/bundle/|
@@ -165,13 +166,13 @@ Draft5: {{ if (.Site.GetPage "blog/draftsection/sub/page") }}FOUND{{ end }}|
165166
Pag Blog Num Pages: 4
166167
Blog Num RegularPages: 4
167168
Blog Num Pages: 5
168-
169+
169170
Draft1: |
170171
Draft2: FOUND|
171172
Draft3: FOUND|
172173
Draft4: FOUND|
173174
Draft5: FOUND|
174-
175+
175176
`)
176177
}
177178

@@ -185,7 +186,7 @@ title = "Integration Test"
185186
disableKinds=["page", "section", "taxonomy", "term", "sitemap", "robotsTXT", "RSS"]
186187
-- layouts/index.html --
187188
Home: {{ .Title }}|
188-
189+
189190
`
190191

191192
b := NewIntegrationTestBuilder(
@@ -358,3 +359,41 @@ p1-foo.txt
358359
b.AssertFileExists("public/s1/p1-foo.txt", true) // failing test
359360
b.AssertFileExists("public/s1/p1/index.html", true)
360361
}
362+
363+
func TestSitemapOverrideFilename(t *testing.T) {
364+
t.Parallel()
365+
366+
files := `
367+
-- hugo.toml --
368+
baseURL = 'https://example.org/'
369+
disableKinds = ['page','rss','section','taxonomy','term']
370+
defaultContentLanguage = 'de'
371+
defaultContentLanguageInSubdir = true
372+
[languages.de]
373+
[languages.en]
374+
[sitemap]
375+
filename = 'foo.xml'
376+
-- layouts/index.html --
377+
irrelevant
378+
`
379+
380+
b := Test(t, files)
381+
382+
b.AssertFileExists("public/de/foo.xml", true)
383+
b.AssertFileExists("public/en/foo.xml", true)
384+
b.AssertFileContent("public/foo.xml",
385+
"<loc>https://example.org/de/foo.xml</loc>",
386+
"<loc>https://example.org/en/foo.xml</loc>",
387+
)
388+
389+
files = strings.ReplaceAll(files, "filename = 'foo.xml'", "")
390+
391+
b = Test(t, files)
392+
393+
b.AssertFileExists("public/de/sitemap.xml", true)
394+
b.AssertFileExists("public/en/sitemap.xml", true)
395+
b.AssertFileContent("public/sitemap.xml",
396+
"<loc>https://example.org/de/sitemap.xml</loc>",
397+
"<loc>https://example.org/en/sitemap.xml</loc>",
398+
)
399+
}

0 commit comments

Comments
 (0)