// Run with `bun test`.
import assert from "node:assert/strict";
import { test } from "node:test";
import type { Root } from "mdast";
import { pageHref, pageSlug, pageTitle, remarkWikiLinks, wikiUrl } from "./wiki-links";
const wiki = "/alice/app/wiki";
test("page titles and names convert both ways", () => {
assert.equal(pageSlug(" Getting Started "), "Getting-Started");
assert.equal(pageTitle("Getting-Started"), "Getting Started");
assert.equal(pageHref(wiki, "Home"), wiki);
assert.equal(pageHref(wiki, "C# & .NET"), `${wiki}/C%23%20%26%20.NET`);
});
test("relative links resolve to pages by name, against the wiki's root", () => {
assert.equal(wikiUrl(wiki, "Other-Page", "href"), `${wiki}/Other-Page`);
assert.equal(wikiUrl(wiki, "docs/Other-Page.md#usage", "href"), `${wiki}/Other-Page#usage`);
assert.equal(wikiUrl(wiki, "v1.0-notes", "href"), `${wiki}/v1.0-notes`);
});
test("images go to the raw files, never above the root", () => {
assert.equal(wikiUrl(wiki, "./images/logo.png", "src"), `${wiki}/_raw/images/logo.png`);
assert.equal(wikiUrl(wiki, "../../etc/passwd", "src"), `${wiki}/_raw/etc/passwd`);
});
function linkify(value: string) {
const tree: Root = { type: "root", children: [{ type: "paragraph", children: [{ type: "text", value }] }] };
remarkWikiLinks({ base: wiki })(tree);
return (tree.children[0] as { children: unknown[] }).children;
}
test("[[Page]] and [[Text|Page]] become links", () => {
assert.deepEqual(linkify("See [[Getting Started]] and [[the FAQ|FAQ#install]]."), [
{ type: "text", value: "See " },
{ type: "link", url: `${wiki}/Getting-Started`, children: [{ type: "text", value: "Getting Started" }] },
{ type: "text", value: " and " },
{ type: "link", url: `${wiki}/FAQ#install`, children: [{ type: "text", value: "the FAQ" }] },
{ type: "text", value: "." },
]);
});
test("[[image.png]] embeds the image; external targets stay as they are", () => {
assert.deepEqual(linkify("[[images/arch.png]][[Docs|https://example.com]]"), [
{ type: "image", url: `${wiki}/_raw/images/arch.png`, alt: "arch.png" },
{ type: "link", url: "https://example.com", children: [{ type: "text", value: "Docs" }] },
]);
});