This commit is contained in:
2026-10-24 11:22:21 +08:00
parent 43642cfb56
commit 53ade059d6
6 changed files with 41 additions and 14 deletions
+10 -2
View File
@@ -144,15 +144,23 @@ func GetSlugAbbr(text string) string {
return b.String()
}
// MakeAbbrSlug 优先按标题拼音首字母缩写生成 slug;缩写为空或全数字时回退 5 位随机字母,碰撞时追加 3 位随机字母。
// AbbrSlugMaxLen slug 最大长度(含碰撞时追加的随机后缀)。
const AbbrSlugMaxLen = 20
// MakeAbbrSlug 优先按标题拼音首字母缩写生成 slug;缩写为空或全数字时回退 5 位随机字母,碰撞时追加 3 位随机字母,整体长度不超过 AbbrSlugMaxLen。
func MakeAbbrSlug(source string, exists func(string) bool) string {
abbr := GetSlugAbbr(source)
if abbr == "" || IsSlugAllDigits(abbr) {
abbr = strings.ToLower(GetRandomLetters(5))
}
const suffixLen = 3
baseMax := AbbrSlugMaxLen - suffixLen
if len(abbr) > baseMax {
abbr = abbr[:baseMax]
}
final := abbr
for exists(final) {
final = abbr + strings.ToLower(GetRandomLetters(3))
final = abbr + strings.ToLower(GetRandomLetters(suffixLen))
}
return final
}