Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
package str
|
|
|
|
import (
|
|
"html"
|
|
"regexp"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/deluan/sanitize"
|
|
"github.com/microcosm-cc/bluemonday"
|
|
"github.com/navidrome/navidrome/conf"
|
|
)
|
|
|
|
var ignoredCharsRegex = regexp.MustCompile("[“”‘’'\"\\[({\\])},]")
|
|
var slashRemover = strings.NewReplacer("\\", " ", "/", " ")
|
|
|
|
func SanitizeStrings(text ...string) string {
|
|
// Concatenate all strings, removing extra spaces
|
|
sanitizedText := strings.Builder{}
|
|
for _, txt := range text {
|
|
sanitizedText.WriteString(strings.TrimSpace(txt))
|
|
sanitizedText.WriteByte(' ')
|
|
}
|
|
|
|
// Remove special symbols, accents, extra spaces and slashes
|
|
sanitizedStrings := slashRemover.Replace(Clear(sanitizedText.String()))
|
|
sanitizedStrings = sanitize.Accents(strings.ToLower(sanitizedStrings))
|
|
sanitizedStrings = ignoredCharsRegex.ReplaceAllString(sanitizedStrings, "")
|
|
fullText := strings.Fields(sanitizedStrings)
|
|
|
|
// Remove duplicated words
|
|
slices.Sort(fullText)
|
|
fullText = slices.Compact(fullText)
|
|
|
|
// Returns the sanitized text as a single string
|
|
return strings.Join(fullText, " ")
|
|
}
|
|
|
|
var policy = bluemonday.UGCPolicy()
|
|
|
|
func SanitizeText(text string) string {
|
|
s := policy.Sanitize(text)
|
|
return html.UnescapeString(s)
|
|
}
|
|
|
|
func SanitizeFieldForSorting(originalValue string) string {
|
|
v := strings.TrimSpace(sanitize.Accents(originalValue))
|
|
return Clear(strings.ToLower(v))
|
|
}
|
|
|
|
func SanitizeFieldForSortingNoArticle(originalValue string) string {
|
|
v := strings.TrimSpace(sanitize.Accents(originalValue))
|
|
return Clear(strings.ToLower(strings.TrimSpace(RemoveArticle(v))))
|
|
}
|
|
|
|
func RemoveArticle(name string) string {
|
|
articles := strings.Split(conf.Server.IgnoredArticles, " ")
|
|
for _, a := range articles {
|
|
n := strings.TrimPrefix(name, a+" ")
|
|
if n != name {
|
|
return n
|
|
}
|
|
}
|
|
return name
|
|
}
|