Skip to content

Commit

Permalink
feature: add filter
Browse files Browse the repository at this point in the history
Signed-off-by: Cadmus <[email protected]>
  • Loading branch information
CadmusJiang committed Mar 25, 2021
1 parent 9506624 commit 99de592
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
42 changes: 23 additions & 19 deletions pkg/providers/check-issue/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"regexp"
"unicode"

"github.com/google/go-github/v32/github"
Expand Down Expand Up @@ -78,7 +78,11 @@ func (c *Check) IsIncludeChinese(str string) bool {
// filter <img>
str = filterImg(str)
// filter []
str = filterBracket(str)
str = filterSquareBracket(str)
// filter ("")
str = filterBracketAndColon(str)
// filter ``` ```
str = filterBackQuote(str)
var count int
for _, v := range str {
if unicode.Is(unicode.Han, v) {
Expand All @@ -90,26 +94,26 @@ func (c *Check) IsIncludeChinese(str string) bool {
}

func filterImg(str string) string {
for {
startIndex := strings.Index(str, "<img")
if startIndex == -1 {
break
}
endIndex := startIndex + strings.Index(str[startIndex:], ">")
str = str[0:startIndex] + str[endIndex+1:]
}
re := regexp.MustCompile(`<img.*?>`)
str = re.ReplaceAllString(str, "")
return str
}

func filterBracket(str string) string {
for {
startIndex := strings.Index(str, "[")
if startIndex == -1 {
break
}
endIndex := startIndex + strings.Index(str[startIndex:], "]")
str = str[0:startIndex] + str[endIndex+1:]
}
func filterSquareBracket(str string) string {
re := regexp.MustCompile(`\[.*?\]`)
str = re.ReplaceAllString(str, "")
return str
}

func filterBracketAndColon(str string) string {
re := regexp.MustCompile(`\(\".*?\"\)`)
str = re.ReplaceAllString(str, "")
return str
}

func filterBackQuote(str string) string {
re := regexp.MustCompile("\\`\\`\\`.*?\\`\\`\\`")
str = re.ReplaceAllString(str, "")
return str
}

Expand Down
24 changes: 22 additions & 2 deletions pkg/providers/check-issue/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,31 @@ import (
func TestFilterImg(t *testing.T) {
//str1 := "sfdf"
str := "352452aaaa 中文 <img >sss<><><><> <img width=\"956\" alt=\"屏幕快照 2021-02-03 下午8 13 16\" src=\"https://user-images.githubusercontent.com/5906259/106745942-c7cc2000-665c-11eb-9689-6bc8d77ce982.png\"> <><><>"
fmt.Println(str)
fmt.Println(filterImg(str))
}

func TestFilterBractet(t *testing.T) {
func TestFilterSquareBracket(t *testing.T) {
//str := "[中文]2525[]"
str := "## Error Report\\r\\n\\r\\n**This repository is ONLY used to solve issues related to DOCS.\\r\\nFor other issues (related to TiDB, PD, etc), please move to [other repositories](https://github.com/pingcap/).**\\r\\n\\r\\nPlease answer the following questions before submitting your issue. Thanks!\\r\\n\\r\\n1. What is the URL/path of the document related to this issue?\\r\\n\\r\\nhttps://docs.pingcap.com/zh/tidb/dev/privilege-management\\r\\n\\r\\n2. How would you like to improve it?\\r\\n\\r\\ndelete grand in the table\\r\\n\\r\\n![企业微信截图_e7ea7242-875c-443f-9661-39bec203c1ee](https://user-images.githubusercontent.com/53471087/106870333-f228e680-670b-11eb-9048-14c1d5a729e7.png)\\r\\n\\r\\n\\r\\n\\r\\n\\r\\n"
fmt.Println(filterBracket(str))
fmt.Println(str)
fmt.Println(filterSquareBracket(str))
}

func TestFilterBracketAndColon(t *testing.T) {
str := "mysql> CREATE TABLE `t1` (\n `COL1` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,PRIMARY KEY (`COL1`(5)) USING BTREE\n) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;\nmysql> insert into t1 values(\"ý忑辦孈策炠槝衧魮與\");\nmysql> insert into t1 values(\"ǎ傦眢否畬傮Ȕ炏芭裪\");"
fmt.Println(str)
fmt.Println(filterBracketAndColon(str))
}

func TestFilterBackQuote(t *testing.T) {
str := "```ǎ傦眢否畬傮Ȕ炏芭裪```"
fmt.Println(str)
fmt.Println(filterBackQuote(str))
}

func TestAll(t *testing.T) {
str := "<img> <> [] [<>] () (\"\") ``` ``` "
fmt.Println(str)
fmt.Println(filterImg(filterSquareBracket(filterBracketAndColon(filterBackQuote(str)))))
}

0 comments on commit 99de592

Please sign in to comment.