Repository navigation
Expand file tree
/
Copy pathvalidate-links.js
More file actions
70 lines (63 loc) · 1.83 KB
/
Copy pathvalidate-links.js
File metadata and controls
70 lines (63 loc) · 1.83 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const root = process.cwd();
const mdLinkRegex = /\[[^\]]+\]\(([^)]+)\)/g;
const errors = [];
const explicitFiles = [
'agents/README.md',
'cookbook/README.md',
'hooks/README.md',
'instructions/README.md',
'plugins/README.md',
'skills/README.md',
'workflows/README.md',
'.schemas/README.md',
];
const explicitDirs = [
'plugins/lightspeed-github-ops',
'skills/lightspeed-frontmatter-audit',
'skills/lightspeed-pr-review',
'skills/lightspeed-label-governance',
];
function walk(dir, files = []) {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) walk(full, files);
else if (entry.isFile() && full.endsWith('.md')) files.push(full);
}
return files;
}
const filesToCheck = new Set(explicitFiles.map((f) => path.join(root, f)));
for (const dir of explicitDirs) {
const fullDir = path.join(root, dir);
if (fs.existsSync(fullDir)) {
for (const file of walk(fullDir)) filesToCheck.add(file);
}
}
for (const file of filesToCheck) {
if (!fs.existsSync(file)) continue;
const text = fs.readFileSync(file, 'utf8');
for (const match of text.matchAll(mdLinkRegex)) {
const href = match[1];
if (
href.startsWith('http://') ||
href.startsWith('https://') ||
href.startsWith('#') ||
href.startsWith('mailto:')
) {
continue;
}
const target = href.split('#')[0];
const resolved = path.resolve(path.dirname(file), target);
if (!fs.existsSync(resolved)) {
errors.push(`${path.relative(root, file)} -> ${href}`);
}
}
}
if (errors.length) {
console.error('Link validation failed:');
for (const error of errors) console.error(`- ${error}`);
process.exit(1);
}
console.log('Link validation passed.');