tpl/embedded: Exclude pages without Permalink from sitemap
authorJeremy Epstein <jazepstein@gmail.com>
Sat, 6 Feb 2021 17:30:09 +0000 (04:30 +1100)
committerGitHub <noreply@github.com>
Sat, 6 Feb 2021 17:30:09 +0000 (18:30 +0100)
hugolib/site_test.go
hugolib/sitemap_test.go
tpl/tplimpl/embedded/templates.autogen.go
tpl/tplimpl/embedded/templates/_default/sitemap.xml

index 922563420628344f037af600a74d22c4fc252522..8942f38f88ec43452ed7bcf51fd1a2a1d74c5d42 100644 (file)
@@ -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) {
@@ -979,8 +989,8 @@ func TestClassCollector(t *testing.T) {
 
                        b := newTestSitesBuilder(t)
                        b.WithConfigFile("toml", fmt.Sprintf(`
-                       
-                       
+
+
 minify = %t
 
 [build]
@@ -989,7 +999,7 @@ minify = %t
 `, minify))
 
                        b.WithTemplates("index.html", `
-       
+
 <div id="el1" class="a b c">Foo</div>
 
 Some text.
@@ -1047,7 +1057,7 @@ func TestClassCollectorStress(t *testing.T) {
 
        b := newTestSitesBuilder(t)
        b.WithConfigFile("toml", `
-       
+
 disableKinds = ["home", "section", "term", "taxonomy" ]
 
 [languages]
index 7302f6e6260b7499e607eb5ca52fee4b397d0172..3fa6ab3aeb8e05b4a30aca414531749c3fc039cf 100644 (file)
@@ -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>`
 
@@ -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) {
index 27a405bd84a7cdc793a8a5f28b4ec1ff10d9f039..316d4b1ef70cf829c375ae41a91b199c503bb88c 100644 (file)
@@ -63,6 +63,7 @@ var EmbeddedTemplates = [][2]string{
 <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 }}
@@ -79,6 +80,7 @@ var EmbeddedTemplates = [][2]string{
                 href="{{ .Permalink }}"
                 />{{ end }}
   </url>
+    {{- end -}}
   {{ end }}
 </urlset>
 `},
index 63f51195b04167f620d72b9da01ddb10e53bf482..cdb3748e8625cebe06709dbe5a6a8917c49b25fb 100644 (file)
@@ -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 }}
@@ -18,5 +19,6 @@
                 href="{{ .Permalink }}"
                 />{{ end }}
   </url>
+    {{- end -}}
   {{ end }}
 </urlset>