@@ -34,38 +34,34 @@ import (
3434 "github.com/mozillazg/go-pinyin"
3535)
3636
37+ var (
38+ reCode = regexp .MustCompile (`(?ism)<(pre)>.*<\/pre>` )
39+ reCodeReplace = "{code...}"
40+ reLink = regexp .MustCompile (`(?ism)<a.*?[^<]>(.*)?<\/a>` )
41+ reLinkReplace = " [$1] "
42+ reSpace = regexp .MustCompile (` +` )
43+ reSpaceReplace = " "
44+
45+ spaceReplacer = strings .NewReplacer (
46+ "\n " , " " ,
47+ "\r " , " " ,
48+ "\t " , " " ,
49+ )
50+ )
51+
3752// ClearText clear HTML, get the clear text
38- func ClearText (html string ) (text string ) {
39- if len (html ) == 0 {
40- text = html
41- return
53+ func ClearText (html string ) string {
54+ if html == "" {
55+ return html
4256 }
4357
44- var (
45- re * regexp.Regexp
46- codeReg = `(?ism)<(pre)>.*<\/pre>`
47- codeRepl = "{code...}"
48- linkReg = `(?ism)<a.*?[^<]>(.*)?<\/a>`
49- linkRepl = " [$1] "
50- spaceReg = ` +`
51- spaceRepl = " "
52- )
53- re = regexp .MustCompile (codeReg )
54- html = re .ReplaceAllString (html , codeRepl )
58+ html = reCode .ReplaceAllString (html , reCodeReplace )
59+ html = reLink .ReplaceAllString (html , reLinkReplace )
5560
56- re = regexp .MustCompile (linkReg )
57- html = re .ReplaceAllString (html , linkRepl )
58-
59- text = strings .NewReplacer (
60- "\n " , " " ,
61- "\r " , " " ,
62- "\t " , " " ,
63- ).Replace (strip .StripTags (html ))
61+ text := spaceReplacer .Replace (strip .StripTags (html ))
6462
6563 // replace multiple spaces to one space
66- re = regexp .MustCompile (spaceReg )
67- text = strings .TrimSpace (re .ReplaceAllString (text , spaceRepl ))
68- return
64+ return strings .TrimSpace (reSpace .ReplaceAllString (text , reSpaceReplace ))
6965}
7066
7167func UrlTitle (title string ) (text string ) {
0 commit comments