Skip to content

Commit 4c26532

Browse files
authored
Merge pull request #149 from cosmos/link-validation
Link validation
2 parents d26d357 + 130d585 commit 4c26532

File tree

8 files changed

+1908
-2
lines changed

8 files changed

+1908
-2
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,16 @@ build/
7777
# --------------------------------------------------------
7878

7979
.yarn/
80-
# Keep most scripts ignored, but include versioning and migration tools
80+
# Keep most scripts ignored, but include versioning, migration, and link-validation tools
8181
scripts/*
82+
!scripts/link-validation/
83+
!scripts/link-validation/**
8284
!scripts/versioning/
8385
!scripts/versioning/**
8486
!scripts/migration/
8587
!scripts/migration/**
8688
# Re-ignore node_modules after un-ignoring parent directories
89+
scripts/link-validation/node_modules/
8790
scripts/versioning/node_modules/
8891
scripts/migration/node_modules/
8992
tests/*

cometbft/v0.38/spec/p2p/implementation/peer_manager.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ This is not done in the p2p package, but it is part of the procedure to set up a
117117

118118
The picture below is a first attempt of illustrating the life cycle of an outbound peer:
119119

120-
<img src="../images/p2p_state.png" width="50%" title="Outgoing peers lifecycle">
120+
<img src="../images/p2p_state.png" width="50%" title="Outgoing peers lifecycle" />
121121

122122
A peer can be in the following states:
123123

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"dev": "npx mint dev",
77
"lint": "next lint",
88
"broken-links": "npx mint broken-links",
9+
"validate-links": "cd scripts/link-validation && npm install --silent && npm run check:static",
10+
"validate-links:external": "cd scripts/link-validation && npm install --silent && npm run check:static:external",
11+
"validate-links:verbose": "cd scripts/link-validation && npm install --silent && npm run check:static:verbose",
912
"clean": "rm -rf .next node_modules yarn.lock",
1013
"reset": "yarn clean && yarn install"
1114
},

scripts/link-validation/README.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

Comments
 (0)