Something went wrong. Try again.
A terminal UI for Libby libbui.vinnymac.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123// Draws public/og.png, the 1200×630 link preview: the live page's libbui// window, full-bleed, on the sample library's shelf.//// The machine that renders it may have no CJK fonts, and the shelf has// Chinese titles, so Noto Sans SC is loaded as a webfont under the family// the terminal already asks for. Only its CJK ranges are used: Latin text// and box drawing keep the monospace font, or the grid would drift.//// npm run build && npm run serve &// npm install --no-save playwright-core@1.62.1 @fontsource/noto-sans-sc@5.3.1// node scripts/og.mjs//// Settings, all optional:// OG_URL the page to capture (default http://127.0.0.1:5179/)// PLAYWRIGHT_CORE path to playwright-core's index.mjs, if not installed here// NOTO_SANS_SC path to @fontsource/noto-sans-sc, if not installed here// CHROME a Chromium to launch instead of Playwright's ownimport { readFile } from "node:fs/promises";import { createRequire } from "node:module";import { join } from "node:path";import { fileURLToPath } from "node:url";
const URL_TO_CAPTURE = process.env.OG_URL ?? "http://127.0.0.1:5179/";const OUT = fileURLToPath(new URL("../public/og.png", import.meta.url));const require = createRequire(import.meta.url);
const { chromium } = await import(process.env.PLAYWRIGHT_CORE ?? "playwright-core");const fontDir = process.env.NOTO_SANS_SC ?? join(require.resolve("@fontsource/noto-sans-sc/package.json"), "..");
/** Everything below this is Latin, symbols, box drawing and arrows. */const CJK_START = 0x2e80;const FAMILY = "Noto Sans Mono CJK SC";
/** Keeps the parts of a `unicode-range` list at or above CJK_START. */function cjkOnly(ranges) { const kept = []; for (const item of ranges.split(",")) { const match = /^U\+([0-9a-f]+)(?:-([0-9a-f]+))?$/i.exec(item.trim()); if (match === null) throw new Error(`unexpected unicode-range item ${item}`); const start = Number.parseInt(match[1], 16); const end = match[2] === undefined ? start : Number.parseInt(match[2], 16); if (end < CJK_START) continue; const from = Math.max(start, CJK_START); kept.push(from === end ? `U+${from.toString(16)}` : `U+${from.toString(16)}-${end.toString(16)}`); } return kept.join(",");}
/** @font-face rules for the CJK subsets, pointing at /og-fonts/ on the page's origin. */async function fontFaces() { const css = await readFile(join(fontDir, "400.css"), "utf8"); const rules = []; for (const [, body] of css.matchAll(/@font-face\s*{([^}]*)}/g)) { const file = /url\(\.\/files\/([^)]+\.woff2)\)/.exec(body)?.[1]; const range = /unicode-range:\s*([^;]+);/.exec(body)?.[1]; if (file === undefined || range === undefined) throw new Error(`unexpected @font-face rule: ${body}`); const cjk = cjkOnly(range); if (cjk === "") continue; rules.push( `@font-face { font-family: "${FAMILY}"; font-weight: 400; font-display: block; ` + `src: url(/og-fonts/${file}) format("woff2"); unicode-range: ${cjk}; }`, ); } if (rules.length === 0) throw new Error(`no CJK subsets found in ${fontDir}`); return rules.join("\n");}
const browser = await chromium.launch({ executablePath: process.env.CHROME, args: ["--enable-features=WebAssemblyJSPromiseIntegration"],});try { const page = await browser.newPage({ viewport: { width: 1200, height: 630 }, colorScheme: "dark" }); const errors = []; page.on("pageerror", (error) => errors.push(error)); // Same origin, so the page's font-src 'self' allows it. await page.route("**/og-fonts/*", async (route) => { const name = new URL(route.request().url()).pathname.split("/").pop() ?? ""; if (!/^[\w.-]+\.woff2$/.test(name)) throw new Error(`refusing font request ${name}`); await route.fulfill({ body: await readFile(join(fontDir, "files", name)), contentType: "font/woff2", }); }); // xterm measures each glyph the first time it draws it, so the CJK font // has to be loaded before the shelf appears, not after. await page.addInitScript( ({ css, family }) => { document.addEventListener("DOMContentLoaded", () => { const style = document.createElement("style"); style.textContent = css; document.head.append(style); void document.fonts.load(`14px "${family}"`, "三体沙丘刘慈欣"); }); }, { css: `${await fontFaces()} body > :not(main), main > :not(#try), #try > :not(.window), .window-bar, .keys { display: none !important; } .window { position: fixed; inset: 0; border: 0; border-radius: 0; box-shadow: none; } .screen { block-size: 100vh; }`, family: FAMILY, }, ); await page.goto(URL_TO_CAPTURE);
await page.waitForFunction(() => document.getElementById("terminal")?.classList.contains("live"), undefined, { timeout: 180_000, }); // The sample library draws its covers a moment after the shelf. await page.waitForTimeout(6000); const loaded = await page.evaluate( async (family) => (await document.fonts.load(`14px "${family}"`, "三体沙丘")).length, FAMILY, ); if (loaded === 0) throw new Error(`${FAMILY} did not load; the preview would show boxes`); await page.evaluate(() => document.fonts.ready); if (errors.length > 0) throw new AggregateError(errors, "the page threw while starting"); await page.screenshot({ path: OUT }); console.log(`wrote ${OUT}`);} finally { await browser.close();}