Repository navigation
Expand file tree
/
Copy pathcheck-links.mjs
More file actions
52 lines (42 loc) · 1.7 KB
/
Copy pathcheck-links.mjs
File metadata and controls
52 lines (42 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
import { join, normalize, resolve } from "node:path";
const DIST_DIR = resolve("dist");
const URL_PATTERN = /(?:href|src)=["']([^"']+)["']/g;
function getHtmlFiles(directory) {
return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
const path = join(directory, entry.name);
return entry.isDirectory() ? getHtmlFiles(path) : path.endsWith(".html") ? [path] : [];
});
}
function isInternalUrl(url) {
return url.startsWith("/") && !url.startsWith("//");
}
function getTargetPath(url) {
const pathname = decodeURIComponent(url.split(/[?#]/, 1)[0]);
const relativePath = pathname === "/" ? "index.html" : pathname.replace(/^\//, "");
const target = join(DIST_DIR, relativePath);
if (existsSync(target) && statSync(target).isFile()) return target;
if (existsSync(join(target, "index.html"))) return join(target, "index.html");
return normalize(target);
}
function collectBrokenLinks() {
return getHtmlFiles(DIST_DIR).flatMap((file) => {
const html = readFileSync(file, "utf8");
return [...html.matchAll(URL_PATTERN)]
.map((match) => match[1])
.filter(isInternalUrl)
.filter((url) => !existsSync(getTargetPath(url)))
.map((url) => ({ file, url }));
});
}
if (!existsSync(DIST_DIR)) {
console.error("dist/ does not exist. Run `npm run build` first.");
process.exit(1);
}
const brokenLinks = collectBrokenLinks();
if (brokenLinks.length > 0) {
console.error("Broken internal links found:");
brokenLinks.forEach(({ file, url }) => console.error(`- ${file}: ${url}`));
process.exit(1);
}
console.log(`Checked internal links in ${getHtmlFiles(DIST_DIR).length} HTML files.`);