Skip to content

Commit 2581401

Browse files
authored
Add script that can check canonical URLs for all Markdown files in a directory (#1637)
Signed-off-by: Jack Baldry <[email protected]>
1 parent a17fc90 commit 2581401

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

scripts/check-canonicals

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
3+
set -euf -o pipefail
4+
5+
6+
function usage {
7+
cat <<EOF
8+
Find all canonicalURL front matter values in all Markdown files from a directory and verify they return an OK status code.
9+
10+
Usage:
11+
$0 <PATH>
12+
13+
Examples:
14+
$0 src/data/markdown
15+
EOF
16+
}
17+
18+
19+
if [[ $# -ne 1 ]]; then
20+
usage
21+
exit 1
22+
fi
23+
24+
errors=0
25+
find "$1" -type f -name '*.md' -exec grep -H '^canonicalUrl: ' {} \; | while IFS= read -r line; do
26+
file="$(echo "${line}" | cut -d: -f1)"
27+
url="$(echo "${line}" | cut -d: -f2- | cut -d' ' -f2)"
28+
status="$(curl -X HEAD -s -o /dev/null -w '%{http_code}' "${url}")"
29+
30+
if [[ "${status}" -ne 200 ]]; then
31+
echo "ERROR: ${file}: ${url} returned status ${status}"
32+
errors=$((errors + 1))
33+
else
34+
echo "OK: ${file}: ${url} returned status ${status}" >&2
35+
fi
36+
37+
sleep 0.1
38+
done

0 commit comments

Comments
 (0)