|
| 1 | +#!/usr/bin/env node |
| 2 | +// Link validation script for PR checks |
| 3 | +// Checks for broken internal links in the built site |
| 4 | + |
| 5 | +import fs from 'fs'; |
| 6 | +import path from 'path'; |
| 7 | +import { fileURLToPath } from 'url'; |
| 8 | + |
| 9 | +const __filename = fileURLToPath(import.meta.url); |
| 10 | +const __dirname = path.dirname(__filename); |
| 11 | + |
| 12 | +const DIST_DIR = path.resolve(process.cwd(), 'dist'); |
| 13 | + |
| 14 | +// Collect all HTML files |
| 15 | +function collectHTMLFiles(dir, files = []) { |
| 16 | + const entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 17 | + |
| 18 | + for (const entry of entries) { |
| 19 | + const fullPath = path.join(dir, entry.name); |
| 20 | + if (entry.isDirectory()) { |
| 21 | + collectHTMLFiles(fullPath, files); |
| 22 | + } else if (entry.isFile() && entry.name.endsWith('.html')) { |
| 23 | + files.push(fullPath); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + return files; |
| 28 | +} |
| 29 | + |
| 30 | +// Extract internal links from HTML |
| 31 | +function extractInternalLinks(html, filePath) { |
| 32 | + const links = []; |
| 33 | + |
| 34 | + // Match href attributes |
| 35 | + const hrefRegex = /href=["']([^"']+)["']/g; |
| 36 | + let match; |
| 37 | + |
| 38 | + while ((match = hrefRegex.exec(html)) !== null) { |
| 39 | + const href = match[1]; |
| 40 | + |
| 41 | + // Skip external links, anchors, mailto, tel, etc. |
| 42 | + if (href.startsWith('http://') || |
| 43 | + href.startsWith('https://') || |
| 44 | + href.startsWith('mailto:') || |
| 45 | + href.startsWith('tel:') || |
| 46 | + href.startsWith('#')) { |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + links.push({ href, file: filePath }); |
| 51 | + } |
| 52 | + |
| 53 | + return links; |
| 54 | +} |
| 55 | + |
| 56 | +// Check if a link resolves to a real file |
| 57 | +function checkLink(link, baseDir) { |
| 58 | + let targetPath = link.href; |
| 59 | + |
| 60 | + // Remove query strings and anchors |
| 61 | + targetPath = targetPath.split('?')[0].split('#')[0]; |
| 62 | + |
| 63 | + // Handle absolute paths |
| 64 | + if (targetPath.startsWith('/')) { |
| 65 | + targetPath = path.join(baseDir, targetPath); |
| 66 | + } else { |
| 67 | + // Handle relative paths |
| 68 | + const linkDir = path.dirname(link.file); |
| 69 | + targetPath = path.join(linkDir, targetPath); |
| 70 | + } |
| 71 | + |
| 72 | + // Normalize path |
| 73 | + targetPath = path.normalize(targetPath); |
| 74 | + |
| 75 | + // Check if file exists |
| 76 | + if (fs.existsSync(targetPath)) { |
| 77 | + return { valid: true }; |
| 78 | + } |
| 79 | + |
| 80 | + // Check with .html extension |
| 81 | + if (fs.existsSync(targetPath + '.html')) { |
| 82 | + return { valid: true }; |
| 83 | + } |
| 84 | + |
| 85 | + // Check if it's a directory with index.html |
| 86 | + if (fs.existsSync(path.join(targetPath, 'index.html'))) { |
| 87 | + return { valid: true }; |
| 88 | + } |
| 89 | + |
| 90 | + return { |
| 91 | + valid: false, |
| 92 | + error: `Link points to non-existent path: ${targetPath}` |
| 93 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +async function main() { |
| 97 | + console.log('🔗 Checking internal links...\n'); |
| 98 | + |
| 99 | + if (!fs.existsSync(DIST_DIR)) { |
| 100 | + console.error('❌ dist/ directory not found. Build the site first.'); |
| 101 | + process.exit(1); |
| 102 | + } |
| 103 | + |
| 104 | + // Collect all HTML files |
| 105 | + const htmlFiles = collectHTMLFiles(DIST_DIR); |
| 106 | + console.log(`Found ${htmlFiles.length} HTML files\n`); |
| 107 | + |
| 108 | + if (htmlFiles.length === 0) { |
| 109 | + console.error('❌ No HTML files found in dist/'); |
| 110 | + process.exit(1); |
| 111 | + } |
| 112 | + |
| 113 | + // Extract and check all links |
| 114 | + const allLinks = []; |
| 115 | + const brokenLinks = []; |
| 116 | + |
| 117 | + for (const file of htmlFiles) { |
| 118 | + const html = fs.readFileSync(file, 'utf-8'); |
| 119 | + const links = extractInternalLinks(html, file); |
| 120 | + allLinks.push(...links); |
| 121 | + } |
| 122 | + |
| 123 | + console.log(`Checking ${allLinks.length} internal links...\n`); |
| 124 | + |
| 125 | + // Check each unique link |
| 126 | + const checkedLinks = new Set(); |
| 127 | + |
| 128 | + for (const link of allLinks) { |
| 129 | + const linkKey = `${link.file}::${link.href}`; |
| 130 | + |
| 131 | + // Skip if already checked |
| 132 | + if (checkedLinks.has(linkKey)) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + checkedLinks.add(linkKey); |
| 136 | + |
| 137 | + const result = checkLink(link, DIST_DIR); |
| 138 | + |
| 139 | + if (!result.valid) { |
| 140 | + brokenLinks.push({ |
| 141 | + file: path.relative(DIST_DIR, link.file), |
| 142 | + href: link.href, |
| 143 | + error: result.error |
| 144 | + }); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // Report results |
| 149 | + if (brokenLinks.length > 0) { |
| 150 | + console.error(`❌ Found ${brokenLinks.length} broken internal links:\n`); |
| 151 | + |
| 152 | + // Group by file |
| 153 | + const byFile = {}; |
| 154 | + brokenLinks.forEach(link => { |
| 155 | + if (!byFile[link.file]) byFile[link.file] = []; |
| 156 | + byFile[link.file].push(link.href); |
| 157 | + }); |
| 158 | + |
| 159 | + Object.entries(byFile).forEach(([file, links]) => { |
| 160 | + console.error(`📄 ${file}`); |
| 161 | + links.forEach(link => console.error(` ❌ ${link}`)); |
| 162 | + console.error(''); |
| 163 | + }); |
| 164 | + |
| 165 | + process.exit(1); |
| 166 | + } else { |
| 167 | + console.log(`✅ All ${checkedLinks.size} internal links are valid!`); |
| 168 | + process.exit(0); |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +main(); |
0 commit comments