Files
aresv2/pkg/utis/slug.go
Beyhan Oğur 4362c3b83f first commit
2026-04-26 21:33:39 +03:00

49 lines
1.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package utils
import (
"regexp"
"strings"
"unicode"
)
// Slugify converts a string to a URL-friendly slug, replacing Turkish characters
// with ASCII equivalents and ensuring lowercase, hyphen-separated result.
func Slugify(s string) string {
if s == "" {
return ""
}
// Map Turkish characters to ASCII
replacer := strings.NewReplacer(
"ç", "c", "Ç", "c",
"ğ", "g", "Ğ", "g",
"ı", "i", "İ", "i",
"ö", "o", "Ö", "o",
"ş", "s", "Ş", "s",
"ü", "u", "Ü", "u",
"", "", "'", "",
)
s = replacer.Replace(s)
// Normalize: keep letters, numbers, and spaces
var b strings.Builder
for _, r := range strings.TrimSpace(s) {
if unicode.IsLetter(r) || unicode.IsDigit(r) || unicode.IsSpace(r) {
b.WriteRune(r)
} else {
// convert other punctuation to space
b.WriteRune(' ')
}
}
out := strings.ToLower(b.String())
// replace spaces with hyphens and collapse multiple hyphens
out = strings.TrimSpace(out)
// replace any sequence of non-alnum with hyphen
re := regexp.MustCompile(`[^a-z0-9]+`)
out = re.ReplaceAllString(out, "-")
out = strings.Trim(out, "-")
return out
}