From 2ea52f1752f252e202a01bb20bbf6b1478c0295b Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 2 Mar 2023 23:49:28 +0800 Subject: [PATCH] =?UTF-8?q?elighterjs=E5=81=9A=E6=88=90=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.example.yaml | 2 ++ internal/pkg/cache/feed.go | 14 +++++------ internal/pkg/config/config.go | 1 + .../plugins/wphandle/enlightjs/enlighterjs.go | 20 ++++++++++++++++ internal/plugins/wphandle/handle.go | 23 +++++++++++++++++++ internal/plugins/{ => wpposts}/posts.go | 2 +- .../theme/twentyfifteen/layout/footer.gohtml | 5 +--- .../theme/twentyfifteen/layout/head.gohtml | 1 - .../theme/twentyfifteen/posts/detail.gohtml | 2 +- internal/theme/twentyfifteen/twentyfifteen.go | 4 ++++ .../twentyseventeen/layout/footer.gohtml | 5 ---- .../theme/twentyseventeen/layout/head.gohtml | 1 - .../theme/twentyseventeen/posts/detail.gohtml | 2 +- .../theme/twentyseventeen/twentyseventeen.go | 3 +++ internal/theme/wp/detail.go | 5 ++-- internal/theme/wp/index.go | 2 +- internal/theme/wp/listpostplugins.go | 7 +++--- internal/theme/wp/wp.go | 16 +++++++------ 18 files changed, 81 insertions(+), 34 deletions(-) create mode 100644 internal/plugins/wphandle/enlightjs/enlighterjs.go create mode 100644 internal/plugins/wphandle/handle.go rename internal/plugins/{ => wpposts}/posts.go (97%) diff --git a/config.example.yaml b/config.example.yaml index d5ee995..fd3a006 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -93,5 +93,7 @@ postOrder: "desc" uploadDir: "" # pprof route path 为空表示不开启pprof,否则为pprof的路由 pprof: "/debug/pprof" +# 程序插件 +plugins: ["enlightjs"] # 列表页面post使用的插件 listPagePlugins: ["passwordProject","digest","twentyseventeen_postThumbnail"] \ No newline at end of file diff --git a/internal/pkg/cache/feed.go b/internal/pkg/cache/feed.go index 7cded30..90d975e 100644 --- a/internal/pkg/cache/feed.go +++ b/internal/pkg/cache/feed.go @@ -7,7 +7,7 @@ import ( str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/models" - "github.com/fthvgb1/wp-go/internal/plugins" + "github.com/fthvgb1/wp-go/internal/plugins/wpposts" "github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/plugin/digest" "github.com/fthvgb1/wp-go/rss2" @@ -60,8 +60,8 @@ func feed(arg ...any) (xml []string, err error) { rs.Items = slice.Map(posts, func(t models.Posts) rss2.Item { desc := "无法提供摘要。这是一篇受保护的文章。" if t.PostPassword != "" { - plugins.PasswordProjectTitle(&t) - plugins.PasswdProjectContent(&t) + wpposts.PasswordProjectTitle(&t) + wpposts.PasswdProjectContent(&t) } else { desc = digest.Raw(t.PostContent, 55, fmt.Sprintf("/p/%d", t.Id)) } @@ -116,8 +116,8 @@ func postFeed(arg ...any) (x string, err error) { rs.Link = fmt.Sprintf("%s/p/%d", site, post.Id) rs.LastBuildDate = time.Now().Format(timeFormat) if post.PostPassword != "" { - plugins.PasswordProjectTitle(&post) - plugins.PasswdProjectContent(&post) + wpposts.PasswordProjectTitle(&post) + wpposts.PasswdProjectContent(&post) if len(comments) > 0 { t := comments[len(comments)-1] rs.Items = []rss2.Item{ @@ -168,8 +168,8 @@ func commentsFeed(args ...any) (r []string, err error) { desc := "评论受保护:要查看请输入密码。" content := t.CommentContent if post.PostPassword != "" { - plugins.PasswordProjectTitle(&post) - plugins.PasswdProjectContent(&post) + wpposts.PasswordProjectTitle(&post) + wpposts.PasswdProjectContent(&post) content = post.PostContent } else { desc = digest.ClearHtml(t.CommentContent) diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 2a2ebac..8dbb1a1 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -34,6 +34,7 @@ type Config struct { ListPagePlugins []string `yaml:"listPagePlugins"` PaginationStep int `yaml:"paginationStep"` ShowQuerySql bool `yaml:"showQuerySql"` + Plugins []string `yaml:"plugins"` } type CacheTime struct { diff --git a/internal/plugins/wphandle/enlightjs/enlighterjs.go b/internal/plugins/wphandle/enlightjs/enlighterjs.go new file mode 100644 index 0000000..8a994d5 --- /dev/null +++ b/internal/plugins/wphandle/enlightjs/enlighterjs.go @@ -0,0 +1,20 @@ +package enlightjs + +import ( + str "github.com/fthvgb1/wp-go/helper/strings" + "github.com/fthvgb1/wp-go/internal/theme/wp" +) + +func EnlighterJS(h *wp.Handle) { + h.PushGroupHeadScript(20, func(h *wp.Handle) string { + return `` + }) + + h.PushGroupFooterScript(20, func(h *wp.Handle) string { + return str.Join(``, "\n", enlighterjs) + }) +} + +var enlighterjs = `` diff --git a/internal/plugins/wphandle/handle.go b/internal/plugins/wphandle/handle.go new file mode 100644 index 0000000..c19afee --- /dev/null +++ b/internal/plugins/wphandle/handle.go @@ -0,0 +1,23 @@ +package wphandle + +import ( + "github.com/fthvgb1/wp-go/helper/maps" + "github.com/fthvgb1/wp-go/internal/plugins/wphandle/enlightjs" + "github.com/fthvgb1/wp-go/internal/theme/wp" +) + +var plugins = wp.HandlePlugins{ + "enlightjs": enlightjs.EnlighterJS, +} + +func Plugins() wp.HandlePlugins { + return maps.Copy(plugins) +} + +func RegisterPlugins(h *wp.Handle, calls ...string) { + for _, call := range calls { + if fn, ok := plugins[call]; ok { + fn(h) + } + } +} diff --git a/internal/plugins/posts.go b/internal/plugins/wpposts/posts.go similarity index 97% rename from internal/plugins/posts.go rename to internal/plugins/wpposts/posts.go index c241f32..62d3ed8 100644 --- a/internal/plugins/posts.go +++ b/internal/plugins/wpposts/posts.go @@ -1,4 +1,4 @@ -package plugins +package wpposts import ( "fmt" diff --git a/internal/theme/twentyfifteen/layout/footer.gohtml b/internal/theme/twentyfifteen/layout/footer.gohtml index ec3cc11..a7ec791 100644 --- a/internal/theme/twentyfifteen/layout/footer.gohtml +++ b/internal/theme/twentyfifteen/layout/footer.gohtml @@ -8,10 +8,7 @@ var screenReaderText = {"expand":"\u5c55\u5f00\u5b50\u83dc\u5355<\/span>","collapse":"\u6298\u53e0\u5b50\u83dc\u5355<\/span>"}; - - + {{template "common/footer" .}} {{ block "footer" .}} {{end}} diff --git a/internal/theme/twentyfifteen/layout/head.gohtml b/internal/theme/twentyfifteen/layout/head.gohtml index 56c155e..76a245f 100644 --- a/internal/theme/twentyfifteen/layout/head.gohtml +++ b/internal/theme/twentyfifteen/layout/head.gohtml @@ -15,7 +15,6 @@ - - - - {{template "common/footer" .}} {{ block "footer" .}} {{end}} diff --git a/internal/theme/twentyseventeen/layout/head.gohtml b/internal/theme/twentyseventeen/layout/head.gohtml index d8718e9..015ba2e 100644 --- a/internal/theme/twentyseventeen/layout/head.gohtml +++ b/internal/theme/twentyseventeen/layout/head.gohtml @@ -53,7 +53,6 @@ - diff --git a/internal/theme/twentyseventeen/posts/detail.gohtml b/internal/theme/twentyseventeen/posts/detail.gohtml index fa2d5b1..5f16e7a 100644 --- a/internal/theme/twentyseventeen/posts/detail.gohtml +++ b/internal/theme/twentyseventeen/posts/detail.gohtml @@ -1,7 +1,7 @@ {{template "layout/base" .}} {{define "content"}} - {{ if gt .post.Id 0}} + {{ if and (.post) (gt .post.Id 0) }} {{if .post.Thumbnail.Path}}