#!/usr/bin/env bun
/**
* The public site: markdown in, HTML out, one template and one stylesheet, both ours.
*
* Hand-rolled rather than a site framework because the site is small, the repository is
* TypeScript, and a look of our own is the point: a generator we own is less to carry than a
* theme we would spend the same effort overriding.
*
* The tool reference is rendered from the tool definitions themselves, so it cannot drift from
* what the server answers, and it is never committed.
*/
import { cpSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import process from 'node:process';
import { marked, type Tokens } from 'marked';
import { displayPath, HARNESSES, type Harness } from '../src/harnesses.js';
import { runLine } from '../src/runner.js';
import { SERVER_VERSION } from '../src/server-version.js';
import { requiredSetsSaid, TOOL_SPECS } from '../src/tool-definitions.js';
import { argumentShape, renderToolsMarkdown, SHARED_PARAGRAPHS } from '../src/tool-reference.js';
import { HeadingIds } from './doc-slug.js';
const DOCS = 'docs';
const THEME = join(DOCS, 'theme');
const OUT = 'site';
/** Counted rather than written down, so a tool added or dropped cannot leave the prose stale. */
const TOOL_COUNT = String(TOOL_SPECS.length);
/**
* Every harness gdharness can write itself into, as a markdown table.
*
* From the same table `gdharness setup` reads, so the page cannot list a harness the command
* does not know or miss one it does.
*/
function renderHarnesses(): string {
// Three columns, and scope is the heading above each table rather than a fourth. A column whose
// every cell reads the same is a column that costs width and says nothing, and with six of them
// the flags and the paths were breaking across lines in the middle of a word.
const table = (scope: Harness['scope']): string[] => [
'| Harness | Flag | Config |',
'| --- | --- | --- |',
...HARNESSES.filter((harness) => harness.scope === scope).map(
(harness) => `| ${harness.name} | \`--${harness.id}\` | \`${displayPath(harness, 'linux')}\` |`,
),
];
const project = HARNESSES.filter((harness) => harness.scope === 'project').length;
return [
`### Inside the project (${project})`,
'',
...table('project'),
'',
`### No project-level config (${HARNESSES.length - project})`,
'',
'Setting one of these up writes in your home directory and affects every project you open with',
'it, so it happens only when you name it by flag or answer yes at a terminal.',
'',
...table('home'),
].join('\n');
}
/** How setup decides what to write: one command in, three ways out, one thing at the end. */
const FLOW = [
{
when: 'Harnesses named by flag',
writes: 'Exactly those, and nothing is asked.',
},
{
when: 'None named, a terminal to ask at',
writes: 'It asks about each harness it finds here or on this machine, one at a time.',
},
{
when: 'None named, nobody to ask',
writes: 'The harnesses this project already uses, and it names the rest with the flag for each.',
},
] as const;
/**
* The one diagram on the site, drawn in elements rather than in characters.
*
* It was ASCII art in a code block, which is how a flow chart is usually written down and is not
* how one should be published: the box-drawing glyphs are not in the mono face, so the browser
* fetched them from whatever font had them and the verticals landed off their columns, and even
* once that was ASCII the lines could not join up at any line height a browser reads code at.
* Boxes and rules are elements the stylesheet can place exactly, in the theme, at any width.
*/
function renderFlow(): string {
const lanes = FLOW.map((step) =>
[
'
',
`
${escaped(step.when)}
`,
`
${escaped(step.writes)}
`,
'
',
].join(''),
);
return [
'
',
'
gdharness setup
',
`
${lanes.join('')}
`,
'
then the skill, unless --no-skill
',
'
',
].join('\n');
}
/** The same three ways out, for the twin an agent reads, where a diagram is nothing. */
function renderFlowText(): string {
return [
'| When you run `gdharness setup` | What it writes |',
'| --- | --- |',
...FLOW.map((step) => `| ${step.when} | ${step.writes} |`),
'',
'Every one of them ends by writing the skill, unless `--no-skill`.',
].join('\n');
}
/**
* The same command under both runners, one line each.
*
* Either works, so showing one and mentioning the other in prose leaves half the readers doing a
* translation in their head.
*/
function bothRunners(rest: string): string {
return [runLine('npx', SERVER_VERSION, rest), runLine('bunx', SERVER_VERSION, rest)].join('\n');
}
/**
* Every harness, by name.
*
* All of them rather than a chosen few, and alphabetical rather than in table order: any shorter
* list is somebody's judgement about whose harness matters, and the table's own order puts the
* four that share `.mcp.json` first, which would read as a ranking it is not.
*/
function pickable(): readonly Harness[] {
return [...HARNESSES].toSorted((left, right) => left.name.localeCompare(right.name, 'en'));
}
/**
* The install picker: choose a harness, read the one command and the one file it writes.
*
* Radio inputs and sibling selectors, so it works with no JavaScript at all, and generated from
* the harness table so a panel cannot describe a harness `setup` does not know.
*/
function renderPicker(): string {
const chosen = pickable();
const inputs = chosen.map(
(harness, index) =>
``,
);
const chips = chosen.map((harness) => ``);
const panels = chosen.map((harness) => {
const command = bothRunners(`setup --${harness.id}`);
const where =
harness.scope === 'project'
? `Writes ${escaped(displayPath(harness, 'linux'))} inside the project.`
: `${escaped(harness.name)} has no project-level config, so this writes ${escaped(displayPath(harness, 'linux'))} and affects every project you open with it.`;
const how =
harness.snippet === undefined
? ''
: ' Its file is documented but the key it holds servers under is not, so this prints the block rather than guessing.';
const left = harness.manual === undefined ? '' : ` ${escaped(harness.manual)}`;
const skill = harness.skills === undefined ? '.agents/skills' : harness.skills.dir.replaceAll('\\', '/');
return [
`
`,
`
${codeText(command)}
`,
`
${where}${how} The skill goes to ${escaped(skill)}.${left}
`,
'
',
].join('');
});
// Last, and deliberately at the same level as the rest: the reader whose harness is not here is
// not out of luck, because the thing being installed is an ordinary MCP server.
inputs.push('');
chips.push('');
panels.push(
[
'
',
`
${codeText(bothRunners('setup'))}
`,
'
Any MCP client that can spawn a local stdio server will do. With no harness named it asks about the ones it finds, and the entry it writes is the same everywhere. How it works has every file and key, and says which ones it cannot write for you.
',
'
',
].join(''),
);
// The rules tying each input to its chip and its panel are generated with them, so a harness
// added to the table cannot outrun a hand-maintained stylesheet.
const rules = [...chosen.map((harness) => harness.id), 'other'].flatMap((id) => [
`#pick-${id}:checked ~ .panels .panel-${id}{display:block}`,
`#pick-${id}:checked ~ .chips label[for="pick-${id}"]{color:var(--signal-ink);background:var(--signal);border-color:var(--signal)}`,
`#pick-${id}:focus-visible ~ .chips label[for="pick-${id}"]{outline:2px solid var(--signal);outline-offset:2px}`,
]);
return [
'
',
``,
...inputs,
`
${chips.join('')}
`,
`
${panels.join('')}
`,
'
',
].join('\n');
}
/** The same choice as plain markdown, for the twin an agent reads. */
function renderPickerText(): string {
const rows = pickable().map(
(harness) => `| ${harness.name} | \`--${harness.id}\` | \`${displayPath(harness, 'linux')}\` |`,
);
return [
'Every harness takes the same command with its own flag, under either runner:',
'',
'```bash',
bothRunners('setup --'),
'```',
'',
'Leave the flag off and it asks about the ones it finds.',
'',
'| Harness | Flag | Writes |',
'| --- | --- | --- |',
...rows,
'',
'Not listed is not unsupported: any MCP client that can spawn a local stdio server will do.',
].join('\n');
}
/**
* Whether this harness reads the shared skills directory, or needs its own copy.
*
* A harness that names no directory of its own reads the shared one, which is the common case and
* the reason the convention exists.
*/
function readsSharedSkills(harness: Harness): boolean {
return harness.skills === undefined || harness.skills.shared;
}
/**
* A count of the table, for prose that would otherwise state one and be wrong later.
*
* Both numbers this replaced were written when there were eleven harnesses and never raised:
* "eight have no project-level config" when it was eleven, and "nine read the shared skills
* directory" when it was thirty-two of thirty-five.
*/
function counted(fits: (harness: Harness) => boolean): number {
return HARNESSES.filter(fits).length;
}
/**
* The placeholders every page carries, so a command on the page is one the reader can run.
*
* The markdown twin gets the same facts without the markup: a reader who asked for markdown is
* usually an agent, and a picker made of radio inputs is nothing to it.
*/
function filled(text: string, markup = true): string {
return text
.replaceAll('{{version}}', SERVER_VERSION)
.replaceAll('{{tools}}', TOOL_COUNT)
.replaceAll('{{harness-count}}', String(HARNESSES.length))
.replaceAll('{{home-count}}', String(counted((harness) => harness.scope === 'home')))
.replaceAll('{{shared-skill-count}}', String(counted(readsSharedSkills)))
.replaceAll('{{own-skill-count}}', String(counted((harness) => !readsSharedSkills(harness))))
.replaceAll('{{mcp-json-count}}', String(counted((harness) => harness.file === '.mcp.json')))
.replaceAll('{{harnesses}}', renderHarnesses())
.replaceAll('{{picker}}', markup ? renderPicker() : renderPickerText())
.replaceAll('{{flow}}', markup ? renderFlow() : renderFlowText());
}
/** The three things gdharness is made of, and where each one is documented. */
const PARTS = [
{
kicker: 'Inside Godot',
title: 'Four addons',
body: 'They make the open editor and the running game answerable, reload the editor when files change on disk, and give test suites waits, steps and input.',
link: 'architecture.html',
linkText: 'How it works',
},
{
kicker: 'The server',
title: `${TOOL_COUNT} tools`,
body: 'Named domain_verb, with four godot:// resources. An unknown op is refused with the valid set listed.',
link: 'tools.html',
linkText: 'Tool reference',
},
{
kicker: 'The CLI',
title: 'One command',
body: 'Addons in, plugins on, class list rebuilt, the skill written, and the server registered with your harness.',
link: 'install.html',
linkText: 'Install',
},
];
/**
* The front page: a statement, the two ways in, and three cards that route you onward.
*
* Built here rather than from markdown because a hero and a row of cards are not prose, and
* writing them as raw HTML inside a markdown file would be the worst of both.
*/
function renderHome(): string {
const paste = [
'Install gdharness here by following https://aureliolo.github.io/gdharness/agent.md,',
'then tell me what it asked you to recommend back to me.',
].join('\n');
const cards = PARTS.map((part) =>
[
'
Your agent can see the editor you have open, the game that is running, and the project on disk, and it can change all three.
',
'',
`
${cards.join('')}
`,
'',
'
Hand it to your agent
',
`paste this
${codeText(paste)}
`,
'
It reads the guide, installs the addons, writes the config for the harness it is running in, and reports the two things it cannot do for itself.
',
'
Or do it yourself
',
renderPicker(),
'
With no harness named it asks about each one it finds, here or on this machine, and writes nothing outside the project directory without a flag or a typed yes. How it works has every harness it knows.
',
'',
'
Fork of GoPeak v2.3.9, September 2026, MIT: the original MCP server godot-mcp by Solomon Elias, GoPeak by HaD0Yun, and completely reworked since to be hardened, condensed and more streamlined. Not affiliated with GoPeak or the Godot Foundation.
',
].join('\n');
}
/** The same page as markdown, which is what an agent is pointed at. */
function renderHomeText(): string {
return [
'# gdharness',
'',
'Drive a Godot 4 project from an agent: the editor that is open, the game that is running, and',
'the project on disk. An agent cannot see a running game. With gdharness it can ask one.',
'',
'Godot 4.7 or newer, and Node 22 or newer for `npx`. It runs under Bun 1.4 too.',
'',
'## Install',
'',
'```text hand this to an agent',
'Install gdharness here by following https://aureliolo.github.io/gdharness/agent.md,',
'then tell me what it asked you to recommend back to me.',
'```',
'',
'Or run it yourself. It is the same command every time; what changes is the file it writes.',
'',
'{{picker}}',
'',
'With no harness named it asks about each one it finds, here or on this machine, and writes',
'nothing outside the project directory without a flag or a typed yes.',
'',
'## Three parts',
'',
'**Inside Godot.** Four addons in your project. They make the open editor and the running game',
'answerable, reload the editor when files change on disk, and give test suites waits, steps and',
'input.',
'',
`**The MCP server.** ${TOOL_COUNT} tools named \`domain_verb\`, and four \`godot://\` resources. An`,
'unknown op or argument is refused with the valid set listed, and every answer is read back from',
'the engine rather than echoed from the request.',
'',
'**The CLI.** Installs the addons, writes the skill, registers the server, checks all of it, and',
'takes it back out again.',
'',
'## Pages',
'',
'- [Install](install.md): install, verify, update, uninstall.',
'- [How it works](architecture.md): what an install writes, how it decides, and what talks to',
' what once it is running.',
'- [Test runs](test-runs.md): `gdharness test`, what fails a run, and the engine slots.',
'- [Spread benches](spread.md): `gdharness spread`, a bench over several engines gathered back whole.',
'- [Tools](tools.md): every tool, op and argument.',
'',
'## Project',
'',
'Fork of [GoPeak](https://github.com/HaD0Yun/Doyunha-Gopeak) v2.3.9, September 2026, MIT: the',
'original MCP server [godot-mcp](https://github.com/Coding-Solo/godot-mcp) by',
'[Solomon Elias](https://github.com/Coding-Solo), GoPeak by [HaD0Yun](https://github.com/HaD0Yun),',
'and completely reworked since to be hardened, condensed and more streamlined. Not affiliated with',
'GoPeak or the Godot Foundation.',
'',
].join('\n');
}
/** Where the site lives, for the canonical links and llms.txt. */
const SITE_URL = 'https://aureliolo.github.io/gdharness';
interface Page {
/** The markdown file under docs/, or nothing for a page this script writes itself. */
readonly source?: string;
/** Where the rendered page lands, which is also its link. */
readonly path: string;
/** The same page as plain markdown, which is what an agent is pointed at. */
readonly text: string;
/** In the browser tab, and in the navigation unless `nav` says otherwise. */
readonly title: string;
/** What the navigation calls it, where the title would read oddly in a row of links. */
readonly nav?: string;
/** One line for llms.txt, which is the index an agent reads first. */
readonly summary: string;
/** The front page is not a page of documentation and is not laid out like one. */
readonly home?: boolean;
readonly render?: () => string;
readonly renderText?: () => string;
}
const PAGES: readonly Page[] = [
{
path: 'index.html',
text: 'index.md',
title: 'gdharness',
// The wordmark beside it already says gdharness, and the same word twice in a row of links
// reads as a mistake rather than as a destination.
nav: 'Overview',
summary: 'What gdharness is, what it is made of, and the rules it is built to.',
home: true,
render: renderHome,
renderText: renderHomeText,
},
{
source: 'install.md',
path: 'install.html',
text: 'install.md',
title: 'Install',
summary: 'Installing and configuring it, step by step, with what to check after each.',
},
{
source: 'architecture.md',
path: 'architecture.html',
text: 'architecture.md',
title: 'How it works',
summary: 'The parts, what connects to what, the ports, and which tools use which.',
},
{
source: 'test-runs.md',
path: 'test-runs.html',
text: 'test-runs.md',
title: 'Test runs',
summary: 'gdharness test: running and judging the suites of a game, and the engine slots it shares.',
},
{
source: 'spread.md',
path: 'spread.html',
text: 'spread.md',
title: 'Spread benches',
summary: 'gdharness spread: a bench played over several engines and gathered back in run order.',
},
{
path: 'tools.html',
text: 'tools.md',
title: 'Tools',
summary: 'Every tool, every op and every argument, generated from the server itself.',
render: renderTools,
renderText: renderToolsMarkdown,
},
];
/**
* The one page written for an agent rather than for a reader, and the only one with no rendered
* twin: it is published as markdown, listed in llms.txt, and left out of the navigation, because
* a person who lands on it has been sent the wrong link.
*/
const AGENT_GUIDE = {
source: 'agent.md',
text: 'agent.md',
summary: 'Do this to install gdharness into a project. Terse, imperative, for an agent.',
} as const;
/** The ids taken by the headings of the page being rendered, new for each page. */
let pageIds = new HeadingIds();
marked.use({
renderer: {
heading(this: { parser: { parseInline: (tokens: Tokens.Generic[]) => string } }, token: Tokens.Heading) {
const text = this.parser.parseInline(token.tokens);
return `${text}\n`;
},
// A fence says its language and, after it, what the block is: ```bash verify the download.
// The label becomes the bar along the top, which is what tells one block from the next when
// a page is mostly commands.
code(token: Tokens.Code) {
const [language = '', ...rest] = (token.lang ?? '').split(' ');
const label = rest.join(' ');
const bar = label === '' ? '' : `${escaped(label)}`;
codeBlocks.push(token.text);
return `${bar}
${coloured(token.text, language)}
\n`;
},
},
});
/**
* The little colour a block of JSON or shell needs to be read at a glance.
*
* Hand-rolled and deliberately small: three tokens, no library, no theme to keep up with. Anything
* it does not recognise comes out as plain text, which is the right failure for a highlighter.
*/
function coloured(code: string, language: string): string {
const text = escaped(code);
if (language === 'json' || language === 'jsonc') {
return text
.replaceAll(/^(\s*)(\/\/.*)$/gm, '$1$2')
.replaceAll(/"([^&]*?)"(\s*:)/g, '"$1"$2')
.replaceAll(/:(\s*)"([^&]*?)"/g, ':$1"$2"')
.replaceAll(/\b(true|false|null|-?\d+(?:\.\d+)?)\b/g, '$1');
}
if (language === 'bash' || language === 'sh') {
return text
.replaceAll(/(^|\s)(#.*)$/gm, '$1$2')
.replaceAll(/(^|\n)([a-z][\w.-]*)/g, '$1$2')
.replaceAll(/(\s)(--?[a-z][\w-]*)/g, '$1$2');
}
return text;
}
/** Anything that ends up in HTML and did not come from the markdown renderer. */
function escaped(text: string): string {
return text
.replaceAll('&', '&')
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('"', '"');
}
/** One call and the answer it comes back with, which is what the whole reference is a list of. */
const EXAMPLE_CALL = `project_settings {
"projectPath": "/home/you/game",
"op": "set",
"setting": "display/window/size/viewport_width",
"value": 1280
}
{
"setting_path": "display/window/size/viewport_width",
"old_value": 1152,
"new_value": 1280,
"was_new": false,
"saved": true
}
`;
/** The tool reference, from the definitions the server answers with. */
function renderTools(): string {
const parts: string[] = [
// Every other page opens with its own name, because the markdown behind it starts with one.
// This page is built rather than parsed, so it has to say so itself.
'
Tools
\n',
`
${TOOL_SPECS.length} tools, named domain_verb. A tool that does several `,
'related things takes an op. An unknown op or argument is refused with the valid set ',
'listed. Generated from the server.
\n',
'
A call and its answer
\n',
`
${codeText(EXAMPLE_CALL)}
\n`,
'
new_value is read from the engine after the write. Engine stderr comes back ',
'under engine_messages. Every call takes projectPath, except the ',
'runtime_* and debug_* tools, where it picks between running games.
\n',
];
const family = (name: string): string => name.split('_')[0] ?? name;
let current = '';
for (const tool of TOOL_SPECS) {
if (family(tool.name) !== current) {
current = family(tool.name);
parts.push(`
${escaped(current)}
\n`);
}
parts.push(
`\n`,
`
${escaped(tool.name)}
\n`,
);
if (tool.description !== '') {
parts.push(`
${escaped(tool.description)}
\n`);
}
// The answer is read after the call, so it comes after what the call takes.
parts.push(renderOperations(tool), renderArguments(tool));
const answer = tool.answer.split('\n\n').filter((paragraph) => paragraph !== '');
if (answer.length > 0) {
parts.push('
Answer
\n', ...answer.map((paragraph) => `
${escaped(paragraph)}
\n`));
}
// This page states each shared paragraph in every tool that has it, so its detail goes there too.
for (const { text, detail } of SHARED_PARAGRAPHS) {
if (`${tool.description}${tool.answer}`.includes(text)) {
parts.push(`
${escaped(detail)}
\n`);
}
}
parts.push('\n');
}
return parts.join('');
}
type ToolSpec = (typeof TOOL_SPECS)[number];
/** The table of a tool's ops, or the line naming what a tool without ops needs, or nothing. */
function renderOperations(tool: ToolSpec): string {
const operations = Object.entries(tool.operations ?? {});
if (operations.length === 0) {
return tool.requires.length > 0
? `
\n',
].join('');
}
/** A tool's arguments other than `projectPath`, or nothing for a tool with none. */
function renderArguments(tool: ToolSpec): string {
const parameters = Object.entries(tool.parameters).filter(([name]) => name !== 'projectPath');
if (parameters.length === 0) {
return '';
}
const rows = parameters.map(([name, schema]) => {
const type = argumentShape(schema);
const note = typeof schema['description'] === 'string' ? schema['description'] : '';
return (
`
${escaped(name)}${escaped(type)}
` +
`
${escaped(note)}
\n`
);
});
return ['Arguments\n
\n', ...rows, '
\n\n'].join(
'',
);
}
/**
* The one navigation, on every page and listing every page including the front one.
*
* It is the page list, so a page outside it is one nothing links to, and there is nowhere for a
* second copy to drift out of step with the first.
*/
function renderTopLinks(current: string): string {
return PAGES.map((page) => {
const here = page.path === current ? ' class="on" aria-current="page"' : '';
return `${escaped(page.nav ?? page.title)}`;
}).join('\n ');
}
/**
* The index an agent reads before it reads anything else.
*
* Every link goes to the markdown rather than to the rendered page, and the first one is the guide
* written for an agent rather than for a reader.
*/
function renderLlmsTxt(): string {
const lines = [
'# gdharness',
'',
'> A harness for driving a Godot 4 project from an agent: four addons that make the editor',
'> and the running game answerable and help its test suites, a server that puts them in front of an agent as tools,',
'> engine operations that answer with nothing open, and a command line that installs it all.',
'',
`Version ${SERVER_VERSION}. Every release is signed and carries a build attestation, and the`,
'install guide below refuses to go on without checking it.',
'',
'## Start here',
'',
`- [Install it into a project](${SITE_URL}/${AGENT_GUIDE.text}): ${AGENT_GUIDE.summary}`,
'',
'## Docs',
'',
];
for (const page of PAGES) {
lines.push(`- [${page.title}](${SITE_URL}/${page.text}): ${page.summary}`);
}
lines.push(
'',
'## Optional',
'',
`- [Everything above, in one file](${SITE_URL}/llms-full.txt)`,
'- [Repository](https://github.com/Aureliolo/gdharness)',
'',
);
return lines.join('\n');
}
function build(): void {
// One template for every page, the front one included. It had a second copy of its own for a
// while, and the copy went on linking to a page that had been deleted: the same chrome written
// twice is the same chrome wrong once. What the front page needs instead is a class on the
// body, which its own block in the stylesheet hangs off.
const template = readFileSync(join(THEME, 'page.html'), 'utf8');
// A markdown file nobody listed is a page with no way to reach it, which is worse than one
// that does not exist: it publishes and nothing links to it.
const listed = new Set(
[...PAGES.map((page) => page.source), AGENT_GUIDE.source].filter((source) => source !== undefined),
);
const orphans = readdirSync(DOCS)
.filter((name) => name.endsWith('.md') && name !== 'README.md')
.filter((name) => !listed.has(name));
if (orphans.length > 0) {
throw new Error(`docs/${orphans.join(', docs/')} would publish with nothing linking to them.`);
}
rmSync(OUT, { recursive: true, force: true });
mkdirSync(OUT, { recursive: true });
/** The markdown of one page, filled in, which is what the text twin holds. */
const textOf = (page: { source?: string; renderText?: () => string }): string =>
filled(
page.renderText !== undefined ? page.renderText() : readFileSync(join(DOCS, page.source ?? ''), 'utf8'),
false,
);
for (const page of PAGES) {
// Substituted before parsing, not after, so that a generated table arrives as markdown and
// is rendered as a table rather than dropped into the page as its own source.
pageIds = new HeadingIds();
const rendered =
page.render !== undefined
? page.render()
: marked.parse(filled(readFileSync(join(DOCS, page.source ?? ''), 'utf8')), { async: false });
// A table is the one thing here that cannot be made to fit a phone, so it gets to scroll
// inside its own box rather than taking the page sideways with it. The opening paragraph
// is the lede, which is a presentation decision and so belongs here rather than as markup
// inside the markdown.
const content = rendered
.replaceAll('
', '
')
.replaceAll('
', '
')
.replace(/(<\/h1>\s*)
/, '$1
');
const html = template
.replaceAll('{{title}}', escaped(page.path === 'index.html' ? page.title : `${page.title} ยท gdharness`))
.replaceAll('{{bodyclass}}', page.home === true ? ' class="home"' : '')
.replaceAll('{{description}}', escaped(page.summary))
.replaceAll('{{canonical}}', `${SITE_URL}/${page.path}`)
.replaceAll('{{toplinks}}', renderTopLinks(page.path))
.replaceAll('{{version}}', escaped(SERVER_VERSION))
.replaceAll('{{content}}', content);
writeFileSync(join(OUT, page.path), html, 'utf8');
writeFileSync(join(OUT, page.text), textOf(page), 'utf8');
}
// The guide written for an agent: markdown only, and in no navigation. A person who lands on it
// was sent the wrong link.
writeFileSync(join(OUT, AGENT_GUIDE.text), textOf(AGENT_GUIDE), 'utf8');
cpSync(join(THEME, 'site.css'), join(OUT, 'site.css'));
writeFileSync(join(OUT, 'llms.txt'), renderLlmsTxt(), 'utf8');
// The whole of it in one fetch, for a reader that would rather not make eight.
writeFileSync(
join(OUT, 'llms-full.txt'),
[textOf(AGENT_GUIDE), ...PAGES.map(textOf)].join('\n\n---\n\n'),
'utf8',
);
// GitHub Pages runs Jekyll over what it is given unless told not to, and Jekyll drops every
// file whose name begins with an underscore.
writeFileSync(join(OUT, '.nojekyll'), '', 'utf8');
checkOutput();
console.log(`built ${PAGES.length} pages, ${PAGES.length + 1} text files, into ${OUT}/`);
}
/**
* How wide a line of code may be before it stops fitting the one column.
*
* A block that does not fit either slides sideways, hiding half of itself behind a gesture
* nobody makes, or wraps and loses the alignment it was drawn with. Both are the document's
* fault rather than the stylesheet's, so the build says which line is too long and the document
* is written to fit.
*/
const CODE_COLUMNS = 84;
/**
* Every code block, as the text it was written as, collected while the pages render.
*
* Kept here rather than read back out of the HTML: by then the highlighter has wrapped each token
* in markup and escaped the text, and measuring that means stripping tags and decoding entities,
* which is indistinguishable from a sanitiser that does not work. The source is the thing being
* measured anyway.
*/
const codeBlocks: string[] = [];
/**
* A block of code, escaped for the page and measured on the way through.
*
* Every code block goes through this or through the markdown renderer, so one built by hand in
* this file is as answerable for fitting the column as one written in a document.
*/
function codeText(text: string): string {
codeBlocks.push(text);
return escaped(text);
}
/** Every id a page offers, which is what a `#fragment` pointed at it has to find. */
function anchorsIn(html: string): Set {
return new Set([...html.matchAll(/\sid="([^"]*)"/g)].map((match) => match[1] ?? ''));
}
/** Every link a file makes, whether it is written as HTML, as markdown, or bare in angles. */
function linksIn(text: string): string[] {
const patterns = [
/href="([^"]*)"/g,
/\[[^\]]*\]\(([^)\s]+)\)/g,
/<([\w.-]+\.(?:html|md|txt)(?:#[^>\s]*)?)>/g,
];
return patterns.flatMap((pattern) => [...text.matchAll(pattern)].map((match) => match[1] ?? ''));
}
/**
* Box drawing and block elements, which look like the obvious way to draw a diagram and are not.
*
* IBM Plex Mono has no glyphs in these blocks, so the browser draws them from whatever other font
* it can find, at that font's advance width. The verticals then sit off the columns they were
* aligned to and the horizontals come apart, which is what every one of these diagrams did on the
* live site. ASCII draws the same picture out of characters the face actually has.
*/
function drawingCharactersIn(text: string): string[] {
const found = new Set();
for (const character of text) {
const code = character.codePointAt(0) ?? 0;
// U+2190..U+21FF arrows, U+2500..U+257F box drawing, U+2580..U+259F block elements.
if ((code >= 0x2190 && code <= 0x21ff) || (code >= 0x2500 && code <= 0x259f)) {
found.add(character);
}
}
return [...found];
}
/**
* Text that is not tab, newline or carriage return but is still below a space.
*
* Compared by code point rather than matched by a character class: writing that class means
* putting escapes in a regular expression, and an escape in this file is what an edit can turn
* into a raw control byte.
*/
function controlCharactersIn(text: string): number[] {
const found: number[] = [];
for (let index = 0; index < text.length; index += 1) {
const code = text.charCodeAt(index);
if (code < 0x20 && code !== 0x09 && code !== 0x0a && code !== 0x0d) {
found.push(code);
}
}
return found;
}
/**
* What the build refuses to publish.
*
* Both of these shipped once. A page was deleted while the front page's hand-written link row
* went on pointing at it, and a CSS escape was mangled into a control byte that rendered as an
* empty box on every card. Neither is something a person notices by reading the source, and both
* are one pass over the output to catch.
*/
function checkOutput(): void {
const names = readdirSync(OUT);
const published = new Set(names);
const anchors = new Map>();
for (const name of names.filter((file) => file.endsWith('.html'))) {
anchors.set(name, anchorsIn(readFileSync(join(OUT, name), 'utf8')));
}
const wrong: string[] = codeBlocks.flatMap((block) => block.split('\n')).flatMap(codeLineProblems);
for (const name of names.filter((file) => /\.(?:html|md|txt|css)$/.test(file))) {
const text = readFileSync(join(OUT, name), 'utf8');
for (const code of controlCharactersIn(text)) {
wrong.push(`${name} holds the control character U+${code.toString(16).padStart(4, '0').toUpperCase()}`);
}
for (const link of linksIn(text)) {
const problem = linkProblem(name, link, published, anchors);
if (problem !== null) {
wrong.push(problem);
}
}
}
if (wrong.length > 0) {
throw new Error(`the site would publish broken:\n ${wrong.join('\n ')}`);
}
}
/** What is wrong with a code [param line], one entry per fault. */
function codeLineProblems(line: string): string[] {
const wrong: string[] = [];
if (line.length > CODE_COLUMNS) {
wrong.push(`a code line is ${line.length} characters, ${CODE_COLUMNS} fit: ${line.slice(0, 56)}...`);
}
const drawn = drawingCharactersIn(line);
if (drawn.length > 0) {
wrong.push(
`a code line draws with ${drawn.join(' ')}, which the mono face has no glyphs for: ${line.slice(0, 40)}...`,
);
}
return wrong;
}
/** What is wrong with the [param link] in the page [param name], or null when it resolves. */
function linkProblem(
name: string,
link: string,
published: ReadonlySet,
anchors: ReadonlyMap>,
): string | null {
// Anything with a scheme, and anything protocol-relative, is somebody else's to serve.
if (/^(?:[a-z][a-z0-9+.-]*:|\/\/)/i.test(link)) {
return null;
}
const [target = '', fragment = ''] = link.split('#');
const file = target === '' ? name : target;
if (!published.has(file)) {
return `${name} links to ${link}, and ${file} is not published`;
}
// Only a rendered page has ids to look for. A fragment into markdown is whatever anchor
// the reader's own renderer makes of a heading, which nothing here decides.
const known = anchors.get(file);
return fragment !== '' && known !== undefined && !known.has(fragment)
? `${name} links to ${link}, and ${file} has no id="${fragment}"`
: null;
}
try {
build();
} catch (error) {
console.error(error instanceof Error ? error.message : error);
process.exit(1);
}