Skip to content

Commit

Permalink
feat: Update link-check process and add JSON output for broken links
Browse files Browse the repository at this point in the history
- Updated process to manually run `npm start` to start the Eleventy server, followed by `npm run link-check` to check for broken links.
- Enhanced the link-checker script to output broken links to a `broken-links.json` file for easier review and debugging.
- Updated documentation to reflect the new process for checking broken links.
  • Loading branch information
shawnthompson committed Aug 23, 2024
1 parent 831c36b commit ece2001
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"main": ".eleventy.js",
"scripts": {
"newPage": "node scripts/create-new-page.js",
"link-checker": "node scripts/link-checker.js",
"link-check": "npm-run-all --parallel start link-checker",
"link-check": "node scripts/link-checker.js",
"serve:port": "node scripts/serve-with-port.js",
"test": "echo \"Error: no test specified\" && exit 1",
"spellcheck": "cspell --no-progress -u \"src/en/**/*.html\" \"src/en/**/*.md\" \"src/fr/**/*.html\" \"src/fr/**/*.md\"",
Expand All @@ -16,8 +15,8 @@
"sass-build": "sass --style=compressed src/_scss/base.scss _site/css/da11yn.css",
"watch:sass": "npm run sass-start -- --watch",
"start": "cross-env ELEVENTY_ENV=local npm-run-all sass-start serve:port --parallel watch:*",
"start-dev": "cross-env ELEVENTY_ENV=dev npm-run-all sass-start --parallel watch:*",
"start-prod": "cross-env ELEVENTY_ENV=prod npm-run-all sass-start --parallel watch:*",
"start-dev": "cross-env ELEVENTY_ENV=dev npm-run-all sass-start serve:port --parallel watch:*",
"start-prod": "cross-env ELEVENTY_ENV=prod npm-run-all sass-start serve:port --parallel watch:*",
"dev": "cross-env ELEVENTY_ENV=dev npm-run-all sass-build --parallel eleventy",
"debug": "DEBUG=Eleventy* npx @11ty/eleventy",
"build": "npm-run-all sass-build eleventy"
Expand Down
19 changes: 16 additions & 3 deletions scripts/link-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,32 @@ const fs = require('fs');
const port = fs.readFileSync('.eleventy-port', 'utf8');
const siteUrl = `http://localhost:${port}`;

const brokenLinks = [];

const siteChecker = new blc.SiteChecker(
{
excludeExternalLinks: true, // Exclude external links from checking
filterLevel: 3, // Level of filtering, can adjust based on need
filterLevel: 1, // Level of filtering, can adjust based on need
},
{
link: (result) => {
if (result.broken) {
console.error(`Broken link found on ${result.base.original} -> ${result.url.original} - ${result.status} (${result.statusText})`);
brokenLinks.push({
page: result.base.original,
link: result.url.original,
linkText: result.html.text || 'N/A', // Captures the link text
status: result.http.response && result.http.response.statusCode ? result.http.response.statusCode : 'N/A',
statusText: result.http.response && result.http.response.statusMessage ? result.http.response.statusMessage : 'N/A'
});
}
},
end: () => {
console.log('Site link check complete.');
if (brokenLinks.length > 0) {
fs.writeFileSync('./broken-links.json', JSON.stringify(brokenLinks, null, 2));
console.log(`Broken links found! See broken-links.json for details.`);
} else {
console.log('No broken links found.');
}
},
}
);
Expand Down

0 comments on commit ece2001

Please sign in to comment.