Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

chore: exclude link checker scripts from version control

f-trycua770867bc98a9
diff --git a/.gitignore b/.gitignore
index bfff79d1ee..e1d11b8ced 100644
--- a/.gitignore
+++ b/.gitignore
@@ -205,4 +205,10 @@ storage/
.Trash-1000/
post-provision
# Local secrets for act
-.secrets
\ No newline at end of file
+.secrets
+
+# Link checker scripts (dev tools)
+scripts/check-repo-md-links.py
+docs/scripts/check-links.py
+docs/scripts/check-all-links.py
+docs/scripts/check-mdx-links.py
\ No newline at end of file
diff --git a/docs/scripts/check-all-links.py b/docs/scripts/check-all-links.py
deleted file mode 100755
index 37299831f6..0000000000
--- a/docs/scripts/check-all-links.py
+++ /dev/null
@@ -1,288 +0,0 @@
-#!/usr/bin/env python3
-"""
-Comprehensive Link Checker - Checks for broken internal links across all file types.
-
-This script scans various file types in the docs directory and validates
-that all internal links point to existing files.
-
-Supported file types:
-- MDX files (.mdx)
-- TypeScript/JavaScript files (.ts, .tsx, .js, .jsx)
-- JSON files (.json)
-- CSS files (.css)
-
-Usage:
- python scripts/check-all-links.py [docs_dir]
-
-Example:
- python scripts/check-all-links.py
- python scripts/check-all-links.py /path/to/docs
-"""
-
-import os
-import re
-import sys
-from pathlib import Path
-from collections import defaultdict
-
-
-# File extensions to scan
-SCAN_EXTENSIONS = {'.mdx', '.md', '.tsx', '.ts', '.jsx', '.js', '.json', '.css'}
-
-# Directories to skip
-SKIP_DIRS = {'node_modules', '.next', '.git', 'dist', 'build', '.turbo', '.cache'}
-
-
-def find_files(base_dir: Path) -> list[Path]:
- """Find all scannable files in the directory."""
- files = []
- for root, dirs, filenames in os.walk(base_dir):
- # Skip certain directories
- dirs[:] = [d for d in dirs if d not in SKIP_DIRS]
-
- for filename in filenames:
- path = Path(root) / filename
- if path.suffix in SCAN_EXTENSIONS:
- files.append(path)
- return files
-
-
-def extract_links_from_content(content: str, file_type: str) -> list[tuple[str, int]]:
- """Extract all internal links from content with line numbers."""
- links = []
- lines = content.split('\n')
-
- for line_num, line in enumerate(lines, 1):
- # Skip comments
- stripped = line.strip()
- if stripped.startswith('//') or stripped.startswith('*') or stripped.startswith('/*'):
- continue
-
- # Markdown links: [text](/path) or [text](/path#anchor)
- md_link_pattern = r'\[([^\]]*)\]\((/[^)#\s]+)(?:#[^)]*)?\)'
- for match in re.finditer(md_link_pattern, line):
- links.append((match.group(2), line_num))
-
- # JSX/HTML href attributes: href="/path" or href='/path'
- href_pattern = r'href=["\'](/[^"\'#\s]+)(?:#[^"\']*)?["\']'
- for match in re.finditer(href_pattern, line):
- links.append((match.group(1), line_num))
-
- # JSX/HTML src attributes: src="/path" or src='/path'
- src_pattern = r'src=["\'](/[^"\'#\s]+)["\']'
- for match in re.finditer(src_pattern, line):
- links.append((match.group(1), line_num))
-
- # Next.js Link component: to="/path" or to='/path'
- to_pattern = r'\bto=["\'](/[^"\'#\s]+)(?:#[^"\']*)?["\']'
- for match in re.finditer(to_pattern, line):
- links.append((match.group(1), line_num))
-
- # JavaScript/TypeScript string paths (common patterns)
- # e.g., path: '/cua/guide', redirect: '/docs/cua'
- js_path_pattern = r'(?:path|href|to|redirect|url|link|route):\s*["\'](/[^"\'#\s]+)["\']'
- for match in re.finditer(js_path_pattern, line, re.IGNORECASE):
- links.append((match.group(1), line_num))
-
- # Image markdown: ![alt](/path)
- img_md_pattern = r'!\[[^\]]*\]\((/[^)#\s]+)(?:#[^)]*)?\)'
- for match in re.finditer(img_md_pattern, line):
- links.append((match.group(1), line_num))
-
- return links
-
-
-def normalize_link(link: str, docs_dir: Path, public_dir: Path, src_dir: Path) -> tuple[Path | None, str]:
- """Convert a link to a file path. Returns (path, type)."""
- # Skip external links
- if link.startswith(('http://', 'https://', 'mailto:', 'tel:', '#', 'data:')):
- return None, 'external'
-
- # Remove leading slash
- clean_link = link.lstrip('/')
-
- # Skip Next.js internal routes
- if clean_link.startswith('_next/'):
- return None, 'internal'
-
- # Check if it's a public asset (images, etc.)
- # With basePath: '/docs', image paths are /docs/img/... which maps to public/img/
- if clean_link.startswith('docs/img/'):
- return public_dir / clean_link[5:], 'public' # Remove 'docs/' prefix
- if clean_link.startswith('docs/favicon'):
- return public_dir / clean_link[5:], 'public' # Remove 'docs/' prefix
- if clean_link.startswith(('img/', 'images/', 'favicon', 'icons/')):
- return public_dir / clean_link, 'public'
-
- # Check if it's a docs path
- if clean_link.startswith(('cua/', 'cuabench/')):
- docs_content_dir = docs_dir / 'content' / 'docs'
- return docs_content_dir / clean_link, 'docs'
-
- # Check source directory for components, etc.
- if clean_link.startswith(('components/', 'lib/', 'app/', 'src/')):
- return src_dir / clean_link, 'src'
-
- # Default: check in public directory first, then docs
- public_path = public_dir / clean_link
- if public_path.exists() or (public_path.parent.exists() and any(public_path.parent.glob(f"{public_path.name}*"))):
- return public_path, 'public'
-
- # Check docs content
- docs_content_dir = docs_dir / 'content' / 'docs'
- return docs_content_dir / clean_link, 'docs'
-
-
-def check_file_exists(file_path: Path, link_type: str) -> bool:
- """Check if a file exists (with various extensions and index files)."""
- # Direct match
- if file_path.exists():
- return True
-
- # For docs, try various MDX/MD patterns
- if link_type == 'docs':
- # Try with .mdx extension
- mdx_path = file_path.with_suffix('.mdx')
- if mdx_path.exists():
- return True
-
- # Try with .md extension
- md_path = file_path.with_suffix('.md')
- if md_path.exists():
- return True
-
- # Try as directory with index.mdx
- index_mdx = file_path / 'index.mdx'
- if index_mdx.exists():
- return True
-
- # Try as directory with index.md
- index_md = file_path / 'index.md'
- if index_md.exists():
- return True
-
- # For public assets, check common image extensions
- if link_type == 'public':
- for ext in ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp', '.ico']:
- if file_path.with_suffix(ext).exists():
- return True
-
- # For source files, try various extensions
- if link_type == 'src':
- for ext in ['.tsx', '.ts', '.jsx', '.js', '.css']:
- if file_path.with_suffix(ext).exists():
- return True
- # Try index files
- for ext in ['.tsx', '.ts', '.jsx', '.js']:
- index_file = file_path / f'index{ext}'
- if index_file.exists():
- return True
-
- return False
-
-
-def check_links(docs_dir: Path) -> dict[str, list[tuple[str, int, str, str]]]:
- """Check all links in all files and return broken ones."""
- broken_links = defaultdict(list)
-
- public_dir = docs_dir / 'public'
- src_dir = docs_dir / 'src'
- content_dir = docs_dir / 'content'
-
- # Find all files to scan
- all_files = find_files(docs_dir)
-
- print(f"\nπŸ” Scanning {len(all_files)} files in {docs_dir}\n")
- print("-" * 60)
-
- # Track unique broken links to avoid duplicates
- seen_broken = set()
-
- for file_path in all_files:
- try:
- content = file_path.read_text(encoding='utf-8')
- except Exception as e:
- print(f"⚠️ Error reading {file_path}: {e}")
- continue
-
- file_type = file_path.suffix
- links = extract_links_from_content(content, file_type)
-
- for link, line_num in links:
- # Skip external and internal Next.js links
- if link.startswith(('http://', 'https://', 'mailto:', 'tel:', '#', 'data:')):
- continue
-
- target_path, link_type = normalize_link(link, docs_dir, public_dir, src_dir)
- if target_path is None:
- continue
-
- if not check_file_exists(target_path, link_type):
- # Create unique key to avoid duplicates
- key = (str(file_path), link, line_num)
- if key not in seen_broken:
- seen_broken.add(key)
- rel_file = file_path.relative_to(docs_dir)
- broken_links[str(rel_file)].append((link, line_num, str(target_path), link_type))
-
- return broken_links
-
-
-def main():
- # Determine docs directory
- if len(sys.argv) > 1:
- docs_dir = Path(sys.argv[1])
- else:
- # Default: look for docs directory relative to script location
- script_dir = Path(__file__).parent
- docs_dir = script_dir.parent
-
- if not docs_dir.exists():
- print(f"❌ Directory not found: {docs_dir}")
- sys.exit(1)
-
- broken_links = check_links(docs_dir)
-
- # Summary
- print("\n" + "=" * 60)
-
- total_broken = sum(len(links) for links in broken_links.values())
- files_with_broken = len(broken_links)
-
- print(f"\nπŸ“Š Summary:")
- print(f" Files with broken links: {files_with_broken}")
- print(f" Total broken links: {total_broken}")
-
- if broken_links:
- print(f"\n❌ Broken Links:\n")
-
- # Group by link type
- by_type = defaultdict(list)
- for file_path, links in sorted(broken_links.items()):
- for link, line_num, target_path, link_type in links:
- by_type[link_type].append((file_path, link, line_num, target_path))
-
- for link_type, items in sorted(by_type.items()):
- print(f"\n πŸ“ {link_type.upper()} links ({len(items)}):\n")
- for file_path, link, line_num, target_path in items:
- print(f" {file_path}:{line_num}")
- print(f" Link: {link}")
- print(f" Expected: {target_path}")
- print()
-
- # Suggest fixes
- print("\nπŸ’‘ Common issues to check:")
- print(" - Links with /docs/ prefix should NOT have /docs/ (basePath handles it)")
- print(" - Internal doc links should start with /cua/ or /cuabench/")
- print(" - Image paths should start with /img/ and exist in public/img/")
- print(" - Check if target files were renamed or moved")
- print()
-
- return 1
- else:
- print("\nβœ… All internal links are valid!\n")
- return 0
-
-
-if __name__ == '__main__':
- sys.exit(main())
diff --git a/docs/scripts/check-links.py b/docs/scripts/check-links.py
deleted file mode 100755
index 1bc1c92a34..0000000000
--- a/docs/scripts/check-links.py
+++ /dev/null
@@ -1,175 +0,0 @@
-#!/usr/bin/env python3
-"""
-Link checker for the docs website.
-Crawls all pages starting from the base URL and reports broken links.
-
-Usage:
- python scripts/check-links.py [base_url]
-
-Example:
- python scripts/check-links.py http://localhost:8090
-"""
-
-import sys
-import urllib.request
-import urllib.parse
-import urllib.error
-from html.parser import HTMLParser
-from collections import deque
-import ssl
-
-# Ignore SSL certificate errors for local development
-ssl._create_default_https_context = ssl._create_unverified_context
-
-
-class LinkExtractor(HTMLParser):
- """Extract all href and src attributes from HTML."""
-
- def __init__(self):
- super().__init__()
- self.links = []
-
- def handle_starttag(self, tag, attrs):
- attrs_dict = dict(attrs)
- if tag == 'a' and 'href' in attrs_dict:
- self.links.append(attrs_dict['href'])
- elif tag in ('img', 'script') and 'src' in attrs_dict:
- self.links.append(attrs_dict['src'])
- elif tag == 'link' and 'href' in attrs_dict:
- self.links.append(attrs_dict['href'])
-
-
-def normalize_url(base_url, link):
- """Normalize a link relative to a base URL."""
- # Skip mailto, tel, javascript, and anchor-only links
- if link.startswith(('mailto:', 'tel:', 'javascript:', '#')):
- return None
-
- # Parse and join URLs
- parsed = urllib.parse.urljoin(base_url, link)
-
- # Remove fragment
- parsed = urllib.parse.urldefrag(parsed)[0]
-
- return parsed
-
-
-def is_internal(url, base_domain):
- """Check if URL is internal to the base domain."""
- parsed = urllib.parse.urlparse(url)
- return parsed.netloc == base_domain or parsed.netloc == ''
-
-
-def fetch_url(url, timeout=10):
- """Fetch a URL and return (status_code, content, content_type)."""
- try:
- req = urllib.request.Request(
- url,
- headers={'User-Agent': 'LinkChecker/1.0'}
- )
- response = urllib.request.urlopen(req, timeout=timeout)
- content_type = response.headers.get('Content-Type', '')
- content = response.read() if 'text/html' in content_type else b''
- return response.status, content, content_type
- except urllib.error.HTTPError as e:
- return e.code, b'', ''
- except urllib.error.URLError as e:
- return None, b'', str(e.reason)
- except Exception as e:
- return None, b'', str(e)
-
-
-def check_links(base_url):
- """Crawl the website and check all links."""
- parsed_base = urllib.parse.urlparse(base_url)
- base_domain = parsed_base.netloc
-
- visited = set()
- to_visit = deque([base_url])
- broken_links = []
- checked_external = {}
-
- print(f"\nπŸ” Checking links starting from {base_url}\n")
- print("-" * 60)
-
- while to_visit:
- current_url = to_visit.popleft()
-
- if current_url in visited:
- continue
-
- visited.add(current_url)
-
- # Only crawl internal pages
- if not is_internal(current_url, base_domain):
- continue
-
- status, content, content_type = fetch_url(current_url)
-
- if status is None:
- print(f"❌ ERROR: {current_url}")
- broken_links.append((current_url, 'Connection error', None))
- continue
- elif status >= 400:
- print(f"❌ {status}: {current_url}")
- broken_links.append((current_url, status, None))
- continue
- else:
- print(f"βœ“ {status}: {current_url}")
-
- # Parse HTML and extract links
- if 'text/html' in content_type and content:
- try:
- parser = LinkExtractor()
- parser.feed(content.decode('utf-8', errors='ignore'))
-
- for link in parser.links:
- normalized = normalize_url(current_url, link)
- if normalized is None:
- continue
-
- if is_internal(normalized, base_domain):
- # Queue internal links for crawling
- if normalized not in visited:
- to_visit.append(normalized)
- else:
- # Check external links once
- if normalized not in checked_external:
- ext_status, _, _ = fetch_url(normalized, timeout=5)
- checked_external[normalized] = ext_status
- if ext_status is None or ext_status >= 400:
- print(f" ⚠️ External broken: {normalized} (from {current_url})")
- broken_links.append((normalized, ext_status, current_url))
- except Exception as e:
- print(f" ⚠️ Parse error on {current_url}: {e}")
-
- # Summary
- print("\n" + "=" * 60)
- print(f"\nπŸ“Š Summary:")
- print(f" Pages checked: {len(visited)}")
- print(f" Broken links: {len(broken_links)}")
-
- if broken_links:
- print(f"\n❌ Broken Links:\n")
- for url, status, source in broken_links:
- status_str = str(status) if status else 'Error'
- if source:
- print(f" [{status_str}] {url}")
- print(f" Found on: {source}")
- else:
- print(f" [{status_str}] {url}")
- print()
- return 1
- else:
- print("\nβœ… All links are valid!\n")
- return 0
-
-
-if __name__ == '__main__':
- base_url = sys.argv[1] if len(sys.argv) > 1 else 'http://localhost:8090'
-
- # Ensure base URL has /docs prefix
- if not base_url.endswith('/docs'):
- base_url = base_url.rstrip('/') + '/docs'
-
- sys.exit(check_links(base_url))
diff --git a/docs/scripts/check-mdx-links.py b/docs/scripts/check-mdx-links.py
deleted file mode 100755
index fc9ec2898a..0000000000
--- a/docs/scripts/check-mdx-links.py
+++ /dev/null
@@ -1,194 +0,0 @@
-#!/usr/bin/env python3
-"""
-MDX Link Checker - Checks for broken internal links in MDX files.
-
-This script scans MDX files in the docs/content/docs directory and validates
-that all internal links point to existing files.
-
-Usage:
- python scripts/check-mdx-links.py [docs_dir]
-
-Example:
- python scripts/check-mdx-links.py
- python scripts/check-mdx-links.py /path/to/docs/content/docs
-"""
-
-import os
-import re
-import sys
-from pathlib import Path
-from collections import defaultdict
-
-
-def find_mdx_files(docs_dir: Path) -> list[Path]:
- """Find all MDX files in the docs directory."""
- return list(docs_dir.rglob("*.mdx"))
-
-
-def extract_links(content: str) -> list[tuple[str, int]]:
- """Extract all internal links from MDX content with line numbers."""
- links = []
-
- # Markdown links: [text](/path) or [text](/path#anchor)
- md_link_pattern = r'\[([^\]]*)\]\((/[^)#\s]+)(?:#[^)]*)?\)'
-
- # JSX href attributes: href="/path" or href='/path'
- jsx_href_pattern = r'href=["\'](/[^"\'#\s]+)(?:#[^"\']*)?["\']'
-
- lines = content.split('\n')
- for line_num, line in enumerate(lines, 1):
- # Find markdown links
- for match in re.finditer(md_link_pattern, line):
- links.append((match.group(2), line_num))
-
- # Find JSX href attributes
- for match in re.finditer(jsx_href_pattern, line):
- links.append((match.group(1), line_num))
-
- return links
-
-
-def normalize_link_to_file_path(link: str, docs_dir: Path, public_dir: Path) -> tuple[Path | None, str]:
- """Convert an internal link to a file path. Returns (path, type)."""
- # Skip external links and anchors
- if link.startswith(('http://', 'https://', 'mailto:', 'tel:', '#')):
- return None, 'external'
-
- # Remove leading slash
- clean_link = link.lstrip('/')
-
- # Check if it's a public asset (images, etc.)
- # With basePath: '/docs', image paths are /docs/img/... which maps to public/img/
- if clean_link.startswith('docs/img/'):
- return public_dir / clean_link[5:], 'public' # Remove 'docs/' prefix
- if clean_link.startswith('docs/favicon'):
- return public_dir / clean_link[5:], 'public' # Remove 'docs/' prefix
- if clean_link.startswith(('img/', '_next/', 'favicon')):
- return public_dir / clean_link, 'public'
-
- # Skip links that go outside docs (like /get-started which doesn't exist)
- if not clean_link.startswith(('cua/', 'cuabench/')):
- # These are likely old/invalid paths
- return docs_dir / clean_link, 'docs'
-
- # Build the file path
- file_path = docs_dir / clean_link
-
- return file_path, 'docs'
-
-
-def check_file_exists(file_path: Path) -> bool:
- """Check if a file exists (with or without .mdx extension, or as index.mdx)."""
- # Direct match
- if file_path.exists():
- return True
-
- # Try with .mdx extension
- mdx_path = file_path.with_suffix('.mdx')
- if mdx_path.exists():
- return True
-
- # Try as directory with index.mdx
- index_path = file_path / 'index.mdx'
- if index_path.exists():
- return True
-
- # Try parent directory with filename.mdx (for paths like /cua/guide)
- parent_index = file_path.parent / (file_path.name + '.mdx')
- if parent_index.exists():
- return True
-
- return False
-
-
-def check_links(docs_dir: Path, public_dir: Path) -> dict[str, list[tuple[str, int, str]]]:
- """Check all links in MDX files and return broken ones."""
- broken_links = defaultdict(list)
-
- mdx_files = find_mdx_files(docs_dir)
-
- print(f"\nπŸ” Scanning {len(mdx_files)} MDX files in {docs_dir}\n")
- print("-" * 60)
-
- for mdx_file in mdx_files:
- try:
- content = mdx_file.read_text(encoding='utf-8')
- except Exception as e:
- print(f"⚠️ Error reading {mdx_file}: {e}")
- continue
-
- links = extract_links(content)
-
- for link, line_num in links:
- # Skip external links
- if link.startswith(('http://', 'https://', 'mailto:', 'tel:')):
- continue
-
- # Skip anchor-only links
- if link.startswith('#'):
- continue
-
- file_path, link_type = normalize_link_to_file_path(link, docs_dir, public_dir)
- if file_path is None:
- continue
-
- if not check_file_exists(file_path):
- rel_file = mdx_file.relative_to(docs_dir)
- broken_links[str(rel_file)].append((link, line_num, str(file_path)))
-
- return broken_links
-
-
-def main():
- # Determine docs directory
- if len(sys.argv) > 1:
- docs_dir = Path(sys.argv[1])
- else:
- # Default: look for docs/content/docs relative to script location
- script_dir = Path(__file__).parent
- docs_dir = script_dir.parent / 'content' / 'docs'
-
- # Public directory for static assets
- public_dir = docs_dir.parent.parent / 'public'
-
- if not docs_dir.exists():
- print(f"❌ Directory not found: {docs_dir}")
- sys.exit(1)
-
- broken_links = check_links(docs_dir, public_dir)
-
- # Summary
- print("\n" + "=" * 60)
-
- total_broken = sum(len(links) for links in broken_links.values())
- files_with_broken = len(broken_links)
-
- print(f"\nπŸ“Š Summary:")
- print(f" Files with broken links: {files_with_broken}")
- print(f" Total broken links: {total_broken}")
-
- if broken_links:
- print(f"\n❌ Broken Links:\n")
-
- for file_path, links in sorted(broken_links.items()):
- print(f" πŸ“„ {file_path}")
- for link, line_num, target_path in links:
- print(f" Line {line_num}: {link}")
- print(f" β†’ Expected: {target_path}")
- print()
-
- # Suggest fixes for common patterns
- print("\nπŸ’‘ Common issues to check:")
- print(" - Links starting with /docs/ should NOT have /docs/ prefix")
- print(" - Internal links should start with /cua/ or /cuabench/")
- print(" - Check if the target file was renamed or moved")
- print()
-
- return 1
- else:
- print("\nβœ… All internal links are valid!\n")
- return 0
-
-
-if __name__ == '__main__':
- sys.exit(main())
diff --git a/scripts/check-repo-md-links.py b/scripts/check-repo-md-links.py
deleted file mode 100755
index 75ab76965a..0000000000
--- a/scripts/check-repo-md-links.py
+++ /dev/null
@@ -1,203 +0,0 @@
-#!/usr/bin/env python3
-"""
-Check for broken internal links in all .md and .mdx files across the entire repository.
-This script validates:
-- Relative links to other markdown files
-- Relative links to directories
-- Absolute paths within the repo
-- Image references
-"""
-
-import os
-import re
-import sys
-from pathlib import Path
-from collections import defaultdict
-from urllib.parse import urlparse
-
-# Directories to skip
-SKIP_DIRS = {'node_modules', '.next', '.git', 'dist', 'build', '.turbo', '.cache', '__pycache__', '.venv', 'venv'}
-
-# Link patterns
-MD_LINK_PATTERN = re.compile(r'\[([^\]]*)\]\(([^)]+)\)')
-HTML_HREF_PATTERN = re.compile(r'(?:href|src)=["\']([^"\']+)["\']')
-
-
-def find_md_files(root_dir: Path) -> list[Path]:
- """Find all .md and .mdx files in the repository."""
- md_files = []
- for dirpath, dirnames, filenames in os.walk(root_dir):
- # Skip unwanted directories
- dirnames[:] = [d for d in dirnames if d not in SKIP_DIRS]
-
- for filename in filenames:
- if filename.endswith(('.md', '.mdx')):
- md_files.append(Path(dirpath) / filename)
-
- return md_files
-
-
-def extract_links(file_path: Path) -> list[tuple[int, str]]:
- """Extract all links from a markdown file. Returns list of (line_number, link)."""
- links = []
- try:
- content = file_path.read_text(encoding='utf-8')
- except Exception as e:
- print(f"Warning: Could not read {file_path}: {e}")
- return links
-
- for line_num, line in enumerate(content.split('\n'), 1):
- # Find markdown links
- for match in MD_LINK_PATTERN.finditer(line):
- link = match.group(2).split('#')[0].strip() # Remove anchors
- if link:
- links.append((line_num, link))
-
- # Find HTML href/src attributes
- for match in HTML_HREF_PATTERN.finditer(line):
- link = match.group(1).split('#')[0].strip()
- if link:
- links.append((line_num, link))
-
- return links
-
-
-def is_external_link(link: str) -> bool:
- """Check if a link is external."""
- return link.startswith(('http://', 'https://', 'mailto:', 'tel:', 'ftp://', '//'))
-
-
-def is_anchor_only(link: str) -> bool:
- """Check if a link is an anchor-only link."""
- return link.startswith('#')
-
-
-def resolve_link(link: str, source_file: Path, repo_root: Path) -> tuple[Path | None, str]:
- """
- Resolve a link to an absolute path.
- Returns (resolved_path, link_type).
- """
- if is_external_link(link) or is_anchor_only(link):
- return None, 'external'
-
- # Handle absolute paths (starting with /)
- if link.startswith('/'):
- # Could be repo-relative or docs-relative
- clean_link = link.lstrip('/')
-
- # Check if it's a docs path
- if clean_link.startswith(('docs/', 'cua/', 'cuabench/')):
- # This is likely a docs link - check in docs/content/docs
- docs_content = repo_root / 'docs' / 'content' / 'docs'
- if clean_link.startswith('docs/'):
- return docs_content / clean_link[5:], 'docs'
- return docs_content / clean_link, 'docs'
-
- # Check if it exists from repo root
- return repo_root / clean_link, 'repo'
-
- # Handle relative paths
- source_dir = source_file.parent
- resolved = (source_dir / link).resolve()
-
- return resolved, 'relative'
-
-
-def check_path_exists(path: Path) -> bool:
- """Check if a path exists, considering it could be a file or directory."""
- if path.exists():
- return True
-
- # Try with common extensions
- for ext in ['.md', '.mdx', '.tsx', '.ts', '.jsx', '.js', '.py', '.json']:
- if path.with_suffix(ext).exists():
- return True
-
- # Try as directory with index file
- if path.is_dir():
- for index in ['index.md', 'index.mdx', 'README.md']:
- if (path / index).exists():
- return True
-
- # Check if it's a file reference without extension
- parent = path.parent
- name = path.name
- if parent.exists():
- # Check for any file with this base name
- matches = list(parent.glob(f"{name}.*"))
- if matches:
- return True
-
- return False
-
-
-def main():
- # Determine repo root (go up from script location)
- script_dir = Path(__file__).resolve().parent
- repo_root = script_dir.parent # Assuming script is in /scripts
-
- # Allow override via command line
- if len(sys.argv) > 1:
- repo_root = Path(sys.argv[1]).resolve()
-
- print(f"Scanning for .md/.mdx files in: {repo_root}\n")
-
- md_files = find_md_files(repo_root)
- print(f"Found {len(md_files)} markdown files\n")
-
- broken_links = defaultdict(list)
- total_links = 0
- external_links = 0
-
- for md_file in md_files:
- links = extract_links(md_file)
-
- for line_num, link in links:
- total_links += 1
-
- if is_external_link(link) or is_anchor_only(link):
- external_links += 1
- continue
-
- resolved_path, link_type = resolve_link(link, md_file, repo_root)
-
- if resolved_path is None:
- continue
-
- if not check_path_exists(resolved_path):
- rel_file = md_file.relative_to(repo_root)
- broken_links[str(rel_file)].append({
- 'line': line_num,
- 'link': link,
- 'expected': str(resolved_path),
- 'type': link_type
- })
-
- # Print results
- print("=" * 70)
- print(f"\nSummary:")
- print(f" Total links checked: {total_links}")
- print(f" External/anchor links (skipped): {external_links}")
- print(f" Internal links checked: {total_links - external_links}")
- print(f" Broken links found: {sum(len(v) for v in broken_links.values())}")
- print(f" Files with broken links: {len(broken_links)}")
-
- if broken_links:
- print("\n" + "=" * 70)
- print("\nBroken Links:\n")
-
- for file_path, links in sorted(broken_links.items()):
- print(f" {file_path}")
- for info in links:
- print(f" Line {info['line']}: {info['link']}")
- print(f" -> Expected: {info['expected']}")
- print()
-
- sys.exit(1)
- else:
- print("\n All internal links are valid!")
- sys.exit(0)
-
-
-if __name__ == '__main__':
- main()