astro-ghostcms/.pnpm-store/v3/files/b8/7fe027fa79ed85746902c2d2f55...

38 lines
1.4 KiB
Plaintext

import { markHTMLString } from "../escape.js";
import { createRenderInstruction } from "./instruction.js";
import { renderElement } from "./util.js";
const uniqueElements = (item, index, all) => {
const props = JSON.stringify(item.props);
const children = item.children;
return index === all.findIndex((i) => JSON.stringify(i.props) === props && i.children == children);
};
function renderAllHeadContent(result) {
result._metadata.hasRenderedHead = true;
const styles = Array.from(result.styles).filter(uniqueElements).map(
(style) => style.props.rel === "stylesheet" ? renderElement("link", style) : renderElement("style", style)
);
result.styles.clear();
const scripts = Array.from(result.scripts).filter(uniqueElements).map((script) => {
return renderElement("script", script, false);
});
const links = Array.from(result.links).filter(uniqueElements).map((link) => renderElement("link", link, false));
let content = styles.join("\n") + links.join("\n") + scripts.join("\n");
if (result._metadata.extraHead.length > 0) {
for (const part of result._metadata.extraHead) {
content += part;
}
}
return markHTMLString(content);
}
function* renderHead() {
yield createRenderInstruction({ type: "head" });
}
function* maybeRenderHead() {
yield createRenderInstruction({ type: "maybe-head" });
}
export {
maybeRenderHead,
renderAllHeadContent,
renderHead
};