mirror of
https://github.com/documenso/documenso.git
synced 2025-11-24 05:32:12 +10:00
26 lines
802 B
TypeScript
26 lines
802 B
TypeScript
const diacriticRegex = /\p{Diacritic}/gu;
|
|
const nonAlphanumericRegex = /[^.\p{L}\p{N}\p{Zs}\p{Emoji}]+/gu;
|
|
const whitespaceUnderscoreRegex = /[\s_#]+/g;
|
|
const dashStartRegex = /^-+/;
|
|
const multipleDotsRegex = /\.{2,}/g;
|
|
|
|
export const generateURLSlug = (str: string, forDisplayingInput?: boolean) => {
|
|
if (!str) {
|
|
return '';
|
|
}
|
|
|
|
const slug = str
|
|
.toLowerCase()
|
|
.trim()
|
|
.normalize('NFD')
|
|
.replace(diacriticRegex, '')
|
|
.replace(nonAlphanumericRegex, '-')
|
|
.replace(whitespaceUnderscoreRegex, '-')
|
|
.replace(dashStartRegex, '') // Remove dashes from start
|
|
.replace(multipleDotsRegex, '.'); // Replace consecutive periods with a single period
|
|
|
|
return forDisplayingInput ? slug : slug.replace(/-*$/g, ''); // Remove dashes from end
|
|
};
|
|
|
|
export default generateURLSlug;
|