Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created JS code to search for dead links in the README.md and README-etc.md #222

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions link_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const fetch = require('node-fetch');
const fs = require('fs');

function readContent(filePath) {
return fs.readFileSync(filePath, 'utf8');
}

function findLinks(content) {
return content.match(/\[([^\]]+)\]\(([^)]+)\)/g).map(link => {
const [linkText, linkUrl] = link.match(/\[([^\]]+)\]\(([^)]+)\)/);
return { linkText, linkUrl };
});
}

async function testLink(linkObj) {
try {
const response = await fetch(linkObj.linkUrl);
if (response.status === 404 || response.status === 502) {
return { ...linkObj, isWorking: false };
}
return { ...linkObj, isWorking: true };
} catch (error) {
return { ...linkObj, isWorking: false };
}
}

async function testLinksInFile(filePath, notWorkingLinks) {
const content = readContent(filePath);
const links = findLinks(content);

for (const linkObj of links) {
const result = await testLink(linkObj);
if (!result.isWorking) {
notWorkingLinks.push({ filePath, linkObj });
}
}
}

async function main() {
const notWorkingLinks = [];

await testLinksInFile('README.md', notWorkingLinks);
await testLinksInFile('README-etc.md', notWorkingLinks);

const brokenLinks = notWorkingLinks.filter(link => !link.isWorking);

if (brokenLinks.length > 0) {
console.log('Broken Links:');
for (const { filePath, linkObj } of brokenLinks) {
console.log(`- Link "${linkObj.linkText}" in ${filePath} (${linkObj.linkUrl})`);
}
} else {
console.log('All links are working.');
}
}

main();
202 changes: 202 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
},
"license": "CC0 1.0",
"dependencies": {
"markdownlint": "^0.6.1"
"axios": "^1.4.0",
"markdownlint": "^0.6.1",
"node-fetch": "^2.6.12"
}
}