From 61ae3abbc29a8f82eacfbd168179a7f5c8872e95 Mon Sep 17 00:00:00 2001 From: xfy Date: Wed, 3 Jun 2026 10:33:11 +0800 Subject: [PATCH] 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. --- src/api/posts.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/api/posts.rs b/src/api/posts.rs index 8da393f..5b0e890 100644 --- a/src/api/posts.rs +++ b/src/api/posts.rs @@ -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() }