forked from cloudflare/cloudflare-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawl-api-links.js
88 lines (71 loc) · 2.02 KB
/
crawl-api-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
import puppeteer from "puppeteer";
import core from "@actions/core";
const navigationTimeout = 120000; // Set the navigation timeout to 120 seconds (120,000 milliseconds)
function arrayToHTMLList(array) {
let html = "<ul>";
for (let i = 0; i < array.length; i++) {
html += "<li>" + array[i] + "</li>";
}
html += "</ul>";
return html;
}
async function checkLinks() {
const browser = await puppeteer.launch({
headless: "new",
});
const page = await browser.newPage();
const sitemapUrl = "https://developers.cloudflare.com/sitemap.xml";
await page.goto(sitemapUrl, { timeout: navigationTimeout });
const sitemapLinks = await page.$$eval("url loc", (elements) =>
elements.map((el) => el.textContent),
);
const visitedLinks = [];
const brokenLinks = [];
for (const link of sitemapLinks) {
if (!link) {
continue; // Skip if the link is empty
}
await page.goto(link, {
waitUntil: "networkidle0",
timeout: navigationTimeout,
});
const pageLinks = await page.$$eval("a", (elements) =>
elements.map((el) => el.href),
);
for (const pageLink of pageLinks) {
if (!pageLink || visitedLinks.includes(pageLink)) {
continue; // Skip if the pageLink is empty or has already been visited
}
if (
pageLink.includes("developers.cloudflare.com/api/operations/") ||
pageLink.startsWith("/api/operations/")
) {
console.log(`Evaluating link: ${pageLink}`);
await page.goto(pageLink, {
waitUntil: "networkidle0",
timeout: navigationTimeout,
});
visitedLinks.push(pageLink);
const statusCode = await page.evaluate(() => {
return {
url: window.location.href,
};
});
if (statusCode.url === "https://developers.cloudflare.com/api/") {
brokenLinks.push(pageLink);
}
}
}
}
await browser.close();
console.log("Broken links:");
console.log(brokenLinks);
if (brokenLinks.length > 0) {
core.setOutput("brokenLinks", arrayToHTMLList(brokenLinks));
}
process.exit(0);
}
checkLinks().catch((error) => {
console.error(error);
process.exit(1);
});