From 5e2e53ee7b9997b7e918d5a530e66d91c55bb14a Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 10 Jan 2025 16:55:12 +0800 Subject: [PATCH 1/2] Revert "Make MDX integration check noop (#12913)" --- .changeset/calm-tips-think.md | 5 +++++ packages/integrations/mdx/src/server.ts | 27 ++++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 .changeset/calm-tips-think.md diff --git a/.changeset/calm-tips-think.md b/.changeset/calm-tips-think.md new file mode 100644 index 000000000000..2e5af5133563 --- /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 e41a338989e6..d5398a049f7a 100644 --- a/packages/integrations/mdx/src/server.ts +++ b/packages/integrations/mdx/src/server.ts @@ -1,16 +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() { - return false; +// 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 = {}; + 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); + } } export async function renderToStaticMarkup( From 82d7de340622bf628be4b75e767d19fdb082f1a2 Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 10 Jan 2025 16:56:53 +0800 Subject: [PATCH 2/2] Typo --- packages/integrations/mdx/src/server.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/integrations/mdx/src/server.ts b/packages/integrations/mdx/src/server.ts index d5398a049f7a..79934eb3229a 100644 --- a/packages/integrations/mdx/src/server.ts +++ b/packages/integrations/mdx/src/server.ts @@ -24,6 +24,7 @@ export async function check( } catch (e) { throwEnhancedErrorIfMdxComponent(e as Error, Component); } + return false; } export async function renderToStaticMarkup(