Skip to content

Commit

Permalink
tpl/embedded: Exclude pages without Permalink from sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaza authored Feb 6, 2021
1 parent 92c6c40 commit 4867cd1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
18 changes: 14 additions & 4 deletions hugolib/site_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,21 @@ categories = [ "hugo" ]
+++
Front Matter with Ordered Pages 4. This is longer content`

var weightedPage5 = `+++
weight = "5"
title = "Five"
[_build]
render = "never"
+++
Front Matter with Ordered Pages 5`

var weightedSources = [][2]string{
{filepath.FromSlash("sect/doc1.md"), weightedPage1},
{filepath.FromSlash("sect/doc2.md"), weightedPage2},
{filepath.FromSlash("sect/doc3.md"), weightedPage3},
{filepath.FromSlash("sect/doc4.md"), weightedPage4},
{filepath.FromSlash("sect/doc5.md"), weightedPage5},
}

func TestOrderedPages(t *testing.T) {
Expand Down Expand Up @@ -979,8 +989,8 @@ func TestClassCollector(t *testing.T) {

b := newTestSitesBuilder(t)
b.WithConfigFile("toml", fmt.Sprintf(`
minify = %t
[build]
Expand All @@ -989,7 +999,7 @@ minify = %t
`, minify))

b.WithTemplates("index.html", `
<div id="el1" class="a b c">Foo</div>
Some text.
Expand Down Expand Up @@ -1047,7 +1057,7 @@ func TestClassCollectorStress(t *testing.T) {

b := newTestSitesBuilder(t)
b.WithConfigFile("toml", `
disableKinds = ["home", "section", "term", "taxonomy" ]
[languages]
Expand Down
3 changes: 3 additions & 0 deletions hugolib/sitemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ import (

const sitemapTemplate = `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{{ range .Data.Pages }}
{{- if .Permalink -}}
<url>
<loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
<lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
<changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
<priority>{{ .Sitemap.Priority }}</priority>{{ end }}
</url>
{{- end -}}
{{ end }}
</urlset>`

Expand Down Expand Up @@ -80,6 +82,7 @@ func doTestSitemapOutput(t *testing.T, internal bool) {

content := readDestination(th, th.Fs, outputSitemap)
c.Assert(content, qt.Not(qt.Contains), "404")
c.Assert(content, qt.Not(qt.Contains), "<loc></loc>")
}

func TestParseSitemap(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions tpl/tplimpl/embedded/templates.autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tpl/tplimpl/embedded/templates/_default/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
{{ range .Data.Pages }}
{{- if .Permalink -}}
<url>
<loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
<lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
Expand All @@ -18,5 +19,6 @@
href="{{ .Permalink }}"
/>{{ end }}
</url>
{{- end -}}
{{ end }}
</urlset>

0 comments on commit 4867cd1

Please sign in to comment.