const blogCollection = defineCollection({
schema: ({ image }) =>
z.object({
title: z.string(),
description: z.string(),
heroImage: image().refine((img) => img.width >= 1200, {
message: "Hero image must be at least 1200px wide",
}),
heroImageAlt: z.string(),
publishDate: z.string().transform((str: string) => new Date(str)),
updateDate: z.string().transform((str: string) => new Date(str)),
slug: z.string(),
}),
});
export const collection = {
blog: blogCollection
};
In this example, we have a blog content collection with a few defined fields for the frontmatter mdx files.
Within a .astro file I import the collection like so:
const blogPosts = await getCollection('blog');
Which returns the type of:
const blogPosts: ({
id: "choosing-a-standing-desk.mdx";
slug: "choosing-a-standing-desk";
body: string;
collection: "blog";
data: any;
} & {
render(): Promise<{
Content: AstroComponentFactory;
headings: MarkdownHeading[];
remarkPluginFrontmatter: Record<...>;
}>;
})[]
data here does not have the type information defined in the config.