Files
documenso/packages/lib/utils/generate-url-slug.ts
Adithya Krishna 39869c46a3 chore: team url slugify and profanity filter
Signed-off-by: Adithya Krishna <adi@documenso.com>
2023-12-27 18:57:38 +05:30

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;