-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (71 loc) · 2.69 KB
/
index.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
import fetch from "node-fetch"
import { parseStringPromise } from "xml2js"
import { JSDOM } from "jsdom"
function logInColor(text, colorCode) {
console.log(`\x1b[${colorCode}m${text}\x1b[0m`)
}
async function analyzeHTML(url, classNames, totalCounts) {
const html = await downloadHTML(url)
const dom = new JSDOM(html)
classNames.forEach((className, index) => {
const instanceCount = dom.window.document.querySelectorAll(`.${className}`).length
if (instanceCount > 0) {
logInColor(`Found ${instanceCount} instances of .${className} in ${url}`, 31 + (index % 7)) // 31 to 37 are color codes for red to white
}
totalCounts[className] += instanceCount
})
console.log(`Analyzed HTML for ${url}`)
}
async function downloadHTML(url) {
const response = await fetch(url)
if (!response.ok) {
throw new Error(`Failed to fetch ${url}. Status: ${response.status}`)
}
return await response.text()
}
async function downloadSitemapAndParse(url) {
const response = await fetch(url)
if (!response.ok) {
throw new Error(`Failed to fetch ${url}. Status: ${response.status}`)
}
const xmlContent = await response.text()
const parsedResult = await parseStringPromise(xmlContent)
if (!parsedResult || !parsedResult.urlset || !parsedResult.urlset.url) {
console.log(parsedResult)
throw new Error(`Invalid sitemap format for ${url}`)
}
return parsedResult.urlset.url.map((entry) => entry.loc[0])
}
async function main(sitemapURLs, classNames) {
const urls = []
const totalCounts = {}
classNames.forEach((className) => {
totalCounts[className] = 0
})
for (const sitemapURL of sitemapURLs) {
console.log("Downloading sitemap")
urls.push(...(await downloadSitemapAndParse(sitemapURL)))
}
for (const url of urls) {
try {
await analyzeHTML(url, classNames, totalCounts)
} catch (error) {
console.error(`Error processing ${url}: ${error.message}`)
}
}
Object.keys(totalCounts).forEach((className) => {
console.log(`Total instances of .${className}: ${totalCounts[className]}`)
})
}
main(
[
"https://ubuntu.com/static/files/sitemap.xml",
"https://ubuntu.com/tutorials/sitemap.xml",
"https://ubuntu.com/engage/sitemap.xml",
"https://ubuntu.com/server/docs/sitemap.xml",
"https://ubuntu.com/ceph/docs/sitemap.xml",
"https://ubuntu.com/security/livepatch/docs/sitemap.xml",
"https://ubuntu.com/robotics/docs/sitemap.xml",
],
["p-button", "p-button--positive", "p-button--negative", "p-button--brand", "p-button--link", "p-button--base"]
)