Skip to content

Commit cff97e6

Browse files
committed
fix: slug and unslugify utils
1 parent 75e76ee commit cff97e6

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

src/runtime/utils/text.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -148,22 +148,18 @@ export const truncate = (text: string, maxLength = 80, ellipses = true) => {
148148
}
149149

150150
export const slugify = (text: string) => {
151-
const slug = text
151+
return text
152152
.normalize('NFKD') // The normalize() using NFKD method returns the Unicode Normalization Form of a given string.
153153
.toLowerCase() // Convert the string to lowercase letters
154-
.trim() // Remove whitespace from both sides of a string (optional)
155-
.replace(/[^a-z0-9\s_]/g, '') // Remove characters that are NOT alphanumeric (a-z, 0-9), whitespace, or underscores
156-
.replace(/\s+/g, '-') // Replace spaces with -
157-
.replace(/_/g, '-') // Replace _ with -
158-
.replace(/-{2,}/g, '-') // Replace multiple - with single -
159-
.replace(/-$/g, '') // Remove trailing -
160-
161-
return slug
154+
.trim() // Remove whitespace from both sides of a string
155+
.replace(/[^a-z0-9\s_-]/g, '') // Remove characters that are NOT alphanumeric (a-z, 0-9), whitespace, hyphens, or underscores
156+
.replace(/[\s_]+/g, '-') // Replace sequences of one or more whitespace characters and/or underscores with a single hyphen
157+
.replace(/-{2,}/g, '-') // Replace multiple consecutive hyphens with a single hyphen
158+
.replace(/^-+|-+$/g, '') // Remove any leading or trailing hyphens
162159
}
163160

164161
export const unslugify = (text: string) => {
165-
const unslug = text.replace(/-/g, ' ') // Replace - with spaces
166-
return unslug
162+
return text.replace(/-/g, ' ') // Replace hyphens with spaces
167163
}
168164

169165
// Not inspired by this, but useful reference for corner cases:

0 commit comments

Comments
 (0)