package models import "strings" // Slugify turns text into a slug by the rule the admin form's slug preset // uses: lower-case ASCII letters and digits are kept, every run of other // characters becomes one hyphen, and there is no hyphen at either end. // Letters outside ASCII are not transliterated; they count as other // characters. A text without a letter or digit gives the empty string. func Slugify(text string) string { var b strings.Builder b.Grow(len(text)) gap := false for _, r := range strings.ToLower(text) { if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') { if gap && b.Len() > 0 { b.WriteByte('-') } gap = false b.WriteRune(r) continue } gap = true } return b.String() }