"""Fix H1 headings in md files: keep first # as chapter, demote rest to ##.""" import os base = r'e:/Project/SI/2026_DesignAI/officefile' files_to_fix = [ '01-introduction/01-introduction.md', '03-1d-sequence/03-1d-sequence.md', '05-3d-spatial/05-3d-spatial.md', '08-reinforcement/08-reinforcement.md', ] for rel_path in files_to_fix: fpath = os.path.join(base, rel_path) with open(fpath, 'r', encoding='utf-8') as f: lines = f.readlines() seen_first_h1 = False changes = 0 in_code_block = False for i, line in enumerate(lines): # Track fenced code blocks if line.startswith('```'): in_code_block = not in_code_block continue if in_code_block: continue # Only target H1 headings (start with "# " but not "## ") if line.startswith('# ') and not line.startswith('## '): if not seen_first_h1: seen_first_h1 = True continue # Keep the first H1 as chapter title # Demote: "# heading" -> "## heading" lines[i] = '#' + line # "# " -> "## " changes += 1 with open(fpath, 'w', encoding='utf-8') as f: f.writelines(lines) print(f'{rel_path}: demoted {changes} H1 -> H2 (kept first as chapter)')