@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 189 lines 8.12 KB
Raw
1 import { promises as fs } from "node:fs";
2 import path from "node:path";
3 import { fileURLToPath } from "node:url";
4 import type { AstroIntegration } from "astro";
5 import { rewriteReferenceLinks } from "../src/reference-links.ts";
6 import { SOCIAL_REDIRECTS } from "../src/social-redirects.ts";
7 import { rewriteForPagefind } from "./pagefind-ignore-noise.ts";
8
9 /** Populated before the sitemap integration runs. */
10 export const noindexPaths = new Set();
11 const noindexRegex = /<meta\b(?=[^>]*\bname="robots")(?=[^>]*\bcontent="[^"]*noindex)/i;
12
13 /**
14 * Build-output checks — one pass over every rendered HTML page:
15 *
16 * 1. Case-sensitive internal-link check: validates every internal
17 * `<a href>` / `<img src>` against a case-sensitive Set of output
18 * paths. Case-sensitivity matters: `fs.existsSync`-based checkers
19 * (e.g. astro-broken-links-checker, which this replaced) resolve
20 * `/foo/Bar` to `/foo/bar` on macOS but 404 on Linux CI.
21 *
22 * 2. Diff-block indent check: in every rendered ```diff code block,
23 * each line's indentation must be even (the docs use 2-space
24 * indents everywhere). An odd indent means a `+`/`-` marker line
25 * was authored in the wrong convention — expressive-code strips
26 * only the marker character, so markers must be written as an
27 * EXTRA column followed by the line's full indentation, with
28 * context lines flush. See git history: three different authoring
29 * conventions had accumulated and all rendered misaligned.
30 *
31 * 3. `og:image` check: every page's og:image URL must not contain
32 * "undefined"/"null" (a broken slug lookup — Starlight 0.39 renamed
33 * routeData `slug` to `id` and the old field silently reads as
34 * undefined), and when OG images were emitted (full builds; the
35 * DOCS_FAST target skips them) the URL's path must exist in the
36 * build output.
37 */
38 export function buildOutputChecks(): AstroIntegration {
39 return {
40 name: "build-output-checks",
41 hooks: {
42 "astro:build:done": async ({ dir, logger }) => {
43 const started = performance.now();
44 const distPath = fileURLToPath(dir);
45 noindexPaths.clear();
46
47 const paths = new Set<string>();
48 const dirs = new Set<string>();
49
50 // One directory inventory and one HTML read per page. Keep only paths
51 // globally; HTML buffers are limited to the small worker batch below.
52 const entries = await fs.readdir(distPath, { recursive: true, withFileTypes: true });
53 for (const entry of entries) {
54 const rel =
55 "/" +
56 path
57 .relative(distPath, path.join(entry.parentPath, entry.name))
58 .split(path.sep)
59 .join("/");
60 if (entry.isDirectory()) dirs.add(rel);
61 else if (entry.isFile()) paths.add(rel);
62 }
63
64 const linkExists = new Map<string, boolean>();
65 const broken = new Map<string, Set<string>>();
66
67 const oddIndents: { file: string; line: string }[] = [];
68 const badOgImages: { file: string; url: string }[] = [];
69 const htmlFiles = [...paths].filter((p) => p.endsWith(".html")).sort();
70 const hasOgImages = [...paths].some((p) => p.startsWith("/og/") && p.endsWith(".webp"));
71
72 async function checkFile(htmlFile: string) {
73 const before = await fs.readFile(path.join(distPath, htmlFile.slice(1)), "utf8");
74 const html = rewriteForPagefind(rewriteReferenceLinks(before));
75 if (noindexRegex.test(html)) {
76 noindexPaths.add(
77 htmlFile.endsWith("/index.html") ? htmlFile.slice(0, -"index.html".length) : htmlFile,
78 );
79 }
80 const links = [
81 ...html.matchAll(/<a\s+[^>]*href="([^"#?]+)/gi),
82 ...html.matchAll(/<img\s+[^>]*src="([^"#?]+)/gi),
83 ].map((m) => m[1]);
84
85 for (const link of links) {
86 if (!link.startsWith("/")) continue; // skip external, anchors, mailto, etc.
87 let exists = linkExists.get(link);
88 if (exists === undefined) {
89 const clean = link.replace(/\/$/, "");
90 exists =
91 Object.hasOwn(SOCIAL_REDIRECTS, clean) ||
92 paths.has(clean) ||
93 paths.has(clean + "/index.html") ||
94 paths.has(clean + ".html") ||
95 dirs.has(clean);
96 linkExists.set(link, exists);
97 }
98 if (!exists) {
99 if (!broken.has(link)) broken.set(link, new Set());
100 broken.get(link)?.add(htmlFile);
101 }
102 }
103
104 // og:image check (see integration docstring).
105 for (const m of html.matchAll(/property="og:image"\s+content="([^"]+)"/g)) {
106 const url = m[1];
107 let pathname;
108 try {
109 pathname = new URL(url).pathname;
110 } catch {
111 badOgImages.push({ file: htmlFile, url });
112 continue;
113 }
114 if (/\b(?:undefined|null)\b/.test(pathname) || (hasOgImages && !paths.has(pathname))) {
115 badOgImages.push({ file: htmlFile, url });
116 }
117 }
118
119 // Diff-block indent check (see integration docstring).
120 if (html.includes("highlight ins") || html.includes("highlight del")) {
121 for (const fig of html.matchAll(/<figure class="frame[^"]*">.*?<\/figure>/gs)) {
122 const block = fig[0];
123 if (!block.includes("highlight ins") && !block.includes("highlight del")) continue;
124 for (const m of block.matchAll(
125 /<div class="ec-line[^"]*"><div class="code">(.*?)<\/div><\/div>/gs,
126 )) {
127 const text = m[1]
128 .replace(/<[^>]+>/g, "")
129 .replace(/&quot;/g, '"')
130 .replace(/&#39;/g, "'")
131 .replace(/&lt;/g, "<")
132 .replace(/&gt;/g, ">")
133 .replace(/&amp;/g, "&");
134 const trimmed = text.trim();
135 if (!trimmed) continue;
136 // JSDoc continuation lines legitimately indent by one.
137 if (trimmed.startsWith("*")) continue;
138 const indent = text.length - text.trimStart().length;
139 if (indent % 2 === 1) {
140 oddIndents.push({ file: htmlFile, line: text.slice(0, 60) });
141 }
142 }
143 }
144 }
145 if (html !== before) await fs.writeFile(path.join(distPath, htmlFile.slice(1)), html);
146 }
147
148 // Read/scan in bounded parallel batches — serial reads dominate
149 // the checker's runtime on 4k+ pages.
150 const BATCH = 8;
151 for (let i = 0; i < htmlFiles.length; i += BATCH) {
152 await Promise.all(htmlFiles.slice(i, i + BATCH).map(checkFile));
153 }
154
155 if (broken.size > 0) {
156 let msg = "Case-sensitive broken links detected:\n";
157 for (const [link, docs] of broken.entries()) {
158 msg += `\n ${link}\n Found in:\n`;
159 for (const doc of docs) msg += ` - ${doc}\n`;
160 }
161 logger.error(msg);
162 throw new Error(`Case-sensitive broken links detected (${broken.size})`);
163 }
164 if (badOgImages.length > 0) {
165 let msg = "Broken og:image URLs detected:\n";
166 for (const { file, url } of badOgImages.slice(0, 20)) {
167 msg += ` ${file}: ${url}\n`;
168 }
169 logger.error(msg);
170 throw new Error(`Broken og:image URLs detected (${badOgImages.length})`);
171 }
172 if (oddIndents.length > 0) {
173 let msg =
174 "Misindented diff-block lines detected (write markers as an " +
175 "extra column before the line's full indentation, context " +
176 "lines flush):\n";
177 for (const { file, line } of oddIndents.slice(0, 20)) {
178 msg += ` ${file}: ${JSON.stringify(line)}\n`;
179 }
180 logger.error(msg);
181 throw new Error(`Misindented diff-block lines detected (${oddIndents.length})`);
182 }
183 logger.info(
184 `Build-output checks passed (${htmlFiles.length} pages: links + diff indents + search exclusions; ${noindexPaths.size} noindex) in ${((performance.now() - started) / 1000).toFixed(2)}s`,
185 );
186 },
187 },
188 };
189 }