From 97bd2a6b1b22d537569368bb17eae6a99d99a39c Mon Sep 17 00:00:00 2001 From: Henning Vogt Date: Sat, 10 Jun 2023 20:18:51 +0200 Subject: [PATCH] Add sitemap.xml --- generator.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++- go.mod | 2 +- page.go | 15 +++++++++++++++ sitemap/url.go | 11 +++++++++++ sitemap/urlset.go | 9 +++++++++ 5 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 sitemap/url.go create mode 100644 sitemap/urlset.go diff --git a/generator.go b/generator.go index 2a98a93..ce4ae39 100644 --- a/generator.go +++ b/generator.go @@ -1,11 +1,14 @@ package main import ( + "encoding/xml" "html/template" "os" "path/filepath" cp "github.com/otiai10/copy" + + "github.com/bytehunger/snack/sitemap" ) const outputDir = "build" @@ -56,7 +59,9 @@ func (g *Generator) Generate() error { } } - return nil + err = g.GenerateSitemap(site) + + return err } func (g *Generator) GeneratePage(data *RenderData) error { @@ -140,3 +145,44 @@ func (g *Generator) GeneratePage(data *RenderData) error { return nil } + +func (g *Generator) GenerateSitemap(site Site) error { + urls := []sitemap.URL{} + + for _, page := range site.Pages { + + // Do not include noindex pages. + if page.NoIndex { + continue + } + + // Append all pages to the URLSet. + urls = append(urls, sitemap.URL{ + Loc: page.URL(site.Host), + }) + } + + urlset := sitemap.URLSet{ + URLs: urls, + XMLNS: "http://www.sitemaps.org/schemas/sitemap/0.9", + } + + sitemap, err := xml.MarshalIndent(urlset, "", " ") + + if err != nil { + return err + } + + path := filepath.Join(outputDir, "sitemap.xml") + file, err := os.Create(path) + + if err != nil { + return err + } + + defer file.Close() + + _, err = file.Write(sitemap) + + return err +} diff --git a/go.mod b/go.mod index 32e230c..3fe56e7 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/webhunger-ch/snack +module github.com/bytehunger/snack go 1.18 diff --git a/page.go b/page.go index d7ee90b..3ce738e 100644 --- a/page.go +++ b/page.go @@ -1,5 +1,7 @@ package main +import "net/url" + type Page struct { Title string `yaml:"title"` Path string `yaml:"path"` @@ -7,3 +9,16 @@ type Page struct { Sections []Section `yaml:"sections"` NoIndex bool `yaml:"noIndex" json:"noIndex"` } + +// Assemble the complete URL for the page with given domain. +func (p *Page) URL(host string) string { + url, err := url.Parse(host) + + if err != nil { + panic(err) + } + + url.Path = p.Path + + return url.String() +} diff --git a/sitemap/url.go b/sitemap/url.go new file mode 100644 index 0000000..1dc95da --- /dev/null +++ b/sitemap/url.go @@ -0,0 +1,11 @@ +package sitemap + +import "encoding/xml" + +type URL struct { + XMLName xml.Name `xml:"url"` + Loc string `xml:"loc"` + ChangeFreq string `xml:"changefreq,omitempty"` + LastMod string `xml:"lastmod,omitempty"` + Priority string `xml:"priority,omitempty"` +} diff --git a/sitemap/urlset.go b/sitemap/urlset.go new file mode 100644 index 0000000..745c959 --- /dev/null +++ b/sitemap/urlset.go @@ -0,0 +1,9 @@ +package sitemap + +import "encoding/xml" + +type URLSet struct { + XMLName xml.Name `xml:"urlset"` + XMLNS string `xml:"xmlns,attr"` + URLs []URL `xml:"url"` +}