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"
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
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
|
2021-06-06 12:39:42 +00:00
|
|
|
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{
|
2021-06-10 20:09:50 +00:00
|
|
|
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{
|
2021-06-10 20:09:50 +00:00
|
|
|
Loc: a.getFullAddress(taxPath),
|
2021-03-09 20:35:17 +00:00
|
|
|
})
|
|
|
|
// Values
|
2021-06-06 12:39:42 +00:00
|
|
|
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{
|
2021-06-10 20:09:50 +00:00
|
|
|
Loc: a.getFullAddress(taxPath + "/" + urlize(tv)),
|
2021-03-09 20:35:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Year / month archives
|
2021-06-06 12:39:42 +00:00
|
|
|
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{
|
2021-06-10 20:09:50 +00:00
|
|
|
Loc: a.getFullAddress(yearPath),
|
2021-03-09 20:35:17 +00:00
|
|
|
})
|
|
|
|
already[yearPath] = true
|
|
|
|
}
|
2021-06-27 18:59:11 +00:00
|
|
|
// Month
|
2021-03-09 20:35:17 +00:00
|
|
|
monthPath := yearPath + "/" + fmt.Sprintf("%02d", d.month)
|
|
|
|
if !already[monthPath] {
|
|
|
|
sm.Add(&sitemap.URL{
|
2021-06-10 20:09:50 +00:00
|
|
|
Loc: a.getFullAddress(monthPath),
|
2021-03-09 20:35:17 +00:00
|
|
|
})
|
|
|
|
already[monthPath] = true
|
|
|
|
}
|
2021-06-27 18:59:11 +00:00
|
|
|
// Day
|
2021-03-09 20:35:17 +00:00
|
|
|
dayPath := monthPath + "/" + fmt.Sprintf("%02d", d.day)
|
|
|
|
if !already[dayPath] {
|
|
|
|
sm.Add(&sitemap.URL{
|
2021-06-10 20:09:50 +00:00
|
|
|
Loc: a.getFullAddress(dayPath),
|
2021-03-09 20:35:17 +00:00
|
|
|
})
|
|
|
|
already[dayPath] = true
|
|
|
|
}
|
2021-06-27 18:59:11 +00:00
|
|
|
// XXXX-MM
|
2021-06-10 20:09:50 +00:00
|
|
|
genericMonthPath := bc.getRelativePath("/x/" + fmt.Sprintf("%02d", d.month))
|
2021-03-09 20:35:17 +00:00
|
|
|
if !already[genericMonthPath] {
|
|
|
|
sm.Add(&sitemap.URL{
|
2021-06-10 20:09:50 +00:00
|
|
|
Loc: a.getFullAddress(genericMonthPath),
|
2021-03-09 20:35:17 +00:00
|
|
|
})
|
|
|
|
already[genericMonthPath] = true
|
|
|
|
}
|
2021-06-27 18:59:11 +00:00
|
|
|
// XXXX-MM-DD
|
2021-03-09 20:35:17 +00:00
|
|
|
genericMonthDayPath := genericMonthPath + "/" + fmt.Sprintf("%02d", d.day)
|
|
|
|
if !already[genericMonthDayPath] {
|
|
|
|
sm.Add(&sitemap.URL{
|
2021-06-10 20:09:50 +00:00
|
|
|
Loc: a.getFullAddress(genericMonthDayPath),
|
2021-03-09 20:35:17 +00:00
|
|
|
})
|
|
|
|
already[genericMonthDayPath] = true
|
|
|
|
}
|
2021-06-27 18:59:11 +00:00
|
|
|
// XXXX-XX-DD
|
|
|
|
genericDayPath := bc.getRelativePath("/x/x/" + fmt.Sprintf("%02d", d.day))
|
|
|
|
if !already[genericDayPath] {
|
|
|
|
sm.Add(&sitemap.URL{
|
|
|
|
Loc: a.getFullAddress(genericDayPath),
|
|
|
|
})
|
|
|
|
already[genericDayPath] = true
|
|
|
|
}
|
2021-03-09 20:35:17 +00:00
|
|
|
}
|
2020-10-15 17:50:34 +00:00
|
|
|
}
|
2021-03-09 20:35:17 +00:00
|
|
|
// Photos
|
2021-07-12 14:19:28 +00:00
|
|
|
if pc := bc.Photos; pc != nil && pc.Enabled {
|
2021-03-09 20:35:17 +00:00
|
|
|
sm.Add(&sitemap.URL{
|
2021-07-12 14:19:28 +00:00
|
|
|
Loc: a.getFullAddress(bc.getRelativePath(defaultIfEmpty(pc.Path, defaultPhotosPath))),
|
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
|
2021-07-12 14:19:28 +00:00
|
|
|
if bsc := bc.Search; bsc != nil && bsc.Enabled {
|
2021-03-09 20:35:17 +00:00
|
|
|
sm.Add(&sitemap.URL{
|
2021-07-12 14:19:28 +00:00
|
|
|
Loc: a.getFullAddress(bc.getRelativePath(defaultIfEmpty(bsc.Path, defaultSearchPath))),
|
2021-03-09 20:35:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
// Stats
|
2021-07-12 14:19:28 +00:00
|
|
|
if bsc := bc.BlogStats; bsc != nil && bsc.Enabled {
|
2021-03-09 20:35:17 +00:00
|
|
|
sm.Add(&sitemap.URL{
|
2021-07-12 14:19:28 +00:00
|
|
|
Loc: a.getFullAddress(bc.getRelativePath(defaultIfEmpty(bsc.Path, defaultBlogStatsPath))),
|
2021-03-09 20:35:17 +00:00
|
|
|
})
|
|
|
|
}
|
2021-05-08 19:22:48 +00:00
|
|
|
// Blogroll
|
2021-07-12 14:19:28 +00:00
|
|
|
if brc := bc.Blogroll; brc != nil && brc.Enabled {
|
2021-05-08 19:22:48 +00:00
|
|
|
sm.Add(&sitemap.URL{
|
2021-07-12 14:19:28 +00:00
|
|
|
Loc: a.getFullAddress(bc.getRelativePath(defaultIfEmpty(brc.Path, defaultBlogrollPath))),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// Geo map
|
|
|
|
if mc := bc.Map; mc != nil && mc.Enabled {
|
|
|
|
sm.Add(&sitemap.URL{
|
|
|
|
Loc: a.getFullAddress(bc.getRelativePath(defaultIfEmpty(mc.Path, defaultGeoMapPath))),
|
2021-05-08 19:22:48 +00:00
|
|
|
})
|
|
|
|
}
|
2021-07-22 12:04:46 +00:00
|
|
|
// Contact
|
|
|
|
if cc := bc.Contact; cc != nil && cc.Enabled {
|
|
|
|
sm.Add(&sitemap.URL{
|
|
|
|
Loc: a.getFullAddress(bc.getRelativePath(defaultIfEmpty(cc.Path, defaultContactPath))),
|
|
|
|
})
|
|
|
|
}
|
2021-03-09 20:35:17 +00:00
|
|
|
// Custom pages
|
|
|
|
for _, cp := range bc.CustomPages {
|
|
|
|
sm.Add(&sitemap.URL{
|
2021-06-10 20:09:50 +00:00
|
|
|
Loc: a.getFullAddress(cp.Path),
|
2021-03-09 20:35:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-07-27 10:51:08 +00:00
|
|
|
// Published posts
|
2021-07-03 10:11:57 +00:00
|
|
|
if posts, err := a.db.getPosts(&postsRequestConfig{status: statusPublished, withoutParameters: true}); err == nil {
|
2021-03-09 20:35:17 +00:00
|
|
|
for _, p := range posts {
|
2021-06-06 12:39:42 +00:00
|
|
|
item := &sitemap.URL{Loc: a.fullPostURL(p)}
|
2021-03-09 20:35:17 +00:00
|
|
|
var lastMod time.Time
|
|
|
|
if p.Updated != "" {
|
2021-07-27 10:51:08 +00:00
|
|
|
lastMod = timeNoErr(dateparse.ParseLocal(p.Updated))
|
2021-03-09 20:35:17 +00:00
|
|
|
}
|
|
|
|
if p.Published != "" && lastMod.IsZero() {
|
2021-07-27 10:51:08 +00:00
|
|
|
lastMod = timeNoErr(dateparse.ParseLocal(p.Published))
|
2021-03-09 20:35:17 +00:00
|
|
|
}
|
|
|
|
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...
|
2021-02-16 15:26:21 +00:00
|
|
|
_, _ = sm.WriteTo(w) // Already minified
|
2020-09-22 15:15:17 +00:00
|
|
|
}
|