diff --git a/apps/sanity/schema/ui/define-slug-for-document.tsx b/apps/sanity/schema/ui/define-slug-for-document.tsx new file mode 100644 index 0000000..4d4d817 --- /dev/null +++ b/apps/sanity/schema/ui/define-slug-for-document.tsx @@ -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 && <> That slug can't be changed.} + {prefix && <> The slug should start with a prefix: {prefix}} + + ), + ...!!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; + }) + }), +]