diff --git a/commands/hugo.go b/commands/hugo.go index cced30143..235f95f75 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -109,6 +109,7 @@ func InitializeConfig() { viper.SetDefault("CanonifyUrls", false) viper.SetDefault("Indexes", map[string]string{"tag": "tags", "category": "categories"}) viper.SetDefault("Permalinks", make(hugolib.PermalinkOverrides, 0)) + viper.SetDefault("Sitemap", hugolib.Sitemap{"", -1}) if hugoCmdV.PersistentFlags().Lookup("build-drafts").Changed { viper.Set("BuildDrafts", Draft) diff --git a/hugolib/page.go b/hugolib/page.go index 6417a63fa..7305e0ecd 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -141,7 +141,7 @@ func renderBytes(content []byte, pagefmt string) []byte { func newPage(filename string) *Page { page := Page{contentType: "", File: File{FileName: filename, Extension: "html"}, - Node: Node{Keywords: []string{}}, + Node: Node{Keywords: []string{}, Sitemap: Sitemap{Priority: -1}}, Params: make(map[string]interface{})} jww.DEBUG.Println("Reading from", page.File.FileName) @@ -342,6 +342,8 @@ func (page *Page) update(f interface{}) error { } case "status": page.Status = cast.ToString(v) + case "sitemap": + page.Sitemap = parseSitemap(cast.ToStringMap(v)) default: // If not one of the explicit values, store in Params switch vv := v.(type) { diff --git a/hugolib/site.go b/hugolib/site.go index f20cabc6f..839c87e7e 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -749,10 +749,33 @@ func (s *Site) RenderSitemap() error { return nil } + sitemapDefault := parseSitemap(viper.GetStringMap("Sitemap")) + optChanged := false n := s.NewNode() - n.Data["Pages"] = s.Pages + + // Prepend homepage to the list of pages + pages := make(Pages, 0) + + page := &Page{} + page.Site = s.Info + page.Url = "/" + + pages = append(pages, page) + pages = append(pages, s.Pages...) + + n.Data["Pages"] = pages + + for _, page := range pages { + if page.Sitemap.ChangeFreq == "" { + page.Sitemap.ChangeFreq = sitemapDefault.ChangeFreq + } + + if page.Sitemap.Priority == -1 { + page.Sitemap.Priority = sitemapDefault.Priority + } + } // Force `UglyUrls` option to force `sitemap.xml` file name switch s.Target.(type) { diff --git a/hugolib/sitemap.go b/hugolib/sitemap.go index 9510555aa..a8e38fe18 100644 --- a/hugolib/sitemap.go +++ b/hugolib/sitemap.go @@ -1,18 +1,28 @@ package hugolib -import jww "github.com/spf13/jwalterweatherman" +import ( + "github.com/spf13/cast" + jww "github.com/spf13/jwalterweatherman" +) type Sitemap struct { ChangeFreq string - Priority float32 + Priority float64 } -func (s Sitemap) Validate() { - if s.Priority < 0 { - jww.WARN.Printf("Sitemap priority should be greater than 0, found: %f", s.Priority) - s.Priority = 0 - } else if s.Priority > 1 { - jww.WARN.Printf("Sitemap priority should be lesser than 1, found: %f", s.Priority) - s.Priority = 1 +func parseSitemap(input map[string]interface{}) Sitemap { + sitemap := Sitemap{Priority: -1} + + for key, value := range input { + switch key { + case "changefreq": + sitemap.ChangeFreq = cast.ToString(value) + case "priority": + sitemap.Priority = cast.ToFloat64(value) + default: + jww.WARN.Printf("Unknown Sitemap field: %s\n", key) + } } + + return sitemap } diff --git a/hugolib/template_embedded.go b/hugolib/template_embedded.go index 29d7ce150..15c8cf545 100644 --- a/hugolib/template_embedded.go +++ b/hugolib/template_embedded.go @@ -70,8 +70,8 @@ func (t *GoHtmlTemplate) EmbedTemplates() { {{ .Permalink }} {{ safeHtml ( .Date.Format "2006-01-02T15:04:05-07:00" ) }}{{ with .Sitemap.ChangeFreq }} - {{ . }}{{ end }}{{ with .Sitemap.Priority }} - {{ . }}{{ end }} + {{ . }}{{ end }}{{ if ge .Sitemap.Priority 0.0 }} + {{ .Sitemap.Priority }}{{ end }} {{ end }} `)