46 lines
1.0 KiB
Bash
Executable File
46 lines
1.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
cd "$repo_root"
|
|
|
|
missing_count=0
|
|
|
|
while IFS=$'\t' read -r src link; do
|
|
case "$link" in
|
|
''|'#'*|mailto:*|http://*|https://*)
|
|
continue
|
|
;;
|
|
esac
|
|
|
|
# Remove optional anchors and query strings for filesystem checks.
|
|
target="${link%%#*}"
|
|
target="${target%%\?*}"
|
|
|
|
[[ -z "$target" ]] && continue
|
|
|
|
if [[ "$target" == /* ]]; then
|
|
# Treat /path as repository-root relative for markdown in GitHub.
|
|
path="${repo_root}${target}"
|
|
else
|
|
path="$(dirname "$src")/$target"
|
|
fi
|
|
|
|
if [[ ! -e "$path" ]]; then
|
|
printf 'BROKEN: %s -> %s (missing: %s)\n' "$src" "$link" "$path"
|
|
missing_count=$((missing_count + 1))
|
|
fi
|
|
done < <(
|
|
while IFS= read -r file; do
|
|
perl -ne 'while(/\[[^\]]*\]\(([^)]+)\)/g){print "$ARGV\t$1\n"}' "$file"
|
|
done < <(rg --files -g '*.md')
|
|
)
|
|
|
|
if [[ "$missing_count" -gt 0 ]]; then
|
|
printf '\nFound %d broken markdown link(s).\n' "$missing_count"
|
|
exit 1
|
|
fi
|
|
|
|
echo "Markdown local links: OK"
|
|
|