29 lines
734 B
Go
29 lines
734 B
Go
// ingestion/internal/wiki/slug.go
|
|
package wiki
|
|
|
|
import (
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
// Slug converts a title to a kebab-case slug suitable for wiki filenames.
|
|
// Rules: lowercase, spaces/hyphens/underscores → hyphens, strip everything else.
|
|
func Slug(title string) string {
|
|
var b strings.Builder
|
|
prevHyphen := true // start true to trim leading hyphens
|
|
for _, r := range strings.ToLower(title) {
|
|
switch {
|
|
case r == ' ' || r == '-' || r == '_':
|
|
if !prevHyphen {
|
|
b.WriteRune('-')
|
|
prevHyphen = true
|
|
}
|
|
case unicode.IsLetter(r) || unicode.IsDigit(r):
|
|
b.WriteRune(r)
|
|
prevHyphen = false
|
|
// all other characters (apostrophes, colons, dots, etc.) are dropped
|
|
}
|
|
}
|
|
return strings.TrimRight(b.String(), "-")
|
|
}
|