-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.js
37 lines (32 loc) · 1.05 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const slotName = (str) =>
str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase());
function check(Component, props, children) {
if (typeof Component !== "function") return false;
const { html } = renderToStaticMarkup(Component, props, children);
return typeof html === "string";
}
function renderToStaticMarkup(Component, props, slotted, metadata) {
//console.log('renderToStaticMarkup', Component, 'props', props, 'slotted', slotted, metadata);
const needsHydrate = metadata?.astroStaticSlot ? !!metadata.hydrate : true;
const tagName = needsHydrate ? "astro-slot" : "astro-static-slot";
const el = Component(props);
for (const [key, value] of Object.entries(slotted)) {
const name = slotName(key);
if (name == "default") {
el.insertAdjacentHTML("beforeend", value);
} else {
el.insertAdjacentHTML(
"beforeend",
`<${tagName} name="${name}">${value}</${tagName}>`
);
}
}
return {
html: el.outerHTML,
};
}
export default {
check,
renderToStaticMarkup,
supportsAstroStaticSlot: true,
};