From d003ea01d0d0444c34143d0041585718ecda43d6 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Wed, 22 Jan 2025 09:03:37 +0100 Subject: [PATCH] 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. --- cli.ts | 25 +++++++++++++++++++++---- content/bases.tsx | 4 ++-- content/index.tsx | 8 ++++---- content/projects.tsx | 14 +++++++------- content/updates.tsx | 6 +++--- 5 files changed, 37 insertions(+), 20 deletions(-) diff --git a/cli.ts b/cli.ts index dfa4420..b9252f7 100644 --- a/cli.ts +++ b/cli.ts @@ -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`); diff --git a/content/bases.tsx b/content/bases.tsx index cf9f3ae..9e81ea7 100644 --- a/content/bases.tsx +++ b/content/bases.tsx @@ -9,12 +9,12 @@ export function BasePage(props: BaseProps) { {props.title} - +
{props.children} diff --git a/content/index.tsx b/content/index.tsx index e4cef5f..1b01c06 100644 --- a/content/index.tsx +++ b/content/index.tsx @@ -5,7 +5,7 @@ import { updates } from "./updates.js" const title = "Hornwitser's Site"; export const index = { title, - ref: "index.html", + ref: "/index.html", content:
@@ -21,13 +21,13 @@ export const index = {

I'm a red dragon that mostly dabble in hobby programming and the occasional artwork.

-

Latest Updates

+

Latest Updates

Projects

, diff --git a/content/projects.tsx b/content/projects.tsx index a5f0493..43b12b5 100644 --- a/content/projects.tsx +++ b/content/projects.tsx @@ -4,23 +4,23 @@ import type { Page } from "./types.js"; export const projects: Page[] = [ { title: "Buddhabrot renderer", - ref: "projects/buddhabrot.html", + ref: "/projects/buddhabrot.html", }, { title: "Wooden Drawing Board", - ref: "projects/drafting-board.html", + ref: "/projects/drafting-board.html", }, { title: "Flying Hornwitser Paper Craft", - ref: "projects/paper-hornwitser.html", + ref: "/projects/paper-hornwitser.html", }, { title: "Prototype Soren Plush", - ref: "projects/plush-soren.html", + ref: "/projects/plush-soren.html", }, { title: "Blender to CSS export script", - ref: "projects/blender-css.html", + ref: "/projects/blender-css.html", }, ].map(page => ({ title: page.title, @@ -35,12 +35,12 @@ export const projects: Page[] = [ const title = "Hornwitser's Projects"; export const projectsIndex: Page = { title, - ref: "projects.html", + ref: "/projects.html", content:

{title}

diff --git a/content/updates.tsx b/content/updates.tsx index 6c5d095..935af7e 100644 --- a/content/updates.tsx +++ b/content/updates.tsx @@ -5,7 +5,7 @@ export const updates: Page[] = [ { published: "2025-xx-xx", title: "Website Launch", - ref: "updates/site-launch.html", + ref: "/updates/site-launch.html", } ].map(page => ({ title: page.title, @@ -20,12 +20,12 @@ export const updates: Page[] = [ const title = "Website Updates"; export const updatesIndex: Page = { title, - ref: "updates.html", + ref: "/updates.html", content:

{title}