diff options
author | 2023-09-05 12:48:39 +0200 | |
---|---|---|
committer | 2023-09-05 12:48:39 +0200 | |
commit | 93a1231f14d97339e38d8a67cf541337960e7d5e (patch) | |
tree | 88b85d809fea2cd14883c6cab6b2eef2f70762b9 /packages/integrations/mdx | |
parent | fd6ce9a99acb611cb0ec31b7875ae5ba02e23d72 (diff) | |
download | astro-93a1231f14d97339e38d8a67cf541337960e7d5e.tar.gz astro-93a1231f14d97339e38d8a67cf541337960e7d5e.tar.zst astro-93a1231f14d97339e38d8a67cf541337960e7d5e.zip |
Add location data to MDX compile errors (#8405)
Diffstat (limited to 'packages/integrations/mdx')
-rw-r--r-- | packages/integrations/mdx/src/index.ts | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts index 8366698d5..9053e824c 100644 --- a/packages/integrations/mdx/src/index.ts +++ b/packages/integrations/mdx/src/index.ts @@ -2,7 +2,7 @@ import { markdownConfigDefaults } from '@astrojs/markdown-remark'; import { toRemarkInitializeAstroData } from '@astrojs/markdown-remark/dist/internal.js'; import { compile as mdxCompile, type CompileOptions } from '@mdx-js/mdx'; import type { PluggableList } from '@mdx-js/mdx/lib/core.js'; -import type { AstroIntegration, ContentEntryType, HookParameters } from 'astro'; +import type { AstroIntegration, ContentEntryType, HookParameters, SSRError } from 'astro'; import astroJSXRenderer from 'astro/jsx/renderer.js'; import { parse as parseESM } from 'es-module-lexer'; import fs from 'node:fs/promises'; @@ -129,27 +129,33 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI const code = await fs.readFile(fileId, 'utf-8'); const { data: frontmatter, content: pageContent } = parseFrontmatter(code, id); - const compiled = await mdxCompile(new VFile({ value: pageContent, path: id }), { - ...mdxPluginOpts, - elementAttributeNameCase: 'html', - remarkPlugins: [ - // Ensure `data.astro` is available to all remark plugins - toRemarkInitializeAstroData({ userFrontmatter: frontmatter }), - ...(mdxPluginOpts.remarkPlugins ?? []), - ], - recmaPlugins: [ - ...(mdxPluginOpts.recmaPlugins ?? []), - () => recmaInjectImportMetaEnvPlugin({ importMetaEnv }), - ], - SourceMapGenerator: config.vite.build?.sourcemap - ? SourceMapGenerator - : undefined, - }); + try { + const compiled = await mdxCompile(new VFile({ value: pageContent, path: id }), { + ...mdxPluginOpts, + elementAttributeNameCase: 'html', + remarkPlugins: [ + // Ensure `data.astro` is available to all remark plugins + toRemarkInitializeAstroData({ userFrontmatter: frontmatter }), + ...(mdxPluginOpts.remarkPlugins ?? []), + ], + recmaPlugins: [ + ...(mdxPluginOpts.recmaPlugins ?? []), + () => recmaInjectImportMetaEnvPlugin({ importMetaEnv }), + ], + SourceMapGenerator: config.vite.build?.sourcemap + ? SourceMapGenerator + : undefined, + }); - return { - code: escapeViteEnvReferences(String(compiled.value)), - map: compiled.map, - }; + return { + code: escapeViteEnvReferences(String(compiled.value)), + map: compiled.map, + }; + } catch (e: any) { + const err: SSRError = e; + err.loc = { file: fileId, line: e.line, column: e.column }; + throw err; + } }, }, { |