forked from navidrome/navidrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsanitize_strings.go
40 lines (34 loc) · 885 Bytes
/
sanitize_strings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package utils
import (
"html"
"regexp"
"sort"
"strings"
"github.com/deluan/sanitize"
"github.com/microcosm-cc/bluemonday"
)
var quotesRegex = regexp.MustCompile("[“”‘’'\"\\[\\(\\{\\]\\)\\}]")
func SanitizeStrings(text ...string) string {
sanitizedText := strings.Builder{}
for _, txt := range text {
sanitizedText.WriteString(strings.TrimSpace(sanitize.Accents(strings.ToLower(txt))) + " ")
}
words := make(map[string]struct{})
for _, w := range strings.Fields(sanitizedText.String()) {
words[w] = struct{}{}
}
var fullText []string
for w := range words {
w = quotesRegex.ReplaceAllString(w, "")
if w != "" {
fullText = append(fullText, w)
}
}
sort.Strings(fullText)
return strings.Join(fullText, " ")
}
var policy = bluemonday.UGCPolicy()
func SanitizeText(text string) string {
s := policy.Sanitize(text)
return html.UnescapeString(s)
}