From 129d66c10f2be00bdfb44792d22575f675f63f7d Mon Sep 17 00:00:00 2001 From: mantikoros Date: Mon, 10 Jan 2022 16:47:02 -0600 Subject: [PATCH] improve slugs --- common/util/slugify.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/common/util/slugify.ts b/common/util/slugify.ts index 506320b9..712a1d65 100644 --- a/common/util/slugify.ts +++ b/common/util/slugify.ts @@ -1,4 +1,8 @@ -export const slugify = (text: any, separator = "-"): string => { +export const slugify = ( + text: string, + separator = "-", + maxLength = 35 +): string => { return text .toString() .normalize("NFD") // split an accented letter in the base letter and the acent @@ -6,5 +10,7 @@ export const slugify = (text: any, separator = "-"): string => { .toLowerCase() .trim() .replace(/[^a-z0-9 ]/g, "") // remove all chars not letters, numbers and spaces (to be replaced) - .replace(/\s+/g, separator); + .replace(/\s+/g, separator) + .substring(0, maxLength) + .replace(new RegExp(separator + "+$", "g"), ""); // remove terminal separators };