|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Verify markdown relative links in each skill folder stay inside that folder, |
| 4 | + * except links that resolve to packages/ (framework sources). |
| 5 | + * |
| 6 | + * Usage (from repo root): node skills/check-relative-links.mjs |
| 7 | + */ |
| 8 | +import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'; |
| 9 | +import { dirname, join, relative, resolve, sep } from 'node:path'; |
| 10 | +import { fileURLToPath } from 'node:url'; |
| 11 | + |
| 12 | +const skillsRoot = dirname(fileURLToPath(import.meta.url)); |
| 13 | +const repoRoot = resolve(skillsRoot, '..'); |
| 14 | +const packagesRoot = join(repoRoot, 'packages'); |
| 15 | +const LINK_RE = /\[([^\]]*)\]\(([^)]+)\)/g; |
| 16 | + |
| 17 | +function isInside(child, parent) { |
| 18 | + const rel = relative(parent, child); |
| 19 | + return rel === '' || (!rel.startsWith(`..${sep}`) && rel !== '..' && !rel.startsWith('..')); |
| 20 | +} |
| 21 | + |
| 22 | +function skillDirs() { |
| 23 | + return readdirSync(skillsRoot) |
| 24 | + .filter((name) => name.startsWith('ignition-')) |
| 25 | + .map((name) => join(skillsRoot, name)) |
| 26 | + .filter((path) => statSync(path).isDirectory()); |
| 27 | +} |
| 28 | + |
| 29 | +function markdownFiles(skillDir) { |
| 30 | + const files = [join(skillDir, 'SKILL.md')]; |
| 31 | + const refs = join(skillDir, 'references'); |
| 32 | + if (existsSync(refs) && statSync(refs).isDirectory()) { |
| 33 | + for (const name of readdirSync(refs)) { |
| 34 | + if (name.endsWith('.md')) { |
| 35 | + files.push(join(refs, name)); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + return files.filter((file) => existsSync(file)); |
| 40 | +} |
| 41 | + |
| 42 | +let failed = false; |
| 43 | +let checked = 0; |
| 44 | + |
| 45 | +for (const skillDir of skillDirs()) { |
| 46 | + const skillName = skillDir.split(sep).pop(); |
| 47 | + for (const file of markdownFiles(skillDir)) { |
| 48 | + const text = readFileSync(file, 'utf8'); |
| 49 | + const matches = [...text.matchAll(LINK_RE)]; |
| 50 | + if (matches.length === 0) { |
| 51 | + process.stdout.write(`OK ${skillName}/${relative(skillDir, file)} (no markdown links)\n`); |
| 52 | + continue; |
| 53 | + } |
| 54 | + for (const match of matches) { |
| 55 | + let href = match[2].trim().replace(/\s+"[^"]*"$/, '').replace(/\s+'[^']*'$/, ''); |
| 56 | + if ( |
| 57 | + href === '' || |
| 58 | + href.startsWith('#') || |
| 59 | + href.startsWith('http://') || |
| 60 | + href.startsWith('https://') || |
| 61 | + href.startsWith('mailto:') |
| 62 | + ) { |
| 63 | + process.stdout.write(`OK ${skillName}/${relative(skillDir, file)} -> ${href}\n`); |
| 64 | + checked += 1; |
| 65 | + continue; |
| 66 | + } |
| 67 | + const pathPart = href.split('#')[0]; |
| 68 | + if (pathPart === '') { |
| 69 | + process.stdout.write(`OK ${skillName}/${relative(skillDir, file)} -> ${href}\n`); |
| 70 | + checked += 1; |
| 71 | + continue; |
| 72 | + } |
| 73 | + const resolved = resolve(dirname(file), pathPart); |
| 74 | + const relFile = `${skillName}/${relative(skillDir, file)}`; |
| 75 | + if (isInside(resolved, skillDir)) { |
| 76 | + if (!existsSync(resolved)) { |
| 77 | + process.stdout.write(`FAIL ${relFile} -> ${href} (missing in-skill target)\n`); |
| 78 | + failed = true; |
| 79 | + } else { |
| 80 | + process.stdout.write(`OK ${relFile} -> ${href}\n`); |
| 81 | + } |
| 82 | + checked += 1; |
| 83 | + continue; |
| 84 | + } |
| 85 | + if (isInside(resolved, packagesRoot)) { |
| 86 | + process.stdout.write(`OK ${relFile} -> ${href} (framework source)\n`); |
| 87 | + checked += 1; |
| 88 | + continue; |
| 89 | + } |
| 90 | + process.stdout.write( |
| 91 | + `FAIL ${relFile} -> ${href} leaves skill folder (${relative(repoRoot, resolved)})\n`, |
| 92 | + ); |
| 93 | + failed = true; |
| 94 | + checked += 1; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +process.stdout.write(`checked=${checked} failed=${failed ? 'yes' : 'no'}\n`); |
| 100 | +process.exit(failed ? 1 : 0); |
0 commit comments