Skip to content

Commit a8e3144

Browse files
louis-preclaude
andauthored
feat: Detect cross-section relative links in link validator (#1085)
Relative links between site sections (e.g., from Guides to API Reference) don't work in GitBook — they must be absolute URLs. The validator now flags these. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3b66e1a commit a8e3144

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

codegen/validate-links.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { existsSync, readdirSync, readFileSync } from 'node:fs'
22
import { dirname, join, resolve } from 'node:path'
33

4-
import { baseUrl, siteSections, siteUrlPrefix } from './lib/config.js'
4+
import {
5+
baseUrl,
6+
type SiteSection,
7+
siteSections,
8+
siteUrlPrefix,
9+
} from './lib/config.js'
10+
11+
function findSiteSection(filePath: string): SiteSection | undefined {
12+
return siteSections.find(({ root }) => filePath.startsWith(root + '/'))
13+
}
514

615
const absoluteUrlPattern = new RegExp(
716
`${baseUrl.replaceAll('.', '\\.')}[^)\\s]+`,
@@ -98,6 +107,23 @@ function checkRelativeLink(file: string, line: number, rawLink: string): void {
98107
url: rawLink,
99108
reason: `File not found: ${resolved}`,
100109
})
110+
return
111+
}
112+
113+
// Check that relative links don't cross site section boundaries
114+
const sourceSection = findSiteSection(file)
115+
const targetSection = findSiteSection(resolved)
116+
if (
117+
sourceSection != null &&
118+
targetSection != null &&
119+
sourceSection.root !== targetSection.root
120+
) {
121+
brokenLinks.push({
122+
file,
123+
line,
124+
url: rawLink,
125+
reason: `Cross-section relative link: "${sourceSection.name}" -> "${targetSection.name}". Use an absolute URL instead`,
126+
})
101127
}
102128
}
103129

0 commit comments

Comments
 (0)