-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
put defineSlugForDocument in ui folder
- Loading branch information
1 parent
5e04edc
commit d1138fa
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,41 @@ | ||
import { defineField } from "sanity"; | ||
import { slugify } from "../../utils/slugify"; | ||
import { isUniqueSlug } from "../../utils/is-unique-slug"; | ||
|
||
export const defineSlugForDocument = ({ prefix = '', slug }: { prefix?: string, slug?: string }) => [ | ||
defineField({ | ||
name: 'title', | ||
type: 'string', | ||
title: 'Title', | ||
description: 'The title of the document, used for display in the Breadcrumbs.', | ||
validation: Rule => Rule.required(), | ||
}), | ||
defineField({ | ||
name: 'slug', | ||
type: 'slug', | ||
title: 'Slug', | ||
description: ( | ||
<> | ||
Slug is a unique identifier for the document, used for SEO and links. | ||
{slug && <> <strong><em>That slug can't be changed.</em></strong></>} | ||
{prefix && <> The slug should start with a prefix: <strong>{prefix}</strong></>} | ||
</> | ||
), | ||
...!!slug && { | ||
initialValue: { current: slug }, | ||
readOnly: true, | ||
}, | ||
options: { | ||
source: 'title', | ||
slugify: (slug: string) => `${prefix || '/'}${slugify(slug)}`, | ||
isUnique: isUniqueSlug, | ||
}, | ||
validation: (Rule) => | ||
Rule.required().custom((value) => { | ||
if (prefix && value?.current && !value.current.startsWith(prefix)) { | ||
return `Slug should start with ${prefix}`; | ||
} | ||
return true; | ||
}) | ||
}), | ||
] |