Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion codegen/validate-links.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { existsSync, readdirSync, readFileSync } from 'node:fs'
import { dirname, join, resolve } from 'node:path'

import { baseUrl, siteSections, siteUrlPrefix } from './lib/config.js'
import {
baseUrl,
type SiteSection,
siteSections,
siteUrlPrefix,
} from './lib/config.js'

function findSiteSection(filePath: string): SiteSection | undefined {
return siteSections.find(({ root }) => filePath.startsWith(root + '/'))
}

const absoluteUrlPattern = new RegExp(
`${baseUrl.replaceAll('.', '\\.')}[^)\\s]+`,
Expand Down Expand Up @@ -98,6 +107,23 @@ function checkRelativeLink(file: string, line: number, rawLink: string): void {
url: rawLink,
reason: `File not found: ${resolved}`,
})
return
}

// Check that relative links don't cross site section boundaries
const sourceSection = findSiteSection(file)
const targetSection = findSiteSection(resolved)
if (
sourceSection != null &&
targetSection != null &&
sourceSection.root !== targetSection.root
) {
brokenLinks.push({
file,
line,
url: rawLink,
reason: `Cross-section relative link: "${sourceSection.name}" -> "${targetSection.name}". Use an absolute URL instead`,
})
}
}

Expand Down
Loading