// @ts-check
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { defineConfig } from "astro/config";
import svelte from "@astrojs/svelte";
import sitemap from "@astrojs/sitemap";
import shikiDarkJson from "./src/assets/generated/shiki-luxe.json" with {
type: "json",
};
// A JSON import widens `type` to `string`, and Shiki's own type wants the literal `"light" | "dark"`.
// The generator asserts that field is exactly one of those, so this narrows something TypeScript
// cannot see rather than claiming something unchecked.
/** @type {import("shiki").ThemeRegistrationRaw} */
const shikiDark = /** @type {any} */ (shikiDarkJson);
/**
* The guide's links between its own pages, pointed at the site's routes.
*
* `docs/guide/*.md` is read on GitHub and in the terminal as much as here, so it links the way a
* repository does, `configuration.md#github`. Left alone, that resolved against the page's own URL
* on the site and led nowhere. A page's route is its file's name, so the rewrite is exact.
*
* Needs `@astrojs/markdown-remark`: Astro 7's default Markdown processor takes no rehype plugins.
*/
function guideLinks() {
/** @param {any} node */
const walk = (node) => {
const href = node.type === "element" && node.tagName === "a" ? node.properties?.href : null;
const page = typeof href === "string" ? href.match(/^([a-z0-9-]+)\.md(#.*)?$/) : null;
if (page) {
node.properties.href = `/docs/${page[1] === "index" ? "" : `${page[1]}/`}${page[2] ?? ""}`;
}
node.children?.forEach(walk);
};
return walk;
}
/** The guide's generated screenshots, `scripts/site-assets.sh` writes them. */
const guideImages = fileURLToPath(new URL("../docs/guide/images/", import.meta.url));
/**
* The guide's screenshots, inlined: ``.
*
* Written as an ordinary image so it shows on GitHub, but drawn inline here: an SVG behind ``
* is its own document and cannot see the page's `@font-face`, so every Nerd Font icon in it would
* be tofu for anyone without the face installed. The alt text doubles as the caption. A remark
* plugin, so it runs before Astro's own image handling would claim the node.
*/
function guideShots() {
const escape = (/** @type {string} */ text) =>
text.replace(/&/g, "&").replace(/ {
node.children?.forEach((/** @type {any} */ child, /** @type {number} */ index) => {
const file = child.type === "image" ? child.url.match(/^images\/([a-z0-9-]+\.svg)$/)?.[1] : null;
if (!file) return walk(child);
const alt = escape(child.alt ?? "");
const svg = readFileSync(guideImages + file, "utf8").replace(
"