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.
This commit is contained in:
Hornwitser 2025-01-22 09:03:37 +01:00
parent 17f8693eae
commit d003ea01d0
5 changed files with 37 additions and 20 deletions

25
cli.ts
View file

@ -1,6 +1,23 @@
import { pages } from "./content/pages.js"
import * as fs from "node:fs";
import * as posix from "node:path/posix";
import { prettify, htmlDocument } from "antihtml";
import * as fs from "node:fs"
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)) {
@ -15,8 +32,8 @@ for (const page of pages) {
fs.mkdirSync(dir);
}
}
console.log(`writing ${outDir}/${page.ref}`);
fs.writeFileSync(`${outDir}/${page.ref}`, htmlDocument(prettify(page.content)));
console.log(`writing ${outDir}${page.ref}`);
fs.writeFileSync(`${outDir}${page.ref}`, pageToHtml(page));
}
console.log(`writing ${outDir}/style.css`);