Add slugify utility for filename sanitization

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-13 21:39:41 +01:00
parent 3d7efb14f7
commit d4a1f0dc23
2 changed files with 97 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
const UMLAUT_MAP: Record<string, string> = {
ä: 'ae',
ö: 'oe',
ü: 'ue',
ß: 'ss',
Ä: 'Ae',
Ö: 'Oe',
Ü: 'Ue',
}
export function slugify(input: string): string {
return (
input
// Transliterate German umlauts
.replace(/[äöüßÄÖÜ]/g, (ch) => UMLAUT_MAP[ch] ?? ch)
.toLowerCase()
// Remove non-ASCII characters
.replace(/[^\x20-\x7E]/g, '')
// Replace non-alphanumeric characters with hyphens
.replace(/[^a-z0-9]+/g, '-')
// Collapse consecutive hyphens
.replace(/-{2,}/g, '-')
// Trim leading/trailing hyphens
.replace(/^-|-$/g, '')
// Truncate to 60 characters
.slice(0, 60)
)
}