elighterjs做成插件

This commit is contained in:
xing 2023-03-02 23:49:28 +08:00
parent d3be997b8f
commit 2ea52f1752
18 changed files with 81 additions and 34 deletions

View File

@ -93,5 +93,7 @@ postOrder: "desc"
uploadDir: ""
# pprof route path 为空表示不开启pprof,否则为pprof的路由
pprof: "/debug/pprof"
# 程序插件
plugins: ["enlightjs"]
# 列表页面post使用的插件
listPagePlugins: ["passwordProject","digest","twentyseventeen_postThumbnail"]

View File

@ -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)

View File

@ -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 {

View File

@ -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 `<link rel='stylesheet' id='enlighterjs-css' href='/wp-content/plugins/enlighter/cache/enlighterjs.min.css' media='all' />`
})
h.PushGroupFooterScript(20, func(h *wp.Handle) string {
return str.Join(`<script src='/wp-content/plugins/enlighter/cache/enlighterjs.min.js?ver=0A0B0C' id='enlighterjs-js'></script>`, "\n", enlighterjs)
})
}
var enlighterjs = `<script id='enlighterjs-js-after'>
!function(e,n){if("undefined"!=typeof EnlighterJS){var o={"selectors":{"block":"pre.EnlighterJSRAW","inline":"code.EnlighterJSRAW"},"options":{"indent":4,"ampersandCleanup":true,"linehover":true,"rawcodeDbclick":false,"textOverflow":"break","linenumbers":true,"theme":"enlighter","language":"generic","retainCssClasses":false,"collapse":false,"toolbarOuter":"","toolbarTop":"{BTN_RAW}{BTN_COPY}{BTN_WINDOW}{BTN_WEBSITE}","toolbarBottom":""}};(e.EnlighterJSINIT=function(){EnlighterJS.init(o.selectors.block,o.selectors.inline,o.options)})()}else{(n&&(n.error||n.log)||function(){})("Error: EnlighterJS resources not loaded yet!")}}(window,console);
</script>`

View File

@ -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)
}
}
}

View File

@ -1,4 +1,4 @@
package plugins
package wpposts
import (
"fmt"

View File

@ -8,10 +8,7 @@
var screenReaderText = {"expand":"<span class=\"screen-reader-text\">\u5c55\u5f00\u5b50\u83dc\u5355<\/span>","collapse":"<span class=\"screen-reader-text\">\u6298\u53e0\u5b50\u83dc\u5355<\/span>"};
</script>
<script src='/wp-content/themes/twentyfifteen/js/functions.js?ver=20220524' id='twentyfifteen-script-js'></script>
<script src='/wp-content/plugins/enlighter/cache/enlighterjs.min.js?ver=0A0B0C' id='enlighterjs-js'></script>
<script id='enlighterjs-js-after'>
!function(e,n){if("undefined"!=typeof EnlighterJS){var o={"selectors":{"block":"pre.EnlighterJSRAW","inline":"code.EnlighterJSRAW"},"options":{"indent":4,"ampersandCleanup":true,"linehover":true,"rawcodeDbclick":false,"textOverflow":"break","linenumbers":true,"theme":"enlighter","language":"generic","retainCssClasses":false,"collapse":false,"toolbarOuter":"","toolbarTop":"{BTN_RAW}{BTN_COPY}{BTN_WINDOW}{BTN_WEBSITE}","toolbarBottom":""}};(e.EnlighterJSINIT=function(){EnlighterJS.init(o.selectors.block,o.selectors.inline,o.options)})()}else{(n&&(n.error||n.log)||function(){})("Error: EnlighterJS resources not loaded yet!")}}(window,console);
</script>
{{template "common/footer" .}}
{{ block "footer" .}}
{{end}}

View File

@ -15,7 +15,6 @@
<link href='https://fonts.gstatic.com' crossorigin rel='preconnect' />
<link rel="alternate" type="application/rss+xml" title="{{ .title }} &raquo; Feed" href="/feed" />
<link rel="alternate" type="application/rss+xml" title="{{ .title }} &raquo; 评论Feed" href="/comments/feed" />
<link rel='stylesheet' id='enlighterjs-css' href='/wp-content/plugins/enlighter/cache/enlighterjs.min.css?ver=0A0B0C' media='all' />
<script>
window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/","ext":".png","svgUrl":"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/svg\/","svgExt":".svg","source":{"concatemoji":"\/wp-includes\/js\/wp-emoji-release.min.js?ver=6.0.2"}};

View File

@ -1,7 +1,7 @@
{{template "layout/base" .}}
{{define "content"}}
{{ if and .post gt .post.Id 0}}
{{ if and (.post) (gt .post.Id 0)}}
<div id="primary" class="content-area">
<main id="main" class="site-main">
<article id="post-{{.post.Id}}"

View File

@ -3,8 +3,10 @@ package twentyfifteen
import (
"embed"
"encoding/json"
"github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/plugins/wphandle"
"github.com/fthvgb1/wp-go/internal/theme/wp"
)
@ -37,6 +39,8 @@ func Hook(h *wp.Handle) {
func dispatch(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
h.WidgetAreaData()
h.GetPassword()
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)
h.PushGroupHeadScript(10, CalCustomBackGround, colorSchemeCss)
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(customHeader, 10))
switch h.Scene() {

View File

@ -6,11 +6,6 @@
<script src="/wp-content/themes/twentyseventeen/assets/js/skip-link-focus-fix.js?ver=20161114" id="twentyseventeen-skip-link-focus-fix-js"></script>
<script src="/wp-content/themes/twentyseventeen/assets/js/global.js?ver=20211130" id="twentyseventeen-global-js"></script>
<script src="/wp-content/themes/twentyseventeen/assets/js/jquery.scrollTo.js?ver=2.1.3" id="jquery-scrollto-js"></script>
<script src='/wp-content/plugins/enlighter/cache/enlighterjs.min.js?ver=0A0B0C' id='enlighterjs-js'></script>
<script id='enlighterjs-js-after'>
!function(e,n){if("undefined"!=typeof EnlighterJS){var o={"selectors":{"block":"pre.EnlighterJSRAW","inline":"code.EnlighterJSRAW"},"options":{"indent":4,"ampersandCleanup":true,"linehover":true,"rawcodeDbclick":false,"textOverflow":"break","linenumbers":true,"theme":"enlighter","language":"generic","retainCssClasses":false,"collapse":false,"toolbarOuter":"","toolbarTop":"{BTN_RAW}{BTN_COPY}{BTN_WINDOW}{BTN_WEBSITE}","toolbarBottom":""}};(e.EnlighterJSINIT=function(){EnlighterJS.init(o.selectors.block,o.selectors.inline,o.options)})()}else{(n&&(n.error||n.log)||function(){})("Error: EnlighterJS resources not loaded yet!")}}(window,console);
</script>
{{template "common/footer" .}}
{{ block "footer" .}}
{{end}}

View File

@ -53,7 +53,6 @@
<!--[if lt IE 9]>
<link rel='stylesheet' id='twentyseventeen-ie8-css' href='/wp-content/themes/twentyseventeen/assets/css/ie8.css?ver=20161202' media='all' />
<![endif]-->
<link rel='stylesheet' id='enlighterjs-css' href='/wp-content/plugins/enlighter/cache/enlighterjs.min.css?ver=0A0B0C' media='all' />
<!--[if lt IE 9]>
<script src='/wp-content/themes/twentyseventeen/assets/js/html5.js?ver=20161020' id='html5-js'></script>
<![endif]-->

View File

@ -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}}
<div class="single-featured-image-header">
<img width="{{.post.Thumbnail.OriginAttachmentData.Width}}" height="{{.post.Thumbnail.OriginAttachmentData.Height}}" src="{{.post.Thumbnail.Path}}" class="attachment-twentyseventeen-featured-image size-twentyseventeen-featured-image wp-post-image" alt="{{.post.PostTitle}}" decoding="async" loading="lazy" srcset="{{.post.Thumbnail.Srcset}}" sizes="{{.post.Thumbnail.Sizes}}">

