-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlink_analyzer.js
More file actions
224 lines (210 loc) · 7.84 KB
/
link_analyzer.js
File metadata and controls
224 lines (210 loc) · 7.84 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
const fs = require('fs').promises;
const path = require('path');
const { pathExists } = require('./paligo');
/**
* @typedef {{
* url: string,
* absoluteUrl: string,
* linkText: string,
* type: "preview" | "file" | "urn"
* }} BrokenLink
*/
/**
* @typedef {{
* pageTitle: string,
* breadcrumb: Array<string>,
* brokenLinks: Array<BrokenLink>
* }} FileResult
*/
/**
* @typedef {Record<string, FileResult>} LinkAnalysis
*/
/**
* Converts a relative URL to an absolute URL.
* @param {string} relativeUrl
* @param {string} basePath
* @returns {string}
*/
function convertToAbsoluteUrl(relativeUrl, basePath) {
if (relativeUrl.match(/^http/)) {
return new URL(relativeUrl);
}
return new URL(relativeUrl, new URL(basePath, 'https://docs.bitrise.io/').href);
}
/**
* Analyzes the links in a single HTML file.
* @param {string} basePath
* @param {string} fullPath
* @returns {Promise<FileResult>}
*/
async function analyseLinksInFile(basePath, fullPath) {
return analyseLinks(await fs.readFile(fullPath, 'utf8'), basePath, fullPath);
}
/**
* Analyzes the links in the given HTML content.
* @param {string} content
* @param {string} basePath
* @param {string} fullPath
* @returns {Promise<FileResult>}
*/
async function analyseLinks(content, basePath, fullPath) {
/** @type {Array<BrokenLink>} */
const brokenLinks = [];
const relativePath = path.relative(basePath, fullPath);
const pageTitle = content.match(/<title>(.*?)<\/title>/)?.[1] || 'Unknown title';
const breadcrumb = content.match(/<ul class="breadcrumb">(.*?)<\/ul>/s)?.[1] || '';
const breadcrumbItems = Array.from(breadcrumb.matchAll(/<li class="breadcrumb-link">\s*(.*?)\s*<\/li>/g)).map(match => {
const link = match[1];
if (link.match(/href="[^"]*"/)) {
return link.replace(/href="([^"]*)"/, (_, p1) => {
return `href="${convertToAbsoluteUrl(p1, relativePath).pathname}"`
});
}
return link;
});
const previewLinks = content.match(/href="\/document\/preview\/[0-9]+.*?"[^>]*>(.*?)<\/a>/g);
if (previewLinks) {
process.stdout.write(`\nBroken links detected in ${relativePath}:\n`);
for (let link of previewLinks) {
const [, url, linkText] = link.match(/href="([^"]*)"[^>]*>(.*?)<\/a>/) || [];
process.stderr.write(` - ${url}\n ${linkText}\n`);
brokenLinks.push({
url,
absoluteUrl: new URL(url, 'https://bitrise.paligoapp.com/').href,
linkText,
type: 'preview'
});
}
}
const linkedFilesMatch = content.match(/(src|href)="([^"]*)"[^>]*>(.*?)<\/a>/g);
if (linkedFilesMatch) {
for (let match of linkedFilesMatch) {
const [, , url, linkText] = match.match(/(src|href)="([^"]*)"[^>]*>(.*?)<\/a>/) || [];
if (!url.match(/^(http|mailto|#|\/)/) || url.match(/^https:\/\/docs\.bitrise\.io/)) {
const absoluteUrl = convertToAbsoluteUrl(url, relativePath);
const exists = await pathExists(path.join(basePath, absoluteUrl.pathname));
if (!exists) {
if (brokenLinks.length === 0) {
process.stdout.write(`\nBroken links detected in ${relativePath}:\n`);
}
process.stderr.write(` - ${url}\n ${linkText}\n`);
brokenLinks.push({
url,
absoluteUrl: absoluteUrl.pathname + absoluteUrl.search + absoluteUrl.hash,
linkText,
type: url.match(/^urn:/) ? 'urn' : 'file'
});
}
}
}
}
return {
pageTitle,
breadcrumb: breadcrumbItems.slice(1),
brokenLinks
};
}
/**
* Analyzes the HTML files in the given directory.
* @param {string} basePath
* @param {string|null} processPath
* @returns {Promise<LinkAnalysis>}
*/
async function analyzeHtmlFiles(basePath, processPath=null) {
if (!processPath) {
processPath = basePath;
}
/** @type {LinkAnalysis} */
const linkAnalysis = new Map();
const entries = await fs.readdir(processPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(processPath, entry.name);
if (entry.isDirectory()) {
const subDirLinkAnalysis = await analyzeHtmlFiles(basePath, fullPath);
Object.keys(subDirLinkAnalysis).forEach(key => {
linkAnalysis[key] = subDirLinkAnalysis[key];
});
} else if (entry.isFile() && entry.name.endsWith('.html') && entry.name !== '404.html') {
// process.stdout.write(`Analyzing: ${path.relative(basePath, fullPath)}: \n`);
const fileAnalysis = await analyseLinksInFile(basePath, fullPath);
// if (fileCount > 0) process.stdout.write(`${path.relative(basePath, fullPath)} broken links found: ${fileCount}\n\n`);
if (fileAnalysis.brokenLinks.length > 0) linkAnalysis[path.relative(basePath, fullPath)] = fileAnalysis;
}
}
return linkAnalysis;
}
/**
* Generates an HTML report of broken links and writes it to outputPath.
* @param {LinkAnalysis} linkAnalysis - Output from analyzeHtmlFiles
* @param {string} outputPath - Path to write the HTML report
* @returns {Promise<void>}
*/
async function generateBrokenLinksReport(linkAnalysis, outputPath) {
let html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Broken Links Report</title>
<link rel="stylesheet" href="/css/_broken_links.css">
</head>
<body>
<h1>Broken Links Report</h1>
<p>Generated on: ${new Date().toLocaleString()}</p>
`;
if (Object.keys(linkAnalysis).length === 0) {
html += '<p class="no-links">No broken links found.</p>';
} else {
html += `<h2>${Object.values(linkAnalysis).reduce((acc, curr) => acc + curr.brokenLinks.length, 0)} broken links found in ${Object.keys(linkAnalysis).length} files.</h2>`;
for (const file of Object.keys(linkAnalysis).sort()) {
const result = linkAnalysis[file];
html += `<div class="breadcrumb">
<span class="counter">${result.brokenLinks.length}</span>
${result.breadcrumb.join(' • ')}
${result.breadcrumb.length ? ' • ' : ''}
<a href="${file}" target="_blank"><strong>${result.pageTitle}</strong></a>
</div>`;
html += `<ul>`;
for (const link of result.brokenLinks) {
html += '<li>';
if (link.type === 'urn') {
html += '<img src="/css/image/paligo-logo.png" alt="Paligo" style="filter: grayscale(100%);" />';
html += `<span class="urn">${link.url}</span> (${link.linkText})`;
} else if (link.type === 'preview') {
html += '<img src="/css/image/paligo-logo.png" alt="Paligo" />';
html += `<a href="${link.absoluteUrl}" target="_blank">${link.url}</a> (${link.linkText})`;
} else {
html += '<img src="/favicon.ico" alt="Bitrise" />';
html += `<a href="${link.url.match(/^http/) ? link.url : link.absoluteUrl}" target="_blank">${link.url.match(/^http/) ? link.url : link.absoluteUrl}</a> (${link.linkText})`;
}
html += '</li>';
}
html += '</ul>';
}
}
html += '</body></html>';
await fs.writeFile(outputPath, html, 'utf8');
}
if (require.main === module) {
if (process.argv[2] && process.argv[3]) {
analyseLinksInFile(process.argv[2], process.argv[3]);
} else if (process.argv[2]) {
const outputPath = path.join(process.argv[2], '_broken_links.html');
fs.access(outputPath).then(() => {
process.stdout.write(`Removing existing report: ${outputPath}\n`);
return fs.unlink(outputPath);
}).catch(() => Promise.resolve()).then(() => {
return analyzeHtmlFiles(process.argv[2]);
}).then(linkAnalysis => {
process.stdout.write(`Total broken links found: ${Object.values(linkAnalysis).reduce((acc, curr) => acc + curr.brokenLinks.length, 0)}\n`);
generateBrokenLinksReport(linkAnalysis, path.join(process.argv[2], '_broken_links.html'));
});
} else {
console.error('Usage: node link_analyzer.js <basePath> [fullPath]');
process.exit(1);
}
}
module.exports = {
analyseLinksInFile,
analyseLinks,
generateBrokenLinksReport
};