@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 43 lines 1.67 KB
Raw
1 import type { ExpressiveCodePlugin } from "@astrojs/starlight/expressive-code";
2
3 /**
4 * Install specs for alchemy's own packages (`alchemy`, `@alchemy.run/*`).
5 *
6 * On main.alchemy.run and PR previews, website.yml sets `PREVIEW_PACKAGE_TAG`
7 * (`<sha>` on main, `pr:<N>:<short-sha>` on PRs) and installs come from the
8 * preview registry. Everywhere else they install `@latest` from npm. A full
9 * commit sha is shortened — the registry resolves short shas too.
10 */
11 const tag = process.env.PREVIEW_PACKAGE_TAG?.replace(/^([0-9a-f]{7})[0-9a-f]{33}$/, "$1");
12
13 export const installSpec = (name: string) =>
14 tag ? `https://pkg.alchemy.run/${name}/${tag}` : `${name}@latest`;
15
16 const INSTALL =
17 /^\s*(?:npm (?:i|install|add)|pnpm (?:add|i|install)|bun (?:add|i|install)|yarn add)\b/;
18 // `alchemy` or `@alchemy.run/<name>`, with any `@<version>`, as a whole
19 // (optionally quoted) argument.
20 const PACKAGE = /(?<=^|\s)(["']?)(alchemy|@alchemy\.run\/[a-z0-9-]+)(?:@[\w.^~-]+)?\1(?=\s|$)/g;
21
22 /** Rewrites install commands in code blocks to {@link installSpec}. */
23 export function previewInstallUrls(): ExpressiveCodePlugin {
24 return {
25 name: "preview-install-urls",
26 hooks: {
27 preprocessCode({ codeBlock }) {
28 for (const line of codeBlock.getLines()) {
29 if (!INSTALL.test(line.text)) continue;
30 // Edit right to left so earlier columns stay valid.
31 for (const match of [...line.text.matchAll(PACKAGE)].reverse()) {
32 const [whole, quote, name] = match;
33 line.editText(
34 match.index,
35 match.index + whole.length,
36 `${quote}${installSpec(name!)}${quote}`,
37 );
38 }
39 }
40 },
41 },
42 };
43 }