fix: ToC so it doesn’t include itself or duplicate the end marker (#4388)
turns out the ToC was including itself when generating, which messed up comparisons and sometimes made the file rewrite endlessly. also fixed the slice so `<!-- End ToC -->` doesn’t get duplicated when we insert the new ToC. should behave nicely now - no extra rewrites, no doubled markers. Co-authored-by: Eric Traut <etraut@openai.com>
This commit is contained in:
@@ -87,8 +87,9 @@ def check_or_fix(readme_path: Path, fix: bool) -> int:
|
||||
# extract current ToC list items
|
||||
current_block = lines[begin_idx + 1 : end_idx]
|
||||
current = [l for l in current_block if l.lstrip().startswith("- [")]
|
||||
# generate expected ToC
|
||||
expected = generate_toc_lines(content)
|
||||
# generate expected ToC from content without current ToC
|
||||
toc_content = lines[:begin_idx] + lines[end_idx+1:]
|
||||
expected = generate_toc_lines("\n".join(toc_content))
|
||||
if current == expected:
|
||||
return 0
|
||||
if not fix:
|
||||
@@ -108,7 +109,7 @@ def check_or_fix(readme_path: Path, fix: bool) -> int:
|
||||
return 1
|
||||
# rebuild file with updated ToC
|
||||
prefix = lines[: begin_idx + 1]
|
||||
suffix = lines[end_idx:]
|
||||
suffix = lines[end_idx+1:]
|
||||
new_lines = prefix + [""] + expected + [""] + suffix
|
||||
readme_path.write_text("\n".join(new_lines) + "\n", encoding="utf-8")
|
||||
print(f"Updated ToC in {readme_path}.")
|
||||
|
||||
Reference in New Issue
Block a user