2020-07-28 19:17:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-22 07:20:56 +00:00
|
|
|
"context"
|
2020-07-28 19:17:07 +00:00
|
|
|
"errors"
|
2020-08-05 17:54:04 +00:00
|
|
|
"fmt"
|
2020-07-28 19:17:07 +00:00
|
|
|
"net/http"
|
2020-08-24 18:49:33 +00:00
|
|
|
"reflect"
|
2020-09-24 15:13:03 +00:00
|
|
|
"strings"
|
2022-01-26 20:08:46 +00:00
|
|
|
"time"
|
2020-10-06 17:07:48 +00:00
|
|
|
|
2021-03-03 17:19:55 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2020-10-06 17:07:48 +00:00
|
|
|
"github.com/vcraescu/go-paginator"
|
2022-02-22 15:52:03 +00:00
|
|
|
"go.goblog.app/app/pkgs/bufferpool"
|
2020-07-28 19:17:07 +00:00
|
|
|
)
|
|
|
|
|
2020-07-30 19:18:13 +00:00
|
|
|
var errPostNotFound = errors.New("post not found")
|
2020-07-28 19:17:07 +00:00
|
|
|
|
2020-10-15 15:32:46 +00:00
|
|
|
type post struct {
|
2021-07-12 14:19:28 +00:00
|
|
|
Path string
|
|
|
|
Content string
|
|
|
|
Published string
|
|
|
|
Updated string
|
|
|
|
Parameters map[string][]string
|
|
|
|
Blog string
|
|
|
|
Section string
|
|
|
|
Status postStatus
|
2022-09-23 09:05:07 +00:00
|
|
|
Visibility postVisibility
|
2021-07-12 14:19:28 +00:00
|
|
|
Priority int
|
2020-10-06 17:07:48 +00:00
|
|
|
// Not persisted
|
2021-08-05 06:09:34 +00:00
|
|
|
Slug string
|
|
|
|
RenderedTitle string
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 20:56:46 +00:00
|
|
|
type postStatus string
|
2022-09-23 09:05:07 +00:00
|
|
|
type postVisibility string
|
2021-01-15 20:56:46 +00:00
|
|
|
|
|
|
|
const (
|
2022-09-23 09:05:07 +00:00
|
|
|
statusDeletedSuffix postStatus = "-deleted"
|
2022-01-03 12:55:44 +00:00
|
|
|
|
|
|
|
statusNil postStatus = ""
|
|
|
|
statusPublished postStatus = "published"
|
2022-09-23 09:05:07 +00:00
|
|
|
statusPublishedDeleted postStatus = statusPublished + statusDeletedSuffix
|
2022-01-03 12:55:44 +00:00
|
|
|
statusDraft postStatus = "draft"
|
2022-09-23 09:05:07 +00:00
|
|
|
statusDraftDeleted postStatus = statusDraft + statusDeletedSuffix
|
2022-01-03 12:55:44 +00:00
|
|
|
statusScheduled postStatus = "scheduled"
|
2022-09-23 09:05:07 +00:00
|
|
|
statusScheduledDeleted postStatus = statusScheduled + statusDeletedSuffix
|
|
|
|
|
|
|
|
visibilityNil postVisibility = ""
|
|
|
|
visibilityPublic postVisibility = "public"
|
|
|
|
visibilityUnlisted postVisibility = "unlisted"
|
|
|
|
visibilityPrivate postVisibility = "private"
|
2021-01-15 20:56:46 +00:00
|
|
|
)
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) servePost(w http.ResponseWriter, r *http.Request) {
|
2021-08-05 06:09:34 +00:00
|
|
|
p, err := a.getPost(r.URL.Path)
|
2022-01-03 12:55:44 +00:00
|
|
|
if errors.Is(err, errPostNotFound) {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serve404(w, r)
|
2020-07-29 14:41:36 +00:00
|
|
|
return
|
2020-07-28 19:17:07 +00:00
|
|
|
} else if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
2020-07-29 14:41:36 +00:00
|
|
|
return
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
2021-02-24 12:16:33 +00:00
|
|
|
if asRequest, ok := r.Context().Value(asRequestKey).(bool); ok && asRequest {
|
2021-06-10 20:09:50 +00:00
|
|
|
if r.URL.Path == a.getRelativePath(p.Blog, "") {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveActivityStreams(p.Blog, w, r)
|
2021-03-23 06:27:12 +00:00
|
|
|
return
|
|
|
|
}
|
2022-11-23 21:16:56 +00:00
|
|
|
a.serveActivityStreamsPost(p, w, r)
|
2020-10-26 16:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-01 17:37:21 +00:00
|
|
|
canonical := p.firstParameter("original")
|
|
|
|
if canonical == "" {
|
2021-06-06 12:39:42 +00:00
|
|
|
canonical = a.fullPostURL(p)
|
2020-11-01 17:37:21 +00:00
|
|
|
}
|
2022-01-20 17:22:10 +00:00
|
|
|
renderMethod := a.renderPost
|
2021-06-11 06:24:41 +00:00
|
|
|
if p.Path == a.getRelativePath(p.Blog, "") {
|
2022-01-20 17:22:10 +00:00
|
|
|
renderMethod = a.renderStaticHome
|
2020-12-23 15:53:10 +00:00
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
w.Header().Add("Link", fmt.Sprintf("<%s>; rel=shortlink", a.shortPostURL(p)))
|
2022-01-03 12:55:44 +00:00
|
|
|
status := http.StatusOK
|
2022-09-23 09:05:07 +00:00
|
|
|
if p.Deleted() {
|
2022-01-03 12:55:44 +00:00
|
|
|
status = http.StatusGone
|
|
|
|
}
|
2022-01-30 15:40:53 +00:00
|
|
|
a.renderWithStatusCode(w, r, status, renderMethod, &renderData{
|
2021-01-17 11:53:07 +00:00
|
|
|
BlogString: p.Blog,
|
2020-11-01 17:37:21 +00:00
|
|
|
Canonical: canonical,
|
2020-10-15 15:32:46 +00:00
|
|
|
Data: p,
|
2020-10-12 16:47:23 +00:00
|
|
|
})
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 20:08:46 +00:00
|
|
|
const defaultRandomPath = "/random"
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) redirectToRandomPost(rw http.ResponseWriter, r *http.Request) {
|
2021-12-20 13:00:11 +00:00
|
|
|
blog, _ := a.getBlog(r)
|
|
|
|
randomPath, err := a.getRandomPostPath(blog)
|
2021-03-22 07:20:56 +00:00
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(rw, r, err.Error(), http.StatusInternalServerError)
|
2021-03-22 07:20:56 +00:00
|
|
|
return
|
2021-01-20 12:38:24 +00:00
|
|
|
}
|
2021-03-22 07:20:56 +00:00
|
|
|
http.Redirect(rw, r, randomPath, http.StatusFound)
|
2021-01-20 12:38:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 20:08:46 +00:00
|
|
|
const defaultOnThisDayPath = "/onthisday"
|
|
|
|
|
|
|
|
func (a *goBlog) redirectToOnThisDay(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, bc := a.getBlog(r)
|
|
|
|
// Get current local month and day
|
|
|
|
now := time.Now()
|
|
|
|
month := now.Month()
|
|
|
|
day := now.Day()
|
|
|
|
// Build the path
|
|
|
|
targetPath := fmt.Sprintf("/x/%02d/%02d", month, day)
|
|
|
|
targetPath = bc.getRelativePath(targetPath)
|
|
|
|
// Redirect
|
|
|
|
http.Redirect(w, r, targetPath, http.StatusFound)
|
|
|
|
}
|
|
|
|
|
2020-08-24 18:49:33 +00:00
|
|
|
type postPaginationAdapter struct {
|
2020-10-19 18:25:30 +00:00
|
|
|
config *postsRequestConfig
|
2020-11-17 16:38:17 +00:00
|
|
|
nums int64
|
2021-08-05 06:09:34 +00:00
|
|
|
a *goBlog
|
2020-08-24 18:49:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 16:38:17 +00:00
|
|
|
func (p *postPaginationAdapter) Nums() (int64, error) {
|
2020-08-24 18:49:33 +00:00
|
|
|
if p.nums == 0 {
|
2021-08-05 06:09:34 +00:00
|
|
|
nums, _ := p.a.db.countPosts(p.config)
|
2020-11-17 16:38:17 +00:00
|
|
|
p.nums = int64(nums)
|
2020-08-24 18:49:33 +00:00
|
|
|
}
|
2020-11-17 16:38:17 +00:00
|
|
|
return p.nums, nil
|
2020-08-24 18:49:33 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 07:28:03 +00:00
|
|
|
func (p *postPaginationAdapter) Slice(offset, length int, data any) error {
|
2020-08-31 19:12:43 +00:00
|
|
|
modifiedConfig := *p.config
|
|
|
|
modifiedConfig.offset = offset
|
|
|
|
modifiedConfig.limit = length
|
|
|
|
|
2021-08-05 06:09:34 +00:00
|
|
|
posts, err := p.a.getPosts(&modifiedConfig)
|
2020-08-24 18:49:33 +00:00
|
|
|
reflect.ValueOf(data).Elem().Set(reflect.ValueOf(&posts).Elem())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) serveHome(w http.ResponseWriter, r *http.Request) {
|
2021-12-20 13:00:11 +00:00
|
|
|
blog, _ := a.getBlog(r)
|
2021-03-22 07:20:56 +00:00
|
|
|
if asRequest, ok := r.Context().Value(asRequestKey).(bool); ok && asRequest {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveActivityStreams(blog, w, r)
|
2021-03-22 07:20:56 +00:00
|
|
|
return
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
|
2021-06-10 20:09:50 +00:00
|
|
|
path: a.getRelativePath(blog, ""),
|
2021-03-22 07:20:56 +00:00
|
|
|
})))
|
2020-08-25 18:55:32 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 13:44:57 +00:00
|
|
|
func (a *goBlog) serveDrafts(w http.ResponseWriter, r *http.Request) {
|
2021-12-20 13:00:11 +00:00
|
|
|
_, bc := a.getBlog(r)
|
2021-07-14 13:44:57 +00:00
|
|
|
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
|
2022-01-03 12:55:44 +00:00
|
|
|
path: bc.getRelativePath("/editor/drafts"),
|
|
|
|
title: a.ts.GetTemplateStringVariant(bc.Lang, "drafts"),
|
|
|
|
description: a.ts.GetTemplateStringVariant(bc.Lang, "draftsdesc"),
|
2022-09-23 09:05:07 +00:00
|
|
|
status: []postStatus{statusDraft},
|
2021-07-14 13:44:57 +00:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) servePrivate(w http.ResponseWriter, r *http.Request) {
|
2021-12-20 13:00:11 +00:00
|
|
|
_, bc := a.getBlog(r)
|
2021-07-14 13:44:57 +00:00
|
|
|
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
|
2022-01-03 12:55:44 +00:00
|
|
|
path: bc.getRelativePath("/editor/private"),
|
|
|
|
title: a.ts.GetTemplateStringVariant(bc.Lang, "privateposts"),
|
|
|
|
description: a.ts.GetTemplateStringVariant(bc.Lang, "privatepostsdesc"),
|
2022-09-23 09:05:07 +00:00
|
|
|
status: []postStatus{statusPublished},
|
|
|
|
visibility: []postVisibility{visibilityPrivate},
|
2021-07-14 13:44:57 +00:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) serveUnlisted(w http.ResponseWriter, r *http.Request) {
|
2021-12-20 13:00:11 +00:00
|
|
|
_, bc := a.getBlog(r)
|
2021-07-14 13:44:57 +00:00
|
|
|
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
|
2022-01-03 12:55:44 +00:00
|
|
|
path: bc.getRelativePath("/editor/unlisted"),
|
|
|
|
title: a.ts.GetTemplateStringVariant(bc.Lang, "unlistedposts"),
|
|
|
|
description: a.ts.GetTemplateStringVariant(bc.Lang, "unlistedpostsdesc"),
|
2022-09-23 09:05:07 +00:00
|
|
|
status: []postStatus{statusPublished},
|
|
|
|
visibility: []postVisibility{visibilityUnlisted},
|
2021-07-14 13:44:57 +00:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2021-12-11 18:56:40 +00:00
|
|
|
func (a *goBlog) serveScheduled(w http.ResponseWriter, r *http.Request) {
|
2021-12-20 13:00:11 +00:00
|
|
|
_, bc := a.getBlog(r)
|
2021-12-11 18:56:40 +00:00
|
|
|
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
|
2022-01-03 12:55:44 +00:00
|
|
|
path: bc.getRelativePath("/editor/scheduled"),
|
|
|
|
title: a.ts.GetTemplateStringVariant(bc.Lang, "scheduledposts"),
|
|
|
|
description: a.ts.GetTemplateStringVariant(bc.Lang, "scheduledpostsdesc"),
|
2022-09-23 09:05:07 +00:00
|
|
|
status: []postStatus{statusScheduled},
|
2022-01-03 12:55:44 +00:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) serveDeleted(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, bc := a.getBlog(r)
|
|
|
|
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
|
|
|
|
path: bc.getRelativePath("/editor/deleted"),
|
|
|
|
title: a.ts.GetTemplateStringVariant(bc.Lang, "deletedposts"),
|
|
|
|
description: a.ts.GetTemplateStringVariant(bc.Lang, "deletedpostsdesc"),
|
2022-09-23 09:05:07 +00:00
|
|
|
status: []postStatus{statusPublishedDeleted, statusDraftDeleted, statusScheduledDeleted},
|
2021-12-11 18:56:40 +00:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) serveDate(w http.ResponseWriter, r *http.Request) {
|
2021-03-22 07:20:56 +00:00
|
|
|
var year, month, day int
|
|
|
|
if ys := chi.URLParam(r, "year"); ys != "" && ys != "x" {
|
2022-02-26 19:38:52 +00:00
|
|
|
year = stringToInt(ys)
|
2021-03-22 07:20:56 +00:00
|
|
|
}
|
|
|
|
if ms := chi.URLParam(r, "month"); ms != "" && ms != "x" {
|
2022-02-26 19:38:52 +00:00
|
|
|
month = stringToInt(ms)
|
2021-03-22 07:20:56 +00:00
|
|
|
}
|
|
|
|
if ds := chi.URLParam(r, "day"); ds != "" {
|
2022-02-26 19:38:52 +00:00
|
|
|
day = stringToInt(ds)
|
2021-03-22 07:20:56 +00:00
|
|
|
}
|
|
|
|
if year == 0 && month == 0 && day == 0 {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serve404(w, r)
|
2021-03-22 07:20:56 +00:00
|
|
|
return
|
|
|
|
}
|
2022-02-22 15:52:03 +00:00
|
|
|
title, dPath := bufferpool.Get(), bufferpool.Get()
|
2021-02-26 09:31:38 +00:00
|
|
|
if year != 0 {
|
2022-02-22 15:52:03 +00:00
|
|
|
_, _ = fmt.Fprintf(title, "%0004d", year)
|
|
|
|
_, _ = fmt.Fprintf(dPath, "%0004d", year)
|
2021-02-26 09:31:38 +00:00
|
|
|
} else {
|
2022-01-20 17:22:10 +00:00
|
|
|
_, _ = title.WriteString("XXXX")
|
|
|
|
_, _ = dPath.WriteString("x")
|
2021-02-26 09:31:38 +00:00
|
|
|
}
|
|
|
|
if month != 0 {
|
2022-02-22 15:52:03 +00:00
|
|
|
_, _ = fmt.Fprintf(title, "-%02d", month)
|
|
|
|
_, _ = fmt.Fprintf(dPath, "/%02d", month)
|
2021-02-26 09:31:38 +00:00
|
|
|
} else if day != 0 {
|
2022-01-20 17:22:10 +00:00
|
|
|
_, _ = title.WriteString("-XX")
|
|
|
|
_, _ = dPath.WriteString("/x")
|
2021-02-26 09:31:38 +00:00
|
|
|
}
|
|
|
|
if day != 0 {
|
2022-02-22 15:52:03 +00:00
|
|
|
_, _ = fmt.Fprintf(title, "-%02d", day)
|
|
|
|
_, _ = fmt.Fprintf(dPath, "/%02d", day)
|
2020-12-13 14:16:47 +00:00
|
|
|
}
|
2021-12-20 13:00:11 +00:00
|
|
|
_, bc := a.getBlog(r)
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
|
2021-12-20 13:00:11 +00:00
|
|
|
path: bc.getRelativePath(dPath.String()),
|
2020-12-13 14:16:47 +00:00
|
|
|
year: year,
|
|
|
|
month: month,
|
2020-12-26 19:40:22 +00:00
|
|
|
day: day,
|
2021-02-26 09:31:38 +00:00
|
|
|
title: title.String(),
|
2021-03-22 07:20:56 +00:00
|
|
|
})))
|
2022-02-22 15:52:03 +00:00
|
|
|
bufferpool.Put(title, dPath)
|
2020-12-13 14:16:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 16:03:05 +00:00
|
|
|
type indexConfig struct {
|
2020-12-26 19:40:22 +00:00
|
|
|
path string
|
2021-07-12 14:19:28 +00:00
|
|
|
section *configSection
|
|
|
|
tax *configTaxonomy
|
2020-12-26 19:40:22 +00:00
|
|
|
taxValue string
|
|
|
|
parameter string
|
|
|
|
year, month, day int
|
|
|
|
title string
|
|
|
|
description string
|
2022-01-11 11:40:31 +00:00
|
|
|
summaryTemplate summaryTyp
|
2022-09-23 09:05:07 +00:00
|
|
|
status []postStatus
|
|
|
|
visibility []postVisibility
|
2020-09-21 16:03:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 14:19:28 +00:00
|
|
|
const defaultPhotosPath = "/photos"
|
|
|
|
|
2021-06-29 15:07:08 +00:00
|
|
|
const indexConfigKey contextKey = "indexConfig"
|
2021-03-22 07:20:56 +00:00
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) {
|
2021-03-22 07:20:56 +00:00
|
|
|
ic := r.Context().Value(indexConfigKey).(*indexConfig)
|
2021-12-20 13:00:11 +00:00
|
|
|
blog, bc := a.getBlog(r)
|
2021-03-22 07:20:56 +00:00
|
|
|
search := chi.URLParam(r, "search")
|
|
|
|
if search != "" {
|
2021-07-12 14:19:28 +00:00
|
|
|
// Decode and sanitize search
|
2021-09-01 09:14:49 +00:00
|
|
|
search = cleanHTMLText(searchDecode(search))
|
2021-03-22 07:20:56 +00:00
|
|
|
}
|
|
|
|
var sections []string
|
|
|
|
if ic.section != nil {
|
|
|
|
sections = []string{ic.section.Name}
|
|
|
|
} else {
|
2021-12-20 13:00:11 +00:00
|
|
|
for sectionKey := range bc.Sections {
|
2021-03-22 07:20:56 +00:00
|
|
|
sections = append(sections, sectionKey)
|
2020-09-21 16:03:05 +00:00
|
|
|
}
|
2020-08-05 17:14:10 +00:00
|
|
|
}
|
2022-09-23 09:05:07 +00:00
|
|
|
defaultStatus, defaultVisibility := a.getDefaultPostStates(r)
|
|
|
|
status := ic.status
|
|
|
|
if len(status) == 0 {
|
|
|
|
status = defaultStatus
|
2022-01-03 12:55:44 +00:00
|
|
|
}
|
2022-09-23 09:05:07 +00:00
|
|
|
visibility := ic.visibility
|
|
|
|
if len(visibility) == 0 {
|
|
|
|
visibility = defaultVisibility
|
2021-07-14 13:44:57 +00:00
|
|
|
}
|
2021-03-22 07:20:56 +00:00
|
|
|
p := paginator.New(&postPaginationAdapter{config: &postsRequestConfig{
|
|
|
|
blog: blog,
|
|
|
|
sections: sections,
|
|
|
|
taxonomy: ic.tax,
|
|
|
|
taxonomyValue: ic.taxValue,
|
|
|
|
parameter: ic.parameter,
|
|
|
|
search: search,
|
|
|
|
publishedYear: ic.year,
|
|
|
|
publishedMonth: ic.month,
|
|
|
|
publishedDay: ic.day,
|
2022-09-23 09:05:07 +00:00
|
|
|
status: status,
|
|
|
|
visibility: visibility,
|
2021-07-12 14:19:28 +00:00
|
|
|
priorityOrder: true,
|
2021-12-20 13:00:11 +00:00
|
|
|
}, a: a}, bc.Pagination)
|
2022-02-26 19:38:52 +00:00
|
|
|
p.SetPage(stringToInt(chi.URLParam(r, "page")))
|
2021-03-22 07:20:56 +00:00
|
|
|
var posts []*post
|
|
|
|
err := p.Results(&posts)
|
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
2021-03-22 07:20:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Meta
|
|
|
|
title := ic.title
|
|
|
|
description := ic.description
|
|
|
|
if ic.tax != nil {
|
|
|
|
title = fmt.Sprintf("%s: %s", ic.tax.Title, ic.taxValue)
|
|
|
|
} else if ic.section != nil {
|
|
|
|
title = ic.section.Title
|
|
|
|
description = ic.section.Description
|
|
|
|
} else if search != "" {
|
2021-12-20 13:00:11 +00:00
|
|
|
title = fmt.Sprintf("%s: %s", bc.Search.Title, search)
|
2021-03-22 07:20:56 +00:00
|
|
|
}
|
|
|
|
// Check if feed
|
|
|
|
if ft := feedType(chi.URLParam(r, "feed")); ft != noFeed {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.generateFeed(blog, ft, w, r, posts, title, description)
|
2021-03-22 07:20:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Path
|
|
|
|
path := ic.path
|
|
|
|
if strings.Contains(path, searchPlaceholder) {
|
|
|
|
path = strings.ReplaceAll(path, searchPlaceholder, searchEncode(search))
|
|
|
|
}
|
|
|
|
// Navigation
|
|
|
|
var hasPrev, hasNext bool
|
|
|
|
var prevPage, nextPage int
|
|
|
|
var prevPath, nextPath string
|
|
|
|
hasPrev, _ = p.HasPrev()
|
|
|
|
if hasPrev {
|
|
|
|
prevPage, _ = p.PrevPage()
|
|
|
|
} else {
|
|
|
|
prevPage, _ = p.Page()
|
|
|
|
}
|
|
|
|
if prevPage < 2 {
|
|
|
|
prevPath = path
|
|
|
|
} else {
|
2021-06-10 20:09:50 +00:00
|
|
|
prevPath = fmt.Sprintf("%s/page/%d", strings.TrimSuffix(path, "/"), prevPage)
|
2021-03-22 07:20:56 +00:00
|
|
|
}
|
|
|
|
hasNext, _ = p.HasNext()
|
|
|
|
if hasNext {
|
|
|
|
nextPage, _ = p.NextPage()
|
|
|
|
} else {
|
|
|
|
nextPage, _ = p.Page()
|
|
|
|
}
|
2021-06-10 20:09:50 +00:00
|
|
|
nextPath = fmt.Sprintf("%s/page/%d", strings.TrimSuffix(path, "/"), nextPage)
|
2021-03-22 07:20:56 +00:00
|
|
|
summaryTemplate := ic.summaryTemplate
|
|
|
|
if summaryTemplate == "" {
|
2022-01-11 11:40:31 +00:00
|
|
|
summaryTemplate = defaultSummary
|
2021-03-22 07:20:56 +00:00
|
|
|
}
|
2022-01-30 15:40:53 +00:00
|
|
|
a.render(w, r, a.renderIndex, &renderData{
|
2021-12-20 13:00:11 +00:00
|
|
|
Canonical: a.getFullAddress(path),
|
2022-01-20 17:22:10 +00:00
|
|
|
Data: &indexRenderData{
|
|
|
|
title: title,
|
|
|
|
description: description,
|
|
|
|
posts: posts,
|
|
|
|
hasPrev: hasPrev,
|
|
|
|
hasNext: hasNext,
|
|
|
|
first: path,
|
|
|
|
prev: prevPath,
|
|
|
|
next: nextPath,
|
|
|
|
summaryTemplate: summaryTemplate,
|
2021-03-22 07:20:56 +00:00
|
|
|
},
|
|
|
|
})
|
2020-08-05 17:14:10 +00:00
|
|
|
}
|