小优化

This commit is contained in:
xing 2023-02-09 12:57:18 +08:00
parent febaab279d
commit d88dd6b853
2 changed files with 16 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package common package common
import ( import (
"context"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/pkg/config" "github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
@ -29,11 +30,11 @@ func PluginFn[T any](a []Plugin[T], h Handle, fn Fn[T]) Fn[T] {
} }
var plugin = []Plugin[models.Posts]{ var plugin = []Plugin[models.Posts]{
PasswordProject, Digest, PasswordProject,
} }
func Plugins() []Plugin[models.Posts] { func Plugins() []Plugin[models.Posts] {
return plugin return slice.Copy(plugin)
} }
func Default[T any](t T) T { func Default[T any](t T) T {
@ -61,3 +62,14 @@ func Digest(next Fn[models.Posts], h Handle, post models.Posts) models.Posts {
} }
return next(post) return next(post)
} }
func Digests(ctx context.Context) Fn[models.Posts] {
return func(post models.Posts) models.Posts {
if post.PostExcerpt != "" {
plugins.PostExcerpt(&post)
} else {
plugins.Digest(ctx, &post, config.GetConfig().DigestWordCount)
}
return post
}
}

View File

@ -48,12 +48,12 @@ func Hook(h2 common.Handle) {
h.index() h.index()
} }
var plugin = slice.Copy(common.Plugins()) var plugin = common.Plugins()
func (h handle) index() { func (h handle) index() {
if h.Stats != plugins.Empty404 { if h.Stats != plugins.Empty404 {
posts := h.GinH["posts"].([]models.Posts) posts := h.GinH["posts"].([]models.Posts)
posts = slice.Map(posts, common.PluginFn[models.Posts](plugin, h.Handle, common.Default[models.Posts])) posts = slice.Map(posts, common.PluginFn[models.Posts](plugin, h.Handle, common.Digests(h.C)))
p, ok := h.GinH["pagination"] p, ok := h.GinH["pagination"]
if ok { if ok {
pp, ok := p.(pagination.ParsePagination) pp, ok := p.(pagination.ParsePagination)