From 423614ebb6ddb76cc8d11f3e3b6ae111a4a82662 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Fri, 16 Aug 2024 12:09:20 +0800 Subject: [PATCH] Fix MDX stack trace location with frontmatter (#11717) --- .changeset/mighty-trees-teach.md | 5 +++++ packages/integrations/mdx/src/vite-plugin-mdx.ts | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 .changeset/mighty-trees-teach.md diff --git a/.changeset/mighty-trees-teach.md b/.changeset/mighty-trees-teach.md new file mode 100644 index 0000000000..289a418161 --- /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 b232d1c83f..7b37535d8e 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);