From 1dc9d796fa83211ddbe29f78bd852ed1f225bf58 Mon Sep 17 00:00:00 2001 From: xing Date: Sun, 25 Sep 2022 01:44:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper/func.go | 6 +++--- helper/func_test.go | 5 +++++ plugins/Excerpt.go | 24 +----------------------- 3 files changed, 9 insertions(+), 26 deletions(-) diff --git a/helper/func.go b/helper/func.go index 2d36e0d..8a7d4f8 100644 --- a/helper/func.go +++ b/helper/func.go @@ -135,7 +135,7 @@ func StripTagsX(str, allowable string) string { } 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"} +var selfCloseTags = map[string]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) @@ -147,13 +147,13 @@ func CloseHtmlTag(str string) string { ss := strings.TrimSpace(tagx.FindString(s)) if ss[len(ss)-1] != '>' { ss = fmt.Sprintf("%s>", ss) - if IsContainInArr(ss, selfCloseTags) { + if _, ok := selfCloseTags[ss]; ok { continue } } tagss = append(tagss, ss) } - r := SliceMap(ClearClosedTag(tagss), func(s string) string { + r := SliceMap(SliceReverse(ClearClosedTag(tagss)), func(s string) string { return fmt.Sprintf("", strings.Trim(s, "<>")) }) return strings.Join(r, "") diff --git a/helper/func_test.go b/helper/func_test.go index 36694e1..fc7c03d 100644 --- a/helper/func_test.go +++ b/helper/func_test.go @@ -287,6 +287,11 @@ func TestCloseHtmlTag(t *testing.T) { args: args{str: `
GRANT privileges ON databasename.tablename TO 'username'@'h...

继续阅读

`}, want: "
", }, + { + name: "t2", + args: args{str: `
`}, + want: "
", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/plugins/Excerpt.go b/plugins/Excerpt.go index 98d5c7f..ea741ae 100644 --- a/plugins/Excerpt.go +++ b/plugins/Excerpt.go @@ -46,7 +46,7 @@ func ExceptRaw(str string, limit, id int) string { break } else { content = string(ru[:end]) - closeTag := CloseHtmlTag(content) + closeTag := helper.CloseHtmlTag(content) tmp := `%s......%s

继续阅读

` if strings.Contains(closeTag, "pre") || strings.Contains(closeTag, "code") { tmp = `%s%s......

继续阅读

` @@ -66,25 +66,3 @@ 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(helper.ClearClosedTag(tagss), func(s string) string { - return fmt.Sprintf("", strings.Trim(s, "<>")) - }) - return strings.Join(r, "") -}