Fix text loss at page boundaries during incremental parsing#96
Open
alfarhan wants to merge 1 commit intobigbag:mainfrom
Open
Fix text loss at page boundaries during incremental parsing#96alfarhan wants to merge 1 commit intobigbag:mainfrom
alfarhan wants to merge 1 commit intobigbag:mainfrom
Conversation
All format parsers (TXT, Markdown, EPUB, FB2) share a bug where text is silently dropped when a paragraph spans a page-batch boundary during incremental parsing (hot extend). Root cause: when layoutAndExtractLines() is called and the page batch limit (maxPages) is reached mid-paragraph, the processLine callback stops adding lines to pages. However, the extract loop continues consuming words from the ParsedText — calling extractLine() which destructively erases words. After flushBlock returns false, the ParsedText is destroyed or reset, permanently losing those words. The next parsing session resumes from a saved file offset that is past the lost text. The fix has two parts: 1. Pass an abort callback to layoutAndExtractLines() so it stops the extract loop when the page limit is hit. This preserves unconsumed words in the ParsedText object. 2. Preserve the interrupted ParsedText (as a class member) so the remaining words are flushed at the start of the next parsePages call instead of being discarded. Parsers fixed: - PlainTextParser: added pendingBlock_ member, abort callback - MarkdownParser: added pendingTextBlock_ member, abort callback, also fixed memory-flush path (includeLastLine=false) - Fb2Parser: added abort callback, conditional textBlock reset - ChapterHtmlSlimParser: changed includeLastLine from false to true in emergency memory-pressure split (the abort callback was already passed correctly) Tested on Xteink X4 (ESP32-C3) with long Arabic RTL markdown files that previously exhibited missing text between pages. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
All format parsers (TXT, Markdown, EPUB, FB2) silently drop text when a paragraph spans a page-batch boundary during incremental parsing (hot extend). This manifests as missing sentences/paragraphs when reading — the user turns the page and text that should follow is gone.
Root Cause
When
layoutAndExtractLines()is called and the page batch limit (maxPages) is reached mid-paragraph:processLinecallback stops adding lines to pages (returns or sets a flag)extractLine()destructively erases words from theParsedTextobjectflushBlockreturns false, theParsedTextis destroyed/reset — remaining words are permanently lostparsePagescall resumes from a saved file offset that is past the lost textThis affects every parser because they all share the same pattern of calling
layoutAndExtractLines()without an abort callback to stop word consumption when the page limit is hit.The Fix
Part 1 — Stop consuming words when the page limit is hit:
Pass an abort callback to
layoutAndExtractLines()that returnstruewhen the page batch limit is reached. The existing abort check atParsedText.cpp:286-287already handles this correctly — it returnsfalsewithout consuming more words. The problem was that no caller was passing this callback.Part 2 — Preserve unconsumed words for the next batch:
When the page limit interrupts a paragraph mid-layout, the
ParsedTextobject still contains the remaining words. Instead of destroying it, save it as a class member (pendingBlock_/pendingTextBlock_) and flush it at the start of the nextparsePagescall.Parsers Fixed
pendingBlock_member, abort callback influshBlock, resume logicpendingTextBlock_member, abort callback influshTextBlockand memory-flush pathcurrentTextBlock_resetincludeLastLinefromfalsetotruein emergency split (abort callback was already passed)Test plan
🤖 Generated with Claude Code