diff options
author | 2025-01-10 17:18:37 +0800 | |
---|---|---|
committer | 2025-01-10 17:18:37 +0800 | |
commit | 3a267f33a2a2576c9065c88646ed67f5a7a8ba0b (patch) | |
tree | d6ea02a5970760fe8520138792782c9bd05800a6 | |
parent | faf74af522f4499ab95531b24a0a1c14070abe8b (diff) | |
download | astro-3a267f33a2a2576c9065c88646ed67f5a7a8ba0b.tar.gz astro-3a267f33a2a2576c9065c88646ed67f5a7a8ba0b.tar.zst astro-3a267f33a2a2576c9065c88646ed67f5a7a8ba0b.zip |
Revert "Make MDX integration check noop (#12913)" (#12959)
-rw-r--r-- | .changeset/calm-tips-think.md | 5 | ||||
-rw-r--r-- | packages/integrations/mdx/src/server.ts | 26 |
2 files changed, 25 insertions, 6 deletions
diff --git a/.changeset/calm-tips-think.md b/.changeset/calm-tips-think.md new file mode 100644 index 000000000..2e5af5133 --- /dev/null +++ b/.changeset/calm-tips-think.md @@ -0,0 +1,5 @@ +--- +'@astrojs/mdx': patch +--- + +Reverts https://github.com/withastro/astro/commit/9a3b48c5c3e8f597159454f06c5a0ce8e709bc50 which caused a regression for rendering inline MDX components and MDX files from content collections diff --git a/packages/integrations/mdx/src/server.ts b/packages/integrations/mdx/src/server.ts index e41a33898..79934eb32 100644 --- a/packages/integrations/mdx/src/server.ts +++ b/packages/integrations/mdx/src/server.ts @@ -1,15 +1,29 @@ import type { NamedSSRLoadedRendererValue } from 'astro'; import { AstroError } from 'astro/errors'; -import { jsx } from 'astro/jsx-runtime'; +import { AstroJSX, jsx } from 'astro/jsx-runtime'; import { renderJSX } from 'astro/runtime/server/index.js'; const slotName = (str: string) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase()); -// MDX components are tagged with `__astro_tag_component__` in `vite-plugin-mdx-postprocess.ts` -// and tagged componenets can directly tell Astro which renderer to use in build-time, rather -// than checking the components in runtime. So this function is made a no-op as any untagged -// component wouldn't have belonged to MDX. -export async function check() { +// NOTE: In practice, MDX components are always tagged with `__astro_tag_component__`, so the right renderer +// is used directly, and this check is not often used to return true. +export async function check( + Component: any, + props: any, + { default: children = null, ...slotted } = {}, +) { + if (typeof Component !== 'function') return false; + const slots: Record<string, any> = {}; + for (const [key, value] of Object.entries(slotted)) { + const name = slotName(key); + slots[name] = value; + } + try { + const result = await Component({ ...props, ...slots, children }); + return result[AstroJSX]; + } catch (e) { + throwEnhancedErrorIfMdxComponent(e as Error, Component); + } return false; } |