feat(cli): stamp font compiler version in localized HTML

This commit is contained in:
Miguel Ángel
2026-08-26 01:11:38 +00:00
parent ec68d40cc6
commit d6de083411
3 changed files with 33 additions and 5 deletions
+15 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it, vi } from "vitest";
import { runFontLocalize, type FontLocalizeIo } from "./fontLocalize.js";
import { runFontLocalize, stampFontCompilerVersion, type FontLocalizeIo } from "./fontLocalize.js";
function makeIo(input: string): {
io: FontLocalizeIo;
@@ -70,3 +70,17 @@ describe("runFontLocalize", () => {
expect(harness.errors.join(" ")).toContain("empty output");
});
});
describe("stampFontCompilerVersion", () => {
it("records the compiler version inside the document head", () => {
const stamped = stampFontCompilerVersion(
"<!doctype html><html><head><title>x</title></head><body></body></html>",
"0.8.15",
);
expect(stamped).toContain('<meta name="hyperframes-font-compiler-version" content="0.8.15">');
expect(stamped.indexOf("hyperframes-font-compiler-version")).toBeLessThan(
stamped.indexOf("</head>"),
);
});
});
+11
View File
@@ -4,6 +4,17 @@ export interface FontLocalizeIo {
writeError(value: string): void;
}
export function stampFontCompilerVersion(html: string, version: string): string {
const safeVersion = version.replace(/[^A-Za-z0-9.+-]/g, "") || "unknown";
const tag = `<meta name="hyperframes-font-compiler-version" content="${safeVersion}">`;
const headClose = html.search(/<\/head\s*>/i);
if (headClose >= 0) return `${html.slice(0, headClose)}${tag}${html.slice(headClose)}`;
const doctype = /^\s*<!doctype[^>]*>/i.exec(html);
if (!doctype) return `${tag}${html}`;
const insertAt = doctype.index + doctype[0].length;
return `${html.slice(0, insertAt)}${tag}${html.slice(insertAt)}`;
}
function safeErrorName(error: unknown): string {
const name = error instanceof Error ? error.name : "UnknownError";
return /^[A-Za-z][A-Za-z0-9]*$/.test(name) ? name : "Error";
+7 -4
View File
@@ -1,6 +1,7 @@
// fallow-ignore-file unused-file
import { injectDeterministicFontFaces } from "@hyperframes/producer";
import { runFontLocalize } from "./fontLocalize.js";
import { runFontLocalize, stampFontCompilerVersion } from "./fontLocalize.js";
import { VERSION } from "./version.js";
async function readStdin(): Promise<string> {
const chunks: Buffer[] = [];
@@ -18,10 +19,12 @@ export async function main(): Promise<number> {
writeOutput: (value) => process.stdout.write(value),
writeError: (value) => process.stderr.write(value),
},
(html) =>
injectDeterministicFontFaces(html, {
async (html) => {
const localized = await injectDeterministicFontFaces(html, {
failClosedFontFetch: true,
allowSystemFontCapture: false,
}),
});
return stampFontCompilerVersion(localized, VERSION);
},
);
}