Based on the knowledge sources, you cannot pass additional props (like slug) directly from your Astro page to the MDX file and then access them inside the MDX content itself. MDX files do not have access to Astro.props or any props passed from the parent component/page. The only data available inside the MDX file are frontmatter variables and exported variables defined within the MDX file itself.
However, you can pass props (including slug) to your layout component (e.g., BlogPost.astro) when rendering the MDX content. The layout will receive these props via Astro.props, and you can use them there. The MDX content itself will be rendered inside the <slot /> of your layout, but it will not have access to those props.
Example usage:
Suppose you have a dynamic route that renders your MDX content and passes the slug:
---
// src/pages/blog/[slug].astro
import { getCollection, render } 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.id },
props: { ...post.data, slug: post.id }, // Pass slug here
}));
}
const { Content } = await render(Astro.props);
---
<BlogPost {...Astro.props}>
<Content />
</BlogPost>
Your BlogPost.astro layout (as you posted) will receive slug in Astro.props and can use it as needed.
But:
Inside your MDX file, you cannot access slug (or any other prop passed from the page/layout) directly. You can only use frontmatter variables or variables you export from the MDX file itself.
Summary:
- Pass
slug as a prop to your layout (BlogPost.astro), and use it there.
- You cannot access
slug inside the MDX content itself.