diff --git a/src/generator/generator.py b/src/generator/generator.py index 4978067..f7f3e6a 100644 --- a/src/generator/generator.py +++ b/src/generator/generator.py @@ -455,14 +455,13 @@ def _remove_duplicate_headings(text: str) -> str: line = lines[i].rstrip() if line.startswith('# ') and not line.startswith('## '): h1_text = line[2:].strip().lower() - prev_heading = '' - for prev in reversed(result): - prev_stripped = prev.strip() - if prev_stripped: - if prev_stripped.startswith('## '): - prev_heading = prev_stripped[3:].strip().lower() - break - if prev_heading and prev_heading == h1_text: + window = result[-5:] if len(result) >= 5 else result[:] + recent_h2s = { + p.strip()[3:].strip().lower() + for p in window + if p.strip().startswith('## ') + } + if h1_text in recent_h2s: i += 1 continue result.append(lines[i])