mirror of https://github.com/jlelse/GoBlog
Refactor code for search results
This commit is contained in:
parent
a2acc2b6ce
commit
272353ad7a
18
posts.go
18
posts.go
|
@ -291,6 +291,7 @@ type indexConfig struct {
|
||||||
summaryTemplate summaryTyp
|
summaryTemplate summaryTyp
|
||||||
status []postStatus
|
status []postStatus
|
||||||
visibility []postVisibility
|
visibility []postVisibility
|
||||||
|
search string
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultPhotosPath = "/photos"
|
const defaultPhotosPath = "/photos"
|
||||||
|
@ -300,11 +301,6 @@ const indexConfigKey contextKey = "indexConfig"
|
||||||
func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) {
|
func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
ic := r.Context().Value(indexConfigKey).(*indexConfig)
|
ic := r.Context().Value(indexConfigKey).(*indexConfig)
|
||||||
blog, bc := a.getBlog(r)
|
blog, bc := a.getBlog(r)
|
||||||
search := chi.URLParam(r, "search")
|
|
||||||
if search != "" {
|
|
||||||
// Decode and sanitize search
|
|
||||||
search = cleanHTMLText(searchDecode(search))
|
|
||||||
}
|
|
||||||
sections := lo.Map(ic.sections, func(i *configSection, _ int) string { return i.Name })
|
sections := lo.Map(ic.sections, func(i *configSection, _ int) string { return i.Name })
|
||||||
if ic.section != nil {
|
if ic.section != nil {
|
||||||
sections = append(sections, ic.section.Name)
|
sections = append(sections, ic.section.Name)
|
||||||
|
@ -324,7 +320,7 @@ func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
taxonomy: ic.tax,
|
taxonomy: ic.tax,
|
||||||
taxonomyValue: ic.taxValue,
|
taxonomyValue: ic.taxValue,
|
||||||
parameter: ic.parameter,
|
parameter: ic.parameter,
|
||||||
search: search,
|
search: ic.search,
|
||||||
publishedYear: ic.year,
|
publishedYear: ic.year,
|
||||||
publishedMonth: ic.month,
|
publishedMonth: ic.month,
|
||||||
publishedDay: ic.day,
|
publishedDay: ic.day,
|
||||||
|
@ -347,8 +343,8 @@ func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
title = ic.section.Title
|
title = ic.section.Title
|
||||||
} else if ic.tax != nil {
|
} else if ic.tax != nil {
|
||||||
title = fmt.Sprintf("%s: %s", ic.tax.Title, ic.taxValue)
|
title = fmt.Sprintf("%s: %s", ic.tax.Title, ic.taxValue)
|
||||||
} else if search != "" {
|
} else if ic.search != "" {
|
||||||
title = fmt.Sprintf("%s: %s", bc.Search.Title, search)
|
title = fmt.Sprintf("%s: %s", bc.Search.Title, ic.search)
|
||||||
}
|
}
|
||||||
title += ic.titleSuffix
|
title += ic.titleSuffix
|
||||||
// Description
|
// Description
|
||||||
|
@ -363,12 +359,8 @@ func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
a.generateFeed(blog, ft, w, r, posts, title, description)
|
a.generateFeed(blog, ft, w, r, posts, title, description)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Path
|
|
||||||
path := ic.path
|
|
||||||
if strings.Contains(path, searchPlaceholder) {
|
|
||||||
path = strings.ReplaceAll(path, searchPlaceholder, searchEncode(search))
|
|
||||||
}
|
|
||||||
// Navigation
|
// Navigation
|
||||||
|
path := ic.path
|
||||||
var hasPrev, hasNext bool
|
var hasPrev, hasNext bool
|
||||||
var prevPage, nextPage int
|
var prevPage, nextPage int
|
||||||
var prevPath, nextPath string
|
var prevPath, nextPath string
|
||||||
|
|
13
search.go
13
search.go
|
@ -5,6 +5,8 @@ import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultSearchPath = "/search"
|
const defaultSearchPath = "/search"
|
||||||
|
@ -30,8 +32,17 @@ func (a *goBlog) serveSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *goBlog) serveSearchResult(w http.ResponseWriter, r *http.Request) {
|
func (a *goBlog) serveSearchResult(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var searchParamValue, decodedSearch string
|
||||||
|
// Get search parameter from path
|
||||||
|
searchParamValue = chi.URLParam(r, "search")
|
||||||
|
if searchParamValue != "" {
|
||||||
|
// Decode and sanitize search
|
||||||
|
decodedSearch = cleanHTMLText(searchDecode(searchParamValue))
|
||||||
|
}
|
||||||
|
// Serve index
|
||||||
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
|
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
|
||||||
path: r.Context().Value(pathKey).(string) + "/" + searchPlaceholder,
|
path: r.Context().Value(pathKey).(string) + "/" + searchParamValue,
|
||||||
|
search: decodedSearch,
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue