Skip to content

Commit

Permalink
Adds script to fix links
Browse files Browse the repository at this point in the history
$ cd uppy
$ ./bin/check-broken-links.sh

The broken links will be written to uppy/link_check_log.txt

This script has been tested on Mac OS only.
  • Loading branch information
anthony0030 committed Oct 24, 2024
1 parent f8df085 commit 0047aed
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ yarn-error.log
.env
tsconfig.tsbuildinfo
tsconfig.build.tsbuildinfo
link_check_log.txt

dist/
lib/
Expand Down
36 changes: 36 additions & 0 deletions bin/check-broken-links.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

# Log file to store the results
LOGFILE="link_check_log.txt"

# Clear the log file if it exists
> $LOGFILE

# Base directory where you want to scan (the parent of bin)
BASE_DIR="$(cd "$(dirname "$0")/.." && pwd)"

# Find all .md files in the project directory and subdirectories, excluding node_modules
find "$BASE_DIR" -path "$BASE_DIR/node_modules" -prune -o -print | while read -r file; do
echo "Scanning $file for broken links..."

# Make the file path relative to BASE_DIR to remove the './bin/../' part
real_file="${file#$BASE_DIR/}"

# Use sed to extract URLs from Markdown format ([text](url))
# This matches URLs within parentheses after a closing square bracket
sed -n 's/.*](\([^)]*\)).*/\1/p' "$file" | while IFS=: read -r url; do
# Get the line number by searching for the URL
line=$(grep -n "$url" "$file" | cut -d: -f1)

# Check if URL is valid by getting the HTTP status code
status_code=$(curl -o /dev/null --silent --head --write-out "%{http_code}" "$url")

# Print and log only if status is 404 (error)
if [ "$status_code" -eq 404 ]; then
output="Broken link found: File: $real_file | Line: $line | URL: $url | Status: ERROR (404)"
echo "$output" | tee -a $LOGFILE
fi
done
done

echo "Link check completed. Results saved in $LOGFILE." | tee -a $LOGFILE

0 comments on commit 0047aed

Please sign in to comment.