From ece20010be4ac0e9155d56735c7fa83c1164d8b1 Mon Sep 17 00:00:00 2001 From: Shawn Thompson Date: Fri, 23 Aug 2024 12:47:26 -0400 Subject: [PATCH] feat: Update link-check process and add JSON output for broken links - 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. --- package.json | 7 +++---- scripts/link-checker.js | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) 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.'); + } }, } );