@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 07:15 UTC

main
mjs 87 lines 3.47 KB
Raw
1 import { readFileSync } from "node:fs";
2 // Screenshot sections of a release post for tweets.
3 //
4 // bun screenshot-sections.mjs <post-url> <out-dir> <shots.json>
5 //
6 // shots.json maps an image name to the blocks to keep, in order:
7 //
8 // { "01-effect-41": [["effect-41", "h"], ["effect-41", 1], ["effect-41", 3]] }
9 //
10 // Each entry is [heading id, "h" | n]: "h" is the heading itself, n is the
11 // n-th top-level block after it (0-based, counting paragraphs). Every other
12 // block on the page is hidden, so the image shows the heading and its code,
13 // diagram or table without the prose between them. Run with --list to print
14 // each heading's blocks and their indexes.
15 import { chromium } from "playwright-core";
16
17 const [url, out, spec] = process.argv.slice(2);
18 const browser = await chromium.launch();
19 const page = await browser.newPage({
20 viewport: { width: 1100, height: 1000 },
21 deviceScaleFactor: 2,
22 colorScheme: "dark",
23 reducedMotion: "reduce", // animated figures render their final frame
24 });
25
26 if (out === "--list") {
27 await page.goto(url, { waitUntil: "networkidle" });
28 const rows = await page.evaluate(() => {
29 const lines = [];
30 for (const k of document.querySelector(".sl-markdown-content").children) {
31 const h = k.matches(".sl-heading-wrapper") && k.querySelector("h2,h3,h4");
32 if (h) (lines.push(`${h.tagName} ${h.id}`), (lines.n = 0));
33 else if (lines.length) {
34 const text = k.textContent.trim().replace(/\s+/g, " ").slice(0, 40);
35 lines.push(` ${lines.n++}: ${k.tagName.toLowerCase()} ${text}`);
36 }
37 }
38 return lines;
39 });
40 console.log(rows.join("\n"));
41 await browser.close();
42 process.exit(0);
43 }
44
45 const shots = JSON.parse(readFileSync(spec, "utf8"));
46 for (const [name, keep] of Object.entries(shots)) {
47 await page.goto(url, { waitUntil: "networkidle" });
48 await page.addStyleTag({
49 content: `
50 header.header, .sidebar-pane, .right-sidebar-container, mobile-starlight-toc,
51 .sl-anchor-link, .copy, footer { display: none !important }
52 :root { --sl-content-width: 54rem }
53 .main-pane { width: 100% !important }
54 .main-frame { padding-inline-start: 0 !important; padding-top: 0 !important }
55 .expressive-code pre { overflow: visible !important }
56 .sl-markdown-content > * { margin-top: 1.25rem !important }
57 .sl-markdown-content > .sl-heading-wrapper,
58 .sl-markdown-content .sl-heading-wrapper h2 { border-top: 0 !important; padding-top: 0 !important }
59 `,
60 });
61 const clip = await page.evaluate((keep) => {
62 const root = document.querySelector(".sl-markdown-content");
63 const kids = [...root.children];
64 const indexes = keep.map(([id, i]) => {
65 const heading = document.getElementById(id);
66 if (!heading) throw new Error(`no heading #${id}`);
67 const at = kids.indexOf(heading.closest(".sl-heading-wrapper"));
68 return i === "h" ? at : at + 1 + i;
69 });
70 kids.forEach((k, i) => {
71 if (!indexes.includes(i)) k.style.display = "none";
72 });
73 const first = kids[Math.min(...indexes)].getBoundingClientRect();
74 const last = kids[Math.max(...indexes)].getBoundingClientRect();
75 const box = root.getBoundingClientRect();
76 return {
77 x: box.left - 32,
78 y: Math.max(0, first.top + scrollY - 32),
79 width: box.width + 64,
80 height: last.bottom - first.top + 64,
81 };
82 }, keep);
83 await page.waitForTimeout(200);
84 await page.screenshot({ path: `${out}/${name}.png`, fullPage: true, clip });
85 console.log(`${out}/${name}.png`);
86 }
87 await browser.close();