Kill markdown-section's help and end-of-section mutants

This commit is contained in:
2026-11-17 12:09:15 +01:00
parent 7460454d93
commit fd9215ac9c
2 changed files with 8 additions and 7 deletions
+2 -3
View File
@@ -56,8 +56,7 @@ def section(text: str, fragment: str) -> str | Failure:
found = headings(text)
wanted = fragment.strip().lower()
matching = [heading for heading in found if wanted in heading[2].lower()]
if len(matching) > 1:
matching = [heading for heading in matching if heading[2].lower() == wanted] or matching
matching = [heading for heading in matching if heading[2].lower() == wanted] or matching
if not matching:
return Failure(f"no heading holds '{fragment}'")
lines = text.splitlines()
@@ -65,5 +64,5 @@ def section(text: str, fragment: str) -> str | Failure:
listed = "\n".join(f"{index + 1}: {lines[index]}" for index, _, _ in matching)
return Failure(f"{len(matching)} headings hold '{fragment}', so use more of one:\n{listed}")
start, level, _ = matching[0]
end = next((index for index, deeper, _ in found if index > start and deeper <= level), len(lines))
end = next((index for index, deeper, _ in found if index > start and deeper <= level), None)
return "\n".join(lines[start:end]).rstrip("\n") + "\n"