Post schema for Sanity and Astro
An article with its own page: a journal post, a guide, a news item, an insight. The core is the title, a summary, the date and a cover; the text is the body. An author, topics and an update date are extensions.
- Core fields
- 4
- Extensions
- 3 (5 fields)
- schema.org
- BlogPosting
- Licence
- MIT
Free, with no account. Same data in both editions: the Astro entry id is the Sanity slug.
Core fields
What every post has. A slug is added in Sanity, and a Portable Text body (Markdown in Astro).
| Field | Type | Required | What it holds |
|---|---|---|---|
| title | string | Yes | Title |
| summary | text | No | SummaryFor the card and the search result. |
| date | date | Yes | Date |
| cover | image | No | Cover3:2, at least 1600 wide. |
Extensions
Fields for one kind of site, on top of the core. An extension never changes a core field, and there are no settings: a different need is a different extension.
byline: Author
Who wrote it: a reference to a person, so the author page lists their posts. Fits news, blogs, professional services.
| Field | Type | Required | What it holds |
|---|---|---|---|
| author | reference | No | Author |
topics: Topics
One topic for the label and the archive filter, and a few tags. Fits blogs, news, shops.
| Field | Type | Required | What it holds |
|---|---|---|---|
| topic | string | No | Topic |
| tags | list of string | No | Tags |
updated: Updated
When it was last checked, and how long it takes to read. Fits guides, docs, news.
| Field | Type | Required | What it holds |
|---|---|---|---|
| updated | date | No | Updated |
| readingTime | number | No | Minutes to read |
References point at person in the code here. Change it to the type in your own schema.
Get the code
Plain TypeScript with no package of ours: the files become your code. Tested in a new Sanity Studio and a new Astro project.
For your Studio: the document type, the same fields as an inline object, a GROQ projection and a list query, and the TypeScript type the projection returns.
Install with the shadcn CLI
npx shadcn@latest add https://www.themes.dev/sanity/r/post.jsonThe CLI needs a components.json in the project. No CLI? Copy the files below: they are all there is.
// Sanity schema: post v1.// From themes.dev content models (https://www.themes.dev/sanity/models/), MIT. Your code now: edit it freely.import { defineArrayMember, defineField, defineType } from 'sanity'; const fields = [ defineField({ name: 'title', title: 'Title', type: 'string', validation: (rule) => rule.required().max(90) }), defineField({ name: 'summary', title: 'Summary', description: 'For the card and the search result.', type: 'text', rows: 2, validation: (rule) => rule.max(220) }), defineField({ name: 'date', title: 'Date', type: 'date', validation: (rule) => rule.required() }), defineField({ name: 'cover', title: 'Cover', description: '3:2, at least 1600 wide.', type: 'image', options: { hotspot: true }, fields: [defineField({ name: 'alt', title: 'Alternative text', type: 'string', description: 'What it shows, for people who can’t see it. Empty when the text beside it says it.', validation: (rule) => rule.max(180) })] }),]; /** The page's text: paragraphs, h2 and h3, quotes, lists, strong, emphasis, links and images with alt text. */const body = defineField({ name: 'body', title: 'Text', type: 'array', of: [defineArrayMember({ type: 'block', styles: [{ title: 'Paragraph', value: 'normal' }, { title: 'Heading', value: 'h2' }, { title: 'Subheading', value: 'h3' }, { title: 'Quote', value: 'blockquote' }], marks: { decorators: [{ title: 'Strong', value: 'strong' }, { title: 'Emphasis', value: 'em' }], annotations: [defineArrayMember({ name: 'link', title: 'Link', type: 'object', fields: [defineField({ name: 'href', title: 'Address', type: 'url', validation: (rule) => rule.required().uri({ allowRelative: true, scheme: ['http', 'https', 'mailto', 'tel'] }) })] })] } }), defineArrayMember({ type: 'image', options: { hotspot: true }, fields: [defineField({ name: 'alt', title: 'Alternative text', type: 'string', validation: (rule) => rule.required().max(180) })] })] }); /** The id other entries refer to, and the Astro edition's entry id: the same string in both. */const slug = defineField({ name: 'slug', title: 'Slug', type: 'slug', description: 'Lowercase letters, digits and hyphens.', options: { source: 'title', maxLength: 64 }, validation: (rule) => rule.required() }); /** An article with its own page: a journal post, a guide, a news item, an insight. The core is the title, a summary, the date and a cover; the text is the body. An author, topics and an update date are extensions. */export const post = defineType({ name: 'post', title: 'Post', type: 'document', fields: [fields[0], slug, ...fields.slice(1), body], orderings: [{ title: 'Newest first', name: 'date', by: [{ field: 'date', direction: 'desc' }] }], preview: { select: { title: 'title', subtitle: 'date', media: 'cover' } },}); /** The same fields as an object, for a page that keeps its own posts inline. */export const postEntry = defineType({ name: 'postEntry', title: 'Post', type: 'object', fields, preview: { select: { title: 'title', subtitle: 'date', media: 'cover' } },}); /** GROQ: one post as the PostRecord type (types/post.ts) describes it. */export const postProjection = `{ "id": slug.current, title, summary, date, "cover": cover{ "src": asset->url, "alt": coalesce(alt, ""), "width": asset->metadata.dimensions.width, "height": asset->metadata.dimensions.height } }`; /** GROQ: every post, in order. */export const postListQuery = `*[_type == "post" && defined(slug.current)] | order(date desc, _createdAt asc) ${postProjection}`; /** GROQ: one post by its slug ($slug), with its body. */export const postQuery = `*[_type == "post" && slug.current == $slug][0]{ "id": slug.current, title, summary, date, "cover": cover{ "src": asset->url, "alt": coalesce(alt, ""), "width": asset->metadata.dimensions.width, "height": asset->metadata.dimensions.height }, body[]{ ..., _type == "image" => { "src": asset->url, alt } } }`;// TypeScript types: post v1.// From themes.dev content models (https://www.themes.dev/sanity/models/), MIT. Your code now: edit it freely.import type { ImageRef, RichText } from './kinds'; /** An article with its own page: a journal post, a guide, a news item, an insight. The core is the title, a summary, the date and a cover; the text is the body. An author, topics and an update date are extensions. */export interface Post { title: string; /** For the card and the search result. */ summary?: string; date: string; /** 3:2, at least 1600 wide. */ cover?: ImageRef;} /** What the GROQ projection returns: the fields and the id (the slug, the same as the Astro entry's id). */export type PostRecord = Post & { id: string }; /** What postQuery returns: the record and its body. (In Astro the body is the Markdown file's content.) */export type PostDocument = PostRecord & { body: RichText };// Shared shapes of the generated content models./** An image: the path or URL without size suffix, and its alternative text (empty when the text beside it says it). */export interface ImageRef { src: string; alt: string; /** Intrinsic size, so the page reserves the box before the file arrives. */ width: number; height: number;} export interface Link { label: string; href: string;} /** Portable Text as Sanity stores it: blocks, and images with their alt text. */export type RichText = { _type: string; _key: string; [key: string]: unknown }[];Use it
In Sanity
// sanity.config.tsimport { post, postEntry } from './schemaTypes/post'; export default defineConfig({ // … schema: { types: [post, postEntry] },}); // In your site: the projection returns the Post type in types/post.ts.import { postListQuery } from './schemaTypes/post';const posts = await client.fetch(postListQuery);In Astro
// src/content.config.tsimport { defineCollection } from 'astro:content';import { glob } from 'astro/loaders';import { postSchema } from './models/post'; const posts = defineCollection({ loader: glob({ pattern: '**/*.md', base: './src/content/posts' }), schema: postSchema,}); export const collections = { posts };An image is always { src, alt, width, height } in both editions: the GROQ projection reads the size from the asset, and in Astro it sits in your data, so pages never shift while they load.