-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
slugify.go
51 lines (40 loc) · 1.15 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
51
package slugify
import (
"fmt"
"regexp"
"strings"
"github.com/mozillazg/go-unidecode"
)
// Separator separator between words
var Separator = "-"
// SeparatorForRe for regexp
var SeparatorForRe = regexp.QuoteMeta(Separator)
// ReInValidChar match invalid slug string
var ReInValidChar = regexp.MustCompile(fmt.Sprintf("[^%sa-zA-Z0-9]", SeparatorForRe))
// ReDupSeparatorChar match duplicate separator string
var ReDupSeparatorChar = regexp.MustCompile(fmt.Sprintf("%s{2,}", SeparatorForRe))
// Version return version
func Version() string {
return "0.2.0"
}
// Slugify implements make a pretty slug from the given text.
// e.g. Slugify("kožušček hello world") => "kozuscek-hello-world"
func Slugify(s string) string {
return slugify(s)
}
func slugify(s string) string {
s = unidecode.Unidecode(s)
s = replaceInValidCharacter(s, Separator)
s = removeDupSeparator(s)
s = strings.Trim(s, Separator)
s = strings.ToLower(s)
return s
}
func replaceInValidCharacter(s, repl string) string {
s = ReInValidChar.ReplaceAllString(s, repl)
return s
}
func removeDupSeparator(s string) string {
s = ReDupSeparatorChar.ReplaceAllString(s, Separator)
return s
}