Open History to see the last commit
1import { existsSync, readFileSync, statSync } from 'node:fs'
2import { extname, resolve } from 'node:path'
3import { glob } from 'tinyglobby'
4// @ts-ignore Could not find a declaration file for module 'markdown-link-extractor'.
5import markdownLinkExtractor from 'markdown-link-extractor'
6β
7const errors: Array<{
8 file: string
9 link: string
10 resolvedPath: string
11 reason: string
12}> = []
13β
14function isRelativeLink(link: string) {
15 return (
16 !link.startsWith('/') &&
17 !link.startsWith('http://') &&
18 !link.startsWith('https://') &&
19 !link.startsWith('//') &&
20 !link.startsWith('#') &&
21 !link.startsWith('mailto:')
22 )
23}
24β
25/** Remove any trailing .md */
26function stripExtension(p: string): string {
27 return p.replace(`${extname(p)}`, '')
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β
88async function verifyMarkdownLinks() {
89 // Find all markdown files in docs directory
90 const markdownFiles = await glob('docs/**/*.md', {
91 ignore: ['**/node_modules/**'],
92 })
93β
94 console.log(`Found ${markdownFiles.length} markdown files\n`)
95β
96 // Process each file
97 for (const file of markdownFiles) {
98 const content = readFileSync(file, 'utf-8')
99 const links: Array<string> = markdownLinkExtractor(content)
100β
101 const relativeLinks = links.filter((link: string) => {
102 return isRelativeLink(link)
103 })
104β
105 if (relativeLinks.length > 0) {
106 relativeLinks.forEach((link) => {
107 relativeLinkExists(link, file)
108 })
109 }
110 }
111β
112 if (errors.length > 0) {
113 console.log(`\nβ Found ${errors.length} broken links:`)
114 errors.forEach((err) => {
115 console.log(
116 `${err.file}\n link: ${err.link}\n resolved: ${err.resolvedPath}\n why: ${err.reason}\n`,
117 )
118 })
119 process.exit(1)
120 } else {
121 console.log('\nβ
No broken links found!')
122 }
123}
124β
125verifyMarkdownLinks().catch(console.error)