Skip to content
Snippets Groups Projects
url-test.py 822 B
Newer Older
  • Learn to ignore specific revisions
  • #!/bin/python3
    
    import re
    import requests
    from pathlib import Path
    
    def check_links_in_mdx(directory):
        mdx_files = Path(directory).rglob("*.mdx")
        url_pattern = re.compile(r'\[.*?\]\((http[s]?://.*?)\)')
    
        for mdx_file in mdx_files:
            with open(mdx_file, "r", encoding="utf-8") as f:
                content = f.read()
                links = url_pattern.findall(content)
    
                for link in links:
                    try:
                        response = requests.head(link, allow_redirects=True, timeout=5)
                        if response.status_code >= 400:
                            print(f"Broken link in {mdx_file}: {link} (Status: {response.status_code})")
                    except requests.RequestException:
                        print(f"Error checking {link} in {mdx_file}")
    
    check_links_in_mdx("content/docs")