Open History to see the last commit
1import { existsSync, readFileSync, statSync } from "node:fs"
2import path, { resolve } from "node:path"
3import { glob } from "tinyglobby"
4// @ts-ignore - markdown-link-extractor is not typed
5import markdownLinkExtractor from "markdown-link-extractor"
6
7function isRelativeLink(link: string) {
8 return (
9 link &&
10 !link.startsWith(`/`) &&
11 !link.startsWith(`http://`) &&
12 !link.startsWith(`https://`) &&
13 !link.startsWith(`//`) &&
14 !link.startsWith(`#`) &&
15 !link.startsWith(`mailto:`)
16 )
17}
18
19function normalizePath(p: string): string {
20 // Remove any trailing .md
21 p = p.replace(`${path.extname(p)}`, ``)
22 return p
23}
24
25function fileExistsForLink(
26 link: string,
27 markdownFile: string,
28 errors: Array<any>
29): boolean {
30 // Remove hash if present
31 const filePart = link.split(`#`)[0]
32 // If the link is empty after removing hash, it's not a file
33 if (!filePart) return false
34
35 // Normalize the markdown file path
36 markdownFile = normalizePath(markdownFile)
37
38 // Normalize the path
39 const normalizedPath = normalizePath(filePart)
40
41 // Resolve the path relative to the markdown file's directory
42 let absPath = resolve(markdownFile, normalizedPath)
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 markdownFile,
50 resolvedPath: absPath,
51 reason: `navigates above /docs, invalid`,
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 markdownFile,
81 resolvedPath: absPath,
82 reason: `not found`,
83 })
84 }
85 return exists
86}
87
88async function findMarkdownLinks() {
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 const errors: Array<any> = []
97
98 // Process each file
99 for (const file of markdownFiles) {
100 const content = readFileSync(file, `utf-8`)
101 const links: Array<any> = markdownLinkExtractor(content)
102
103 const filteredLinks = links.filter((link: any) => {
104 if (typeof link === `string`) {
105 return isRelativeLink(link)
106 } else if (link && typeof link.href === `string`) {
107 return isRelativeLink(link.href)
108 }
109 return false
110 })
111
112 if (filteredLinks.length > 0) {
113 filteredLinks.forEach((link: any) => {
114 const href = typeof link === `string` ? link : link.href
115 fileExistsForLink(href, file, errors)
116 })
117 }
118 }
119
120 if (errors.length > 0) {
121 console.log(`\n❌ Found ${errors.length} broken links:`)
122 errors.forEach((err) => {
123 console.log(
124 `${err.link}\n in: ${err.markdownFile}\n path: ${err.resolvedPath}\n why: ${err.reason}\n`
125 )
126 })
127 process.exit(1)
128 } else {
129 console.log(`\n✅ No broken links found!`)
130 }
131}
132
133findMarkdownLinks().catch(console.error)