-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
checkReadme.js
64 lines (55 loc) · 1.67 KB
/
checkReadme.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
// Checks and rewrites the README.md for non working links
const fs = require('fs')
const axios = require('axios')
// Read the README.md file
fs.readFile('README.md', 'utf8', async (err, data) => {
if (err) {
console.error('Error reading README.md:', err)
return
}
// Regular expression to match links in the specified format
const linkRegex = /- \[(.*?)\]\((https?:\/\/.*?)\)(.*)/g
let content = data
let match
const removedLinks = []
while ((match = linkRegex.exec(data)) !== null) {
const [fullMatch, linkText, linkUrl, description] = match
try {
// Check the link validity with a maximum of 3 retries
await checkLinkValidity(linkUrl, 3)
} catch (error) {
console.error(`Removing invalid link: ${linkUrl}`)
content = content.replace(fullMatch, '')
removedLinks.push(linkUrl)
}
}
// Remove extra newline characters
content = content.replace(/\n{2,}/g, '\n\n')
// Write the updated content back to README.md
fs.writeFile('README.md', content, 'utf8', (err) => {
if (err) {
console.error('Error writing README.md:', err)
return
}
console.log('Link validation completed. README.md updated.')
if (removedLinks.length > 0) {
console.log('Removed links:')
removedLinks.forEach((link) => {
console.log(link)
})
}
})
})
async function checkLinkValidity(url, retries) {
try {
await axios.get(url)
} catch (error) {
if (retries > 0) {
console.log(`Retrying link: ${url} (${retries} retries left)`)
await new Promise((resolve) => setTimeout(resolve, 1000))
await checkLinkValidity(url, retries - 1)
} else {
throw error
}
}
}