Skip to content

Commit

Permalink
Merge pull request #507 from RichDom2185/week13/add-link-checker
Browse files Browse the repository at this point in the history
Add link checker
  • Loading branch information
RichDom2185 authored Nov 6, 2022
2 parents 7ffa36e + 30be059 commit 306a6ba
Show file tree
Hide file tree
Showing 6 changed files with 804 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/check-links.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Check all links on website are valid

on: [push, pull_request, workflow_dispatch]

jobs:
check-links:
runs-on: ubuntu-20.04
steps:
- name: Set up repository
uses: actions/checkout@main
- name: Set up PATH
run: echo "$HOME/.gem/ruby/2.7.0/bin" >> $GITHUB_PATH
- name: Set up server dependencies
working-directory: ${{ github.workspace }}/docs
run: gem install --user-install bundler && bundle install
- name: Set up checker dependencies
working-directory: ${{ github.workspace }}/cli-test/linkchecker
run: npm install
- name: Set up server and check all links are valid
working-directory: ${{ github.workspace }}/cli-test/linkchecker
run: python serve-and-check.py
1 change: 1 addition & 0 deletions cli-test/linkchecker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
49 changes: 49 additions & 0 deletions cli-test/linkchecker/check-links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Adapted from https://github.com/w3c/node-linkchecker
import { check } from "node-linkchecker";
const options = {
schemes: ["http:"], // Do not check external links to prevent rate-limiting
userAgent:
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
fragments: true,
};

const HOST = process.env.HOST ?? "http://localhost:4000";
const PATHS_TO_CHECK = [
// Basic
"/",
"/UserGuide.html",
"/DeveloperGuide.html",
// Other Pages
"/AboutUs.html",
"/Configuration.html",
"/DevOps.html",
"/Documentation.html",
"/Logging.html",
"/SettingUp.html",
"/Testing.html",
// PPP
"/team/bryanljx.html",
"/team/eugenetanwl3881.html",
"/team/ferusel.html",
"/team/richdom2185.html",
"/team/yixiann.html",
];

const links = new Set();
const fragments = new Set();
for (const path of PATHS_TO_CHECK) {
console.log("Checking URL: " + HOST + path);
const { brokenLinks, brokenFragments } = await check(HOST + path, options);
brokenLinks.forEach(({ link }) => links.add(link));
brokenFragments.forEach(({ link }) => fragments.add(link));
}
if (links.size > 0) {
process.stderr.write("Broken Links: ");
console.error(links);
process.exitCode = 1;
}
if (fragments.size > 0) {
process.stderr.write("Broken Fragments: ");
console.error(fragments);
process.exitCode = 1;
}
Loading

0 comments on commit 306a6ba

Please sign in to comment.