Add location data to MDX compile errors (#8405)
This commit is contained in:
parent
fd6ce9a99a
commit
93a1231f14
2 changed files with 32 additions and 21 deletions
5
.changeset/spotty-glasses-grin.md
Normal file
5
.changeset/spotty-glasses-grin.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/mdx': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Add location data to MDX compile errors
|
|
@ -2,7 +2,7 @@ import { markdownConfigDefaults } from '@astrojs/markdown-remark';
|
||||||
import { toRemarkInitializeAstroData } from '@astrojs/markdown-remark/dist/internal.js';
|
import { toRemarkInitializeAstroData } from '@astrojs/markdown-remark/dist/internal.js';
|
||||||
import { compile as mdxCompile, type CompileOptions } from '@mdx-js/mdx';
|
import { compile as mdxCompile, type CompileOptions } from '@mdx-js/mdx';
|
||||||
import type { PluggableList } from '@mdx-js/mdx/lib/core.js';
|
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 astroJSXRenderer from 'astro/jsx/renderer.js';
|
||||||
import { parse as parseESM } from 'es-module-lexer';
|
import { parse as parseESM } from 'es-module-lexer';
|
||||||
import fs from 'node:fs/promises';
|
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 code = await fs.readFile(fileId, 'utf-8');
|
||||||
|
|
||||||
const { data: frontmatter, content: pageContent } = parseFrontmatter(code, id);
|
const { data: frontmatter, content: pageContent } = parseFrontmatter(code, id);
|
||||||
const compiled = await mdxCompile(new VFile({ value: pageContent, path: id }), {
|
try {
|
||||||
...mdxPluginOpts,
|
const compiled = await mdxCompile(new VFile({ value: pageContent, path: id }), {
|
||||||
elementAttributeNameCase: 'html',
|
...mdxPluginOpts,
|
||||||
remarkPlugins: [
|
elementAttributeNameCase: 'html',
|
||||||
// Ensure `data.astro` is available to all remark plugins
|
remarkPlugins: [
|
||||||
toRemarkInitializeAstroData({ userFrontmatter: frontmatter }),
|
// Ensure `data.astro` is available to all remark plugins
|
||||||
...(mdxPluginOpts.remarkPlugins ?? []),
|
toRemarkInitializeAstroData({ userFrontmatter: frontmatter }),
|
||||||
],
|
...(mdxPluginOpts.remarkPlugins ?? []),
|
||||||
recmaPlugins: [
|
],
|
||||||
...(mdxPluginOpts.recmaPlugins ?? []),
|
recmaPlugins: [
|
||||||
() => recmaInjectImportMetaEnvPlugin({ importMetaEnv }),
|
...(mdxPluginOpts.recmaPlugins ?? []),
|
||||||
],
|
() => recmaInjectImportMetaEnvPlugin({ importMetaEnv }),
|
||||||
SourceMapGenerator: config.vite.build?.sourcemap
|
],
|
||||||
? SourceMapGenerator
|
SourceMapGenerator: config.vite.build?.sourcemap
|
||||||
: undefined,
|
? SourceMapGenerator
|
||||||
});
|
: undefined,
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
code: escapeViteEnvReferences(String(compiled.value)),
|
code: escapeViteEnvReferences(String(compiled.value)),
|
||||||
map: compiled.map,
|
map: compiled.map,
|
||||||
};
|
};
|
||||||
|
} catch (e: any) {
|
||||||
|
const err: SSRError = e;
|
||||||
|
err.loc = { file: fileId, line: e.line, column: e.column };
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue