Open xavdid opened 1 month ago
Hi. Currently the frontmatter
variable is the raw frontmatter, without parsing. I do think it would be better if it were the parsed data, but there is a workaround. The post.data
has the correct default, and you can pass it as a prop to <Content />
:
---
import { type CollectionEntry, getCollection } from "astro:content";
import BlogPost from "../../layouts/BlogPost.astro";
export async function getStaticPaths() {
const posts = await getCollection("blog");
return posts.map((post) => ({
params: { slug: post.slug },
props: post,
}));
}
type Props = CollectionEntry<"blog">;
const post = Astro.props;
const { Content } = await post.render();
---
<BlogPost {...post.data}>
<Content data={post.data} />
</BlogPost>
Then you can access this as props.data
in MDX:
---
title: 'broken post'
---
I'd expect to see an empty list in props:
<pre>{JSON.stringify(props.data, null, 2)}</pre>
That's fair, that works as a workaround! I think clearer docs around how to read frontmatter from within a post would be useful (especially for this edge case - it was really confusing that title
worked but list
didn't.
Thanks!
Astro Info
If this issue only occurs in one browser, which browser is a problem?
N/A
Describe the Bug
When declaring a
zod
schema with a default value, that default isn't used when parsing mdx frontmatter. For example:And this post:
Correctly passes validation, but doesn't pick up the default
[]
:What's the expected result?
I'd expect the post to show zod's default value if a prop isn't passed:
Link to Minimal Reproducible Example
Participation