astro-ghostcms/.pnpm-store/v3/files/c5/e299a90682d77cdd154d6969a79...

75 lines
2.0 KiB
Plaintext

import { HTMLString, markHTMLString } from "../escape.js";
import { renderChild } from "./any.js";
import { chunkToString } from "./common.js";
const slotString = Symbol.for("astro:slot-string");
class SlotString extends HTMLString {
instructions;
[slotString];
constructor(content, instructions) {
super(content);
this.instructions = instructions;
this[slotString] = true;
}
}
function isSlotString(str) {
return !!str[slotString];
}
function renderSlot(result, slotted, fallback) {
if (!slotted && fallback) {
return renderSlot(result, fallback);
}
return {
async render(destination) {
await renderChild(destination, typeof slotted === "function" ? slotted(result) : slotted);
}
};
}
async function renderSlotToString(result, slotted, fallback) {
let content = "";
let instructions = null;
const temporaryDestination = {
write(chunk) {
if (chunk instanceof Response)
return;
if (typeof chunk === "object" && "type" in chunk && typeof chunk.type === "string") {
if (instructions === null) {
instructions = [];
}
instructions.push(chunk);
} else {
content += chunkToString(result, chunk);
}
}
};
const renderInstance = renderSlot(result, slotted, fallback);
await renderInstance.render(temporaryDestination);
return markHTMLString(new SlotString(content, instructions));
}
async function renderSlots(result, slots = {}) {
let slotInstructions = null;
let children = {};
if (slots) {
await Promise.all(
Object.entries(slots).map(
([key, value]) => renderSlotToString(result, value).then((output) => {
if (output.instructions) {
if (slotInstructions === null) {
slotInstructions = [];
}
slotInstructions.push(...output.instructions);
}
children[key] = output;
})
)
);
}
return { slotInstructions, children };
}
export {
SlotString,
isSlotString,
renderSlot,
renderSlotToString,
renderSlots
};