-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitemap.go
56 lines (49 loc) · 1.46 KB
/
sitemap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package seo
import (
"context"
"encoding/xml"
"fmt"
"time"
)
// A standard xml sitemap
/*<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.example.com/foo.html</loc>
<lastmod>2022-06-04</lastmod>
</url>
</urlset>
*/
type Sitemap struct {
XMLName xml.Name `xml:"urlset"`
XMLNS string `xml:"xmlns,attr"`
Urls []*SitemapURL `xml:"url"`
}
// URL is a structure of <url> in <sitemap>
type SitemapURL struct {
XMLName xml.Name `xml:"url"`
Name string `xml:"name,omitempty"` // Extra field, not part of the standard
Category string `xml:"category,omitempty"` // Extra field, not part of the standard
Description string `xml:"description,omitempty"` // Extra field, not part of the standard
Loc string `xml:"loc"`
ChangeFreq string `xml:"changefreq"`
LastMod string `xml:"lastmod"`
Priority string `xml:"priority"`
}
// Adds a sitemap item to a feed
func (m *MapGenerator) AddToSitemap(ctx context.Context, f Fetcher, sitemap *Sitemap, category, id string, priority float64) error {
e, err := m.Add(ctx, f, id)
if err != nil {
return err
}
sitemap.Urls = append(sitemap.Urls, &SitemapURL{
Name: e.Name,
Category: category,
Description: e.Description,
Loc: e.URL,
ChangeFreq: "daily",
LastMod: e.UpdatedAt.Format(time.RFC3339),
Priority: fmt.Sprint(priority),
})
return nil
}