Skip to content

Commit

Permalink
add ci job to check local link issues
Browse files Browse the repository at this point in the history
  • Loading branch information
doitian committed Apr 14, 2022
1 parent 3a376af commit 68f2efd
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
51 changes: 51 additions & 0 deletions .github/scripts/local-link-checker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

set -e
set -u
[ -n "${DEBUG:-}" ] && set -x || true

function gherr() {
local line="$1"
local file="${line%%:*}"
local lineno="${line#*:}"
lineno="${lineno%%:*}"
local matching="${line#*:*:}"
echo "::error file=${file#./},line=$lineno::Broken link $matching"
}

function check() {
local line dir link target failed=0

while read line; do
dir="$(dirname "${line%%:*}")"
link="${line#*:*:}"

case "$link" in
//*)
# ignore http links
;;
/*)
fail "Not a valid local link"
;;
../*)
target="$(dirname "$dir")${link#..}"
if ! [ -f "$target" ]; then
failed=1
gherr "$line"
fi
;;
*)
# relative to current directory
target="$dir/${link#./}"
if ! [ -f "$target" ]; then
failed=1
gherr "$line"
fi
;;
esac
done

exit "$failed"
}

find . -name '*.md' -print0 | xargs -0 /usr/bin/grep -Hno '[^(): ][^():]*\.md' | check
21 changes: 21 additions & 0 deletions .github/workflows/local-link-checker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Local Link Checker

on:
push:
branches:
- "*"
pull_request:

jobs:
local-link-checker:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: run checker
run: .github/scripts/local-link-checker.sh

- name: info for fixing
if: ${{ failure() }}
run: echo "Run ./.github/scripts/local-link-checker.sh to check locally"
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ _book
.DS_Store
*.pdf
*.patch
*.sh

0 comments on commit 68f2efd

Please sign in to comment.