This commit is contained in:
xing 2022-09-24 17:52:06 +08:00
parent 963a41f27e
commit 2a6d9612f8
6 changed files with 83 additions and 15 deletions

View File

@ -191,13 +191,13 @@ func Index(c *gin.Context) {
post, _ := common.PostsCache.Load(v.Id)
pp := post.(*models.WpPosts)
px := *pp
common.PasswordProjectTitle(&px)
if px.PostPassword != "" && pw != px.PostPassword {
common.PasswdProjectContent(&px)
}
postIds[i] = px
plugins.ApplyPlugin(plug, &postIds[i])
common.PasswordProjectTitle(&postIds[i])
if px.PostPassword != "" && pw != px.PostPassword {
common.PasswdProjectContent(&postIds[i])
} else {
plugins.ApplyPlugin(plug, &postIds[i])
}
}
for i, post := range recent {

7
go.mod
View File

@ -12,6 +12,7 @@ require (
require (
github.com/dlclark/regexp2 v1.7.0 // indirect
github.com/gin-contrib/pprof v1.4.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
@ -27,9 +28,9 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect
golang.org/x/net v0.0.0-20220909164309-bea034e7d591 // indirect
golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 // indirect
golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 // indirect
golang.org/x/net v0.0.0-20220923203811-8be639271d50 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)

View File

@ -102,3 +102,27 @@ func StripTags(str, allowable string) string {
}
return html
}
var tag = regexp.MustCompile(`<(.*?)>`)
func StripTagsX(str, allowable string) string {
if allowable == "" {
return allHtmlTag.ReplaceAllString(str, "")
}
tags := tag.ReplaceAllString(allowable, "$1|")
or := strings.TrimRight(tags, "|")
reg := fmt.Sprintf(`<(/?(%s).*?)>`, or)
regx := fmt.Sprintf(`\{\[(/?(%s).*?)\]\}`, or)
cp, err := regexp.Compile(reg)
if err != nil {
return str
}
rep := cp.ReplaceAllString(str, "{[$1]}")
tmp := tag.ReplaceAllString(rep, "")
rex, err := regexp.Compile(regx)
if err != nil {
return str
}
html := rex.ReplaceAllString(tmp, "<$1>")
return html
}

View File

@ -224,3 +224,42 @@ func TestStripTags(t *testing.T) {
})
}
}
func TestStripTagsX(t *testing.T) {
type args struct {
str string
allowable string
}
tests := []struct {
name string
args args
want string
}{
{
name: "t1",
args: args{
str: "<p>ppppp<span>ffff</span></p><img />",
allowable: "<p><img>",
},
want: "<p>pppppffff</p><img />",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := StripTagsX(tt.args.str, tt.args.allowable); got != tt.want {
t.Errorf("StripTagsX() = %v, want %v", got, tt.want)
}
})
}
}
func BenchmarkStripTags(b *testing.B) {
for i := 0; i < b.N; i++ {
StripTags(`<p>ppppp<span>ffff</span></p><img />`, "<p><img>")
}
}
func BenchmarkStripTagsX(b *testing.B) {
for i := 0; i < b.N; i++ {
StripTagsX(`<p>ppppp<span>ffff</span></p><img />`, "<p><img>")
}
}

View File

@ -25,16 +25,18 @@ func ExceptRaw(str string, limit, id int) string {
content := removeWpBlock.ReplaceAllString(str, "")
content = strings.Trim(content, " \t\n\r\000\x0B")
content = strings.Replace(content, "]]>", "]]&gt;", -1)
content = helper.StripTags(content, "<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 = helper.StripTagsX(content, "<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>")
length := utf8.RuneCountInString(content) + 1
if length > limit {
start, l := 0, limit+1
start, l := 0, limit
end := l
ru := []rune(content)
for {
txt := string([]rune(content)[start:end])
txt := string(ru[start:end])
count := 0
for _, ints := range tag.FindAllStringIndex(txt, -1) {
t := []rune(content[ints[0]:ints[1]])
t := txt[ints[0]:ints[1]]
count += len(t)
l += len(t)
}
@ -45,8 +47,9 @@ func ExceptRaw(str string, limit, id int) string {
} else if count > 0 && length < l {
break
} else {
content = string([]rune(content)[:end])
content = string(ru[:end])
content = fmt.Sprintf(`%s...<p class="read-more"><a href="/p/%d">继续阅读</a></p>`, content, id)
// todo 标签闭合
break
}
}

View File

@ -1,6 +1,7 @@
package route
import (
"github.com/gin-contrib/pprof"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
@ -50,6 +51,6 @@ func SetupRouter() *gin.Engine {
r.GET("/p/date/:year/:month/page/:page", actions.Index)
r.POST("/login", actions.Login)
r.GET("/p/:id", actions.Detail)
pprof.Register(r, "dev/pprof")
return r
}