Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,16 @@ jobs:
mkdir -p "$dir"
mv "$f" "$dir/index.html"
done
echo "Rewrote $(find dist -type d -name index.html -exec dirname {} \; | wc -l) pages"
shell: bash

- name: Regenerate sitemap
# VitePress sitemap is generated per-batch and only lists that batch's
# pages. The combined dist has 4,283 browse pages from the matrix build
# that the deployed sitemap would otherwise miss. Scan the final dist/
# and emit a complete sitemap.xml.
working-directory: docs
run: node generate-sitemap.js ../dist

- name: Upload combined artifact
uses: actions/upload-pages-artifact@v4
with:
Expand Down
4 changes: 4 additions & 0 deletions docs/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ async function main() {
const rewritten = await rewriteCleanUrls(DIST_DIR);
console.log(`Rewrote ${rewritten} pages`);

console.log("=== Regenerating sitemap ===");
const sitemapResult = spawnSync("node", ["generate-sitemap.js", DIST_DIR], { stdio: "inherit" });
if (sitemapResult.status !== 0) process.exit(sitemapResult.status ?? 1);

console.log(`\n=== Build complete: ${await countHtmlFiles(DIST_DIR)} total HTML pages ===`);
}

Expand Down
81 changes: 81 additions & 0 deletions docs/generate-sitemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env node
// Regenerate dist/sitemap.xml from the final dist/ structure.
//
// VitePress's built-in sitemap config only sees the pages from its own build
// — in the multi-batch CI pipeline (docs.yml build-main + build-batch matrix)
// each batch's sitemap lists only that batch's pages. The combine job deploys
// dist-main's sitemap, which is missing all 4,283 formula browse pages that
// were merged in from the batches.
//
// Run this AFTER all browse pages have been merged AND the clean-URLs post-
// process has converted foo.html -> foo/index.html. The script scans dist/
// for index.html files and emits one <url> entry per page.

import { readdir, stat, writeFile } from "node:fs/promises";
import { join, relative, sep } from "node:path";
import { fileURLToPath } from "node:url";

const __dirname = fileURLToPath(new URL(".", import.meta.url));
const DIST = process.argv[2] || join(__dirname, "..", ".vitepress", "dist");
const ORIGIN = process.env.SITE_URL || "http://localhost:5173";
const BASE_PATH = process.env.BASE_PATH || "/formulas/";

// 404 etc. shouldn't appear in the sitemap.
const EXCLUDE_PATHS = new Set(["404"]);

async function findIndexFiles(dir) {
const out = [];
const entries = await readdir(dir, { withFileTypes: true });
for (const e of entries) {
const full = join(dir, e.name);
if (e.isDirectory()) {
out.push(...await findIndexFiles(full));
} else if (e.name === "index.html") {
out.push(full);
}
}
return out;
}

function escapeXml(s) {
return s.replace(/[<>&'"]/g, (c) => ({
"<": "&lt;",
">": "&gt;",
"&": "&amp;",
"'": "&apos;",
'"': "&quot;",
}[c]));
}

async function main() {
const files = await findIndexFiles(DIST);
const entries = [];
for (const f of files) {
const relPath = relative(DIST, f).split(sep).slice(0, -1).join("/");
if (EXCLUDE_PATHS.has(relPath)) continue;
const url = `${BASE_PATH}${relPath}/`.replace(/\/{2,}/g, "/");
let lastmod;
try {
lastmod = (await stat(f)).mtime.toISOString();
} catch {
lastmod = new Date().toISOString();
}
entries.push(
` <url><loc>${escapeXml(`${ORIGIN}${url}`)}</loc><lastmod>${lastmod}</lastmod></url>`
);
}
// Stable sort by URL for reproducible builds.
entries.sort();
const xml =
`<?xml version="1.0" encoding="UTF-8"?>\n` +
`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n` +
`${entries.join("\n")}\n` +
`</urlset>\n`;
await writeFile(join(DIST, "sitemap.xml"), xml);
console.log(`Generated sitemap.xml with ${entries.length} URLs`);
}

main().catch((e) => {
console.error(e);
process.exit(1);
});
Loading