|
| 1 | +# Link Validation Script |
| 2 | + |
| 3 | +This script validates both internal and external links in the Cosmos documentation site. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Install dependencies: |
| 8 | + |
| 9 | +```bash |
| 10 | +`cd scripts/link-validation |
| 11 | +npm install` |
| 12 | +``` |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +### Option 1: Static File Checker (No Server Required) |
| 17 | + |
| 18 | +This method scans MDX/MD files directly without needing the site to run: |
| 19 | + |
| 20 | +```bash |
| 21 | +# Check internal links only (fast, no network required) |
| 22 | +npm run check:static |
| 23 | + |
| 24 | +# Check both internal and external links |
| 25 | +npm run check:static:external |
| 26 | + |
| 27 | +# Verbose mode (shows all checks) |
| 28 | +npm run check:static:verbose |
| 29 | +``` |
| 30 | + |
| 31 | +**Advantages:** |
| 32 | +- ✅ Works even if the site has build errors |
| 33 | +- ✅ Fast for internal links (no network requests) |
| 34 | +- ✅ Checks files directly from source |
| 35 | +- ✅ Can check external links optionally |
| 36 | + |
| 37 | +### Option 2: Live Site Checker (Requires Running Server) |
| 38 | + |
| 39 | +Check links on a running server: |
| 40 | + |
| 41 | +```bash |
| 42 | +# Check localhost (requires `npx mint dev` to be running) |
| 43 | +npm run check |
| 44 | + |
| 45 | +# Check a production URL |
| 46 | +BASE_URL=https://docs.cosmos.network npm run check |
| 47 | + |
| 48 | +# Verbose mode |
| 49 | +npm run check:verbose |
| 50 | +``` |
| 51 | + |
| 52 | +### Integration with Root Package.json |
| 53 | + |
| 54 | +You can also add this to the root `package.json` scripts: |
| 55 | + |
| 56 | +```json |
| 57 | +{ |
| 58 | + "scripts": { |
| 59 | + "validate-links": "cd scripts/link-validation && npm run check" |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +Then run from the root: |
| 65 | + |
| 66 | +```bash |
| 67 | +npm run validate-links |
| 68 | +``` |
| 69 | + |
| 70 | +### Saving Output to a File |
| 71 | + |
| 72 | +To save the broken links report to a file for easier review: |
| 73 | + |
| 74 | +```bash |
| 75 | +# From project root - save to link-validation-report.txt |
| 76 | +npm run validate-links 2>&1 | tee link-validation-report.txt |
| 77 | + |
| 78 | +# From scripts/link-validation directory - save to report.txt |
| 79 | +npm run check:static 2>&1 | tee report.txt |
| 80 | + |
| 81 | +# Save only broken links (filter out success messages) |
| 82 | +npm run validate-links 2>&1 | grep "❌\|BROKEN\|Summary" | tee broken-links-only.txt |
| 83 | + |
| 84 | +# Save with external links included |
| 85 | +npm run validate-links:external 2>&1 | tee link-validation-report-full.txt |
| 86 | +``` |
| 87 | + |
| 88 | +**Note:** The `2>&1` redirects both stdout and stderr to the file, and `tee` displays the output on screen while also saving it to the file. |
| 89 | + |
| 90 | +## How It Works |
| 91 | + |
| 92 | +### Static File Checker (`check-links-static.js`) |
| 93 | + |
| 94 | +1. Scans all `.mdx` and `.md` files in the repository |
| 95 | +2. Extracts links from: |
| 96 | + - Markdown links: `[text](url)` |
| 97 | + - HTML anchor tags: `<a href="url">` |
| 98 | + - Card components: `<Card href="url">` |
| 99 | +3. For internal links: Checks if the target file exists |
| 100 | +4. For external links (optional): Makes HTTP requests to verify accessibility |
| 101 | +5. Reports broken links with file path and line number |
| 102 | +6. Exits with code 1 if broken links are found, 0 otherwise |
| 103 | + |
| 104 | +### Live Site Checker (`check-links.js`) |
| 105 | + |
| 106 | +1. Uses `broken-link-checker` to crawl the site starting from the base URL |
| 107 | +2. Checks both internal and external links |
| 108 | +3. Reports broken links with: |
| 109 | + - The broken URL |
| 110 | + - The page where the broken link was found |
| 111 | + - HTTP status code (if available) |
| 112 | + - Reason for failure |
| 113 | +4. Exits with code 1 if broken links are found, 0 otherwise |
| 114 | + |
| 115 | +## Configuration |
| 116 | + |
| 117 | +The script can be configured via environment variables: |
| 118 | + |
| 119 | +- `BASE_URL` or `NEXT_PUBLIC_BASE_URL`: The base URL to check (default: `http://localhost:3000`) |
| 120 | + |
| 121 | +## Notes |
| 122 | + |
| 123 | +### Static File Checker |
| 124 | +- ✅ **No server required** - Works even if the site has build errors |
| 125 | +- ✅ Fast for internal links - No network requests needed |
| 126 | +- ⚠️ External link checking requires network access and may take longer |
| 127 | +- ⚠️ Internal link checking uses file system paths, so relative paths must be correct |
| 128 | + |
| 129 | +### Live Site Checker |
| 130 | +- ⚠️ Requires the site to be running (`npx mint dev` or production URL) |
| 131 | +- ✅ More accurate for checking rendered pages |
| 132 | +- ⚠️ External link checking may take longer and depends on network connectivity |
| 133 | +- The script respects `robots.txt` files |
| 134 | +- Rate limiting is applied to avoid overwhelming servers |
0 commit comments