2 Commits

Author SHA1 Message Date
4e3f22a528 make sitemap url limit adjustable 2023-03-04 15:59:06 +01:00
ec86a0396a fixed multiple slashes in path 2023-03-03 21:01:05 +01:00

View File

@@ -4,6 +4,7 @@ import (
"encoding/xml"
"fmt"
"math"
"path"
)
type ChangeFrequency string
@@ -45,13 +46,14 @@ type sitemapXML struct {
}
type SiteMap struct {
urls []*urlXML
BaseURL string
urls []*urlXML
BaseURL string
MaxURLPerSiteMap int
}
func (m *SiteMap) AddURL(url string, lastMod *string, freq *ChangeFrequency, prio *float64) {
m.urls = append(m.urls, &urlXML{
Loc: fmt.Sprintf("https://%s/%s", m.BaseURL, url),
Loc: fmt.Sprintf("https://%s", path.Join(m.BaseURL, url)),
LastMod: lastMod,
ChangeFreq: freq,
Priority: prio,
@@ -63,8 +65,7 @@ func prefixXML(content []byte) []byte {
}
func (m *SiteMap) SiteMapIndex() ([]byte, error) {
nSiteMaps := int(math.Floor(float64(len(m.urls))/50000 + 1))
nSiteMaps := int(math.Floor(float64(len(m.urls))/float64(m.MaxURLPerSiteMap) + 1))
index := &siteMapIndexXML{}
for i := 0; i < nSiteMaps; i++ {
@@ -82,17 +83,17 @@ func (m *SiteMap) SiteMapIndex() ([]byte, error) {
}
func (m *SiteMap) SiteMap(id int) ([]byte, error) {
if 50000*(id) >= len(m.urls) {
if m.MaxURLPerSiteMap*(id) >= len(m.urls) {
return nil, nil
}
siteMap := &urlSetXML{}
var urls []*urlXML
if 50000*(id+1) >= len(m.urls) {
urls = m.urls[50000*id:]
if m.MaxURLPerSiteMap*(id+1) >= len(m.urls) {
urls = m.urls[m.MaxURLPerSiteMap*id:]
} else {
urls = m.urls[50000*id : 50000*(id+1)]
urls = m.urls[m.MaxURLPerSiteMap*id : m.MaxURLPerSiteMap*(id+1)]
}
siteMap.URLs = append(siteMap.URLs, urls...)