-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdetect-broken-links.sh
executable file
·63 lines (49 loc) · 1.67 KB
/
detect-broken-links.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
# NOTE(leo): script detects broken links by checking if internal
# ones match a .mdx file with correct path, and checks the status
# code of the response for http ones.
ignore_list=(
"/api" # api is not served by astro
)
verify_link() {
error=0
while read -r line; do
file=$(echo "$line" | grep -oE '.*\.mdx')
link=$(echo "$line" | cut -d '(' -f2 | cut -d ')' -f1)
ignoring=0
for ignored in "${ignore_list[@]}"; do
if [[ $ignored =~ $link ]]; then
ignoring=1
break
fi
done
if [[ $ignoring -eq 1 ]]; then
continue
fi
# test internal links
if [[ $link =~ ^/ ]]; then
# remove potential trailing slash and hash part
link=$(echo "$link" | grep -oE "^[^#]+" | sed "s:/$::")
# check if the file exists (sometimes it is an index)
if ! [[ $(find src/content/docs -type f -wholename "src/content/docs$link.mdx" -o -wholename "src/content/docs$link/index.mdx") ]]; then
echo "- Internal link broken '$link' in file '$file'"
error=1
fi
# test external links
elif [[ $link =~ ^http ]]; then
status_code=$(curl -s -o /dev/null -w "%{http_code}" -I "$link")
if [[ $status_code == 000 || $status_code -ge 400 && $status_code -lt 500 ]]; then
echo "- Http link '$link' in file '$file' is broken (status_code=$status_code)"
error=1
fi
fi
done
return $error
}
# Get links in .mdx files
links=$(grep -ro --include "*.mdx" -E "\[.*\w+\.*]\((https?://.*|/.*)\)" src/content/docs | sort -u)
echo "** Start links validity check **"
verify_link <<<"$links"
return_status=$?
echo "** End links validity check **"
exit $return_status