54 lines
1.6 KiB
JavaScript
54 lines
1.6 KiB
JavaScript
import { AstroJSX, jsx } from "../jsx-runtime/index.js";
|
|
import { renderJSX } from "../runtime/server/jsx.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) {
|
|
const error = e;
|
|
if (Component[Symbol.for("mdx-component")]) {
|
|
throw createFormattedError({
|
|
message: error.message,
|
|
title: error.name,
|
|
hint: `This issue often occurs when your MDX component encounters runtime errors.`,
|
|
name: error.name,
|
|
stack: error.stack
|
|
});
|
|
}
|
|
}
|
|
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;
|
|
const html = await renderJSX(result, jsx(Component, { ...props, ...slots, children }));
|
|
return { html };
|
|
}
|
|
function createFormattedError({ message, name, stack, hint }) {
|
|
const error = new Error(message);
|
|
error.name = name;
|
|
error.stack = stack;
|
|
error.hint = hint;
|
|
return error;
|
|
}
|
|
var server_default = {
|
|
check,
|
|
renderToStaticMarkup
|
|
};
|
|
export {
|
|
check,
|
|
server_default as default,
|
|
renderToStaticMarkup
|
|
};
|