diff --git a/.changeset/mighty-trees-teach.md b/.changeset/mighty-trees-teach.md new file mode 100644 index 000000000000..289a41816154 --- /dev/null +++ b/.changeset/mighty-trees-teach.md @@ -0,0 +1,5 @@ +--- +'@astrojs/mdx': patch +--- + +Fixes stack trace location when failed to parse an MDX file with frontmatter diff --git a/packages/integrations/mdx/src/vite-plugin-mdx.ts b/packages/integrations/mdx/src/vite-plugin-mdx.ts index b232d1c83fc8..7b37535d8e98 100644 --- a/packages/integrations/mdx/src/vite-plugin-mdx.ts +++ b/packages/integrations/mdx/src/vite-plugin-mdx.ts @@ -44,9 +44,10 @@ export function vitePluginMdx(mdxOptions: MdxOptions): Plugin { async transform(code, id) { if (!id.endsWith('.mdx')) return; - const { data: frontmatter, content: pageContent } = parseFrontmatter(code, id); + const { data: frontmatter, content: pageContent, matter } = parseFrontmatter(code, id); + const frontmatterLines = matter ? matter.match(/\n/g)?.join('') + '\n\n' : ''; - const vfile = new VFile({ value: pageContent, path: id }); + const vfile = new VFile({ value: frontmatterLines + pageContent, path: id }); // Ensure `data.astro` is available to all remark plugins setVfileFrontmatter(vfile, frontmatter);