Skip to main content

scripts-fix-md042

#!/usr/bin/env python3 """

"""

MD042: No empty links

import re from pathlib import Path

def fix_md042(content): fixed = 0 # Remove links with empty text: content, n1 = re.subn(r'[]([^)]+)', '', content) # Remove reference links with empty text: [] content, n2 = re.subn(r'[][[^]]+]', '', content) return content, n1 + n2

def process_file(file_path, dry_run=False): try: with open(file_path, 'r', encoding='utf-8') as f: content = f.read() fixed_content, count = fix_md042(content) if count > 0 and not dry_run: with open(file_path, 'w', encoding='utf-8') as f: f.write(fixed_content) return count except Exception as e: print(f"Error: {e}") return 0

if name == 'main': import argparse parser = argparse.ArgumentParser(description='Fix MD042') 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:
for md_file in Path(path_str).rglob('*.md'):
count = process_file(md_file, args.dry_run)
if count:
print(f"{'[DRY RUN] ' if args.dry_run else ''}Fixed {count} issues in {md_file}")
total += count
print(f"\nTotal: {total} issues fixed")