View File

@ -8,10 +8,12 @@ import (
"github.com/fthvgb1/wp-go/helper/maps"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"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/wphandle"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/gin-gonic/gin"
@ -47,6 +49,7 @@ func Hook(h *wp.Handle) {
func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
h.WidgetAreaData()
h.GetPassword()
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20))
errHandle := wp.NewHandleFn(errorsHandle, 100)
h.PushHandleFn(constraints.Error404, errHandle)

View File

@ -9,6 +9,7 @@ import (
"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"
)
@ -61,9 +62,9 @@ func (d *DetailHandle) CheckAndGetPost() (err error) {
func (d *DetailHandle) PasswordProject() {
if d.Post.PostPassword != "" {
plugins.PasswordProjectTitle(&d.Post)
wpposts.PasswordProjectTitle(&d.Post)
if d.password != d.Post.PostPassword {
plugins.PasswdProjectContent(&d.Post)
wpposts.PasswdProjectContent(&d.Post)
}
d.ginH["post"] = d.Post
}

View File

@ -145,8 +145,8 @@ func (i *IndexHandle) Render() {
i.PushHandleFn(constraints.Ok, NewHandleFn(func(h *Handle) {
i.ExecPostsPlugin()
i.Pagination()
}, 10))
i.ginH["posts"] = i.Posts
}, 10))
i.Handle.Render()
}

View File

@ -10,6 +10,7 @@ import (
"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"
)
type Fn[T any] func(T) T
@ -32,9 +33,9 @@ var pluginFns = map[string]Plugin[models.Posts, *Handle]{
func PasswordProject(next Fn[models.Posts], h *Handle, post models.Posts) (r models.Posts) {
r = post
if post.PostPassword != "" {
plugins.PasswordProjectTitle(&r)
wpposts.PasswordProjectTitle(&r)
if h.password != post.PostPassword {
plugins.PasswdProjectContent(&r)
wpposts.PasswdProjectContent(&r)
return
}
}
@ -58,7 +59,7 @@ func ListPostPlugins() map[string]Plugin[models.Posts, *Handle] {
func ProjectTitle(t models.Posts) models.Posts {
if t.PostPassword != "" {
plugins.PasswordProjectTitle(&t)
wpposts.PasswordProjectTitle(&t)
}
return t
}

View File

@ -34,13 +34,7 @@ type Handle struct {
abort bool
}
func (h *Handle) Abort() {
h.abort = true
}
func (h *Handle) CommonThemeMods() wpconfig.ThemeMods {
return h.themeMods
}
type HandlePlugins map[string]HandleFn[*Handle]
// Components Order 为执行顺序,降序执行
type Components struct {
@ -57,6 +51,14 @@ type HandleCall struct {
Order int
}
func (h *Handle) Abort() {
h.abort = true
}
func (h *Handle) CommonThemeMods() wpconfig.ThemeMods {
return h.themeMods
}
func (h *Handle) Err() error {
return h.err
}