This commit is contained in:
xing 2023-03-10 18:33:09 +08:00
parent ad875857d8
commit 179e1e14e2

View File

@ -23,10 +23,12 @@ var recentPostsArgs = map[string]string{
"{$after_sidebar}": "", "{$after_sidebar}": "",
"{$nav}": "", "{$nav}": "",
"{$navCloser}": "", "{$navCloser}": "",
"{$title}": "",
} }
var recentPostsTemplate = `{$before_widget} var recentPostsTemplate = `{$before_widget}
{$nav} {$nav}
{$title}
<ul> <ul>
{$li} {$li}
</ul> </ul>
@ -44,13 +46,14 @@ func RecentPosts(h *Handle) string {
args := GetComponentsArgs(h, components.RecentPostsArgs, recentPostsArgs) args := GetComponentsArgs(h, components.RecentPostsArgs, recentPostsArgs)
args = maps.Merge(recentPostsArgs, args) args = maps.Merge(recentPostsArgs, args)
conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recentConf, int64(2)) conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recentConf, int64(2))
args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") { if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"]) args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"])
args["{$navCloser}"] = "</nav>" args["{$navCloser}"] = "</nav>"
} }
currentPostId := uint64(0) currentPostId := uint64(0)
if h.Scene() == constraints.Detail { if h.Scene() == constraints.Detail {
currentPostId = h.Detail.Post.Id currentPostId = str.ToInteger(h.C.Param("id"), uint64(0))
} }
posts := slice.Map(cache.RecentPosts(h.C, int(conf["number"].(int64))), func(t models.Posts) string { posts := slice.Map(cache.RecentPosts(h.C, int(conf["number"].(int64))), func(t models.Posts) string {
t = ProjectTitle(t) t = ProjectTitle(t)
@ -62,10 +65,10 @@ func RecentPosts(h *Handle) string {
if currentPostId == t.Id { if currentPostId == t.Id {
ariaCurrent = ` aria-current="page"` ariaCurrent = ` aria-current="page"`
} }
return fmt.Sprintf(`<li> return fmt.Sprintf(` <li>
<a href="%s"%s>%s</a> <a href="%s"%s>%s</a>
%s %s
</li>`, str.Join("/p/", number.ToString(t.Id)), ariaCurrent, t.PostTitle, date) </li>`, str.Join("/p/", number.ToString(t.Id)), ariaCurrent, t.PostTitle, date)
}) })
s := strings.ReplaceAll(recentPostsTemplate, "{$li}", strings.Join(posts, "\n")) s := strings.ReplaceAll(recentPostsTemplate, "{$li}", strings.Join(posts, "\n"))
return str.Replace(s, args) return str.Replace(s, args)