hornwitser.no/cli.ts
Hornwitser d003ea01d0 Use absolute refs transformed to relative
Write all links as absolute refs from the virtual root of the website's
path namespace and then transform these into relative links with the
resolveRefs utility function.
2025-01-22 09:03:37 +01:00

40 lines
1 KiB
TypeScript

import * as fs from "node:fs";
import * as posix from "node:path/posix";
import { prettify, htmlDocument } from "antihtml";
import { pages } from "./content/pages.js";
import type { Page } from "./content/types.js";
import { resolveRefs } from "./utils/resolve-refs.js";
function pageToHtml(page: Page) {
if (!page.ref.startsWith("/")) {
throw new Error(`ref "${page.ref}" for "${page.title}" is not absolute.`);
}
return htmlDocument(
prettify(
resolveRefs(
page.content,
posix.dirname(page.ref),
)
)
)
}
const outDir = "build/web";
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir);
}
for (const page of pages) {
const dirSep = page.ref.indexOf("/");
if (dirSep !== -1) {
const dir = `${outDir}/${page.ref.slice(0, dirSep)}`;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
}
console.log(`writing ${outDir}${page.ref}`);
fs.writeFileSync(`${outDir}${page.ref}`, pageToHtml(page));
}
console.log(`writing ${outDir}/style.css`);
fs.writeFileSync(`${outDir}/style.css`, fs.readFileSync("public/style.css"));