-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheck-links.js
More file actions
237 lines (203 loc) · 7.06 KB
/
check-links.js
File metadata and controls
237 lines (203 loc) · 7.06 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// check-links.js
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import fetch from 'node-fetch';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// URLs to ignore during external link checking
const IGNORED_URLS = [
'https://deploystack.io',
'https://deploystack.io/*',
'https://cloud.deploystack.io',
'https://docs.deploystack.io',
'https://mintlify.com',
'https://mintlify.com/*',
'https://discord.gg/',
'https://discord.gg/*'
];
// Check if a URL should be ignored
const shouldIgnoreUrl = (url) => {
for (const pattern of IGNORED_URLS) {
if (pattern.endsWith('/*')) {
// Handle wildcard patterns
const baseUrl = pattern.slice(0, -2); // Remove /*
if (url === baseUrl || url.startsWith(baseUrl + '/')) {
return true;
}
} else {
// Handle exact matches
if (url === pattern) {
return true;
}
}
}
return false;
};
// Read a markdown file and extract all markdown links
const extractLinks = (content) => {
const linkRegex = /\[([^\]]+)\]\(([^)]+)\)/g;
const links = [];
let match;
while ((match = linkRegex.exec(content)) !== null) {
links.push({
text: match[1],
url: match[2],
full: match[0]
});
}
return links;
};
// Check if a local file exists
const checkLocalFile = (linkPath, filePath) => {
// Check for internal links (starting with / but not external URLs)
if (linkPath.startsWith('/') && !linkPath.startsWith('//') && !linkPath.startsWith('http')) {
// Remove hash fragment before checking file existence
const [baseUrl] = linkPath.split('#');
// Map the URL to the actual file location
// Files are in the root directory structure
const actualFilePath = path.join(process.cwd(), baseUrl.substring(1));
// Try both .mdx and .md extensions
const possiblePaths = [
actualFilePath + '.mdx',
actualFilePath + '.md',
path.join(actualFilePath, 'index.mdx'),
path.join(actualFilePath, 'index.md')
];
for (const possiblePath of possiblePaths) {
try {
fs.accessSync(possiblePath, fs.constants.F_OK);
console.log(` ✅ ${linkPath}`);
return true;
} catch (err) {
// Continue to next possible path
}
}
console.log(` ❌ ${linkPath} → File not found (checked: ${possiblePaths.map(p => path.relative(process.cwd(), p)).join(', ')})`);
return false;
}
return null; // not a local file
};
// Check if URL is a localhost URL
const isLocalhostUrl = (url) => {
try {
const urlObj = new URL(url);
const hostname = urlObj.hostname.toLowerCase();
// Check for localhost patterns
return hostname === 'localhost' ||
hostname === '127.0.0.1' ||
hostname === '0.0.0.0' ||
hostname.endsWith('.localhost');
} catch (error) {
return false;
}
};
// Check external URL
const checkExternalUrl = async (url) => {
// Check if it's a localhost URL and skip validation
if (isLocalhostUrl(url)) {
console.log(` ➡️ ${url} (localhost - skipped)`);
return true;
}
// Check if URL should be ignored
if (shouldIgnoreUrl(url)) {
console.log(` ➡️ ${url} (ignored)`);
return true;
}
try {
const response = await fetch(url, {
method: 'HEAD',
timeout: 5000
});
if (response.ok) {
console.log(` ✅ ${url}`);
return true;
} else {
console.log(` ❌ ${url} → Status: ${response.status}`);
return false;
}
} catch (error) {
console.log(` ❌ ${url} → Error: ${error.message}`);
return false;
}
};
// Process a single markdown file
const processFile = async (filePath) => {
const relativePath = path.relative(process.cwd(), filePath);
console.log(`\nFILE: ${relativePath}`);
const content = fs.readFileSync(filePath, 'utf8');
const links = extractLinks(content);
if (links.length === 0) {
console.log(' No hyperlinks found!\n');
return true;
}
console.log(` ${links.length} links found:`);
let allValid = true;
for (const link of links) {
if (link.url.startsWith('/') && !link.url.startsWith('//') && !link.url.startsWith('http')) {
// Internal link (root-level)
const isValid = checkLocalFile(link.url, filePath);
if (!isValid) allValid = false;
} else if (link.url.startsWith('http')) {
const isValid = await checkExternalUrl(link.url);
if (!isValid) allValid = false;
} else if (link.url.startsWith('#')) {
// Skip same-file anchors
console.log(` ➡️ ${link.url} (same-file anchor)`);
} else {
console.log(` ⚠️ ${link.url} → Skipped (not a local or HTTP link)`);
}
}
console.log('');
return allValid;
};
// Process all markdown files in docs directory
const processDirectory = async (dir) => {
let allValid = true;
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
const isValid = await processDirectory(filePath);
if (!isValid) allValid = false;
} else if (file.endsWith('.md') || file.endsWith('.mdx')) {
const isValid = await processFile(filePath);
if (!isValid) allValid = false;
}
}
return allValid;
};
// Directories to skip during scanning
const SKIP_DIRS = ['node_modules', '.git', '.next', 'out', '.github', '.source'];
// Modified processDirectory to skip certain directories
const shouldSkipDirectory = (dirName) => {
return SKIP_DIRS.includes(dirName);
};
// Start processing from current directory
console.log('📝 Checking markdown links...\n');
const scanDirectory = async (dir) => {
let allValid = true;
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
// Skip certain directories
if (!shouldSkipDirectory(file)) {
const isValid = await processDirectory(filePath);
if (!isValid) allValid = false;
}
} else if (file.endsWith('.md') || file.endsWith('.mdx')) {
const isValid = await processFile(filePath);
if (!isValid) allValid = false;
}
}
return allValid;
};
scanDirectory(process.cwd()).then(allValid => {
if (!allValid) {
console.log('❌ Some links are invalid!');
process.exit(1);
}
console.log('✅ All links are valid!');
});