-
Notifications
You must be signed in to change notification settings - Fork 6
/
validate_links.js
149 lines (129 loc) · 4.37 KB
/
validate_links.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const fs = require('fs')
const path = require('path')
const marked = require('marked')
const glob = require('glob')
let flag = true
let errors = []
const root = path.join(__dirname, 'src/pages')
const foldersToCheck = [
'src/pages/docs/**/*.md',
'src/markdoc/partials/**/*.md',
]
const filesToSkipExternalLinkCheck = [
'src/pages/docs/golem/payload-manifest/computation-payload-manifest.schema.md',
'src/pages/docs/golem/payload-manifest/index.md',
]
const isPrivateOrLocalhost = (url) => {
// Regular expression to match private IP addresses and localhost
const privateIpPattern =
/^(http:\/\/|https:\/\/)?(10\.\d{1,3}\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3}|172\.(1[6-9]|2[0-9]|3[0-1])\.\d{1,3}\.\d{1,3}|127\.\d{1,3}\.\d{1,3}\.\d{1,3}|localhost)/i
return privateIpPattern.test(url)
}
const validateExternalLink = async (href) => {
// Added errors as a parameter, assuming it's an array.
if (isPrivateOrLocalhost(href)) {
return
}
try {
const response = await fetch(href, { signal: AbortSignal.timeout(2000) })
if (!response.ok) {
errors.push(`Broken external link: ${href}`)
}
} catch {
errors.push(`Broken external link: ${href}`)
}
}
foldersToCheck.forEach((folder) => {
glob(folder, (err, files) => {
if (err) throw err
files.forEach((file) => {
const data = fs.readFileSync(file, 'utf8')
const renderer = new marked.Renderer()
renderer.image = function (href, title, text) {
if (text === '') {
errors.push(`Missing image name in file ${file}: ${href} \n`)
flag = false
}
if (href.startsWith('http')) {
if (!process.env.VERCEL) {
validateExternalLink(href)
}
} else if (!href.startsWith('/')) {
errors.push(`Missing / in image link in file ${file}: ${href} \n`)
flag = false
} else {
const imagePath = path.join(
__dirname,
'public',
href.replace('/', '')
)
if (!fs.existsSync(imagePath)) {
errors.push(`Broken image in file ${file}: linking to ${href} \n`)
flag = false
}
}
}
renderer.link = async function (href) {
const dirname = path.dirname(file)
if (filesToSkipExternalLinkCheck.includes(file)) {
return
}
if (href.startsWith('docs/')) {
errors.push(`Invalid link in file ${file}: ${href} - should start with "/docs/" instead of "docs/"\n`)
return
}
if (href.startsWith('http')) {
if (
dirname.startsWith(
'src/pages/docs/golem-sdk-task-executor/reference'
) ||
dirname.startsWith('src/pages/docs/golem-js/reference') ||
dirname.startsWith('src/pages/docs/templates')
) {
return
}
if (!process.env.VERCEL) {
await validateExternalLink(href)
}
} else if (!href.startsWith('#')) {
const newHref = path.join(root, `${href.split('#')[0]}.md`)
const indexHref = path.join(root, `${href.split('#')[0]}/index.md`)
if (href.endsWith('index') && !href.endsWith('#index')) {
errors.push(
`Broken link in file ${file}: linking to ${href} --> Links ending with index are not allowed due to Next.js routing. \n`
)
}
if (href.startsWith('../')) {
const cleanHref = href.split('#')[0]
const newHref =
path.join(dirname, '..', cleanHref.replace('../', '')) + '.md'
if (!fs.existsSync(newHref)) {
errors.push(`Broken link in file ${file}: linking to ${href} \n`)
}
return
}
if (!fs.existsSync(newHref) && !fs.existsSync(indexHref)) {
if (
dirname.startsWith('src/pages/docs/golem-js/reference') ||
dirname.startsWith(
'src/pages/docs/golem-sdk-task-executor/reference'
)
) {
return
}
errors.push(`Broken link in file ${file}: linking to ${href} \n`)
}
}
}
marked.parse(data, { renderer })
})
})
})
process.on('exit', () => {
if (errors.length) {
console.error(errors.join('\n'))
process.exit(1)
} else {
console.log('All links are valid')
}
})