From a8d1dcdd5ba9270f40cad1d3c608b1cfa1fadf63 Mon Sep 17 00:00:00 2001 From: xing Date: Wed, 10 Apr 2024 23:48:23 +0800 Subject: [PATCH] optimize code and fix bug --- app/plugins/digest.go | 26 +++++++++++++++++--------- plugin/digest/digest.go | 18 +++++++++--------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/app/plugins/digest.go b/app/plugins/digest.go index 0703e10..0b08cce 100644 --- a/app/plugins/digest.go +++ b/app/plugins/digest.go @@ -9,6 +9,7 @@ import ( "github.com/fthvgb1/wp-go/cache/cachemanager" "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper" + "github.com/fthvgb1/wp-go/helper/maps" str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/plugin/digest" "github.com/fthvgb1/wp-go/safety" @@ -74,12 +75,10 @@ func ParseDigestConf(c DigestConfig) map[string]digest.SpecialSolveConf { if tag == "" { continue } + ec := make(map[rune]digest.SpecialSolve) + specialTags := make(map[string]digest.SpecialSolve) tag = str.Join("<", tag) - var ec map[rune]digest.SpecialSolve - var specialTags map[string]digest.SpecialSolve if len(item.EscapeCharacter) > 0 { - ec = make(map[rune]digest.SpecialSolve) - specialTags = make(map[string]digest.SpecialSolve) for _, esc := range item.EscapeCharacter { for _, i := range esc.Character { s := []rune(i) @@ -106,12 +105,21 @@ func ParseDigestConf(c DigestConfig) map[string]digest.SpecialSolveConf { } } } - specialSolve[tag] = digest.SpecialSolveConf{ - Num: item.Num, - ChuckOvered: item.ChuckOvered, - EscapeCharacter: ec, - Tags: specialTags, + v, ok := specialSolve[tag] + if !ok { + specialSolve[tag] = digest.SpecialSolveConf{ + Num: item.Num, + ChuckOvered: item.ChuckOvered, + EscapeCharacter: ec, + Tags: specialTags, + } + continue } + v.Num = item.Num + v.ChuckOvered = item.ChuckOvered + v.EscapeCharacter = maps.Merge(v.EscapeCharacter, ec) + v.Tags = maps.Merge(v.Tags, specialTags) + specialSolve[tag] = v } } return specialSolve diff --git a/plugin/digest/digest.go b/plugin/digest/digest.go index 19d2387..9ea0bd8 100644 --- a/plugin/digest/digest.go +++ b/plugin/digest/digest.go @@ -184,17 +184,17 @@ func CustomizeHtml(content string, limit int, m map[string]SpecialSolveConf) (st } currentTags := allTags[len(allTags)-1] mm, ok := m[currentTags] - if !ok { + if !ok || len(mm.EscapeCharacter) < 1 { count++ - } else if len(mm.EscapeCharacter) > 0 { - if n, ok := mm.EscapeCharacter[runeContent[i]]; ok { - if (count+n.Num) > limit && n.ChuckOvered { - break - } - count += n.Num - } else { - count++ + continue + } + if n, ok := mm.EscapeCharacter[runeContent[i]]; ok { + if (count+n.Num) > limit && n.ChuckOvered { + break } + count += n.Num + } else { + count++ } } if i > runeTotal {