diff --git a/package.json b/package.json index ed7546c34..1ca2eec44 100644 --- a/package.json +++ b/package.json @@ -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\"", @@ -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" diff --git a/scripts/link-checker.js b/scripts/link-checker.js index c935f14d7..ad6d7c229 100644 --- a/scripts/link-checker.js +++ b/scripts/link-checker.js @@ -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.'); + } }, } );