Skip to content
Closed
Show file tree
Hide file tree
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
32 changes: 32 additions & 0 deletions scripts/verify-links.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { strict as assert } from 'node:assert'
import { execFileSync } from 'node:child_process'
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { test } from 'node:test'

const verifyLinks = fileURLToPath(new URL('./verify-links.ts', import.meta.url))

test('accepts framework example links on every platform', () => {
const fixture = mkdtempSync(join(tmpdir(), 'tanstack-query-verify-links-'))

try {
mkdirSync(join(fixture, 'docs/framework/react'), { recursive: true })
mkdirSync(join(fixture, 'examples/react/basic'), { recursive: true })
writeFileSync(
join(fixture, 'docs/framework/react/quick-start.md'),
'[Basic example](./examples/basic)\n',
)

const output = execFileSync(
process.execPath,
['--experimental-strip-types', verifyLinks],
{ cwd: fixture, encoding: 'utf8' },
)

assert.match(output, /No broken links found/)
} finally {
rmSync(fixture, { recursive: true, force: true })
}
})
13 changes: 9 additions & 4 deletions scripts/verify-links.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync, readFileSync, statSync } from 'node:fs'
import { extname, resolve } from 'node:path'
import { extname, resolve, sep } from 'node:path'
import { glob } from 'tinyglobby'
// @ts-ignore Could not find a declaration file for module 'markdown-link-extractor'.
import markdownLinkExtractor from 'markdown-link-extractor'
Expand Down Expand Up @@ -27,6 +27,10 @@ function stripExtension(p: string): string {
return p.replace(`${extname(p)}`, '')
}

export function normalizePathForMatching(path: string): string {
return path.split(sep).join('/')
}

function relativeLinkExists(link: string, file: string): boolean {
// Remove hash if present
const linkWithoutHash = link.split('#')[0]
Expand All @@ -53,14 +57,15 @@ function relativeLinkExists(link: string, file: string): boolean {
return false
}

// Check if this is an example path
const isExample = absPath.includes('/examples/')
// Normalize only the path used by the slash-based example mapping.
const normalizedAbsPath = normalizePathForMatching(absPath)
const isExample = normalizedAbsPath.includes('/examples/')

let exists = false

if (isExample) {
// Transform /docs/framework/{framework}/examples/ to /examples/{framework}/
absPath = absPath.replace(
absPath = normalizedAbsPath.replace(
/\/docs\/framework\/([^/]+)\/examples\//,
'/examples/$1/',
)
Expand Down