-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
53 lines (44 loc) · 1.61 KB
/
string.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
41
42
43
44
45
46
47
48
49
50
51
52
53
package entity
import (
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"regexp"
"strings"
"unicode"
)
var rxList map[*regexp.Regexp][]byte
func init() {
// Compile regex once to use in stringToSlug().
// We store the compiled regex as the key
// and assign the replacement as the map's value.
rxList = map[*regexp.Regexp][]byte{
regexp.MustCompile("`[-]+`"): []byte("-"),
regexp.MustCompile("[[:space:]]"): []byte("-"),
regexp.MustCompile("[[:blank:]]"): []byte(""),
regexp.MustCompile("`[^a-z0-9]`i"): []byte("-"),
regexp.MustCompile("[!/:-@[-`{-~]"): []byte(""),
regexp.MustCompile("/[^\x20-\x7F]/"): []byte(""),
regexp.MustCompile("`&(amp;)?#?[a-z0-9]+;`i"): []byte("-"),
regexp.MustCompile("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i"): []byte("\\1"),
}
}
// modified version of: https://www.socketloop.com/tutorials/golang-format-strings-to-seo-friendly-url-example
func stringToSlug(s string) (string, error) {
src := []byte(strings.ToLower(s))
// Range over compiled regex and replacements from init().
for rx := range rxList {
src = rx.ReplaceAll(src, rxList[rx])
}
str := strings.Replace(string(src), "'", "", -1)
str = strings.Replace(str, `"`, "", -1)
str = strings.Replace(str, "&", "-", -1)
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
slug, _, err := transform.String(t, str)
if err != nil {
return "", err
}
return strings.TrimSpace(slug), nil
}
func isMn(r rune) bool {
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
}