#!/usr/bin/env node const fs = require('node:fs'); const path = require('node:path'); const root = __dirname; const compiled_root = path.join(root, '.swp/compiled'); const link_pattern = /\[[^\]]*\]\(([^)\s]+)\)/g; function scan(directory) { for (const entry of fs.readdirSync(directory, { withFileTypes: true })) { if (entry.name.startsWith('.')) { continue; } const file = path.join(directory, entry.name); if (entry.isDirectory()) { scan(file); } else if (entry.isFile() && entry.name.endsWith('.md')) { for (const match of fs.readFileSync(file, 'utf8').matchAll(link_pattern)) { const url = match[1].split(/[?#]/, 1)[0]; if (!url.endsWith('.html') || /^[a-z][a-z0-9+.-]*:/i.test(url)) { continue; } const target = url.replace(/^\//, ''); const base = url.startsWith('/') ? compiled_root : path.join(compiled_root, path.relative(root, path.dirname(file))); if (!fs.existsSync(path.resolve(base, target)) && !fs.existsSync(path.resolve(compiled_root, target))) { console.log(match[0]); } } } } } scan(process.cwd());