Sluggify function added to utils
This commit is contained in:
parent
6a6217ae31
commit
ccb115a75b
@ -5,7 +5,10 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"text/template"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
func CreateDirIfNotExists(dir string) error {
|
||||
@ -58,3 +61,29 @@ func Sep() {
|
||||
separator := "════════════════════════════════════════════════"
|
||||
fmt.Println(separator)
|
||||
}
|
||||
|
||||
func Sluggify(s string) string {
|
||||
slug := strings.ToLower(s)
|
||||
|
||||
var b strings.Builder
|
||||
for _, r := range slug {
|
||||
if unicode.IsLetter(r) || unicode.IsDigit(r) {
|
||||
b.WriteRune(r)
|
||||
} else {
|
||||
switch r {
|
||||
case ' ', '-', '_':
|
||||
b.WriteRune('-')
|
||||
}
|
||||
}
|
||||
}
|
||||
slug = b.String()
|
||||
|
||||
reg := regexp.MustCompile("[^a-z0-9-]+")
|
||||
slug = reg.ReplaceAllString(slug, "")
|
||||
|
||||
slug = strings.ReplaceAll(slug, "--", "-")
|
||||
|
||||
slug = strings.Trim(slug, "-")
|
||||
|
||||
return slug
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user