Skip to content

Commit fdaa051

Browse files
feat: improve default slugify utility by triming whitespace first (#14421)
Currently, when you are creating slugs and leave spaces on beggining or the end, they will be replaced with hyphen, this enhancement avoids trims unwanted spaces first. ### What? Utilities for creating slugs from titles ### Why? Badly created slug with hyphen in the end, when user added title with space. ### How? Adding simple `trim()` function before replacing spaces with hyphens Co-authored-by: Paul Popus <paul@payloadcms.com>
1 parent c3ec73f commit fdaa051

7 files changed

Lines changed: 8 additions & 1 deletion

File tree

examples/draft-preview/src/collections/Pages/hooks/formatSlug.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { FieldHook } from 'payload'
22

33
const format = (val: string): string =>
44
val
5+
.trim()
56
.replace(/ /g, '-')
67
.replace(/[^\w-]+/g, '')
78
.toLowerCase()

examples/form-builder/src/utilities/formatSlug.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { FieldHook } from 'payload'
22

33
const format = (val: string): string =>
44
val
5+
.trim()
56
.replace(/ /g, '-')
67
.replace(/[^\w-]+/g, '')
78
.toLowerCase()

examples/live-preview/src/collections/Pages/hooks/formatSlug.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { FieldHook } from 'payload'
22

33
const format = (val: string): string =>
44
val
5+
.trim()
56
.replace(/ /g, '-')
67
.replace(/[^\w-]+/g, '')
78
.toLowerCase()

examples/localization/src/fields/slug/formatSlug.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { FieldHook } from 'payload'
22

33
export const formatSlug = (val: string): string =>
44
val
5+
.trim()
56
.replace(/ /g, '-')
67
.replace(/[^\w-]+/g, '')
78
.toLowerCase()

examples/localization/src/hooks/formatSlug.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { FieldHook } from 'payload'
22

33
const format = (val: string): string =>
44
val
5+
.trim()
56
.replace(/ /g, '-')
67
.replace(/[^\w-]+/g, '')
78
.toLowerCase()
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export const slugify = (val?: string): string | undefined =>
22
val
3-
?.replace(/ /g, '-')
3+
?.trim()
4+
.replace(/ /g, '-')
45
.replace(/[^\w-]+/g, '')
56
.toLowerCase()

test/live-preview/utilities/formatSlug.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { FieldHook } from 'payload'
22

33
const format = (val: string): string =>
44
val
5+
.trim()
56
.replace(/ /g, '-')
67
.replace(/[^\w-]+/g, '')
78
.toLowerCase()

0 commit comments

Comments
 (0)