GoBlog/sitemap.go

146 lines
3.6 KiB
Go
Raw Normal View History

2020-09-22 15:15:17 +00:00
package main
import (
2021-03-09 20:35:17 +00:00
"fmt"
2020-09-22 15:15:17 +00:00
"net/http"
"time"
2020-10-15 15:32:46 +00:00
"github.com/araddon/dateparse"
"github.com/snabb/sitemap"
2020-09-22 15:15:17 +00:00
)
2020-10-15 17:50:34 +00:00
const sitemapPath = "/sitemap.xml"
func (a *goBlog) serveSitemap(w http.ResponseWriter, r *http.Request) {
2020-10-15 17:50:34 +00:00
sm := sitemap.New()
sm.Minify = true
2021-03-09 20:35:17 +00:00
// Blogs
for b, bc := range a.cfg.Blogs {
2021-03-09 20:35:17 +00:00
// Blog
sm.Add(&sitemap.URL{
2021-06-11 06:24:41 +00:00
Loc: a.getFullAddress(bc.getRelativePath("")),
2021-03-09 20:35:17 +00:00
})
// Sections
for _, section := range bc.Sections {
if section.Name != "" {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(section.Name)),
2021-03-09 20:35:17 +00:00
})
}
}
// Taxonomies
for _, taxonomy := range bc.Taxonomies {
if taxonomy.Name != "" {
// Taxonomy
taxPath := bc.getRelativePath("/" + taxonomy.Name)
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(taxPath),
2021-03-09 20:35:17 +00:00
})
// Values
if taxValues, err := a.db.allTaxonomyValues(b, taxonomy.Name); err == nil {
2021-03-09 20:35:17 +00:00
for _, tv := range taxValues {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(taxPath + "/" + urlize(tv)),
2021-03-09 20:35:17 +00:00
})
}
}
}
}
// Year / month archives
if dates, err := a.db.allPublishedDates(b); err == nil {
2021-03-09 20:35:17 +00:00
already := map[string]bool{}
for _, d := range dates {
// Year
yearPath := bc.getRelativePath("/" + fmt.Sprintf("%0004d", d.year))
if !already[yearPath] {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(yearPath),
2021-03-09 20:35:17 +00:00
})
already[yearPath] = true
}
// Specific month
monthPath := yearPath + "/" + fmt.Sprintf("%02d", d.month)
if !already[monthPath] {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(monthPath),
2021-03-09 20:35:17 +00:00
})
already[monthPath] = true
}
// Specific day
dayPath := monthPath + "/" + fmt.Sprintf("%02d", d.day)
if !already[dayPath] {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(dayPath),
2021-03-09 20:35:17 +00:00
})
already[dayPath] = true
}
// Generic month
genericMonthPath := bc.getRelativePath("/x/" + fmt.Sprintf("%02d", d.month))
2021-03-09 20:35:17 +00:00
if !already[genericMonthPath] {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(genericMonthPath),
2021-03-09 20:35:17 +00:00
})
already[genericMonthPath] = true
}
// Specific day
genericMonthDayPath := genericMonthPath + "/" + fmt.Sprintf("%02d", d.day)
if !already[genericMonthDayPath] {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(genericMonthDayPath),
2021-03-09 20:35:17 +00:00
})
already[genericMonthDayPath] = true
}
}
2020-10-15 17:50:34 +00:00
}
2021-03-09 20:35:17 +00:00
// Photos
if bc.Photos != nil && bc.Photos.Enabled {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(bc.Photos.Path)),
2021-03-09 20:35:17 +00:00
})
2020-09-22 15:15:17 +00:00
}
2021-03-09 20:35:17 +00:00
// Search
if bc.Search != nil && bc.Search.Enabled {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(bc.Search.Path)),
2021-03-09 20:35:17 +00:00
})
}
// Stats
if bc.BlogStats != nil && bc.BlogStats.Enabled {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(bc.BlogStats.Path)),
2021-03-09 20:35:17 +00:00
})
}
2021-05-08 19:22:48 +00:00
// Blogroll
if bc.Blogroll != nil && bc.Blogroll.Enabled {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(bc.Blogroll.Path)),
2021-05-08 19:22:48 +00:00
})
}
2021-03-09 20:35:17 +00:00
// Custom pages
for _, cp := range bc.CustomPages {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(cp.Path),
2021-03-09 20:35:17 +00:00
})
}
}
// Posts
if posts, err := a.db.getPosts(&postsRequestConfig{status: statusPublished}); err == nil {
2021-03-09 20:35:17 +00:00
for _, p := range posts {
item := &sitemap.URL{Loc: a.fullPostURL(p)}
2021-03-09 20:35:17 +00:00
var lastMod time.Time
if p.Updated != "" {
lastMod, _ = dateparse.ParseLocal(p.Updated)
}
if p.Published != "" && lastMod.IsZero() {
lastMod, _ = dateparse.ParseLocal(p.Published)
}
if !lastMod.IsZero() {
item.LastMod = &lastMod
}
sm.Add(item)
2020-09-22 15:15:17 +00:00
}
}
2021-03-09 20:35:17 +00:00
// Write...
_, _ = sm.WriteTo(w) // Already minified
2020-09-22 15:15:17 +00:00
}