1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import {
readdir,
readFile,
mkdir,
rm,
cp,
writeFile,
access,
} from "node:fs/promises";
import path from "node:path";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
const repoRoot = path.resolve(new URL("..", import.meta.url).pathname);
const docsDir = path.join(repoRoot, "docs");
const devlogDir = path.join(repoRoot, "devlog");
const devlogImgDir = path.join(devlogDir, "img");
const siteSrcDir = path.join(repoRoot, "site");
const outDir = path.join(repoRoot, "site-out");
const outDocsDir = path.join(outDir, "docs");
const execFileAsync = promisify(execFile);
async function exists(filePath) {
try {
await access(filePath);
return true;
} catch {
return false;
}
}
function isMarkdown(filePath) {
return filePath.toLowerCase().endsWith(".md");
}
async function listMarkdownFiles(dir, prefix = "") {
const entries = await readdir(dir, { withFileTypes: true });
const out = [];
for (const e of entries) {
if (e.name.startsWith(".")) continue;
const rel = path.join(prefix, e.name);
const abs = path.join(dir, e.name);
if (e.isDirectory()) {
out.push(...(await listMarkdownFiles(abs, rel)));
} else if (e.isFile() && isMarkdown(e.name)) {
out.push(rel.replaceAll(path.sep, "/"));
}
}
return out.sort((a, b) => a.localeCompare(b));
}
function titleFromMarkdown(md, fallback) {
const lines = md.split(/\r?\n/);
for (const line of lines) {
const m = /^#\s+(.+)\s*$/.exec(line);
if (m) return m[1].trim();
}
return fallback.replace(/\.md$/i, "");
}
function normalizeTitle(title) {
let t = String(title || "").trim();
// Strip markdown links: [text](url) -> text
t = t.replace(/\[([^\]]+)\]\([^)]+\)/g, "$1");
// If pages follow a "zat - ..." style, drop the redundant prefix in the nav.
t = t.replace(/^zat\s*-\s*/i, "");
// Cheaply capitalize (keeps the rest as-authored).
if (t.length) t = t[0].toUpperCase() + t.slice(1);
return t;
}
async function getBuildId() {
try {
const { stdout } = await execFileAsync("git", ["rev-parse", "HEAD"], {
cwd: repoRoot,
});
const full = String(stdout || "").trim();
if (full) return full.slice(0, 12);
} catch {
// ignore
}
return String(Date.now());
}
async function main() {
await rm(outDir, { recursive: true, force: true });
await mkdir(outDir, { recursive: true });
// Copy static site shell
await cp(siteSrcDir, outDir, { recursive: true });
// Cache-bust immutable assets on Wisp by appending a per-commit query string.
const buildId = await getBuildId();
const outIndex = path.join(outDir, "index.html");
if (await exists(outIndex)) {
let html = await readFile(outIndex, "utf8");
html = html.replaceAll('href="./style.css"', `href="./style.css?v=${buildId}"`);
html = html.replaceAll(
'src="./vendor/marked.min.js"',
`src="./vendor/marked.min.js?v=${buildId}"`,
);
html = html.replaceAll(
'src="./app.js"',
`src="./app.js?v=${buildId}"`,
);
html = html.replaceAll(
'src="./clients.js"',
`src="./clients.js?v=${buildId}"`,
);
html = html.replaceAll(
'href="./favicon.svg"',
`href="./favicon.svg?v=${buildId}"`,
);
await writeFile(outIndex, html, "utf8");
}
// Copy docs
await mkdir(outDocsDir, { recursive: true });
const pages = [];
// Prefer an explicit docs homepage if present; otherwise use repo README as index.
const docsIndex = path.join(docsDir, "index.md");
if (!(await exists(docsIndex))) {
const readme = path.join(repoRoot, "README.md");
if (await exists(readme)) {
let md = await readFile(readme, "utf8");
// Strip docs/ prefix from links since we're now inside the docs context.
md = md.replace(/\]\(docs\//g, "](");
await writeFile(path.join(outDocsDir, "index.md"), md, "utf8");
pages.push({
path: "index.md",
title: normalizeTitle(titleFromMarkdown(md, "index.md")),
});
}
}
const changelog = path.join(repoRoot, "CHANGELOG.md");
const docsChangelog = path.join(docsDir, "changelog.md");
if ((await exists(changelog)) && !(await exists(docsChangelog))) {
const md = await readFile(changelog, "utf8");
await writeFile(path.join(outDocsDir, "changelog.md"), md, "utf8");
pages.push({
path: "changelog.md",
title: normalizeTitle(titleFromMarkdown(md, "changelog.md")),
});
}
const mdFiles = (await exists(docsDir)) ? await listMarkdownFiles(docsDir) : [];
// handoffs/ holds internal, often git-ignored operator notes — never publish them.
const isInternalDoc = (rel) => rel.startsWith("handoffs/");
// Copy all markdown under docs/ (including archives), but only include non-archive
// paths in the sidebar manifest. Skip internal docs entirely.
for (const rel of mdFiles) {
if (isInternalDoc(rel)) continue;
const src = path.join(docsDir, rel);
const dst = path.join(outDocsDir, rel);
await mkdir(path.dirname(dst), { recursive: true });
await cp(src, dst);
const md = await readFile(src, "utf8");
if (!rel.startsWith("archive/")) {
pages.push({ path: rel, title: normalizeTitle(titleFromMarkdown(md, rel)) });
}
}
// Copy devlog files to docs/devlog/ and generate an index
const devlogFiles = (await exists(devlogDir)) ? await listMarkdownFiles(devlogDir) : [];
const devlogEntries = [];
if (await exists(devlogImgDir)) {
await cp(devlogImgDir, path.join(outDocsDir, "devlog", "img"), {
recursive: true,
});
}
for (const rel of devlogFiles) {
const src = path.join(devlogDir, rel);
const dst = path.join(outDocsDir, "devlog", rel);
await mkdir(path.dirname(dst), { recursive: true });
await cp(src, dst);
// devlog/archive/ holds superseded plans and reports: published so links
// resolve, but not devlog entries.
if (rel.startsWith("archive/")) continue;
// create short-name alias (e.g. 001.md for 001-self-publishing-docs.md)
// so ATProto published paths like /devlog/001 resolve correctly
const base = path.basename(rel, ".md");
const shortName = base.replace(/-.*$/, "");
if (shortName !== base) {
await cp(src, path.join(outDocsDir, "devlog", shortName + ".md"));
}
const md = await readFile(src, "utf8");
devlogEntries.push({
path: `devlog/${rel}`,
title: titleFromMarkdown(md, rel),
});
}
// Generate devlog index listing all entries (newest first by filename)
if (devlogEntries.length > 0) {
devlogEntries.sort((a, b) => b.path.localeCompare(a.path));
const indexMd = [
"# devlog",
"",
...devlogEntries.map((e) => `- [${e.title}](${e.path})`),
"",
].join("\n");
await writeFile(path.join(outDocsDir, "devlog", "index.md"), indexMd, "utf8");
}
// Copy runnable examples' prose to docs/examples/ and generate an index.
// The paired .zig source lives in examples/ and is compile-checked by build.zig;
// only the .md prose is published.
const examplesDir = path.join(repoRoot, "examples");
const exampleFiles = (await exists(examplesDir))
? await listMarkdownFiles(examplesDir)
: [];
const exampleEntries = [];
for (const rel of exampleFiles) {
// index.md is generated below; _-prefixed files (e.g. _overview.md) are
// editable copy for the index, not recipes.
if (rel === "index.md" || rel.startsWith("_")) continue;
const src = path.join(examplesDir, rel);
const dst = path.join(outDocsDir, "examples", rel);
await mkdir(path.dirname(dst), { recursive: true });
await cp(src, dst);
const md = await readFile(src, "utf8");
exampleEntries.push({
path: `examples/${rel}`,
title: titleFromMarkdown(md, rel),
});
}
// Recipes are a deliberate on-ramp sequence. The numeric filename prefix
// (01_, 02_, …) carries that order, so a plain filename sort is enough —
// no example list to maintain here. Only the index goes in the top nav.
if (exampleEntries.length > 0) {
exampleEntries.sort((a, b) => a.path.localeCompare(b.path));
// Optional editable overview prose sits above the generated list.
const overviewPath = path.join(examplesDir, "_overview.md");
const overview = (await exists(overviewPath))
? (await readFile(overviewPath, "utf8")).trim()
: "";
const indexMd = [
"# examples",
"",
...(overview ? [overview, ""] : []),
...exampleEntries.map((e) => `- [${e.title}](${e.path})`),
"",
].join("\n");
await writeFile(path.join(outDocsDir, "examples", "index.md"), indexMd, "utf8");
pages.push({ path: "examples/index.md", title: "Examples", children: exampleEntries });
}
// Stable nav order: README homepage, changelog, examples, then the rest.
pages.sort((a, b) => {
const order = (p) => {
if (p === "index.md") return 0;
if (p === "changelog.md") return 1;
if (p === "examples/index.md") return 2;
return 3;
};
const ao = order(a.path);
const bo = order(b.path);
if (ao !== bo) return ao - bo;
return a.title.localeCompare(b.title);
});
await writeFile(
path.join(outDir, "manifest.json"),
JSON.stringify({ pages }, null, 2) + "\n",
"utf8",
);
process.stdout.write(
`Built Wisp docs site: ${pages.length} markdown file(s) -> ${outDir}\n`,
);
}
await main();