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 serveSitemap(w http.ResponseWriter, r *http.Request) {
|
|
|
|
sm := sitemap.New()
|
|
|
|
sm.Minify = true
|
2021-03-09 20:35:17 +00:00
|
|
|
// Blogs
|
|
|
|
for b, bc := range appConfig.Blogs {
|
|
|
|
// Blog
|
|
|
|
blogPath := bc.Path
|
|
|
|
if blogPath == "/" {
|
|
|
|
blogPath = ""
|
|
|
|
}
|
|
|
|
sm.Add(&sitemap.URL{
|
|
|
|
Loc: appConfig.Server.PublicAddress + blogPath,
|
|
|
|
})
|
|
|
|
// Sections
|
|
|
|
for _, section := range bc.Sections {
|
|
|
|
if section.Name != "" {
|
|
|
|
sm.Add(&sitemap.URL{
|
|
|
|
Loc: appConfig.Server.PublicAddress + bc.getRelativePath("/"+section.Name),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Taxonomies
|
|
|
|
for _, taxonomy := range bc.Taxonomies {
|
|
|
|
if taxonomy.Name != "" {
|
|
|
|
// Taxonomy
|
|
|
|
taxPath := bc.getRelativePath("/" + taxonomy.Name)
|
|
|
|
sm.Add(&sitemap.URL{
|
|
|
|
Loc: appConfig.Server.PublicAddress + taxPath,
|
|
|
|
})
|
|
|
|
// Values
|
|
|
|
if taxValues, err := allTaxonomyValues(b, taxonomy.Name); err == nil {
|
|
|
|
for _, tv := range taxValues {
|
|
|
|
sm.Add(&sitemap.URL{
|
|
|
|
Loc: appConfig.Server.PublicAddress + taxPath + "/" + urlize(tv),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Year / month archives
|
|
|
|
if dates, err := allPublishedDates(b); err == nil {
|
|
|
|
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: appConfig.Server.PublicAddress + yearPath,
|
|
|
|
})
|
|
|
|
already[yearPath] = true
|
|
|
|
}
|
|
|
|
// Specific month
|
|
|
|
monthPath := yearPath + "/" + fmt.Sprintf("%02d", d.month)
|
|
|
|
if !already[monthPath] {
|
|
|
|
sm.Add(&sitemap.URL{
|
|
|
|
Loc: appConfig.Server.PublicAddress + monthPath,
|
|
|
|
})
|
|
|
|
already[monthPath] = true
|
|
|
|
}
|
|
|
|
// Specific day
|
|
|
|
dayPath := monthPath + "/" + fmt.Sprintf("%02d", d.day)
|
|
|
|
if !already[dayPath] {
|
|
|
|
sm.Add(&sitemap.URL{
|
|
|
|
Loc: appConfig.Server.PublicAddress + dayPath,
|
|
|
|
})
|
|
|
|
already[dayPath] = true
|
|
|
|
}
|
|
|
|
// Generic month
|
|
|
|
genericMonthPath := blogPath + "/x/" + fmt.Sprintf("%02d", d.month)
|
|
|
|
if !already[genericMonthPath] {
|
|
|
|
sm.Add(&sitemap.URL{
|
|
|
|
Loc: appConfig.Server.PublicAddress + genericMonthPath,
|
|
|
|
})
|
|
|
|
already[genericMonthPath] = true
|
|
|
|
}
|
|
|
|
// Specific day
|
|
|
|
genericMonthDayPath := genericMonthPath + "/" + fmt.Sprintf("%02d", d.day)
|
|
|
|
if !already[genericMonthDayPath] {
|
|
|
|
sm.Add(&sitemap.URL{
|
|
|
|
Loc: appConfig.Server.PublicAddress + genericMonthDayPath,
|
|
|
|
})
|
|
|
|
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: appConfig.Server.PublicAddress + bc.getRelativePath(bc.Photos.Path),
|
|
|
|
})
|
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: appConfig.Server.PublicAddress + bc.getRelativePath(bc.Search.Path),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// Stats
|
|
|
|
if bc.BlogStats != nil && bc.BlogStats.Enabled {
|
|
|
|
sm.Add(&sitemap.URL{
|
|
|
|
Loc: appConfig.Server.PublicAddress + bc.getRelativePath(bc.BlogStats.Path),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// Custom pages
|
|
|
|
for _, cp := range bc.CustomPages {
|
|
|
|
sm.Add(&sitemap.URL{
|
|
|
|
Loc: appConfig.Server.PublicAddress + cp.Path,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Posts
|
|
|
|
if posts, err := getPosts(&postsRequestConfig{status: statusPublished}); err == nil {
|
|
|
|
for _, p := range posts {
|
|
|
|
item := &sitemap.URL{Loc: p.fullURL()}
|
|
|
|
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...
|
2021-02-16 15:26:21 +00:00
|
|
|
_, _ = sm.WriteTo(w) // Already minified
|
2020-09-22 15:15:17 +00:00
|
|
|
}
|