-
-
Notifications
You must be signed in to change notification settings - Fork 46
Linter feature: checking broken links and show in PR #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
anufdo
wants to merge
8
commits into
brisbanesocialchess:main
Choose a base branch
from
anufdo:Linter-feature-checking-broken-links-and-show-in-PR
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fdd0e95
Linter feature: checking broken links and show in PR
anufdo 8563f44
Merge branch 'main' into Linter-feature-checking-broken-links-and-sho…
jbampton d6b3c62
Merge branch 'main' into Linter-feature-checking-broken-links-and-sho…
jbampton 620e17b
chore(link-check): address PR feedback\n- Use cross-platform director…
anufdo 200dceb
ci(link-check): only comment when link-check step fails
anufdo 373746b
chore: resolve lockfile merge conflict; move LINK_CHECKER.md under fr…
anufdo ad53b36
refactor link-checking scripts and update documentation; improve mark…
anufdo 2d08403
refactor: improve link-checking script for safety and performance; va…
anufdo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: Check Links (Action) | ||
|
||
on: | ||
pull_request: | ||
paths: | ||
- '**.md' | ||
|
||
jobs: | ||
link-check: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Link Checker | ||
uses: lycheeverse/lychee-action@v1 | ||
with: | ||
args: | | ||
--verbose | ||
--no-progress | ||
--accept 200,429 | ||
--timeout 30 | ||
--max-redirects 5 | ||
--base https://brisbanesocialchess.github.io | ||
"**/*.md" | ||
fail: true | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
name: Check Markdown Links | ||
|
||
on: | ||
pull_request: | ||
types: [opened, synchronize, reopened] | ||
paths: | ||
- '**.md' | ||
- '.github/workflows/link-check.yml' | ||
- '.markdown-link-check.json' | ||
|
||
jobs: | ||
markdown-link-check: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
pull-requests: write | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: '18' | ||
|
||
- name: Install dependencies | ||
run: npm install markdown-link-check | ||
|
||
- name: Check links in markdown files | ||
run: npx markdown-link-check --config .markdown-link-check.json --verbose . | ||
continue-on-error: true | ||
id: link-check | ||
|
||
- name: Comment on PR if links are broken | ||
if: steps.link-check.outcome == 'failure' | ||
uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
const issue_number = context.issue.number; | ||
const owner = context.repo.owner; | ||
const repo = context.repo.repo; | ||
|
||
const runUrl = `https://github.com/${owner}/${repo}/actions/runs/${process.env.GITHUB_RUN_ID}`; | ||
const comment = `## 🔗 Broken Links Detected | ||
|
||
The link checker found some broken links in your markdown files. Please review and fix them: | ||
|
||
- Check the workflow logs for details: ${runUrl} | ||
- Relative links starting with \`/\` are automatically converted to \`https://brisbanesocialchess.github.io/\` | ||
- Make sure all internal links point to existing files/pages | ||
|
||
You can run the link checker locally with: \`npm run check-links\``; | ||
|
||
github.rest.issues.createComment({ | ||
issue_number, | ||
owner, | ||
repo, | ||
body: comment | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"ignorePatterns": [ | ||
{ | ||
"pattern": "^http://localhost" | ||
}, | ||
{ | ||
"pattern": "^https://localhost" | ||
}, | ||
{ | ||
"pattern": "^#%" | ||
}, | ||
{ | ||
"pattern": "^mailto:" | ||
}, | ||
{ | ||
"pattern": "^\\{\\{" | ||
} | ||
], | ||
"replacementPatterns": [ | ||
{ | ||
"pattern": "^/", | ||
"replacement": "https://brisbanesocialchess.github.io/" | ||
} | ||
], | ||
"httpHeaders": [ | ||
{ | ||
"urls": ["https://github.com"], | ||
"headers": { | ||
"Accept": "text/html", | ||
"User-Agent": "Mozilla/5.0" | ||
} | ||
} | ||
], | ||
"aliveStatusCodes": [200, 206, 429], | ||
"timeout": "15s", | ||
"retryOn429": true, | ||
"retryCount": 3, | ||
"fallbackRetryDelay": "30s" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,6 +143,14 @@ repos: | |
entry: npx lerna run test -- --run | ||
language: system | ||
pass_filenames: false | ||
- id: markdown-link-check | ||
name: Run markdown-link-check | ||
description: Check markdown files for broken links using .markdown-link-check.json | ||
entry: markdown-link-check | ||
language: node | ||
types: [markdown] | ||
args: ['--config', '.markdown-link-check.json'] | ||
additional_dependencies: ['[email protected]'] | ||
- id: prettier | ||
name: Run prettier | ||
description: format files with prettier | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# Link Checker Documentation | ||
|
||
This document explains how the broken link checker works in the Brisbane Social Chess website. | ||
|
||
## Overview | ||
|
||
The project uses `markdown-link-check` to automatically detect broken links in markdown files. This helps maintain link integrity and provides a better user experience. | ||
|
||
## Configuration | ||
|
||
The link checker is configured via `.markdown-link-check.json`: | ||
|
||
### Key Features | ||
|
||
- **Relative Link Conversion**: Links starting with `/` are automatically converted to `https://brisbanesocialchess.github.io/` | ||
- **Ignored Patterns**: | ||
- Localhost links (`http://localhost`, `https://localhost`) | ||
- URL-encoded anchor links (`#%...`) | ||
- Mailto links (`mailto:`) | ||
- Template syntax (links starting with `{{`) | ||
- **Retry Logic**: Automatically retries on 429 (rate limit) responses | ||
- **Timeout**: 15-second timeout for link checks | ||
- **User-Agent**: Proper headers for GitHub links | ||
|
||
## Usage | ||
|
||
### Local Development | ||
|
||
```bash | ||
# Check all markdown files (cross-platform) | ||
npm run check-links | ||
|
||
# Check with verbose output (shows status codes) | ||
npm run check-links-verbose | ||
|
||
# Run the CLI directly on specific files | ||
npx markdown-link-check --config .markdown-link-check.json README.md frontend/LINK_CHECKER.md | ||
``` | ||
|
||
### What Gets Checked | ||
|
||
The link checker automatically scans all `.md` files in the repository while excluding: | ||
|
||
- `node_modules/` directories (including in packages) | ||
- `.git/` directory | ||
- Any subdirectories within excluded paths | ||
|
||
The script uses a Node.js helper (`scripts/check-links.js`) to ensure cross-platform compatibility and proper directory exclusion. | ||
|
||
### Link Types Handled | ||
|
||
1. **External URLs**: Fully qualified URLs (e.g., `https://github.com`) | ||
2. **Relative Links**: Links starting with `/` (converted to full URLs) | ||
3. **File Links**: Links to other markdown files (e.g., `./README.md`) | ||
4. **Anchor Links**: Internal page anchors (e.g., `#section-name`) | ||
5. **Ignored Links**: Mailto, localhost, and encoded anchors | ||
|
||
## GitHub Actions Integration | ||
|
||
Two workflows are available: | ||
|
||
### 1. Custom Workflow (`.github/workflows/link-check.yml`) | ||
|
||
- Runs on pull requests that modify markdown files | ||
- Uses the same configuration as local development | ||
- Comments on PRs when broken links are found | ||
- Provides detailed error information | ||
|
||
### 2. Lychee Action (`.github/workflows/link-check-action.yml`) | ||
|
||
- Alternative implementation using `lychee-action` | ||
- Simpler setup but less customizable | ||
- Also runs on markdown file changes | ||
|
||
## Troubleshooting | ||
|
||
### Common Issues | ||
|
||
1. **404 Errors on Anchor Links**: Make sure the target heading exists in the file | ||
2. **Rate Limiting**: The checker automatically retries on 429 responses | ||
3. **Timeout Issues**: Increase timeout in configuration if needed | ||
|
||
### False Positives | ||
|
||
Some links may appear broken but are actually valid: | ||
|
||
- **Mailto links**: Intentionally ignored | ||
- **Encoded anchors**: Links with emojis in headings are ignored | ||
- **Local development links**: Localhost URLs are ignored | ||
|
||
## Best Practices | ||
|
||
1. **Use Relative Links**: For internal pages, use `/page-name` format | ||
2. **Test Locally**: Run `npm run check-links` before committing | ||
3. **Fix Broken Links**: Address any broken links found by the checker | ||
4. **Update Configuration**: Modify `.markdown-link-check.json` if needed | ||
|
||
## Configuration Reference | ||
|
||
The full configuration is available in `.markdown-link-check.json`. This file controls how the link checker operates, including which patterns to ignore, how to handle relative links, retry logic, and timeout settings. | ||
|
||
Key configuration options: | ||
|
||
- `ignorePatterns`: Patterns for links to skip (localhost, mailto, encoded anchors) | ||
- `replacementPatterns`: Rules for converting relative links to absolute URLs | ||
- `httpHeaders`: Custom headers for specific domains (like GitHub) | ||
- `aliveStatusCodes`: HTTP status codes considered valid (200, 206, 429) | ||
- `timeout`: How long to wait for responses (15s) | ||
- `retryOn429`: Automatically retry on rate limit responses | ||
- `retryCount`: Number of retry attempts (3) | ||
- `fallbackRetryDelay`: Delay between retries (30s) | ||
|
||
## Contributing | ||
|
||
When adding new markdown files or links: | ||
|
||
1. Run the link checker locally | ||
2. Fix any broken links | ||
3. Ensure relative links use the correct format | ||
4. Test that anchor links point to existing headings |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section describes two GitHub Actions workflows that don't seem to be part of this pull request or the repository yet. Documenting functionality that doesn't exist can be confusing for developers. It's best to keep documentation aligned with the current state of the codebase. I'd recommend removing this section for now. It can be added back in a separate pull request when the workflows are actually implemented.