Website: the API portal at /docs/api, with Markdown tables

The repository's docs/api/ (the portal, its surface pages and the generated mod and
server references) is built into the docs section: /docs/api is its README and
/docs/api/<page> each page, in two nav groups, "Build on Horizon" and "API reference".
The pages are read from the checkout or from main on GitHub, like the README, so the
site never holds a copy. Links between portal pages become the site's (and the build's
link check covers them); other relative links go to the file on GitHub.

The renderer gains tables: the "Ways in" table, the references and the other tables
on the site (apis, performance) showed as raw pipes. A link whose label is a code span
no longer throws.

Caddy and the dev server route /docs/api/<page> and redirect /docs/api/ to /docs/api;
Railway rebuilds when docs/api/ changes. /docs/mods links the Mod API, its reference
and its ways in; the summary page is now "APIs at a glance".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-25 15:07:49 +01:00
parent bd591f9857
commit 4a9ca29887
13 changed files with 117 additions and 20 deletions
+1 -1
View File
@@ -46,7 +46,7 @@
<nav class="docs-pager" aria-label="Next and previous pages">
{{DOC_PAGER}}
</nav>
<p class="docs-edit"><a href="https://github.com/ludovic111/horizon/blob/main/website/docs/{{DOC_FILE}}">Edit this page on GitHub</a> · Spotted something wrong? <a href="https://github.com/ludovic111/horizon/issues">Open an issue</a> or ask on <a href="{{DISCORD}}">Discord</a>.</p>
<p class="docs-edit"><a href="https://github.com/ludovic111/horizon/blob/main/{{DOC_FILE}}">Edit this page on GitHub</a> · Spotted something wrong? <a href="https://github.com/ludovic111/horizon/issues">Open an issue</a> or ask on <a href="{{DISCORD}}">Discord</a>.</p>
<p class="disclaimer">Not an official Minecraft product. Not approved by or associated with Mojang or Microsoft.</p>
</main>
</div>
+36 -3
View File
@@ -1,5 +1,5 @@
// A small Markdown renderer for release notes and the README: headings, paragraphs,
// lists (nested by indentation), fenced code, quotes, rules, and inline code, bold,
// A small Markdown renderer for release notes, the README and the docs: headings, paragraphs,
// lists (nested by indentation), fenced code, quotes, rules, tables, and inline code, bold,
// italic and links. Everything is escaped first, so its output never carries raw HTML.
// Shared by build.mjs (Node) and main.js (browser).
@@ -38,12 +38,41 @@ export function inline(text, site = false) {
out = out.replace(/\u0001(\d+)\u0001/g, (_, i) => {
const [label, url] = links[i];
const target = href(url, site);
const body = inline(label, site);
// The label's code spans are already placeholders: put them back for its own pass.
const body = inline(label.replace(/\u0000(\d+)\u0000/g, (_, c) => `\`${codes[c]}\``), site);
return target ? `<a href="${escape(target)}">${body}</a>` : body;
});
return out.replace(/\u0000(\d+)\u0000/g, (_, i) => `<code>${escape(codes[i])}</code>`);
}
// A table: a row of cells, a rule row (`| --- | :-: |`), then body rows, until a line that isn't a row.
const ROW = /^\s*\|.*\|\s*$/;
const RULE = /^\s*\|(\s*:?-+:?\s*\|)+\s*$/;
/** A row's cells: split on `|` outside code spans; `\|` is a literal bar. */
function cells(row) {
const s = row.trim().slice(1, -1);
const out = [""];
let code = false;
for (let i = 0; i < s.length; i++) {
if (s[i] === "\\" && s[i + 1] === "|") out[out.length - 1] += s[++i];
else if (s[i] === "|" && !code) out.push("");
else {
if (s[i] === "`") code = !code;
out[out.length - 1] += s[i];
}
}
return out.map((c) => c.trim());
}
function table(lines, start, site) {
const row = (line, tag) => `<tr>${cells(line).map((c) => `<${tag}>${inline(c, site)}</${tag}>`).join("")}</tr>`;
let i = start + 2;
const body = [];
while (i < lines.length && ROW.test(lines[i])) body.push(row(lines[i++], "td"));
return [`<div class="table-wrap"><table class="md-table"><thead>${row(lines[start], "th")}</thead><tbody>${body.join("")}</tbody></table></div>`, i];
}
const indentOf = (line) => line.match(/^ */)[0].length;
const ITEM = /^(\s*)([-*+]|\d+[.)])\s+(.*)$/;
@@ -131,6 +160,10 @@ export function markdown(source, { shift = 0, site = false, headings = null } =
} else if (/^\s*([-*_])(\s*\1){2,}\s*$/.test(line)) {
out.push("<hr>");
i++;
} else if (ROW.test(line) && RULE.test(lines[i + 1] || "")) {
const [html, end] = table(lines, i, site);
out.push(html);
i = end;
} else if (ITEM.test(line)) {
const [html, end] = list(lines, i, site);
out.push(html);
+25
View File
@@ -0,0 +1,25 @@
// node --test src/: the Markdown the docs pages and release notes are rendered from.
import { test } from "node:test";
import assert from "node:assert/strict";
import { markdown } from "./markdown.js";
test("a table renders with its header, cells and inline Markdown", () => {
const html = markdown("| Way in | Status |\n| --- | :-: |\n| Rust | `cargo horizon-mod` |\n| Lua | planned \\| soon |\n\nAfter.");
assert.equal(
html,
'<div class="table-wrap"><table class="md-table"><thead><tr><th>Way in</th><th>Status</th></tr></thead>' +
"<tbody><tr><td>Rust</td><td><code>cargo horizon-mod</code></td></tr><tr><td>Lua</td><td>planned | soon</td></tr></tbody></table></div>\n<p>After.</p>",
);
});
test("a bar inside a code span stays in its cell", () => {
assert.match(markdown("| a | b |\n|---|---|\n| `x | y` | z |"), /<td><code>x \| y<\/code><\/td><td>z<\/td>/);
});
test("rows without a rule line are a paragraph", () => {
assert.equal(markdown("| a | b |\n| c | d |"), "<p>| a | b | | c | d |</p>");
});
test("a link whose label is code keeps it", () => {
assert.equal(markdown("See [`AGENTS.md`](/docs/agents).", { site: true }), '<p>See <a href="/docs/agents"><code>AGENTS.md</code></a>.</p>');
});
+4
View File
@@ -687,6 +687,10 @@ code { border-radius: 0; background: var(--well); box-shadow: 0 0 0 1px var(--ou
.bench-table td.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.bench-table td.wins { color: var(--ink); font-weight: 600; }
.bench-table td.vs { font-size: var(--text-caption); white-space: nowrap; }
.md-table { width: 100%; border-collapse: collapse; font-size: var(--text-ui); }
.md-table th, .md-table td { padding: 8px 12px 8px 0; text-align: left; border-bottom: 1px solid var(--rule); vertical-align: baseline; }
.md-table th { font-weight: 500; color: var(--ink); }
.md-table td code, .md-table th code { overflow-wrap: anywhere; }
.vs-horizon { color: var(--success); }
.vs-java { color: var(--danger); }
.vs-tie, .vs-none { color: var(--muted); }