Skip to content

Commit

Permalink
fix: define imageRegex as global var
Browse files Browse the repository at this point in the history
  • Loading branch information
PinappleUnderTheSea committed Mar 29, 2024
1 parent 4d11756 commit 6bafc31
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions utils/sensitive/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ package sensitive
import (
"golang.org/x/exp/slices"
"mvdan.cc/xurls/v2"
url2 "net/url"
imageUrl "net/url"
"regexp"
"treehole_next/config"
)

var imageRegex = regexp.MustCompile(
`!\[.*?\]\(([^" )]*)`,
)
var deleteImageRegex = regexp.MustCompile(
`!\[(.*?)\]\(([^" ]*)( ".*")?\)`,
)

// findImagesInMarkdown 从Markdown文本中查找所有图片链接
func findImagesInMarkdown(markdown string) []string {
imageRegex := regexp.MustCompile(
`!\[.*?\]\(([^" )]*)`,
)

matches := imageRegex.FindAllStringSubmatch(markdown, -1)
images := make([]string, 0, len(matches))
Expand Down Expand Up @@ -50,7 +54,7 @@ func hasTextUrl(content string) bool {
}

func checkValidUrl(input string) (bool, error) {
url, err := url2.Parse(input)
url, err := imageUrl.Parse(input)
if err != nil {
return false, err
}
Expand All @@ -61,11 +65,9 @@ func checkValidUrl(input string) (bool, error) {
}

func deleteImagesInMarkdown(markdown string) string {
imageRegex := regexp.MustCompile(
`!\[(.*?)\]\(([^" ]*)( ".*")?\)`,
)

return imageRegex.ReplaceAllStringFunc(markdown, func(s string) string {
submatches := imageRegex.FindStringSubmatch(s)
submatches := deleteImageRegex.FindStringSubmatch(s)
altText := submatches[1]
if len(submatches) > 3 && submatches[3] != "" {
// If there is a title, return it along with the alt text
Expand Down

0 comments on commit 6bafc31

Please sign in to comment.