mirror of https://github.com/jlelse/GoBlog
Working version of date archives for sections
This commit is contained in:
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, a.serveIndex)
|
||||||
r.Get(secPath+feedPath, a.serveIndex)
|
r.Get(secPath+feedPath, a.serveIndex)
|
||||||
r.Get(secPath+paginationPath, 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,
|
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, a.serveDate)
|
||||||
r.Get(yearPath+feedPath, a.serveDate)
|
r.Get(yearPath+feedPath, a.serveDate)
|
||||||
r.Get(yearPath+paginationPath, a.serveDate)
|
r.Get(yearPath+paginationPath, a.serveDate)
|
||||||
|
|
24
posts.go
24
posts.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -224,14 +225,21 @@ func (a *goBlog) serveDate(w http.ResponseWriter, r *http.Request) {
|
||||||
a.serve404(w, r)
|
a.serve404(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, bc := a.getBlog(r)
|
var ic *indexConfig
|
||||||
ic := &indexConfig{
|
if cv := r.Context().Value(indexConfigKey); cv != nil {
|
||||||
path: bc.getRelativePath(datePath),
|
origIc := *(cv.(*indexConfig))
|
||||||
title: title,
|
copyIc := origIc
|
||||||
year: year,
|
ic = ©Ic
|
||||||
month: month,
|
ic.path = path.Join(ic.path, datePath)
|
||||||
day: day,
|
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)))
|
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, ic)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -279,6 +287,7 @@ type indexConfig struct {
|
||||||
parameter string
|
parameter string
|
||||||
year, month, day int
|
year, month, day int
|
||||||
title string
|
title string
|
||||||
|
titleSuffix string
|
||||||
description string
|
description string
|
||||||
summaryTemplate summaryTyp
|
summaryTemplate summaryTyp
|
||||||
status []postStatus
|
status []postStatus
|
||||||
|
@ -342,6 +351,7 @@ func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
} else if search != "" {
|
} else if search != "" {
|
||||||
title = fmt.Sprintf("%s: %s", bc.Search.Title, search)
|
title = fmt.Sprintf("%s: %s", bc.Search.Title, search)
|
||||||
}
|
}
|
||||||
|
title += ic.titleSuffix
|
||||||
// Description
|
// Description
|
||||||
var description string
|
var description string
|
||||||
if ic.description != "" {
|
if ic.description != "" {
|
||||||
|
|
12
sitemap.go
12
sitemap.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/araddon/dateparse"
|
"github.com/araddon/dateparse"
|
||||||
|
@ -117,6 +118,12 @@ func (a *goBlog) serveSitemapBlogArchives(w http.ResponseWriter, r *http.Request
|
||||||
sm.Add(&sitemap.URL{
|
sm.Add(&sitemap.URL{
|
||||||
Loc: a.getFullAddress(bc.getRelativePath(section.Name)),
|
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
|
// Taxonomies
|
||||||
|
@ -138,7 +145,7 @@ func (a *goBlog) serveSitemapBlogArchives(w http.ResponseWriter, r *http.Request
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Date based archives
|
// Date based archives
|
||||||
datePaths, _ := a.sitemapDatePaths(b)
|
datePaths, _ := a.sitemapDatePaths(b, nil)
|
||||||
for _, p := range datePaths {
|
for _, p := range datePaths {
|
||||||
sm.Add(&sitemap.URL{
|
sm.Add(&sitemap.URL{
|
||||||
Loc: a.getFullAddress(bc.getRelativePath(p)),
|
Loc: a.getFullAddress(bc.getRelativePath(p)),
|
||||||
|
@ -212,9 +219,10 @@ union
|
||||||
select distinct '/x/x/' || day from alldates;
|
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{
|
query, args := buildPostsQuery(&postsRequestConfig{
|
||||||
blog: blog,
|
blog: blog,
|
||||||
|
sections: sections,
|
||||||
status: []postStatus{statusPublished},
|
status: []postStatus{statusPublished},
|
||||||
visibility: []postVisibility{visibilityPublic},
|
visibility: []postVisibility{visibilityPublic},
|
||||||
}, "published")
|
}, "published")
|
||||||
|
|
Loading…
Reference in New Issue