scripts-fix-md022-headings-blank-lines
#!/usr/bin/env python3 """
title: "Fix Md022 Headings Blank Lines" component_type: script version: "1.0.0" audience: contributor status: stable summary: "CODITECT Markdown Quality System Copyright © 2025 AZ1.AI INC - All Rights Reserved" keywords: ['blank', 'fix', 'headings', 'lines', 'md022'] tokens: ~500 created: 2025-12-22 updated: 2025-12-22 script_name: "fix-md022-headings-blank-lines.py" language: python executable: true usage: "python3 scripts/fix-md022-headings-blank-lines.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
This software is proprietary and confidential. Unauthorized copying, distribution, or use is strictly prohibited.
CODITECT owns all intellectual property rights to this implementation. """
"""MD022 Fix: Headings should be surrounded by blank lines""" import re from pathlib import Path
def is_heading(line: str) -> bool: return bool(re.match(r'^#{1,6}\s+', line.strip()))
def fix_md022(content: str): lines = content.split('\n') result = [] fixed = 0
for i, line in enumerate(lines):
if is_heading(line):
# Add blank before if needed
if i > 0 and result and result[-1].strip():
result.append('')
fixed += 1
result.append(line)
# Add blank after if needed
if i + 1 < len(lines) and lines[i + 1].strip() and not is_heading(lines[i + 1]):
result.append('')
fixed += 1
else:
result.append(line)
return '\n'.join(result), fixed
def process_file(file_path: Path, dry_run=False): with open(file_path, 'r', encoding='utf-8') as f: content = f.read() fixed_content, count = fix_md022(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(description='Fix MD022: Headings blank lines') 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':
# Process single file
count = process_file(path, args.dry_run)
if count:
print(f"{'[DRY RUN] ' if args.dry_run else ''}Fixed {count} headings in {path}")
total += count
elif path.is_dir():
# Process directory
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} headings in {md_file}")
total += count
print(f"\nTotal: {total} heading boundaries fixed")