Skip to content

Commit

Permalink
Add script to build that detects broken lins
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDrivenMitch committed Oct 4, 2024
1 parent 17486de commit 5c12bbd
Show file tree
Hide file tree
Showing 4 changed files with 2,887 additions and 115 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ jobs:
export GIT_CREDENTIALS='https://axoniq-devops:${{ secrets.LIBRARY_DEVBOT_TOKEN }}@github.com'
echo 'Using' `vale -v`
npx antora playbook.yaml --log-level debug
# - name: Generate non-trailing slash redirects
# run: node generate-redirects.js
- name: Check broken lins
run: node check-broken-links.js
- name: Create `.nojekyll`
run: touch build/site/.nojekyll
- name: Create `CNAME` to serve github pages from our own domain
Expand Down
40 changes: 40 additions & 0 deletions check-broken-links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const blc = require('broken-link-checker');
const express = require('express');
const fs = require('fs');

const app = express()
app.use(express.static('build/site'))


const server = app.listen(3000, () => {
console.log(`Started serving files on port 3000!`)
})

String.prototype.trimUrl = function () {
return this.replaceAll("http://localhost:3000", "")
}

const brokenLinks = []
const siteChecker = new blc.SiteChecker({
excludeExternalLinks: true,
honorRobotExclusions: false,
maxSocketsPerHost: 3,
maxSockets: 10,
}, {
link: (result) => {
if (result.broken && result.url.resolved) {
let statusCode = result.http.response.statusCode;
console.log(`Broken link: ${result.url.resolved?.trimUrl()} (${statusCode}) on page ${result.base.resolved?.trimUrl()}`)
brokenLinks.push(result.url.resolved.trimUrl())
}
},
end: () => {
server.close()
if (brokenLinks.length > 0) {
process.exitCode = 1
}
}
}
)

siteChecker.enqueue("http://localhost:3000/home/index.html")
Loading

0 comments on commit 5c12bbd

Please sign in to comment.