mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-12-05 05:45:00 +00:00
Closes #12608.
**Problem.** `generateHtml` inserts the viewer script, the vendor
scripts and the CSS with `String.prototype.replace` and a string as the
replacement, which expands `$$` to `$` and `$&` to the matched text. The
viewer writes the cost as `$${totalCost.toFixed(3)}`, so the exported
header showed `Cost: 0.300`. `highlight.min.js` contains both patterns
as well: the exported copy had `{{HIGHLIGHT_JS}}` in the R operator
regex in place of `$&`, and the Swift variable rule lost a `$`.
**Fix.** Every value is passed through a function (`.replace("{{JS}}",
() => templateJs)`), which inserts it as is. Applied to all ten
replacements, the CSS ones included, so a value with a `$` in it can no
longer change.
The chained replacements could also replace a placeholder that an
earlier inserted file contains. No inserted file contains one today, so
this PR does not change that; a single `replace(/\{\{(\w+)\}\}/g, ...)`
pass would rule it out if wanted.
**Validation** on main `cb32f74f9`:
- New `test/export-html-embedded-scripts.test.ts` exports a session with
`exportFromFile` and checks that the header keeps
`$${totalCost.toFixed(3)}` and that `template.js` and the three vendor
scripts are embedded unchanged. Three of its five tests (the cost,
`template.js`, `highlight.min.js`) fail without the fix.
- Manual: `pi --export` on a session with one $0.30 reply. Before:
`Cost: 0.300`, and one `{{HIGHLIGHT_JS}}` left in the page. After:
`Cost: $0.300`, none left.
- The existing export tests pass. `npm run check` passes.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
import { exportFromFile } from "../src/core/export-html/index.ts";
|
|
import { CURRENT_SESSION_VERSION, type SessionHeader } from "../src/core/session-manager.ts";
|
|
import { initTheme } from "../src/modes/interactive/theme/theme.ts";
|
|
|
|
const templateDir = new URL("../src/core/export-html/", import.meta.url);
|
|
const readTemplateFile = (path: string) => readFileSync(new URL(path, templateDir), "utf-8");
|
|
|
|
// #12608
|
|
describe("HTML export embedded scripts", () => {
|
|
let tempDir: string;
|
|
let html: string;
|
|
|
|
beforeAll(async () => {
|
|
initTheme("dark");
|
|
tempDir = mkdtempSync(join(tmpdir(), "pi-export-scripts-"));
|
|
const header = {
|
|
type: "session",
|
|
version: CURRENT_SESSION_VERSION,
|
|
id: "embedded-scripts-test",
|
|
timestamp: new Date(0).toISOString(),
|
|
cwd: tempDir,
|
|
} satisfies SessionHeader;
|
|
const sessionPath = join(tempDir, "session.jsonl");
|
|
writeFileSync(sessionPath, `${JSON.stringify(header)}\n`);
|
|
html = readFileSync(await exportFromFile(sessionPath, join(tempDir, "out.html")), "utf8");
|
|
});
|
|
|
|
afterAll(() => {
|
|
rmSync(tempDir, { recursive: true, force: true });
|
|
});
|
|
|
|
it("keeps the dollar sign in the header cost", () => {
|
|
expect(html).toContain("$${totalCost.toFixed(3)}");
|
|
});
|
|
|
|
it.each(["template.js", "vendor/marked.min.js", "vendor/marked-cjk-friendly.min.js", "vendor/highlight.min.js"])(
|
|
"embeds %s unchanged",
|
|
(path) => {
|
|
expect(html).toContain(readTemplateFile(path));
|
|
},
|
|
);
|
|
});
|