Files
oh-my-pi/packages/coding-agent/scripts/generate-template.ts
T
can1357 075e2f47ad fix(coding-agent/export): prevented HTML template $ substitution expansions
- Switched template CSS/JS inlining replacements to callback form in generation scripts to avoid `$` replacement expansion semantics.
- Updated HTML export generation to use callback-based replacements for theme variables and session data injection to prevent accidental `$` substitution parsing.
- Added regression tests for the inlined script ensuring literal `$'` regex tokens remain intact, no closing HTML tags are injected, and the script parses via `new Function`.
2026-05-12 11:15:38 +02:00

34 lines
1.1 KiB
TypeScript

#!/usr/bin/env bun
/**
* Pre-compute the HTML template at publish time.
* This avoids the Bun macro restriction in node_modules.
*/
const dir = new URL("../src/export/html/", import.meta.url).pathname;
// Read all files
const html = await Bun.file(`${dir}template.html`).text();
const css = await Bun.file(`${dir}template.css`).text();
const js = await Bun.file(`${dir}template.js`).text();
// Minify CSS
const minifiedCss = css
.replace(/\/\*[\s\S]*?\*\//g, "")
.replace(/\s+/g, " ")
.replace(/\s*([{}:;,])\s*/g, "$1")
.trim();
// Inline everything; use function replacements so `$'`, `$&`, `$$`, etc. inside
// the embedded CSS/JS are not interpreted as substitution patterns.
const template = html
.replace("<template-css/>", () => `<style>${minifiedCss}</style>`)
.replace("<template-js/>", () => `<script>${js}</script>`);
// Write generated file
const output = `// Auto-generated by scripts/generate-template.ts - DO NOT EDIT
export const TEMPLATE = ${JSON.stringify(template)};
`;
await Bun.write(`${dir}template.generated.ts`, output);
console.log("Generated template.generated.ts");