diff --git a/.github/workflows/link-check.yml b/.github/workflows/link-check.yml index eda7599fe..8c6500163 100644 --- a/.github/workflows/link-check.yml +++ b/.github/workflows/link-check.yml @@ -1,5 +1,6 @@ name: Link Checker +# Trigger the workflow on pull requests to the main or develop branches on: pull_request: branches: @@ -11,17 +12,17 @@ jobs: runs-on: ubuntu-latest steps: - # Step 1: Check out the repository code + # Step 1: Check out the repository code using the latest version of actions/checkout - name: Check out code uses: actions/checkout@v4 - # Step 2: Set up Node.js environment + # Step 2: Set up Node.js environment using actions/setup-node v4.0.3 - name: Set up Node.js uses: actions/setup-node@v4.0.3 with: - node-version: '20' + node-version: '20' # Specify Node.js 20.x - # Step 3: Install dependencies + # Step 3: Install npm dependencies - name: Install dependencies run: npm install @@ -29,20 +30,20 @@ jobs: - name: Create a temporary .eleventy-port file run: echo "8080" > .eleventy-port - # Step 5: Build and serve the site + # Step 5: Build and serve the site on a specific port - name: Build and serve the site - run: npm run serve-only & + run: npm run serve-only & # Run the Eleventy server in the background # Step 6: Wait for the server to start - name: Wait for server to start - run: sleep 10 + run: sleep 15 # Increase the wait time to ensure the server is fully up and running # Step 7: Run the link checker - name: Run link checker - id: link-check # Add ID to reference outputs + id: link-check # Add an ID to reference this step's outputs run: npm run link-check - # Step 8: Upload broken links report regardless of link check result + # Step 8: Upload the broken links report if it exists - name: Upload broken links report if: always() # This runs regardless of the result of the previous step uses: actions/upload-artifact@v4.3.6 @@ -50,16 +51,16 @@ jobs: name: broken-links-report path: broken-links.json - # Step 9: Post a comment on the PR with the broken links details + # Step 9: Post a comment with the results - name: Post comment with broken links - if: failure() # This runs only if broken links were found + if: always() uses: peter-evans/create-or-update-comment@v4 with: - token: ${{ secrets.GITHUB_TOKEN }} + token: ${{ secrets.PAT_TOKEN }} issue-number: ${{ github.event.pull_request.number }} body: | ## :warning: Broken Links Detected - The following broken links were found during the build process: + The following broken links were found: ```json ${{ toJSON(steps.link-check.outputs.brokenLinks) }} ``` diff --git a/package.json b/package.json index af5ddab3a..bf8e2e3f8 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "newPage": "node scripts/create-new-page.js", "link-check": "node scripts/link-checker.js", "serve:port": "node scripts/serve-with-port.js", - "serve-only": "npx @11ty/eleventy --serve --port=8080 --quiet --formats=html,css,js", + "serve-only": "npx eleventy && npx eleventy --serve", "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\"", "eleventy": "npx eleventy", diff --git a/scripts/link-checker.js b/scripts/link-checker.js index ad6d7c229..181601da2 100644 --- a/scripts/link-checker.js +++ b/scripts/link-checker.js @@ -1,38 +1,47 @@ const blc = require('broken-link-checker'); const fs = require('fs'); +const path = require('path'); // Read the port from the .eleventy-port file -const port = fs.readFileSync('.eleventy-port', 'utf8'); -const siteUrl = `http://localhost:${port}`; +const port = fs.readFileSync('.eleventy-port', 'utf8').trim(); +const startUrl = `http://localhost:${port}/en/index.html`; -const brokenLinks = []; +let brokenLinks = []; +// Create the SiteChecker instance const siteChecker = new blc.SiteChecker( { - excludeExternalLinks: true, // Exclude external links from checking - filterLevel: 1, // Level of filtering, can adjust based on need + excludeExternalLinks: true, // Only check internal links + filterLevel: 0, // Ensure all links are checked + recurse: true, // Follow links to other pages within the same domain + maxSockets: 10, // Number of concurrent requests + maxRetries: 2, // Retry broken links twice before reporting }, { link: (result) => { if (result.broken) { + // Log broken link details only to the JSON file, not to the terminal 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' + linkText: result.html.text || "N/A", + status: result.status, + statusText: result.statusText }); } }, end: () => { 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.`); + const outputFilePath = path.join(__dirname, '..', 'broken-links.json'); + fs.writeFileSync(outputFilePath, JSON.stringify(brokenLinks, null, 2)); + console.log(`Broken links found! See ${outputFilePath} for details.`); } else { console.log('No broken links found.'); } + console.log('Site link check complete.'); }, } ); -siteChecker.enqueue(siteUrl); +// Start link checking from /en/index.html +siteChecker.enqueue(startUrl);