import { DocumentTextIcon } from "@sanity/icons"; import { format, parseISO } from "date-fns"; import { defineField, defineType } from "sanity"; import authorType from "./author"; /** * This file is the schema definition for a post. * * Here you'll be able to edit the different fields that appear when you * create or edit a post in the studio. * * Here you can see the different schema types that are available: https://www.sanity.io/docs/schema-types */ export default defineType({ name: "post", title: "Post", icon: DocumentTextIcon, type: "document", fields: [ defineField({ name: "title", title: "Title", type: "string", validation: (rule) => rule.required(), }), defineField({ name: "slug", title: "Slug", type: "slug", description: "A slug is required for the post to show up in the preview", options: { source: "title", maxLength: 96, isUnique: (value, context) => context.defaultIsUnique(value, context), }, validation: (rule) => rule.required(), }), defineField({ name: "content", title: "Content", type: "array", of: [{ type: "block" }], }), defineField({ name: "excerpt", title: "Excerpt", type: "text", }), defineField({ name: "coverImage", title: "Cover Image", type: "image", options: { hotspot: true, aiAssist: { imageDescriptionField: "alt", }, }, fields: [ { name: "alt", type: "string", title: "Alternative text", description: "Important for SEO and accessiblity.", validation: (rule) => { return rule.custom((alt, context) => { if ((context.document?.coverImage as any)?.asset?._ref && !alt) { return "Required"; } return true; }); }, }, ], validation: (rule) => rule.required(), }), defineField({ name: "date", title: "Date", type: "datetime", initialValue: () => new Date().toISOString(), }), defineField({ name: "author", title: "Author", type: "reference", to: [{ type: authorType.name }], }), ], preview: { select: { title: "title", author: "author.name", date: "date", media: "coverImage", }, prepare({ title, media, author, date }) { const subtitles = [ author && `by ${author}`, date && `on ${format(parseISO(date), "LLL d, yyyy")}`, ].filter(Boolean); return { title, media, subtitle: subtitles.join(" ") }; }, }, });