scripts-fix-md052-reference-links
#!/usr/bin/env python3 """
title: "MD052: Reference links should use defined label" component_type: script version: "1.0.0" audience: contributor status: stable summary: "CODITECT Markdown Quality System Copyright © 2025 AZ1.AI INC - All Rights Reserved" import r..." keywords: ['fix', 'links', 'md052', 'reference'] tokens: ~500 created: 2025-12-22 updated: 2025-12-22 script_name: "fix-md052-reference-links.py" language: python executable: true usage: "python3 scripts/fix-md052-reference-links.py [options]" python_version: "3.10+" dependencies: [] modifies_files: false network_access: false requires_auth: false
CODITECT Markdown Quality System Copyright © 2025 AZ1.AI INC - All Rights Reserved"""
MD052: Reference links should use defined label
import re from pathlib import Path
def fix_md052(content): # Find all reference link definitions defined_refs = set(re.findall(r'^[([^]]+)]:\s*', content, re.MULTILINE))
# Find all reference link usages
used_refs = set(re.findall(r'\[([^\]]+)\]\[([^\]]*)\]', content))
fixed = 0
# Remove undefined reference links
for text, ref in used_refs:
ref_name = ref if ref else text
if ref_name not in defined_refs:
content = content.replace(f'[{text}][{ref}]', text)
fixed += 1
return content, fixed
def process_file(file_path, dry_run=False): with open(file_path, 'r', encoding='utf-8') as f: content = f.read() fixed_content, count = fix_md052(content) if count > 0 and not dry_run: with open(file_path, 'w', encoding='utf-8') as f: f.write(fixed_content) return count
if name == 'main': import argparse parser = argparse.ArgumentParser() parser.add_argument('paths', nargs='*', default=['.']) parser.add_argument('--dry-run', action='store_true') args = parser.parse_args()
total = 0
for path_str in args.paths:
path = Path(path_str)
if path.is_file() and path.suffix == '.md':
count = process_file(path, args.dry_run)
if count:
print(f"{'[DRY RUN] ' if args.dry_run else ''}Fixed {count} in {path}")
total += count
elif path.is_dir():
for md_file in path.rglob('*.md'):
count = process_file(md_file, args.dry_run)
if count:
print(f"{'[DRY RUN] ' if args.dry_run else ''}Fixed {count} in {md_file}")
total += count
print(f"\nTotal: {total} fixed")