-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind-unused-images.sh
More file actions
executable file
Β·117 lines (93 loc) Β· 3.17 KB
/
find-unused-images.sh
File metadata and controls
executable file
Β·117 lines (93 loc) Β· 3.17 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
#!/bin/bash
# Script to find unused images in VuePress documentation
# Usage: ./find-unused-images.sh
set -e
IMAGES_DIR="docs/.vuepress/public/images"
DOCS_DIR="docs"
OUTPUT_FILE="unused-images.txt"
echo "π Finding unused images in VuePress documentation..."
echo "Images directory: $IMAGES_DIR"
echo "Documentation directory: $DOCS_DIR"
echo ""
# Check if directories exist
if [ ! -d "$IMAGES_DIR" ]; then
echo "β Error: Images directory '$IMAGES_DIR' not found!"
exit 1
fi
if [ ! -d "$DOCS_DIR" ]; then
echo "β Error: Documentation directory '$DOCS_DIR' not found!"
exit 1
fi
# Initialize counters
total_images=0
unused_images=0
unused_list=()
# Create temporary file for results
temp_file=$(mktemp)
echo "π Analysis Results:" > "$temp_file"
echo "===================" >> "$temp_file"
echo "" >> "$temp_file"
# Find all images and check if they're referenced
while IFS= read -r -d '' image_path; do
total_images=$((total_images + 1))
# Get relative path from public directory
rel_path="${image_path#*public/}"
# Get just the filename
filename=$(basename "$image_path")
# Search for references in markdown files
# Check for various possible reference patterns in VuePress:
# - /images/path/to/image.png (most common in VuePress)
# - ./images/path/to/image.png
# - images/path/to/image.png
# - just the filename: image.png
found=false
# Search recursively in all markdown files
if find "$DOCS_DIR" -name "*.md" -exec grep -l "/$rel_path\|\./$rel_path\|$rel_path\|$filename" {} \; 2>/dev/null | head -1 | grep -q "."; then
found=true
fi
if [ "$found" = false ]; then
unused_images=$((unused_images + 1))
unused_list+=("$image_path")
echo "β UNUSED: $rel_path" >> "$temp_file"
else
echo "β
USED: $rel_path" >> "$temp_file"
fi
done < <(find "$IMAGES_DIR" -type f \( -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.gif" -o -iname "*.svg" -o -iname "*.webp" \) -print0)
# Summary
echo "" >> "$temp_file"
echo "π Summary:" >> "$temp_file"
echo "===========" >> "$temp_file"
echo "Total images: $total_images" >> "$temp_file"
echo "Used images: $((total_images - unused_images))" >> "$temp_file"
echo "Unused images: $unused_images" >> "$temp_file"
if [ $unused_images -gt 0 ]; then
echo "" >> "$temp_file"
echo "ποΈ Unused Images List:" >> "$temp_file"
echo "======================" >> "$temp_file"
for unused_img in "${unused_list[@]}"; do
echo " - $unused_img" >> "$temp_file"
done
echo "" >> "$temp_file"
echo "π‘ To remove unused images, you can run:" >> "$temp_file"
echo " rm -f \\" >> "$temp_file"
for unused_img in "${unused_list[@]}"; do
echo " \"$unused_img\" \\" >> "$temp_file"
done
# Remove the last backslash
sed -i '$ s/ \\$//' "$temp_file"
fi
# Display results
cat "$temp_file"
# Save to file
cp "$temp_file" "$OUTPUT_FILE"
echo ""
echo "π Results saved to: $OUTPUT_FILE"
# Cleanup
rm "$temp_file"
if [ $unused_images -eq 0 ]; then
echo "π Great! All images are being used."
exit 0
else
echo "β οΈ Found $unused_images unused images."
exit 1
fi