Add slugify utility for filename sanitization
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
28
frontend/src/utils/slugify.ts
Normal file
28
frontend/src/utils/slugify.ts
Normal 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)
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user