-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat: support custom slugify functions #14117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
6556001
feat: support custom slugify functions
jacobsfletch 5c7e9a2
Merge branch 'main' into feat/slugify-override
jacobsfletch 790c452
support client-side regen using custom slugify
jacobsfletch 406afea
Merge branch 'main' into feat/slugify-override
jacobsfletch e8e1418
add test and scope gen field name
jacobsfletch 02a89af
convert to server fn
jacobsfletch edf2c4a
lookup config after authorization and cleanup jsdocs
jacobsfletch acf984c
revert gen checkbox name change and cleanup
jacobsfletch 4b2f677
fix build
jacobsfletch f5dbc6e
fix tests
jacobsfletch 80686c8
Merge branch 'main' into feat/slugify-override
jacobsfletch f7d8ab1
poc fix sort test
jacobsfletch d3ae162
deflake sort test
jacobsfletch db486e2
Merge branch 'main' into feat/slugify-override
jacobsfletch b94c269
Merge branch 'test/deflake-sort' into feat/slugify-override
jacobsfletch 6a13349
move custom slugify to field.custom instead of unused serverProps
jacobsfletch 657ff8d
Merge branch 'main' into feat/slugify-override
jacobsfletch 2a901e7
add translations
jacobsfletch eb19514
modify args
jacobsfletch 15518e2
temp
jacobsfletch a3db160
feat: expand getDataByPath util
jacobsfletch adc5014
splits logic
jacobsfletch 3e92036
Merge branch 'feat/get-data-by-path' into feat/slugify-override
jacobsfletch ce4064a
use getDataByPath
jacobsfletch 197a8a2
Merge branch 'main' into feat/slugify-override
jacobsfletch 5c7cbb8
cleanup
jacobsfletch 4f4ab23
support only top-level fields
jacobsfletch dad046f
revert field lookup
jacobsfletch a30e902
rename to valueToSlugify
jacobsfletch 26138a9
improve docs
jacobsfletch 7277672
fix build
jacobsfletch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { | ||
| flattenAllFields, | ||
| getFieldByPath, | ||
| type ServerFunction, | ||
| type SlugifyServerFunctionArgs, | ||
| UnauthorizedError, | ||
| } from 'payload' | ||
| import { slugify as defaultSlugify, type Slugify } from 'payload/shared' | ||
|
|
||
| /** | ||
| * This server function is directly related to the {@link https://payloadcms.com/docs/fields/text#slug-field | Slug Field}. | ||
| * This is a server function that is used to invoke the user's custom slugify function from the client. | ||
| * This pattern is required, as there is no other way for us to pass their function across the client-server boundary. | ||
| * - Not through props | ||
| * - Not from a server function defined within a server component (see below) | ||
| * When a server function contains non-serializable data within its closure, it gets passed through the boundary (and breaks). | ||
| * The only way to pass server functions to the client (that contain non-serializable data) is if it is globally defined. | ||
| * But we also cannot define this function alongside the server component, as we will not have access to their custom slugify function. | ||
| * See `ServerFunctionsProvider` for more details. | ||
| */ | ||
| export const slugifyHandler: ServerFunction< | ||
| SlugifyServerFunctionArgs, | ||
| Promise<ReturnType<Slugify>> | ||
| > = async (args) => { | ||
| const { collectionSlug, globalSlug, path, req, val } = args | ||
|
|
||
| if (!req.user) { | ||
| throw new UnauthorizedError() | ||
| } | ||
|
|
||
| const docConfig = collectionSlug | ||
| ? req.payload.collections[collectionSlug]?.config | ||
| : globalSlug | ||
| ? req.payload.config.globals.find((g) => g.slug === globalSlug) | ||
| : null | ||
|
|
||
| const { field } = getFieldByPath({ | ||
| config: req.payload.config, | ||
| fields: flattenAllFields({ fields: docConfig?.fields || [] }), | ||
| path: path || '', | ||
| }) | ||
|
|
||
| const customSlugify = field.custom.slugify as Slugify | undefined | ||
|
|
||
| const slugify = customSlugify || defaultSlugify | ||
|
|
||
| const result = await slugify(val) | ||
|
|
||
| return result | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| export const slugify = (val?: string) => | ||
| export const slugify: Slugify = (val) => | ||
| val | ||
| ?.replace(/ /g, '-') | ||
| .replace(/[^\w-]+/g, '') | ||
| .toLowerCase() | ||
|
|
||
| export type Slugify = (val?: string) => Promise<string | undefined> | string | undefined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.