UNPKG

@astrojs/mdx

Version:

Add support for MDX pages in your Astro site

62 lines (61 loc) 1.9 kB
import { AstroError } from "astro/errors"; import { AstroJSX, jsx } from "astro/jsx-runtime"; import { chunkToString, renderStreaming } from "astro/runtime/server/index.js"; const slotName = (str) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase()); async function check(Component, props, { default: children = null, ...slotted } = {}) { if (typeof Component !== "function") return false; const slots = {}; 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, Component); } return false; } async function renderToStaticMarkup(Component, props = {}, { default: children = null, ...slotted } = {}) { const slots = {}; for (const [key, value] of Object.entries(slotted)) { const name = slotName(key); slots[name] = value; } const { result } = this; try { let html = ""; const destination = { write(chunk) { if (chunk instanceof Response) return; html += chunkToString(result, chunk); } }; await renderStreaming(jsx(Component, { ...props, ...slots, children }), result, destination); return { html }; } catch (e) { throwEnhancedErrorIfMdxComponent(e, Component); throw e; } } function throwEnhancedErrorIfMdxComponent(error, Component) { if (Component[/* @__PURE__ */ Symbol.for("mdx-component")]) { if (AstroError.is(error)) return; error.title = error.name; error.hint = `This issue often occurs when your MDX component encounters runtime errors.`; throw error; } } const renderer = { name: "astro:jsx", check, renderToStaticMarkup }; var server_default = renderer; export { check, server_default as default, renderToStaticMarkup, slotName };