Skip to content

Commit 94eabd5

Browse files
TMHSDigitalclaude
andcommitted
site: link resolver for entry pages
Relative links to other entries become ../<id>/; any other repo file goes to its GitHub blob (or tree, for a directory) URL with the hash kept; external links pass through. A target that does not exist, or that climbs out of the repo, raises a LinkError. 11 new assertions (55 total). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wc67NX9MM964uaVGn4bpZJ
1 parent 9466d93 commit 94eabd5

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

‎site/lib/lib.test.mjs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ eq("sectionMarkdown Notes", sectionMarkdown(body, "Notes"), "- n1\n- Deprecated:
9191
eq("sectionMarkdown missing", sectionMarkdown(body, "Nope"), null);
9292
eq("deprecationReason", deprecationReason(body), "gone");
9393

94+
/* links -------------------------------------------------------------------- */
95+
import { makeLinkResolver } from "./links.mjs";
96+
const idByPath = new Map([["catalog/2d/kenney-ui-pack.md", "kenney-ui-pack"], ["catalog/audio/kenney-ui-audio.md", "kenney-ui-audio"]]);
97+
const files = new Map([["docs/provenance.md", "file"], ["catalog/2d/README.md", "file"], ["catalog/video", "dir"]]);
98+
const resolve = makeLinkResolver({ entryPath: "catalog/2d/kenney-ui-pack.md", idByPath, repo: "https://github.com/o/r", kindOf: (p) => files.get(p) || null });
99+
eq("sibling entry", resolve("kenney-ui-pack.md").href, "../kenney-ui-pack/");
100+
eq("entry in another category", resolve("../audio/kenney-ui-audio.md").href, "../kenney-ui-audio/");
101+
eq("entry link is internal", resolve("../audio/kenney-ui-audio.md").external, false);
102+
eq("docs go to GitHub blob", resolve("../../docs/provenance.md").href, "https://github.com/o/r/blob/main/docs/provenance.md");
103+
eq("category README goes to GitHub", resolve("README.md").href, "https://github.com/o/r/blob/main/catalog/2d/README.md");
104+
eq("directory goes to GitHub tree", resolve("../video/").href, "https://github.com/o/r/tree/main/catalog/video");
105+
eq("hash is kept", resolve("../../docs/provenance.md#fonts").href, "https://github.com/o/r/blob/main/docs/provenance.md#fonts");
106+
eq("external untouched", resolve("https://a.test/x").href, "https://a.test/x");
107+
eq("external flagged", resolve("https://a.test/x").external, true);
108+
throws("missing target fails", () => resolve("nope.md"), 'link target "nope.md" does not exist');
109+
throws("escaping the repo fails", () => resolve("../../../outside.md"), "does not exist");
110+
94111
/* report (keep last) ------------------------------------------------------ */
95112
if (failures.length) {
96113
console.error(`lib.test failed (${failures.length}):`);

‎site/lib/links.mjs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/** Rewrites links in an entry body for the entry's own page. */
2+
import path from "node:path";
3+
4+
export class LinkError extends Error {}
5+
6+
export function makeLinkResolver({ entryPath, idByPath, repo, kindOf }) {
7+
const dir = path.posix.dirname(entryPath);
8+
return (raw) => {
9+
if (/^(https?:|mailto:)/i.test(raw)) return { href: raw, external: true };
10+
if (raw.startsWith("#")) return { href: raw, external: false };
11+
const at = raw.indexOf("#");
12+
const target = at === -1 ? raw : raw.slice(0, at);
13+
const hash = at === -1 ? "" : raw.slice(at);
14+
const repoPath = path.posix.normalize(path.posix.join(dir, decodeURI(target))).replace(/\/$/, "");
15+
const id = idByPath.get(repoPath);
16+
if (id) return { href: `../${id}/${hash}`, external: false };
17+
const kind = repoPath.startsWith("..") ? null : kindOf(repoPath);
18+
if (!kind) throw new LinkError(`${entryPath}: link target "${raw}" does not exist`);
19+
const mode = kind === "dir" ? "tree" : "blob";
20+
return { href: `${repo}/${mode}/main/${encodeURI(repoPath)}${hash}`, external: true };
21+
};
22+
}

0 commit comments

Comments
 (0)