Creating a New Block
One schema, three jobs: how a block on this blog goes from an idea to a form in the builder, a validated post, and plain HTML.

Everything on this blog is made of blocks. A post is just a list of them (a hero, some rich text, an image, maybe a quote) saved as JSON in content/<slug>/post.json.
The part I like most is that every block is described once. That single description is then used for three completely different things:
- The build uses it to check every post before a single page is generated. A broken post fails the build.
- The page builder reads it to draw the form you fill in.
- The import script uses it to check a post exported from the builder before it lands in the repo.
So if I add a field to a block, the form, the validation and the types all update together. Let's walk through a real one: the Image block.
The anatomy of a block
Each block is a folder with two files:
src/blocks/image/
schema.ts ← what the block's data looks like
ImageBlock.tsx ← how it's drawnPlus one line in each of two lists: the catalogue (src/blocks/catalogue.ts), which knows about every block's schema, and the registry (src/blocks/registry.tsx), which knows about every block's component. The catalogue has no React in it at all, so it's safe to use from Node scripts like the importer.
1. The schema
Schemas are written with Zod. Here's the Image block's, in full:
import { z } from 'zod';
import { type Fields, href, optionalText, sectionFields } from '../fields';
export const imageSchema = (f: Fields) =>
z
.object({
type: z.literal('image'),
image: f.image({ title: 'Image' }),
caption: optionalText('Caption', { description: 'Markdown links are allowed, e.g. "Photo by [Jane](https://…)"' }),
width: z
.enum(['regular', 'wide', 'full'])
.default('regular')
.meta({ title: 'Width', description: 'Regular: the text column. Wide: the content width. Full: edge to edge.' }),
href: href('Link').optional().meta({ title: 'Link', description: 'Optional: make the image a link' }),
...sectionFields,
})
.meta({ title: 'Image', description: 'A single image with an optional caption' });
export type ImageData = z.output<ReturnType<typeof imageSchema>>;A few things worth pointing out:
type: z.literal('image')is how a post's list of blocks knows which is which. All the block schemas are combined into one big discriminated union ontype..meta({ title, description })isn't used for validation at all. It's there for the builder, which turns it into the field's label and the help text underneath....sectionFieldsadds the fields every block shares: background, spacing, an anchor ID, and the Animate checkbox, which switches on the scroll-in animations.ImageDatais the TypeScript type of a valid Image block, derived from the schema, so the component can never disagree with it.
Why the schema is a function
You might have noticed the schema isn't a plain object: it's a function that takes f: Fields. That's because of images.
During the build, an image path like ./images/brain-with-data-flowing.webp should be checked to make sure the file actually exists, and turned into something Astro can optimise (resize, convert to WebP, add a srcset). Astro has a helper for exactly that, image(), but it only exists inside Astro's content collections. In the builder and the import script, the same path is just a string.
So f.image() comes from a little factory that's given the right check for wherever the schema is being used:
// In the build (src/content.config.ts): Astro checks the file exists and optimises it.
schema: ({ image }) => createPostSchema(image()),
// In the builder and the import script: just a string, in the right folder.
export const imagePathSchema = z
.string()
.regex(/^\.\/images\/[\w.-]+\.(png|jpe?g|webp|avif|gif|svg)$/i, 'Images must live in ./images/ (png, jpg, webp, avif, gif or svg)');Everything else about the schema is shared, so the builder and the build always agree.
2. The form (for free)
This is my favourite part. The builder doesn't have a hand-written form for any block. It walks the schema and decides what to draw for each field:
- a string becomes a text box (or a Markdown editor, or a textarea, depending on its
uihint), - an enum becomes a dropdown,
- a boolean becomes a checkbox,
- an array becomes a list you can add to, reorder and remove from,
- an image becomes an image picker with an alt text box,
- and
.default()values are filled in for you.
Here's the form it generates for the Image block, and what happens if you try to export it without choosing an image. The message in red is the one from imagePathSchema above. Nobody wrote it twice.


3. The component
The component is the simplest part. It's a normal React component that receives the block's data, already validated, with the image already optimised:
const SIZES = {
regular: '(min-width: 48rem) 46rem, 100vw',
wide: '(min-width: 75rem) 72rem, 100vw',
full: '100vw',
};
export function ImageBlock({ data }: { data: ImageData }) {
const picture = <Picture image={data.image} sizes={SIZES[data.width]} />;
return (
<Section tone={data.tone} spaceAbove={data.spaceAbove} anchor={data.anchor} className={animateClass(data.animate, 'zoom')}>
<figure className="figure" data-width={data.width}>
{data.href ? <a href={data.href} {...linkAttrs(data.href)}>{picture}</a> : picture}
{data.caption && <InlineMarkdown as="figcaption" source={data.caption} />}
</figure>
</Section>
);
}React only runs when the site is built. Astro renders this to plain HTML, and not a byte of React reaches your browser for an image block. Section, Picture and InlineMarkdown are small shared building blocks that every block uses, so things like spacing, lazy loading and responsive images behave the same everywhere.
The animate field from sectionFields is turned into a couple of CSS class names by animateClass(). The actual animation is pure CSS: a scroll-driven animation that fades the image in as it comes into view, and does nothing at all for anyone who prefers reduced motion.
4. Wiring it up
The last step is telling the catalogue and the registry about the new block:
// src/blocks/catalogue.ts
{
type: 'image',
schema: imageSchema,
defaults: { caption: 'A caption for the image' }, // what the builder inserts
},
// src/blocks/registry.tsx
image: ImageBlock,defaults is the starter content the builder drops in when you add the block, so you can see what it looks like straight away.
And that's it. The next time I run npm run dev, the block:
- appears in the builder's Add block list, with its form,
- is accepted (and checked) by the build in any post, and
- is checked by
npm run import-posttoo.
If I get any of it wrong (a component expecting a field the schema doesn't have, say), TypeScript tells me before I've even saved the file.
What I'd add next
Writing this post made me want a few more blocks: a proper gallery, a tweet/post embed that doesn't need anyone's tracking scripts, and maybe a diagram block for my visual-thinker brain. Each of those is now two files and two lines... which is exactly why I wanted to build it this way in the first place.