mirror of https://github.com/jlelse/GoBlog
Working version of date archives for sections
parent
a6cfa2de86
commit
7c578df700
|
@ -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)
|
||||
|
|
24
posts.go
24
posts.go
|
@ -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
|
||||
}
|
||||
_, bc := a.getBlog(r)
|
||||
ic := &indexConfig{
|
||||
path: bc.getRelativePath(datePath),
|
||||
title: title,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
var ic *indexConfig
|
||||
if cv := r.Context().Value(indexConfigKey); cv != nil {
|
||||
origIc := *(cv.(*indexConfig))
|
||||
copyIc := origIc
|
||||
ic = ©Ic
|
||||
ic.path = path.Join(ic.path, datePath)
|
||||
ic.titleSuffix = ": " + title
|
||||
} else {
|
||||
_, bc := a.getBlog(r)
|
||||
ic = &indexConfig{
|
||||
path: bc.getRelativePath(datePath),
|
||||
title: title,
|
||||
}
|
||||
}
|
||||
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 != "" {
|
||||
|
|
12
sitemap.go
12
sitemap.go
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue