fix: keep split chapter anchors on their first fragment - #52
Conversation
textsplit.py duplicates a chapter's wrapping element (and its id) into every fragment produced when a large spine file is split, so the anchor map's last-write-wins assignment always ended up pointing the TOC/NCX anchor at the chapter's last fragment instead of its first. Readers whose spine->TOC lookup falls back to the previous entry for un-tocced spine items (CrossPoint and YACP both do) then show the previous chapter's title for nearly the entire chapter. Use setdefault so the first fragment containing the id wins instead.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review. 📜 Recent review details🔇 Additional comments (1)
📝 WalkthroughWalkthroughThe split process now keeps the first chunk mapped to a duplicated fragment ID. Subsequent chunks no longer overwrite that mapping during fragment link remapping. ChangesSplit anchor mapping
Estimated code review effort: 1 (Trivial) | ~2 minutes Merge Risk: ⚪ Minimal · up to The change makes split chapter links resolve to the chapter’s first fragment, with no actionable merge-blocking risk remaining after normal checks and review. Suggested reviewers: 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Regression check against a real library (578 books)Ran both
Zero regressions: no book split differently, no new errors, and every anchor target that changed moved to an earlier (more correct) fragment, never a later one. 29 books actually hit the bug pre-fix — this isn't a one-book edge case. A few examples (title — chapters affected):
All 29 were fully corrected by the one-line |
DisclosureThe root-cause investigation (issue #51), this fix, and the regression test across a 578-book library were carried out with Claude Code. I did not have attribution enabled in that session, so it isn't reflected in the commit metadata — flagging it here for transparency. |
Summary
Fixes #51.
textsplit.pyduplicates a chapter's wrapping element (and itsid) into every fragment produced when a large spine file is split. The anchor-map assignment in the split loop was last-write-wins, so it always ended up pointing the chapter's TOC/NCX anchor at its last fragment instead of its first.CrossPoint's (and YACP's) spine→TOC lookup falls back to the previous entry for any spine item with no direct TOC match, by design — reasonable for genuinely un-tocced continuation files, but it means every fragment before the mis-pointed last one now shows the previous chapter's title, correcting only right before the next chapter begins.
Fix
One-line change:
setdefaultinstead of assignment, so the first fragment containing the id wins.Verification
Regenerated the epub for the book that surfaced this and confirmed the resulting
toc.ncxnow points every split chapter at its first fragment instead of its last. Also reproduced and re-verified against a minimal synthetic fixture (attached on #51) with no copyrighted content — same before/after behavior.