Add jdd_html helper function for rendering a page with one or more JDDs in it

This commit is contained in:
Kuba Orlik 2025-08-16 17:36:19 +02:00
parent 140fb13b6a
commit 1668b92793

32
src/back/jdd-html.tsx Normal file
View File

@ -0,0 +1,32 @@
import { JDD, type RawJDDocument } from "@sealcode/jdd";
import html from "./html.js";
import { makeJDDContext } from "./jdd-context.js";
import { registry } from "./jdd-components/registry.js";
import { tempstream } from "tempstream";
export type JDDHtmlArgs = Omit<Parameters<typeof html>["0"], "body"> & {
raw_jdds: RawJDDocument[];
construct_body: (built_jdd_html: JSX.Element) => JSX.Element;
};
export default async function jdd_html(args: JDDHtmlArgs) {
const jdd_context = makeJDDContext(args.ctx);
const jdds = await Promise.all(
args.raw_jdds.map((raw_jdd) => JDD.fromStorage(registry, jdd_context, raw_jdd))
);
const css_clumps = Array.from(
new Set([
...(args?.css_clumps || []),
...jdds.map((jdd) => jdd.getAllCSSClumps()).flat(),
]).values()
);
let built_jdd_html = tempstream``;
for (const jdd of jdds) {
built_jdd_html = tempstream`${built_jdd_html}${jdd.render()}`;
}
return html({
css_clumps,
...args,
body: args.construct_body(built_jdd_html),
});
}