69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package productTranslationService
|
|
|
|
import (
|
|
"strings"
|
|
"unicode"
|
|
|
|
"git.ma-al.com/goc_daniel/b2b/app/db"
|
|
constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data"
|
|
"github.com/dlclark/regexp2"
|
|
"golang.org/x/text/runes"
|
|
"golang.org/x/text/transform"
|
|
"golang.org/x/text/unicode/norm"
|
|
)
|
|
|
|
func IsValidSlug(s string) bool {
|
|
var slug_regex2 = regexp2.MustCompile(constdata.SLUG_REGEX, regexp2.None)
|
|
|
|
ok, _ := slug_regex2.MatchString(s)
|
|
return ok
|
|
}
|
|
|
|
func SanitizeSlug(s string) string {
|
|
s = strings.TrimSpace(strings.ToLower(s))
|
|
|
|
// First apply explicit transliteration for language-specific letters.
|
|
s = transliterateSlug(s)
|
|
|
|
// Then normalize and strip any remaining combining marks.
|
|
s = removeDiacritics(s)
|
|
|
|
// Replace all non-alphanumeric runs with "-"
|
|
var non_alphanum_regex2 = regexp2.MustCompile(constdata.NON_ALNUM_REGEX, regexp2.None)
|
|
s, _ = non_alphanum_regex2.Replace(s, "-", -1, -1)
|
|
|
|
// Collapse repeated "-" and trim edges
|
|
var multi_dash_regex2 = regexp2.MustCompile(constdata.MULTI_DASH_REGEX, regexp2.None)
|
|
s, _ = multi_dash_regex2.Replace(s, "-", -1, -1)
|
|
|
|
s = strings.Trim(s, "-")
|
|
|
|
return s
|
|
}
|
|
|
|
func transliterateSlug(s string) string {
|
|
var cleared string
|
|
|
|
err := db.DB.Raw("SELECT slugify_eu(?)", s).Scan(&cleared).Error
|
|
if err != nil {
|
|
// log error
|
|
_ = err
|
|
return s
|
|
}
|
|
|
|
return cleared
|
|
}
|
|
|
|
func removeDiacritics(s string) string {
|
|
t := transform.Chain(
|
|
norm.NFD,
|
|
runes.Remove(runes.In(unicode.Mn)),
|
|
norm.NFC,
|
|
)
|
|
out, _, err := transform.String(t, s)
|
|
if err != nil {
|
|
return s
|
|
}
|
|
return out
|
|
}
|