import re
from pathlib import Path
def fix_links_in_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
original_content = content
pattern = r'\[([^\]]+)\]\(([^\)]+)\)'
def replace_link(match):
link_text = match.group(1)
url = match.group(2)
if url.startswith('http://') or url.startswith('https://') or \
url.startswith('mailto:') or url.startswith('#') or \
url.startswith('ftp://') or url.startswith('file://'):
return match.group(0)
if url.endswith('.md') or url.endswith('.md#'):
return match.group(0)
if '#' in url:
path, anchor = url.split('#', 1)
if not path:
return match.group(0)
if path.upper() in ['SECURITY', 'RELEASE']:
return match.group(0)
new_url = f"{path}.md#{anchor}"
else:
if url.upper() in ['SECURITY', 'RELEASE']:
return match.group(0)
new_url = f"{url}.md"
return f"[{link_text}]({new_url})"
new_content = re.sub(pattern, replace_link, content)
if new_content != original_content:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
return True
return False
except Exception as e:
print(f"Error processing {file_path}: {e}")
return False
def main():
docs_dir = Path("docs")
if not docs_dir.exists():
print("Error: docs directory not found")
return
md_files = list(docs_dir.rglob("*.md"))
print(f"Found {len(md_files)} markdown files")
print("\nAdding .md extensions to relative links...")
fixed_count = 0
for md_file in md_files:
if fix_links_in_file(md_file):
try:
rel_path = md_file.relative_to(Path.cwd())
except ValueError:
rel_path = md_file
print(f" Fixed: {rel_path}")
fixed_count += 1
print(f"\nFixed {fixed_count} files")
if __name__ == "__main__":
main()