2023-01-12 12:42:16 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
2023-02-02 11:16:18 +00:00
|
|
|
"context"
|
2023-01-12 12:42:16 +00:00
|
|
|
"fmt"
|
2023-05-04 12:37:06 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/config"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/models"
|
2023-01-18 15:02:59 +00:00
|
|
|
"github.com/fthvgb1/wp-go/cache"
|
2023-11-07 07:18:34 +00:00
|
|
|
"github.com/fthvgb1/wp-go/cache/cachemanager"
|
2023-04-18 13:05:37 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper"
|
2023-01-18 15:02:59 +00:00
|
|
|
"github.com/fthvgb1/wp-go/plugin/digest"
|
2023-04-18 13:05:37 +00:00
|
|
|
"regexp"
|
2023-01-12 12:42:16 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2023-10-14 06:57:57 +00:00
|
|
|
"unicode/utf8"
|
2023-01-12 12:42:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var digestCache *cache.MapCache[uint64, string]
|
|
|
|
|
2023-04-18 13:05:37 +00:00
|
|
|
var more = regexp.MustCompile("<!--more(.*?)?-->")
|
|
|
|
|
|
|
|
var removeWpBlock = regexp.MustCompile("<!-- /?wp:.*-->")
|
|
|
|
|
2023-01-12 12:42:16 +00:00
|
|
|
func InitDigestCache() {
|
2023-10-30 13:52:15 +00:00
|
|
|
digestCache = cachemanager.NewMemoryMapCache(nil, digestRaw, config.GetConfig().CacheTime.DigestCacheTime, "digestPlugin", func() time.Duration {
|
|
|
|
return config.GetConfig().CacheTime.DigestCacheTime
|
|
|
|
})
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 13:05:37 +00:00
|
|
|
func RemoveWpBlock(s string) string {
|
|
|
|
return removeWpBlock.ReplaceAllString(s, "")
|
|
|
|
}
|
|
|
|
|
2023-10-26 13:38:31 +00:00
|
|
|
func digestRaw(ctx context.Context, id uint64, arg ...any) (string, error) {
|
2023-04-18 13:05:37 +00:00
|
|
|
s := arg[1].(string)
|
|
|
|
limit := arg[3].(int)
|
2023-01-12 12:42:16 +00:00
|
|
|
if limit < 0 {
|
2023-04-18 13:05:37 +00:00
|
|
|
return s, nil
|
2023-01-12 12:42:16 +00:00
|
|
|
} else if limit == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
2023-04-18 13:05:37 +00:00
|
|
|
|
|
|
|
s = more.ReplaceAllString(s, "")
|
|
|
|
fn := helper.GetContextVal(ctx, "postMoreFn", PostsMore)
|
|
|
|
return Digests(s, id, limit, fn), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Digests(content string, id uint64, limit int, fn func(id uint64, content, closeTag string) string) string {
|
|
|
|
closeTag := ""
|
|
|
|
content = RemoveWpBlock(content)
|
|
|
|
tag := config.GetConfig().DigestAllowTag
|
|
|
|
if tag == "" {
|
|
|
|
tag = "<a><b><blockquote><br><cite><code><dd><del><div><dl><dt><em><h1><h2><h3><h4><h5><h6><i><img><li><ol><p><pre><span><strong><ul>"
|
|
|
|
}
|
|
|
|
content = digest.StripTags(content, tag)
|
2023-10-14 06:57:57 +00:00
|
|
|
length := utf8.RuneCountInString(content) + 1
|
|
|
|
if length <= limit {
|
|
|
|
return content
|
|
|
|
}
|
2023-04-18 13:05:37 +00:00
|
|
|
content, closeTag = digest.Html(content, limit)
|
|
|
|
if fn == nil {
|
|
|
|
return PostsMore(id, content, closeTag)
|
|
|
|
}
|
|
|
|
return fn(id, content, closeTag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PostsMore(id uint64, content, closeTag string) string {
|
|
|
|
tmp := `%s......%s<p class="read-more"><a href="/p/%d">继续阅读</a></p>`
|
|
|
|
if strings.Contains(closeTag, "pre") || strings.Contains(closeTag, "code") {
|
|
|
|
tmp = `%s%s......<p class="read-more"><a href="/p/%d">继续阅读</a></p>`
|
|
|
|
}
|
|
|
|
content = fmt.Sprintf(tmp, content, closeTag, id)
|
|
|
|
return content
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 15:49:48 +00:00
|
|
|
func Digest(ctx context.Context, post *models.Posts, limit int) {
|
2023-04-18 13:05:37 +00:00
|
|
|
content, _ := digestCache.GetCache(ctx, post.Id, time.Second, ctx, post.PostContent, post.Id, limit)
|
2023-02-08 15:49:48 +00:00
|
|
|
post.PostContent = content
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 15:49:48 +00:00
|
|
|
func PostExcerpt(post *models.Posts) {
|
|
|
|
post.PostContent = strings.Replace(post.PostExcerpt, "\n", "<br/>", -1)
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|