Files
fete/frontend/src/utils/slugify.ts
2026-03-13 21:39:41 +01:00

29 lines
690 B
TypeScript

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)
)
}