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

Function relativeLinkExists

scripts/verify-links.ts:30–86  ·  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 // Check if this is an example path
57 const isExample = absPath.includes('/examples/')
58
59 let exists = false
60
61 if (isExample) {
62 // Transform /docs/framework/{framework}/examples/ to /examples/{framework}/
63 absPath = absPath.replace(
64 /\/docs\/framework\/([^/]+)\/examples\//,
65 '/examples/$1/',
66 )
67 // For examples, we want to check if the directory exists
68 exists = existsSync(absPath) && statSync(absPath).isDirectory()
69 } else {
70 // For non-examples, we want to check if the .md file exists
71 if (!absPath.endsWith('.md')) {
72 absPath = `${absPath}.md`
73 }
74 exists = existsSync(absPath)
75 }
76
77 if (!exists) {
78 errors.push({
79 link,
80 file,
81 resolvedPath: absPath,
82 reason: 'Not found',
83 })
84 }
85 return exists
86}
87

Callers 1

verifyMarkdownLinksFunction · 0.85

Calls 2

stripExtensionFunction · 0.85
resolveFunction · 0.85

Tested by

no test coverage detected