-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-links.js
185 lines (158 loc) · 5.24 KB
/
check-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
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
import { parse } from 'node-html-parser';
import fs from 'fs';
import fetch from 'node-fetch';
import { URL, fileURLToPath } from 'url';
import path from 'path';
import pLimit from 'p-limit';
export async function checkLinksInHtml(
htmlContent,
brokenLinksMap,
baseUrl,
documentPath,
checkedLinks = new Map(),
distPath = '',
astroConfigRedirects = {},
logger,
checkExternalLinks = true
) {
const root = parse(htmlContent);
const linkElements = root.querySelectorAll('a[href]');
const links = linkElements.map((el) => el.getAttribute('href'));
// add img src
const imgElements = root.querySelectorAll('img[src]');
const imgLinks = imgElements.map((el) => el.getAttribute('src'));
links.push(...imgLinks);
const limit = pLimit(50); // Limit to 10 concurrent link checks
const checkLinkPromises = links.map((link) =>
limit(async () => {
if (!isValidUrl(link)) {
return;
}
let absoluteLink;
try {
// Differentiate between absolute, domain-relative, and relative links
if (/^https?:\/\//i.test(link) || /^:\/\//i.test(link)) {
// Absolute URL
absoluteLink = link;
} else {
absoluteLink = new URL(link, "https://localhost" + baseUrl).pathname;
// if (link !== absoluteLink) {
// logger.info(`Link ${link} was resolved to ${absoluteLink}`);
// }
}
} catch (err) {
// Invalid URL, skip
logger.error(`Invalid URL in ${normalizePath(documentPath)} ${link} ${err}`);
return;
}
let fetchLink = link;
if (absoluteLink.startsWith('/') && distPath) {
fetchLink = absoluteLink;
}
if (astroConfigRedirects[fetchLink]) {
// Check if the link is a redirect
const redirect = astroConfigRedirects[fetchLink];
if (redirect) {
fetchLink = redirect.destination ? redirect.destination : redirect;
}
}
if (checkedLinks.has(fetchLink)) {
const isBroken = !checkedLinks.get(fetchLink);
if (isBroken) {
addBrokenLink(brokenLinksMap, documentPath, link, distPath);
}
return;
}
let isBroken = false;
if (fetchLink.startsWith('/') && distPath) {
// Internal link in build mode, check if file exists
const relativePath = fetchLink;
// Potential file paths to check
const possiblePaths = [
path.join(distPath, relativePath),
path.join(distPath, relativePath, 'index.html'),
path.join(distPath, `${relativePath}.html`),
];
// Check if any of the possible paths exist
if (!possiblePaths.some((p) => fs.existsSync(p))) {
// console.log('Failed paths', possiblePaths);
isBroken = true;
// Fall back to checking a redirect file if it exists.
}
} else {
// External link, check via HTTP request. Retry 3 times if ECONNRESET
if (checkExternalLinks) {
let retries = 0;
while (retries < 3) {
try {
const response = await fetch(fetchLink, { method: 'GET' });
isBroken = !response.ok;
if (isBroken) {
logger.error(`${response.status} Error fetching ${fetchLink}`);
}
break;
} catch (error) {
isBroken = true;
let statusCodeNumber = error.errno == 'ENOTFOUND' ? 404 : (error.errno);
logger.error(`${statusCodeNumber} error fetching ${fetchLink}`);
if (error.errno === 'ECONNRESET') {
retries++;
continue;
}
break;
}
}
}
}
// Cache the link's validity
checkedLinks.set(fetchLink, !isBroken);
checkedLinks.set(absoluteLink, !isBroken);
if (isBroken) {
addBrokenLink(brokenLinksMap, documentPath, link, distPath);
}
})
);
await Promise.all(checkLinkPromises);
}
function isValidUrl(url) {
// Skip mailto:, tel:, javascript:, and empty links
if (
url.startsWith('mailto:') ||
url.startsWith('tel:') ||
url.startsWith('javascript:') ||
url.startsWith('#') ||
url.trim() === ''
) {
return false;
}
return true;
}
function normalizePath(p) {
p = p.toString();
// Remove query parameters and fragments
p = p.split('?')[0].split('#')[0];
// Remove '/index.html' or '.html' suffixes
if (p.endsWith('/index.html')) {
p = p.slice(0, -'index.html'.length);
} else if (p.endsWith('.html')) {
p = p.slice(0, -'.html'.length);
}
// Ensure leading '/'
if (!p.startsWith('/')) {
p = '/' + p;
}
return p;
}
export function normalizeHtmlFilePath(filePath, distPath = '') {
return normalizePath(distPath ? path.relative(distPath, filePath) : filePath);
}
function addBrokenLink(brokenLinksMap, documentPath, brokenLink, distPath) {
// Normalize document path
documentPath = normalizeHtmlFilePath(documentPath, distPath);
// Normalize broken link for reporting
let normalizedBrokenLink = brokenLink;
if (!brokenLinksMap.has(normalizedBrokenLink)) {
brokenLinksMap.set(normalizedBrokenLink, new Set());
}
brokenLinksMap.get(normalizedBrokenLink).add(documentPath);
}