navidrome/utils/sanitize_strings.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

41 lines
885 B
Go
Raw Permalink Normal View History

package utils
import (
2021-10-27 01:33:21 +02:00
"html"
"regexp"
"sort"
"strings"
2023-01-12 19:39:05 +01:00
"github.com/deluan/sanitize"
2021-10-27 01:33:21 +02:00
"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, " ")
}
2021-10-27 01:33:21 +02:00
var policy = bluemonday.UGCPolicy()
func SanitizeText(text string) string {
s := policy.Sanitize(text)
return html.UnescapeString(s)
}