mirror of
https://github.com/documenso/documenso.git
synced 2025-11-24 05:32:12 +10:00
chore: team url slugify and profanity filter
Signed-off-by: Adithya Krishna <adi@documenso.com>
This commit is contained in:
25
packages/lib/utils/generate-url-slug.ts
Normal file
25
packages/lib/utils/generate-url-slug.ts
Normal file
@ -0,0 +1,25 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user