From 31b521c11c8b67b9d357e891cc3c3a3f40ca59a5 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Thu, 11 Jul 2019 09:18:39 -0400 Subject: [PATCH] Prevent transliterated slugs exceeding limit Transliteration during slug generation can cause slugs to exceed their 80-character limit. This fixes that by making a second truncation pass on the slug during generation. Originally reported on the forum: https://discuss.write.as/t/title-convert-to-url-function-bug-under-chinese/723 --- posts.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/posts.go b/posts.go index d4be947..5418869 100644 --- a/posts.go +++ b/posts.go @@ -1118,10 +1118,20 @@ func getSlugFromPost(title, body, lang string) string { title = parse.PostLede(title, false) // Truncate lede if needed title, _ = parse.TruncToWord(title, 80) + var s string if lang != "" && len(lang) == 2 { - return slug.MakeLang(title, lang) + s = slug.MakeLang(title, lang) + } else { + s = slug.Make(title) } - return slug.Make(title) + + // Transliteration may cause the slug to expand past the limit, so truncate again + s, _ = parse.TruncToWord(s, 80) + return strings.TrimFunc(s, func(r rune) bool { + // TruncToWord doesn't respect words in a slug, since spaces are replaced + // with hyphens. So remove any trailing hyphens. + return r == '-' + }) } // isFontValid returns whether or not the submitted post's appearance is valid.