|
| 1 | +import { existsSync, readdirSync, readFileSync } from 'node:fs' |
| 2 | +import { dirname, join, resolve } from 'node:path' |
| 3 | + |
| 4 | +import { baseUrl, siteSections, siteUrlPrefix } from './lib/config.js' |
| 5 | + |
| 6 | +const absoluteUrlPattern = new RegExp( |
| 7 | + `${baseUrl.replaceAll('.', '\\.')}[^)\\s]+`, |
| 8 | + 'g', |
| 9 | +) |
| 10 | + |
| 11 | +// Matches markdown links like [text](path) but not images , |
| 12 | +// absolute URLs, anchors-only, GitBook template tags, or angle-bracket paths. |
| 13 | +const relativeLinkPattern = |
| 14 | + /(?<!!)\]\((?!https?:\/\/|mailto:|#|{%|<|cursor:|file:)([^)]+)\)/g |
| 15 | + |
| 16 | +interface BrokenLink { |
| 17 | + file: string |
| 18 | + line: number |
| 19 | + url: string |
| 20 | + reason: string |
| 21 | +} |
| 22 | + |
| 23 | +const brokenLinks: BrokenLink[] = [] |
| 24 | + |
| 25 | +function walkDir(dir: string): string[] { |
| 26 | + const files: string[] = [] |
| 27 | + for (const entry of readdirSync(dir, { withFileTypes: true })) { |
| 28 | + const fullPath = join(dir, entry.name) |
| 29 | + if (entry.isDirectory()) { |
| 30 | + files.push(...walkDir(fullPath)) |
| 31 | + } else if (entry.name.endsWith('.md')) { |
| 32 | + files.push(fullPath) |
| 33 | + } |
| 34 | + } |
| 35 | + return files |
| 36 | +} |
| 37 | + |
| 38 | +function checkAbsoluteUrl(file: string, line: number, rawUrl: string): void { |
| 39 | + const cleanUrl = rawUrl.replaceAll('\\', '') |
| 40 | + const url = new URL(cleanUrl) |
| 41 | + url.pathname = url.pathname.replace(siteUrlPrefix, '') |
| 42 | + |
| 43 | + const pathname = url.pathname |
| 44 | + |
| 45 | + const section = siteSections.find(({ urlPrefix }) => |
| 46 | + urlPrefix === '' |
| 47 | + ? true |
| 48 | + : pathname.startsWith(urlPrefix + '/') || pathname === urlPrefix, |
| 49 | + ) |
| 50 | + |
| 51 | + if (section == null) { |
| 52 | + brokenLinks.push({ |
| 53 | + file, |
| 54 | + line, |
| 55 | + url: rawUrl, |
| 56 | + reason: 'No matching site section', |
| 57 | + }) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + const pagePath = pathname.replace(section.urlPrefix, '') |
| 62 | + const targetRoot = join(section.root, pagePath) |
| 63 | + |
| 64 | + const targetMd = `${targetRoot}.md` |
| 65 | + const targetReadme = join(targetRoot, 'README.md') |
| 66 | + |
| 67 | + if (!existsSync(targetMd) && !existsSync(targetReadme)) { |
| 68 | + brokenLinks.push({ |
| 69 | + file, |
| 70 | + line, |
| 71 | + url: rawUrl, |
| 72 | + reason: `File not found: ${targetMd} or ${targetReadme}`, |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function checkRelativeLink(file: string, line: number, rawLink: string): void { |
| 78 | + // Strip GitBook "mention" hint and anchor |
| 79 | + const linkPath = rawLink.replace(/ "mention"$/, '').split('#')[0] |
| 80 | + if (linkPath == null || linkPath === '') return |
| 81 | + |
| 82 | + // Skip asset references |
| 83 | + if (linkPath.includes('.gitbook/assets')) return |
| 84 | + |
| 85 | + // Strip GitBook markdown escapes and decode URL encoding |
| 86 | + const cleanPath = decodeURIComponent( |
| 87 | + linkPath.replaceAll('\\(', '(').replaceAll('\\)', ')').replaceAll('\\', ''), |
| 88 | + ) |
| 89 | + |
| 90 | + const fileDir = dirname(file) |
| 91 | + const resolved = resolve(fileDir, cleanPath) |
| 92 | + |
| 93 | + // Check if target exists as file, as README.md in directory, or as directory |
| 94 | + if (!existsSync(resolved) && !existsSync(join(resolved, 'README.md'))) { |
| 95 | + brokenLinks.push({ |
| 96 | + file, |
| 97 | + line, |
| 98 | + url: rawLink, |
| 99 | + reason: `File not found: ${resolved}`, |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +const files = walkDir('docs') |
| 105 | + |
| 106 | +for (const file of files) { |
| 107 | + const contents = readFileSync(file, 'utf-8') |
| 108 | + const lines = contents.split('\n') |
| 109 | + |
| 110 | + for (let i = 0; i < lines.length; i++) { |
| 111 | + const lineText = lines[i] |
| 112 | + if (lineText == null) continue |
| 113 | + |
| 114 | + // Check absolute docs.seam.co URLs |
| 115 | + for (const match of lineText.matchAll(absoluteUrlPattern)) { |
| 116 | + const rawUrl = match[0].replace(/[).,]*$/, '') |
| 117 | + checkAbsoluteUrl(file, i + 1, rawUrl) |
| 118 | + } |
| 119 | + |
| 120 | + // Check relative links |
| 121 | + for (const match of lineText.matchAll(relativeLinkPattern)) { |
| 122 | + const rawLink = match[1] |
| 123 | + if (rawLink == null) continue |
| 124 | + checkRelativeLink(file, i + 1, rawLink) |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +if (brokenLinks.length > 0) { |
| 130 | + // Group by broken target |
| 131 | + const groups = new Map<string, Array<{ file: string; line: number }>>() |
| 132 | + for (const { file, line, url } of brokenLinks) { |
| 133 | + const existing = groups.get(url) ?? [] |
| 134 | + existing.push({ file, line }) |
| 135 | + groups.set(url, existing) |
| 136 | + } |
| 137 | + |
| 138 | + // eslint-disable-next-line no-console |
| 139 | + console.error( |
| 140 | + `Found ${brokenLinks.length} broken link(s) across ${groups.size} unique target(s):\n`, |
| 141 | + ) |
| 142 | + for (const [target, sources] of groups) { |
| 143 | + // eslint-disable-next-line no-console |
| 144 | + console.error(` ${target}`) |
| 145 | + for (const { file, line } of sources) { |
| 146 | + // eslint-disable-next-line no-console |
| 147 | + console.error(` - ${file}:${line}`) |
| 148 | + } |
| 149 | + // eslint-disable-next-line no-console |
| 150 | + console.error('') |
| 151 | + } |
| 152 | + process.exit(1) |
| 153 | +} else { |
| 154 | + // eslint-disable-next-line no-console |
| 155 | + console.log('All links are valid.') |
| 156 | +} |
0 commit comments