Typesafe markdown blog with Astro 2.0
Astro just released their 2.0 version with lots of goodies, and the treat most important to this post is the introduction of content collections.
With the recent release of Astro 2.0 creating a type-safe blog has never been easier!
Content collections
With content collections you get to write typesafe markdown files by specifying a ZOD schema that decribes your posts, e.g:
const blog = defineCollection({
schema: z.object({
// Define your expected frontmatter properties
title: z.string(),
// Mark certain properties as optional
draft: z.boolean().optional(),
// Transform datestrings to full Date objects
publishDate: z.string().transform((val) => new Date(val))
// Improve SEO with descriptive warnings
description: z.string()
// ...
}),
});