From ad875857d81084eba323ba87056c4c046c073e8e Mon Sep 17 00:00:00 2001 From: xing Date: Fri, 10 Mar 2023 18:20:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=80=E8=BF=91=E6=96=87=E7=AB=A0=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/theme/wp/components/constraints.go | 3 +- internal/theme/wp/recentposts.go | 72 +++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 internal/theme/wp/recentposts.go diff --git a/internal/theme/wp/components/constraints.go b/internal/theme/wp/components/constraints.go index 801e7c4..26d0360 100644 --- a/internal/theme/wp/components/constraints.go +++ b/internal/theme/wp/components/constraints.go @@ -1,5 +1,6 @@ package components const ( - SearchFormArgs = "SearchFormArgs" + SearchFormArgs = "SearchFormArgs" + RecentPostsArgs = "RecentPostsArgs" ) diff --git a/internal/theme/wp/recentposts.go b/internal/theme/wp/recentposts.go new file mode 100644 index 0000000..d645c16 --- /dev/null +++ b/internal/theme/wp/recentposts.go @@ -0,0 +1,72 @@ +package wp + +import ( + "fmt" + "github.com/fthvgb1/wp-go/helper/maps" + "github.com/fthvgb1/wp-go/helper/number" + "github.com/fthvgb1/wp-go/helper/slice" + str "github.com/fthvgb1/wp-go/helper/strings" + "github.com/fthvgb1/wp-go/internal/pkg/cache" + "github.com/fthvgb1/wp-go/internal/pkg/constraints" + "github.com/fthvgb1/wp-go/internal/pkg/models" + "github.com/fthvgb1/wp-go/internal/theme/wp/components" + "github.com/fthvgb1/wp-go/internal/wpconfig" + "strings" +) + +var recentPostsArgs = map[string]string{ + "{$before_widget}": `", + "{$before_title}": `

`, + "{$after_title}": "

", + "{$before_sidebar}": "", + "{$after_sidebar}": "", + "{$nav}": "", + "{$navCloser}": "", +} + +var recentPostsTemplate = `{$before_widget} +{$nav} + +{$navCloser} +{$after_widget} +` + +var recentConf = map[any]any{ + "number": int64(5), + "show_date": false, + "title": "近期文章", +} + +func RecentPosts(h *Handle) string { + args := GetComponentsArgs(h, components.RecentPostsArgs, recentPostsArgs) + args = maps.Merge(recentPostsArgs, args) + conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recentConf, int64(2)) + if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") { + args["{$nav}"] = fmt.Sprintf(`" + } + currentPostId := uint64(0) + if h.Scene() == constraints.Detail { + currentPostId = h.Detail.Post.Id + } + posts := slice.Map(cache.RecentPosts(h.C, int(conf["number"].(int64))), func(t models.Posts) string { + t = ProjectTitle(t) + date := "" + if conf["show_date"].(bool) { + date = fmt.Sprintf(`%s`, t.PostDateGmt.Format("2006年01月02日")) + } + ariaCurrent := "" + if currentPostId == t.Id { + ariaCurrent = ` aria-current="page"` + } + return fmt.Sprintf(`
  • + %s + %s +
  • `, str.Join("/p/", number.ToString(t.Id)), ariaCurrent, t.PostTitle, date) + }) + s := strings.ReplaceAll(recentPostsTemplate, "{$li}", strings.Join(posts, "\n")) + return str.Replace(s, args) +}