diff --git a/helper/func.go b/helper/func.go index 4a15b87..068cc53 100644 --- a/helper/func.go +++ b/helper/func.go @@ -126,3 +126,59 @@ func StripTagsX(str, allowable string) string { html := rex.ReplaceAllString(tmp, "<$1>") return html } + +var tagx = regexp.MustCompile(`()`) +var selfCloseTags = []string{"area", "base", "basefont", "br", "col", "command", "embed", "frame", "hr", "img", "input", "isindex", "link", "meta", "param", "source", "track", "wbr"} + +func CloseHtmlTag(str string) string { + tags := tag.FindAllString(str, -1) + if len(tags) < 1 { + return str + } + var tagss = make([]string, 0, len(tags)) + for _, s := range tags { + ss := strings.TrimSpace(tagx.FindString(s)) + if ss[len(ss)-1] != '>' { + ss = fmt.Sprintf("%s>", ss) + if IsContainInArr(ss, selfCloseTags) { + continue + } + } + tagss = append(tagss, ss) + } + r := SliceMap[string, string](ClearClosedTag(tagss), func(s string) string { + return fmt.Sprintf("", strings.Trim(s, "<>")) + }) + return strings.Join(r, "") +} + +func ClearClosedTag(s []string) []string { + i := 0 + for { + if len(s[i:]) < 2 { + return s + } + l := s[i] + r := fmt.Sprintf(``, strings.Trim(l, "<>")) + if s[i+1] == r { + if len(s[i+1:]) > 1 { + ss := s[:i] + s = append(ss, s[i+2:]...) + + } else { + s = s[:i] + } + i = 0 + continue + } + i++ + } +} + +func SliceMap[T any, R any](arr []T, fun func(T) R) []R { + r := make([]R, 0, len(arr)) + for _, t := range arr { + r = append(r, fun(t)) + } + return r +} diff --git a/helper/func_test.go b/helper/func_test.go index c94f2de..ae98861 100644 --- a/helper/func_test.go +++ b/helper/func_test.go @@ -263,3 +263,66 @@ func BenchmarkStripTagsX(b *testing.B) { StripTagsX(`

pppppffff

`, "

") } } + +func TestCloseHtmlTag(t *testing.T) { + type args struct { + str string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "t1", + args: args{str: `

GRANT privileges ON databasename.tablename TO 'username'@'h...

继续阅读

`}, + want: "
", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := CloseHtmlTag(tt.args.str); !reflect.DeepEqual(got, tt.want) { + t.Errorf("CloseHtmlTag() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_clearTag(t *testing.T) { + type args struct { + s []string + } + tests := []struct { + name string + args args + want []string + }{ + { + name: "t1", + args: args{s: []string{"
", "

", "", ""}}, + want: []string{"

", "

"}, + }, + { + name: "t2", + args: args{s: []string{"

", "
", "
", "", ""}}, + want: []string{"
"}, + }, + { + name: "t3", + args: args{s: []string{"
", "
"}}, + want: []string{}, + }, + { + name: "t4", + args: args{s: []string{"
", "

"}}, + want: []string{"

", "

"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := ClearClosedTag(tt.args.s); !reflect.DeepEqual(got, tt.want) { + t.Errorf("ClearClosedTag() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/plugins/Excerpt.go b/plugins/Excerpt.go index 87ad190..0a34175 100644 --- a/plugins/Excerpt.go +++ b/plugins/Excerpt.go @@ -16,7 +16,6 @@ var tag = regexp.MustCompile(`<.*?>`) var limit = 300 func ExceptRaw(str string, limit, id int) string { - if r := more.FindString(str); r != "" { m := strings.Split(str, r) str = m[0] @@ -40,7 +39,6 @@ func ExceptRaw(str string, limit, id int) string { count += len(t) l += len(t) } - if count > 0 && length > l { start = end end += count @@ -48,8 +46,12 @@ func ExceptRaw(str string, limit, id int) string { break } else { content = string(ru[:end]) - content = fmt.Sprintf(`%s...

继续阅读

`, content, id) - // todo 标签闭合 + closeTag := CloseHtmlTag(content) + if strings.Contains(closeTag, "pre") || strings.Contains(closeTag, "code") { + content = fmt.Sprintf(`%s%s......

继续阅读

`, content, closeTag, id) + } else { + content = fmt.Sprintf(`%s......%s

继续阅读

`, content, closeTag, id) + } break } } @@ -64,3 +66,25 @@ func Except(p *Plugin[models.WpPosts], c *gin.Context, post *models.WpPosts, sce post.PostContent = ExceptRaw(post.PostContent, limit, int(post.Id)) } + +var tagx = regexp.MustCompile(`()`) +var tagAllow = regexp.MustCompile(`<(a|b|blockquote|cite|code|dd|del|div|dl|dt|em|h1|h2|h3|h4|h5|h6|i|li|ol|p|pre|span|strong|ul).*?>`) + +func CloseHtmlTag(str string) string { + tags := tagAllow.FindAllString(str, -1) + if len(tags) < 1 { + return str + } + var tagss = make([]string, 0, len(tags)) + for _, s := range tags { + ss := strings.TrimSpace(tagx.FindString(s)) + if ss[len(ss)-1] != '>' { + ss = fmt.Sprintf("%s>", ss) + } + tagss = append(tagss, ss) + } + r := helper.SliceMap[string, string](helper.ClearClosedTag(tagss), func(s string) string { + return fmt.Sprintf("", strings.Trim(s, "<>")) + }) + return strings.Join(r, "") +}