Fix tag URL encoding for non-ASCII characters

Updated getTagSlug() to properly encode tags with spaces and non-ASCII characters (like Chinese). The function now:
- Normalizes multiple spaces/dashes to single dashes
- Properly encodes non-ASCII characters using encodeURIComponent
- Prevents issues with URL encoding on Vercel deployment

This fixes tags like "Medicine - 醫學" being displayed as "medicine---%E9%86%AB%E5%AD%B8" by generating clean URLs like "medicine-%E9%86%AB%E5%AD%B8".

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-19 23:10:34 +08:00
parent 653f079e1a
commit 5d3d754252

View File

@@ -27,7 +27,14 @@ export function getPageBySlug(slug: string): Page | undefined {
}
export function getTagSlug(tag: string): string {
return tag.toLowerCase().replace(/\s+/g, '-');
// Normalize spaces and convert to lowercase first
// Replace multiple spaces/dashes with single dash
const normalized = tag
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/-+/g, '-');
// Encode URI components to handle non-ASCII characters properly
return encodeURIComponent(normalized);
}
export function getAllTagsWithCount(): { tag: string; slug: string; count: number }[] {