2022-02-21 18:45:35 +00:00
|
|
|
export const slugify = (str: string) => {
|
2023-05-17 13:05:14 +00:00
|
|
|
return str
|
|
|
|
.toLowerCase() // Convert to lowercase
|
|
|
|
.trim() // Remove whitespace from both sides
|
|
|
|
.normalize("NFD") // Normalize to decomposed form for handling accents
|
|
|
|
.replace(/\p{Diacritic}/gu, "") // Remove any diacritics (accents) from characters
|
|
|
|
.replace(/[^\p{L}\p{N}\p{Zs}\p{Emoji}]+/gu, "-") // Replace any non-alphanumeric characters (including Unicode) with a dash
|
|
|
|
.replace(/[\s_]+/g, "-") // Replace whitespace and underscores with a single dash
|
|
|
|
.replace(/^-+/, "") // Remove dashes from start
|
2023-08-18 21:03:55 +00:00
|
|
|
.replace(/-+$/, "") // Remove dashes from end
|
|
|
|
.replace(/\+/g, "-"); // Transform all + to -
|
2022-02-21 18:45:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default slugify;
|