Skip to content

Commit

Permalink
hugolib: Allow override of sitemap file name
Browse files Browse the repository at this point in the history
Closes #12525
  • Loading branch information
jmooring authored and bep committed May 23, 2024
1 parent 931e096 commit 7f30617
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
14 changes: 11 additions & 3 deletions hugolib/content_map_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -1881,11 +1881,19 @@ func (sa *sitePagesAssembler) addStandalonePages() error {
}

if sitemapEnabled {
addStandalone("/_sitemap", kinds.KindSitemap, output.SitemapFormat)
skipSitemapIndex := s.Conf.IsMultihost() || !(s.Conf.DefaultContentLanguageInSubdir() || s.Conf.IsMultilingual())
of := output.SitemapFormat
if s.conf.Sitemap.Filename != "" {
of.BaseName = paths.Filename(s.conf.Sitemap.Filename)
}
addStandalone("/_sitemap", kinds.KindSitemap, of)

skipSitemapIndex := s.Conf.IsMultihost() || !(s.Conf.DefaultContentLanguageInSubdir() || s.Conf.IsMultilingual())
if !skipSitemapIndex {
addStandalone("/_sitemapindex", kinds.KindSitemapIndex, output.SitemapIndexFormat)
of = output.SitemapIndexFormat
if s.conf.Sitemap.Filename != "" {
of.BaseName = paths.Filename(s.conf.Sitemap.Filename)
}
addStandalone("/_sitemapindex", kinds.KindSitemapIndex, of)
}
}

Expand Down
61 changes: 50 additions & 11 deletions hugolib/content_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package hugolib
import (
"fmt"
"path/filepath"
"strings"
"testing"

qt "github.com/frankban/quicktest"
Expand All @@ -27,7 +28,7 @@ func TestContentMapSite(t *testing.T) {
pageTempl := `
---
title: "Page %d"
date: "2019-06-0%d"
date: "2019-06-0%d"
lastMod: "2019-06-0%d"
categories: [%q]
---
Expand All @@ -54,7 +55,7 @@ draft: true
title: "Hugo Home"
cascade:
description: "Common Description"
---
Home Content.
Expand Down Expand Up @@ -110,7 +111,7 @@ IsDescendant overlap2: false: {{ $overlap2.IsDescendant $overlap1 }}
IsAncestor overlap1: false: {{ $overlap1.IsAncestor $overlap2 }}
IsAncestor overlap2: false: {{ $overlap2.IsAncestor $overlap1 }}
FirstSection: {{ $blogSub.FirstSection.RelPermalink }} {{ $blog.FirstSection.RelPermalink }} {{ $home.FirstSection.RelPermalink }} {{ $page.FirstSection.RelPermalink }}
InSection: true: {{ $page.InSection $blog }} false: {{ $page.InSection $blogSub }}
InSection: true: {{ $page.InSection $blog }} false: {{ $page.InSection $blogSub }}
Next: {{ $page2.Next.RelPermalink }}
NextInSection: {{ $page2.NextInSection.RelPermalink }}
Pages: {{ range $blog.Pages }}{{ .RelPermalink }}|{{ end }}
Expand Down Expand Up @@ -140,11 +141,11 @@ Draft5: {{ if (.Site.GetPage "blog/draftsection/sub/page") }}FOUND{{ end }}|
Num Regular: 9
Main Sections: [blog]
Pag Num Pages: 9
Home: Hugo Home|/|2019-06-08|Current Section: /|Resources:
Blog Section: Blogs|/blog/|2019-06-08|Current Section: /blog|Resources:
Home: Hugo Home|/|2019-06-08|Current Section: /|Resources:
Blog Section: Blogs|/blog/|2019-06-08|Current Section: /blog|Resources:
Blog Sub Section: Page 3|/blog/subsection/|2019-06-03|Current Section: /blog/subsection|Resources: application: /blog/subsection/subdata.json|
Page: Page 1|/blog/page1/|2019-06-01|Current Section: /blog|Resources:
Page: Page 1|/blog/page1/|2019-06-01|Current Section: /blog|Resources:
Bundle: Page 12|/blog/bundle/|0001-01-01|Current Section: /blog|Resources: application: /blog/bundle/data.json|page: |
IsDescendant: true: true true: true true: true true: true true: true false: false false: false
IsAncestor: true: true true: true true: true true: true true: true true: true false: false false: false false: false false: false
Expand All @@ -153,7 +154,7 @@ Draft5: {{ if (.Site.GetPage "blog/draftsection/sub/page") }}FOUND{{ end }}|
IsAncestor overlap1: false: false
IsAncestor overlap2: false: false
FirstSection: /blog/ /blog/ / /blog/
InSection: true: true false: false
InSection: true: true false: false
Next: /blog/page3/
NextInSection: /blog/page3/
Pages: /blog/page3/|/blog/subsection/|/blog/page2/|/blog/page1/|/blog/bundle/|
Expand All @@ -165,13 +166,13 @@ Draft5: {{ if (.Site.GetPage "blog/draftsection/sub/page") }}FOUND{{ end }}|
Pag Blog Num Pages: 4
Blog Num RegularPages: 4
Blog Num Pages: 5
Draft1: |
Draft2: FOUND|
Draft3: FOUND|
Draft4: FOUND|
Draft5: FOUND|
`)
}

Expand All @@ -185,7 +186,7 @@ title = "Integration Test"
disableKinds=["page", "section", "taxonomy", "term", "sitemap", "robotsTXT", "RSS"]
-- layouts/index.html --
Home: {{ .Title }}|
`

b := NewIntegrationTestBuilder(
Expand Down Expand Up @@ -358,3 +359,41 @@ p1-foo.txt
b.AssertFileExists("public/s1/p1-foo.txt", true) // failing test
b.AssertFileExists("public/s1/p1/index.html", true)
}

func TestSitemapOverrideFilename(t *testing.T) {
t.Parallel()

files := `
-- hugo.toml --
baseURL = 'https://example.org/'
disableKinds = ['page','rss','section','taxonomy','term']
defaultContentLanguage = 'de'
defaultContentLanguageInSubdir = true
[languages.de]
[languages.en]
[sitemap]
filename = 'foo.xml'
-- layouts/index.html --
irrelevant
`

b := Test(t, files)

b.AssertFileExists("public/de/foo.xml", true)
b.AssertFileExists("public/en/foo.xml", true)
b.AssertFileContent("public/foo.xml",
"<loc>https://example.org/de/foo.xml</loc>",
"<loc>https://example.org/en/foo.xml</loc>",
)

files = strings.ReplaceAll(files, "filename = 'foo.xml'", "")

b = Test(t, files)

b.AssertFileExists("public/de/sitemap.xml", true)
b.AssertFileExists("public/en/sitemap.xml", true)
b.AssertFileContent("public/sitemap.xml",
"<loc>https://example.org/de/sitemap.xml</loc>",
"<loc>https://example.org/en/sitemap.xml</loc>",
)
}

0 comments on commit 7f30617

Please sign in to comment.