diff --git a/.github/hooks/pre-commit b/.github/hooks/pre-commit new file mode 100644 index 0000000..deb7f5d --- /dev/null +++ b/.github/hooks/pre-commit @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +import re +import requests +import subprocess +import sys +import os + +def check_links(file_path): + with open(file_path, 'r') as file: + content = file.read() + + # Regex to find Markdown links + markdown_link_regex = r'\[([^\]]+)\]\((http[s]?://[^)]+)\)' + links = re.findall(markdown_link_regex, content) + + broken_links = [] + for label, url in links: + try: + response = requests.head(url, allow_redirects=True, timeout=10) + if response.status_code >= 400: + broken_links.append((label, url)) + except requests.RequestException: + broken_links.append((label, url)) + + return broken_links + +def get_markdown_files(): + """Returns a list of Markdown files to check in both local and CI/CD environments.""" + if os.getenv("GITHUB_ACTIONS"): # Running in GitHub Actions + result = subprocess.run( + ["git", "ls-files", "*.md"], capture_output=True, text=True + ) + return result.stdout.split() + else: # Running as a pre-commit hook locally + result = subprocess.run( + ["git", "diff", "--cached", "--name-only"], capture_output=True, text=True + ) + return [f for f in result.stdout.split() if f.endswith('.md')] + +def main(): + markdown_files = get_markdown_files() + + exit_code = 0 + for file_path in markdown_files: + broken_links = check_links(file_path) + if broken_links: + print(f"❌ Error: Broken links found in {file_path}:") + for label, url in broken_links: + print(f" - [{label}]({url})") + exit_code = 1 + + sys.exit(exit_code) + +if __name__ == "__main__": + main() diff --git a/.github/workflows/markdown-link-check.yaml b/.github/workflows/markdown-link-check.yaml new file mode 100644 index 0000000..e6b1e0d --- /dev/null +++ b/.github/workflows/markdown-link-check.yaml @@ -0,0 +1,27 @@ +name: Markdown Link Check + +on: + pull_request: + branches: + - main + - master # Adjust based on your default branch + +jobs: + link-check: + name: Check Markdown Links + runs-on: ubuntu-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + - name: Set Up Python + uses: actions/setup-python@v4 + with: + python-version: "3.9" + + - name: Install Dependencies + run: pip install requests + + - name: Run Markdown Link Checker + run: python .github/hooks/pre-commit