fix(api): generate ASCII-only slugs with timestamp fallback

Use is_ascii_alphanumeric() instead of is_alphanumeric() to keep
URLs clean. Non-ASCII characters (CJK, etc.) are replaced with
dashes. When the result is empty (pure non-ASCII title), fall back
to a Unix timestamp.
This commit is contained in:
xfy 2026-06-03 10:33:11 +08:00
parent f5413e00cc
commit 61ae3abbc2

View File

@ -94,7 +94,7 @@ fn slugify(title: &str) -> String {
.to_lowercase()
.chars()
.map(|c| {
if c.is_alphanumeric() || c == '-' || c == '_' {
if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
c
} else {
'-'
@ -102,11 +102,13 @@ fn slugify(title: &str) -> String {
})
.collect();
// Collapse consecutive dashes
let parts: Vec<&str> = slug.split('-').filter(|s| !s.is_empty()).collect();
let slug = parts.join("-");
// Truncate to 100 chars
if slug.is_empty() {
return format!("{}", chrono::Utc::now().timestamp());
}
slug.chars().take(100).collect()
}