strings.go (702B)
1 package domain 2 3 import ( 4 "strings" 5 "unicode" 6 7 "golang.org/x/text/runes" 8 "golang.org/x/text/transform" 9 "golang.org/x/text/unicode/norm" 10 ) 11 12 func Slug(title string) string { 13 // strip apostrophes before normalizing 14 s := strings.ReplaceAll(title, "'", "") 15 16 // unicode normalize: NFD decompose, strip non-spacing marks, recompose 17 t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC) 18 s, _, _ = transform.String(t, s) 19 20 s = strings.ToLower(s) 21 s = strings.Map(func(r rune) rune { 22 if r >= 'a' && r <= 'z' || r >= '0' && r <= '9' { 23 return r 24 } 25 return '-' 26 }, s) 27 28 for strings.Contains(s, "--") { 29 s = strings.ReplaceAll(s, "--", "-") 30 } 31 32 return strings.Trim(s, "-") 33 }