@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 24 lines 1.06 KB
Raw
1 import redirects from "./generated/reference-redirects.json";
2
3 const referenceRedirects: Record<string, string> = redirects;
4
5 /** Shared by the Worker and build output so links and redirects stay in sync. */
6 export function referenceDestination(href: string): string | undefined {
7 const url = new URL(href, "https://alchemy.run");
8 let pathname = url.pathname.replace(/\/$/, "");
9 const markdown = pathname.endsWith(".md");
10 if (markdown) pathname = pathname.slice(0, -3);
11 const target = referenceRedirects[pathname];
12 if (!target) return undefined;
13 const [page, resource] = target.split("#");
14 if (markdown) return `${page}.md${url.search}`;
15 const anchor = url.hash ? `${resource}-${url.hash.slice(1)}` : resource;
16 return `${page}${url.search}#${anchor}`;
17 }
18
19 /** Rewrite authored links without rewriting the hand-maintained guide sources. */
20 export function rewriteReferenceLinks(source: string): string {
21 return source.replace(/(?:href=["']|\]\()(\/providers\/[^\s"'<>)]*)/g, (match, href: string) =>
22 match.replace(href, referenceDestination(href) ?? href),
23 );
24 }