1 import { mkdir, writeFile } from "node:fs/promises"
2 import { resolve } from "node:path"
3
4 import { bannerOf, type BannerInput, type BannerType } from "./banners/index.js"
5 import { renderPng } from "./render.js"
6
7 const STYLES = `
8 :root { color-scheme: dark }
9 body {
10 margin: 0; padding: 48px; background: #0c0c0e; color: #e4e4e7;
11 font: 15px/1.6 ui-sans-serif, system-ui, sans-serif;
12 }
13 main { max-width: 1200px; margin: 0 auto }
14 h1 { font-size: 22px; margin: 0 0 4px }
15 h2 { font-size: 16px; margin: 48px 0 12px; font-weight: 600 }
16 p { color: #a1a1aa; margin: 0 0 8px }
17 img { display: block; width: 100%; border-radius: 10px }
18 figure { margin: 0 0 24px }
19 figcaption {
20 font: 12px ui-monospace, monospace; color: #a1a1aa;
21 margin-bottom: 8px; letter-spacing: .04em; text-transform: uppercase;
22 }
23 .grid { display: grid; gap: 20px; grid-template-columns: 1fr 1fr }
24 .diff { position: relative; border-radius: 10px; overflow: hidden;
25 background: #000 }
26 .diff img { position: absolute; inset: 0; height: 100% }
27 /* Stacked and multiplied, so any misalignment glows. */
28 .diff img + img { mix-blend-mode: difference }
29 .boost { filter: brightness(6) }
30 label { display: inline-flex; gap: 8px; align-items: center;
31 color: #a1a1aa; font-size: 13px }
32 `
33
34 const SCRIPT = `
35 const boost = document.querySelector("#boost")
36 const diff = document.querySelector(".diff")
37 boost?.addEventListener("change", () =>
38 diff.classList.toggle("boost", boost.checked))
39 `
40
41 function escapeHtml(value: string): string {
42 return value
43 .replace(/&/g, "&")
44 .replace(/</g, "&lt;")
45 .replace(/>/g, "&gt;")
46 .replace(/"/g, "&quot;")
47 }
48
49 function figure(caption: string, src: string): string {
50 const safe = escapeHtml(caption)
51
52 return (
53 `<figure><figcaption>${safe}</figcaption>` +
54 `<img src="${escapeHtml(src)}" alt="${safe}"></figure>`
55 )
56 }
57
58 type Rendered = { caption: string; file: string }
59
60 /**
61 * Builds the review page.
62 *
63 * The reference comparison is only rendered for types whose design reproduces
64 * an existing banner; the rest get the grid alone.
65 */
66 function page({
67 type,
68 reference,
69 rendered,
70 }: {
71 type: BannerType
72 reference?: { url: string; caption: string; file: string }
73 rendered: Rendered[]
74 }): string {
75 const others = rendered
76 .filter(({ file }) => file !== reference?.file)
77 .map(({ caption, file }) => figure(caption, file))
78 .join("\n ")
79
80 const comparison = reference
81 ? `<h2>Reference vs generated (${escapeHtml(reference.caption)})</h2>
82 <div class="grid">
83 ${figure("reference", reference.url)}
84 ${figure("generated", reference.file)}
85 </div>
86
87 <h2>Difference overlay</h2>
88 <p>Black means identical.
89 <label><input type="checkbox" id="boost"> amplify 6&times;</label></p>
90 <div class="diff">
91 <img src="${escapeHtml(reference.url)}" alt="reference">
92 <img src="${escapeHtml(reference.file)}" alt="generated">
93 </div>
94
95 <h2>Other banners</h2>`
96 : `<h2>Banners</h2>`
97
98 return `<!doctype html>
99 <html lang="en">
100 <head>
101 <meta charset="utf-8">
102 <meta name="viewport" content="width=device-width, initial-scale=1">
103 <title>${type} banner preview</title>
104 <style>${STYLES}</style>
105 </head>
106 <body>
107 <main>
108 <h1>${escapeHtml(type)} banner preview</h1>
109 <p>Rendered with satori and resvg.</p>
110
111 ${comparison}
112 ${others}
113 </main>
114 <script>${SCRIPT}</script>
115 </body>
116 </html>
117 `
118 }
119
120 /**
121 * Renders a spread of banners of one type and writes an HTML page for eyeballing
122 * them, including a side-by-side and a difference overlay for types that have a
123 * reference banner to reproduce.
124 *
125 * @param inputs Banners to render *alongside* the reference, which is always
126 * included when the type has one — the comparison at the top of the page is
127 * the point of it, and it renders as a broken image if that banner is missing.
128 * @returns Path to the written page.
129 */
130 export async function writePreview({
131 type,
132 outDir,
133 inputs,
134 onRender,
135 }: {
136 type: BannerType
137 outDir: string
138 inputs?: BannerInput[]
139 onRender?: (file: string) => void
140 }): Promise<string> {
141 await mkdir(outDir, { recursive: true })
142
143 const definition = bannerOf(type)
144 const { reference } = definition.preview
145 const queue = [
146 ...(reference ? [reference.input as BannerInput] : []),
147 ...(inputs?.length ? inputs : (definition.preview.inputs as BannerInput[])),
148 ]
149
150 const rendered: Rendered[] = []
151 const seen = new Set<string>()
152
153 for (const input of queue) {
154 const normalized = definition.normalize(input)
155 const caption = definition.label(normalized)
156 const file = `${caption}.png`
157
158 if (seen.has(file)) {
159 continue
160 }
161
162 seen.add(file)
163
164 await writeFile(resolve(outDir, file), await renderPng(input))
165
166 rendered.push({ caption, file })
167 onRender?.(file)
168 }
169
170 const out = resolve(outDir, "preview.html")
171
172 await writeFile(
173 out,
174 page({
175 type,
176 reference: reference && {
177 url: reference.url,
178 caption: rendered[0].caption,
179 file: rendered[0].file,
180 },
181 rendered,
182 })
183 )
184
185 return out
186 }