-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathslugify.go
50 lines (44 loc) · 1.05 KB
/
slugify.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
package slugify
import (
"fmt"
"unicode"
"golang.org/x/text/unicode/norm"
)
var SKIP = []*unicode.RangeTable{
unicode.Mark,
unicode.Sk,
unicode.Lm,
}
var SAFE = []*unicode.RangeTable{
unicode.Letter,
unicode.Number,
}
// Slugify a string. The result will only contain lowercase letters,
// digits and dashes. It will not begin or end with a dash, and it
// will not contain runs of multiple dashes.
//
// It is NOT forced into being ASCII, but may contain any Unicode
// characters, with the above restrictions.
func Slugify(text string) string {
buf := make([]rune, 0, len(text))
dash := false
for _, r := range norm.NFKD.String(text) {
switch {
case unicode.IsOneOf(SAFE, r):
buf = append(buf, unicode.ToLower(r))
dash = true
case unicode.IsOneOf(SKIP, r):
case dash:
buf = append(buf, '-')
dash = false
}
}
if i := len(buf) - 1; i >= 0 && buf[i] == '-' {
buf = buf[:i]
}
return string(buf)
}
// Slugifyf slugfy a formated string
func Slugifyf(format string, a ...interface{}) string {
return Slugify(fmt.Sprintf(format, a...))
}