mirror of
https://github.com/withastro/astro.git
synced 2025-01-20 22:12:38 -05:00
Refactor MDX components exports handling (#7904)
This commit is contained in:
parent
c0de7a7b0f
commit
32669cd475
4 changed files with 15 additions and 7 deletions
5
.changeset/loud-candles-admire.md
Normal file
5
.changeset/loud-candles-admire.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/mdx': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Handle `components` exports handling itself
|
5
.changeset/young-roses-teach.md
Normal file
5
.changeset/young-roses-teach.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': major
|
||||||
|
---
|
||||||
|
|
||||||
|
Remove MDX special `components` export handling
|
|
@ -59,12 +59,7 @@ async function renderPage({ mod, renderContext, env, cookies }: RenderPage) {
|
||||||
locals: renderContext.locals ?? {},
|
locals: renderContext.locals ?? {},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Support `export const components` for `MDX` pages
|
const response = await runtimeRenderPage(
|
||||||
if (typeof (mod as any).components === 'object') {
|
|
||||||
Object.assign(renderContext.props, { components: (mod as any).components });
|
|
||||||
}
|
|
||||||
|
|
||||||
let response = await runtimeRenderPage(
|
|
||||||
result,
|
result,
|
||||||
Component,
|
Component,
|
||||||
renderContext.props,
|
renderContext.props,
|
||||||
|
|
|
@ -171,11 +171,14 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
|
||||||
code += `\nexport const file = ${JSON.stringify(fileId)};`;
|
code += `\nexport const file = ${JSON.stringify(fileId)};`;
|
||||||
}
|
}
|
||||||
if (!moduleExports.find(({ n }) => n === 'Content')) {
|
if (!moduleExports.find(({ n }) => n === 'Content')) {
|
||||||
|
// If have `export const components`, pass that as props to `Content` as fallback
|
||||||
|
const hasComponents = moduleExports.find(({ n }) => n === 'components');
|
||||||
|
|
||||||
// Make `Content` the default export so we can wrap `MDXContent` and pass in `Fragment`
|
// Make `Content` the default export so we can wrap `MDXContent` and pass in `Fragment`
|
||||||
code = code.replace('export default MDXContent;', '');
|
code = code.replace('export default MDXContent;', '');
|
||||||
code += `\nexport const Content = (props = {}) => MDXContent({
|
code += `\nexport const Content = (props = {}) => MDXContent({
|
||||||
...props,
|
...props,
|
||||||
components: { Fragment, ...props.components },
|
components: { Fragment${hasComponents ? ', ...components' : ''}, ...props.components },
|
||||||
});
|
});
|
||||||
export default Content;`;
|
export default Content;`;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue