Skip to content

Commit

Permalink
Add sitemap.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
henvo committed Jun 10, 2023
1 parent 15c950a commit 97bd2a6
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
48 changes: 47 additions & 1 deletion generator.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -56,7 +59,9 @@ func (g *Generator) Generate() error {
}
}

return nil
err = g.GenerateSitemap(site)

return err
}

func (g *Generator) GeneratePage(data *RenderData) error {
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/webhunger-ch/snack
module github.com/bytehunger/snack

go 1.18

Expand Down
15 changes: 15 additions & 0 deletions page.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
package main

import "net/url"

type Page struct {
Title string `yaml:"title"`
Path string `yaml:"path"`
Description string `yaml:"description"`
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()
}
11 changes: 11 additions & 0 deletions sitemap/url.go
Original file line number Diff line number Diff line change
@@ -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"`
}
9 changes: 9 additions & 0 deletions sitemap/urlset.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit 97bd2a6

Please sign in to comment.