-
Notifications
You must be signed in to change notification settings - Fork 4
Add link checker 2 #65
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bf4cf1d
Reapply "test linkcheck"
wlkh 6d01361
Reapply "local linkcheck"
wlkh 1dae3dd
Reapply "Added automatic link-checking and fixed plenty of links"
wlkh 0b0cebb
Reapply "Added automatic link-checking and fixed plenty of links"
wlkh ea72ddc
Reapply "minor change, removed echo paths used for testing linkchecker"
wlkh a10f07e
Revert requirements to same as master
wlkh 36fdb3e
Fixes after review
wlkh 6923173
Fix script for github action
wlkh cb4c475
Rename build job to be less confusing
wlkh 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,99 @@ | ||
| #!/usr/bin/env python3 | ||
| import re | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| # Regex for Markdown links: [text](target) | ||
| LINK_RE = re.compile(r"\[([^\]]+)\]\(([^)]+)\)") | ||
|
|
||
| def slugify_heading(text: str) -> str: | ||
| """Convert Markdown heading text to MkDocs anchor format.""" | ||
| slug = text.strip().lower() | ||
| slug = re.sub(r"[^\w\s-]", "", slug) # remove punctuation | ||
| slug = re.sub(r"\s+", "-", slug) # spaces -> dashes | ||
| return slug | ||
|
|
||
|
|
||
| def extract_headings(md_file: Path): | ||
| """Return a set of anchor slugs from headings in md files.""" | ||
| headings = set() | ||
| if not md_file.is_file(): | ||
| return headings | ||
| for line in md_file.read_text(encoding="utf-8").splitlines(): | ||
| if line.startswith("#"): | ||
| heading_text = line.lstrip("#").strip() | ||
| headings.add(slugify_heading(heading_text)) | ||
| return headings | ||
|
|
||
| def check_md_file(md_file: Path): | ||
| """Check links in md files.""" | ||
| errors = [] | ||
| if not md_file.is_file(): # <-- add this check | ||
| return errors | ||
|
|
||
| for line_number, line in enumerate(md_file.read_text(encoding="utf-8").splitlines(), start=1): | ||
|
|
||
| stripped = line.strip() | ||
| # Skip HTML and python comments | ||
| if stripped.startswith("#"): | ||
| continue | ||
| elif stripped.startswith("<!--") and stripped.endswith("-->"): | ||
| continue | ||
|
|
||
| for match in LINK_RE.finditer(line): | ||
| text, target = match.groups() | ||
|
|
||
| # Skip external links | ||
| if target.startswith(("http://", "https://", "mailto:")): | ||
| continue | ||
|
|
||
| # Remove leading slash for site-root relative links | ||
| elif target.startswith("/"): | ||
| target = target[1:] | ||
|
|
||
| # Split anchor from file | ||
| if "#" in target: | ||
| if target.startswith("#"): | ||
| file_part, anchor = md_file, target[1:] | ||
| else: | ||
| file_part, anchor = target.split("#", 1) | ||
| else: | ||
| file_part, anchor = target, None | ||
|
|
||
| # Resolve relative path' | ||
| target_path = Path(file_part) if isinstance(file_part, Path) else Path(file_part) #Make sure it's a Path object | ||
| target_file = (md_file.parent / target_path).resolve() | ||
|
|
||
| if not target_file.exists() and not target_file.suffix: | ||
| target_file = (md_file.parent / (target_path.name + ".md")).resolve() | ||
| if not target_file.exists(): | ||
| errors.append(f"{md_file}:{line_number}: File not found -> {target}") | ||
| continue | ||
|
|
||
| if target_file.is_file() and anchor: | ||
| headings = extract_headings(target_file) | ||
| if anchor not in headings: | ||
| errors.append(f"{md_file}:{line_number}: Anchor not found -> {target}") | ||
|
|
||
| if target_file.is_dir(): | ||
| continue | ||
|
|
||
| return errors | ||
|
|
||
|
|
||
| def main(md_dir): | ||
| md_dir = Path(md_dir) | ||
| all_errors = [] | ||
| for md_file in md_dir.rglob("*.md"): | ||
| all_errors.extend(check_md_file(md_file)) | ||
|
|
||
| if all_errors: | ||
| print(f'Found {len(all_errors)} internal link errors in md files:') | ||
| for e in all_errors: | ||
| print(e) | ||
| return 1 | ||
| print("No internal link errors found.") | ||
| return 0 | ||
|
|
||
| if __name__ == "__main__": | ||
| exit(main(sys.argv[1])) | ||
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,74 @@ | ||
| #!/bin/bash | ||
wlkh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Pre-push hook: Custom linkchecker for local links, Build MkDocs locally and run LinkChecker on external links | ||
|
|
||
| # Exit on error | ||
| set -e | ||
|
|
||
| # === Paths === | ||
| PROJECT_ROOT="$(git rev-parse --show-toplevel)" | ||
| VENV_BIN="$PROJECT_ROOT/venv/bin" | ||
| TMP_BUILD_DIR="$PROJECT_ROOT/.tmp-mkdocs-build" | ||
| DOCS_DIR="$PROJECT_ROOT/docs" | ||
|
|
||
| # Activate venv if not already activated | ||
| if [ -f "$VENV_BIN/activate" ]; then | ||
| . "$VENV_BIN/activate" | ||
| fi | ||
|
|
||
| #Checking internal links in markdown files only | ||
| echo "Checking internal links in markdown files..." | ||
|
|
||
| set +e | ||
| #Using custom python script for internal link checking: | ||
| python "$PROJECT_ROOT/check-internal-links.py" "$DOCS_DIR" | ||
| RESULT_INTERNAL=$? | ||
|
|
||
| set -e | ||
|
|
||
| if [ $RESULT_INTERNAL -ne 0 ]; then | ||
| echo "Internal linkcheck failed! Please fix Markdown links before pushing." | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "Internal linkcheck passed!" | ||
|
|
||
| # Check if linkchecker exists | ||
| if ! command -v linkchecker >/dev/null 2>&1; then | ||
| echo "Error: linkchecker executable not found in PATH" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Remove old temp build if it exists | ||
| rm -rf "$TMP_BUILD_DIR" | ||
|
|
||
| # Build MkDocs into temporary directory | ||
| echo "Building MkDocs locally into $TMP_BUILD_DIR..." | ||
| python -m mkdocs build -d "$TMP_BUILD_DIR" | ||
|
|
||
| echo "Running LinkChecker on external links against local build..." | ||
| set +e | ||
| # Only report broken links | ||
| linkchecker "file://$TMP_BUILD_DIR/index.html" \ | ||
| --no-status \ | ||
| --check-extern \ | ||
| --recursion-level=2 \ | ||
| --ignore-url='sitemap\.xml\.gz' \ | ||
| --ignore-url='https://github.com/.*/edit/' \ | ||
| --ignore-url='https://www.youtube.com/' \ | ||
| --ignore-url='.*/assets/.*' \ | ||
| --ignore-url='.*/images/.*' | ||
|
|
||
|
|
||
| RESULT_EXTERNAL=$? | ||
| set -e | ||
|
|
||
| # Clean up | ||
| rm -rf "$TMP_BUILD_DIR" | ||
|
|
||
| if [ $RESULT_EXTERNAL -ne 0 ]; then | ||
| echo "External linkcheck failed! Please fix broken links before pushing." | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "External linkcheck passed!" | ||
| exit 0 | ||
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.