import { Ctx } from "./ctx"; import { htmlData } from "sosse/uni"; const defaultTpl = function ({ title, head, bodyAttrs, body }) { return ` ${title ? `${title}` : ""} ${head} ${body} `; }; const createHtmlOptions = function (overrides: HtmlOptions = {}): HtmlOptions { return { head: "", title: "", body: "", bodyAttrs: {}, tpl: defaultTpl, ...overrides, }; }; export type HtmlOptions = { head?: string; title?: string; body?: string; bodyAttrs?: Record; ctx?: Ctx; tpl?: typeof defaultTpl; }; export const html = function (options: HtmlOptions = {}) { let { head, title, body, bodyAttrs, tpl, ctx } = createHtmlOptions(options); const data = htmlData(); htmlData(null); if (data != null) { head += ``; } if (ctx) { for (const injectHtml of Object.values(ctx.injectHtml.head)) { head += injectHtml; } for (const injectHtml of Object.values(ctx.injectHtml.footer)) { body += injectHtml; } } let bodyAttrsString = ""; for (const [key, value] of Object.entries(bodyAttrs)) { bodyAttrsString += ` ${key}="${value}"`; } return tpl({ head, title, body, bodyAttrs: bodyAttrsString, }); }; export const notFoundHtml = function (options: HtmlOptions = {}) { return html( createHtmlOptions({ title: "Page not found", body: "

Page not found

", ...options, }) ); };