16 lines
504 B
TypeScript
16 lines
504 B
TypeScript
|
|
/**
|
||
|
|
* Convert string to URL-safe slug (a-z0-9_).
|
||
|
|
* Used for file names.
|
||
|
|
*/
|
||
|
|
export function slugify(text: string): string {
|
||
|
|
if (!text) return "";
|
||
|
|
|
||
|
|
return text
|
||
|
|
.toLowerCase()
|
||
|
|
.normalize("NFD") // Decompose accented characters
|
||
|
|
.replace(/[\u0300-\u036f]/g, "") // Remove diacritics
|
||
|
|
.replace(/[^a-z0-9_]+/g, "_") // Replace non-alphanumeric with underscore
|
||
|
|
.replace(/^_+|_+$/g, "") // Remove leading/trailing underscores
|
||
|
|
.replace(/_+/g, "_"); // Collapse multiple underscores
|
||
|
|
}
|