Skip to content

Commit

Permalink
start link check from /en/index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnthompson committed Aug 23, 2024
1 parent 4922432 commit f75fa98
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
27 changes: 14 additions & 13 deletions .github/workflows/link-check.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Link Checker

# Trigger the workflow on pull requests to the main or develop branches
on:
pull_request:
branches:
Expand All @@ -11,55 +12,55 @@ 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/[email protected]
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

# Step 4: Create a temporary .eleventy-port file
- 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/[email protected]
with:
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) }}
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 20 additions & 11 deletions scripts/link-checker.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit f75fa98

Please sign in to comment.