Skip to content

Commit

Permalink
put defineSlugForDocument in ui folder
Browse files Browse the repository at this point in the history
  • Loading branch information
milewskibogumil committed Oct 16, 2024
1 parent 5e04edc commit d1138fa
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions apps/sanity/schema/ui/define-slug-for-document.tsx
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&apos;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;
})
}),
]

0 comments on commit d1138fa

Please sign in to comment.