MCPcopy Create free account
hub / github.com/TanStack/query / relativeLinkExists

Function relativeLinkExists

scripts/verify-links.ts:30–91  ·  view source on GitHub ↗
(link: string, file: string)

Source from the content-addressed store, hash-verified

28}
29
30function relativeLinkExists(link: string, file: string): boolean {
31 // Remove hash if present
32 const linkWithoutHash = link.split('#')[0]
33 // If the link is empty after removing hash, it's not a file
34 if (!linkWithoutHash) return false
35
36 // Strip the file/link extensions
37 const filePath = stripExtension(file)
38 const linkPath = stripExtension(linkWithoutHash)
39
40 // Resolve the path relative to the markdown file's directory
41 // Nav up a level to simulate how links are resolved on the web
42 let absPath = resolve(filePath, '..', linkPath)
43
44 // Ensure the resolved path is within /docs
45 const docsRoot = resolve('docs')
46 if (!absPath.startsWith(docsRoot)) {
47 errors.push({
48 link,
49 file,
50 resolvedPath: absPath,
51 reason: 'Path outside /docs',
52 })
53 return false
54 }
55
56 // `resolve` uses the platform separator, but the patterns below are written
57 // with forward slashes, so match against a normalized copy of the path.
58 // Node accepts forward slashes on Windows too, so the result stays usable.
59 absPath = absPath.split(sep).join('/')
60
61 // Check if this is an example path
62 const isExample = absPath.includes('/examples/')
63
64 let exists = false
65
66 if (isExample) {
67 // Transform /docs/framework/{framework}/examples/ to /examples/{framework}/
68 absPath = absPath.replace(
69 /\/docs\/framework\/([^/]+)\/examples\//,
70 '/examples/$1/',
71 )
72 // For examples, we want to check if the directory exists
73 exists = existsSync(absPath) && statSync(absPath).isDirectory()
74 } else {
75 // For non-examples, we want to check if the .md file exists
76 if (!absPath.endsWith('.md')) {
77 absPath = `${absPath}.md`
78 }
79 exists = existsSync(absPath)
80 }
81
82 if (!exists) {
83 errors.push({
84 link,
85 file,
86 resolvedPath: absPath,
87 reason: 'Not found',

Callers 1

verifyMarkdownLinksFunction · 0.85

Calls 2

stripExtensionFunction · 0.85
resolveFunction · 0.85

Tested by

no test coverage detected