-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcheck-links.sh
More file actions
executable file
·118 lines (102 loc) · 3.08 KB
/
check-links.sh
File metadata and controls
executable file
·118 lines (102 loc) · 3.08 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env bash
# Check links in the built site using htmltest
# NOTE: This assumes the site is already built in public/
# Build the site first: hugo --gc --minify --buildFuture
#
# Usage:
# ./check-links.sh # Check entire site
# ./check-links.sh public/blog # Check only blog pages
# ./check-links.sh public/about # Check only about pages
set -e
# Determine which directory to check
TEST_DIR="${1:-public}"
# Check if site is built
if [ ! -d "public" ]; then
echo "❌ Error: public/ directory not found"
echo "Please build the site first: hugo --gc --minify --buildFuture"
exit 1
fi
if [ ! -d "$TEST_DIR" ]; then
echo "❌ Error: Directory $TEST_DIR not found"
exit 1
fi
echo "🔍 Checking links with htmltest in: $TEST_DIR"
echo ""
# Check if htmltest is installed
if ! command -v htmltest &> /dev/null && [ ! -f "./htmltest" ]; then
echo "📦 htmltest not found, installing..."
# Detect OS and architecture
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
case "$ARCH" in
x86_64)
ARCH="amd64"
;;
aarch64|arm64)
ARCH="arm64"
;;
esac
# Download and install htmltest
VERSION="0.17.0"
FILENAME="htmltest_${VERSION}_${OS}_${ARCH}"
DOWNLOAD_URL="https://github.com/wjdp/htmltest/releases/download/v${VERSION}/${FILENAME}.tar.gz"
echo "Downloading htmltest v${VERSION} for ${OS}/${ARCH}..."
# Download and extract
if curl -fsSL "$DOWNLOAD_URL" -o htmltest.tar.gz; then
tar -xzf htmltest.tar.gz htmltest
chmod +x htmltest
rm htmltest.tar.gz
echo "✅ htmltest installed successfully"
else
echo "❌ Failed to download htmltest"
echo "You can install it manually from: https://github.com/wjdp/htmltest/releases"
exit 1
fi
HTMLTEST_CMD="./htmltest"
else
if command -v htmltest &> /dev/null; then
HTMLTEST_CMD="htmltest"
else
HTMLTEST_CMD="./htmltest"
fi
fi
# Create temporary config if testing a subdirectory
if [ "$TEST_DIR" != "public" ]; then
TEMP_CONFIG=$(mktemp)
sed "s|DirectoryPath: \"public\"|DirectoryPath: \"$TEST_DIR\"|" .htmltest.yml > "$TEMP_CONFIG"
CONFIG_FLAG="--conf $TEMP_CONFIG"
else
CONFIG_FLAG=""
fi
# Run htmltest and filter output in real-time to reduce noise
# Add patterns to ignore below (one per line for easy editing)
set +e
$HTMLTEST_CMD $CONFIG_FLAG 2>&1 | grep -v \
-e '^[0-9]+: ' \
-e '^htmltest started' \
-e '^running in concurrent' \
-e 'from cache' \
-e 'Partial Content' \
-e 'OK ---' \
-e 'DOCTYPE' \
-e 'fresh ---' \
-e 'hitting ---' \
-e 'Non-OK status 403' \
-e 'Non-OK status: 403' \
-e '--> <nil>' \
-e 'testDocument' \
-e 'mailto:?subject=' \
-e 'errors in .* documents' \
|| true
HTMLTEST_EXIT=${PIPESTATUS[0]}
set -e
[ -n "$TEMP_CONFIG" ] && rm -f "$TEMP_CONFIG"
if [ $HTMLTEST_EXIT -eq 0 ]; then
echo ""
echo "✅ All links validated successfully!"
exit 0
else
echo ""
echo "❌ Link validation failed - see errors above"
exit 1
fi