Working version of date archives for sections

This commit is contained in:
Jan-Lukas Else 2023-02-24 22:41:33 +01:00
parent a6cfa2de86
commit 7c578df700
3 changed files with 35 additions and 10 deletions

View File

@ -215,6 +215,7 @@ func (a *goBlog) blogSectionsRouter(conf *configBlog) func(r chi.Router) {
r.Get(secPath, a.serveIndex)
r.Get(secPath+feedPath, a.serveIndex)
r.Get(secPath+paginationPath, a.serveIndex)
r.Group(a.dateRoutes(conf, section.Name))
})
}
}
@ -251,7 +252,13 @@ func (a *goBlog) blogDatesRouter(conf *configBlog) func(r chi.Router) {
a.cacheMiddleware,
)
yearPath := conf.getRelativePath(`/{year:(x|\d{4})}`)
r.Group(a.dateRoutes(conf, ""))
}
}
func (a *goBlog) dateRoutes(conf *configBlog, pathPrefix string) func(r chi.Router) {
return func(r chi.Router) {
yearPath := conf.getRelativePath(pathPrefix + `/{year:(x|\d{4})}`)
r.Get(yearPath, a.serveDate)
r.Get(yearPath+feedPath, a.serveDate)
r.Get(yearPath+paginationPath, a.serveDate)

View File

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"path"
"reflect"
"strings"
"time"
@ -224,14 +225,21 @@ func (a *goBlog) serveDate(w http.ResponseWriter, r *http.Request) {
a.serve404(w, r)
return
}
var ic *indexConfig
if cv := r.Context().Value(indexConfigKey); cv != nil {
origIc := *(cv.(*indexConfig))
copyIc := origIc
ic = &copyIc
ic.path = path.Join(ic.path, datePath)
ic.titleSuffix = ": " + title
} else {
_, bc := a.getBlog(r)
ic := &indexConfig{
ic = &indexConfig{
path: bc.getRelativePath(datePath),
title: title,
year: year,
month: month,
day: day,
}
}
ic.year, ic.month, ic.day = year, month, day
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, ic)))
}
@ -279,6 +287,7 @@ type indexConfig struct {
parameter string
year, month, day int
title string
titleSuffix string
description string
summaryTemplate summaryTyp
status []postStatus
@ -342,6 +351,7 @@ func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) {
} else if search != "" {
title = fmt.Sprintf("%s: %s", bc.Search.Title, search)
}
title += ic.titleSuffix
// Description
var description string
if ic.description != "" {

View File

@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"path"
"time"
"github.com/araddon/dateparse"
@ -117,6 +118,12 @@ func (a *goBlog) serveSitemapBlogArchives(w http.ResponseWriter, r *http.Request
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(section.Name)),
})
datePaths, _ := a.sitemapDatePaths(b, []string{section.Name})
for _, p := range datePaths {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(path.Join(section.Name, p))),
})
}
}
}
// Taxonomies
@ -138,7 +145,7 @@ func (a *goBlog) serveSitemapBlogArchives(w http.ResponseWriter, r *http.Request
}
}
// Date based archives
datePaths, _ := a.sitemapDatePaths(b)
datePaths, _ := a.sitemapDatePaths(b, nil)
for _, p := range datePaths {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(p)),
@ -212,9 +219,10 @@ union
select distinct '/x/x/' || day from alldates;
`
func (a *goBlog) sitemapDatePaths(blog string) (paths []string, err error) {
func (a *goBlog) sitemapDatePaths(blog string, sections []string) (paths []string, err error) {
query, args := buildPostsQuery(&postsRequestConfig{
blog: blog,
sections: sections,
status: []postStatus{statusPublished},
visibility: []postVisibility{visibilityPublic},
}, "published")