Skip to content

fix(markdown): avoid import stack overflow and keep final pages reachable - #31

Open
mkroemer wants to merge 2 commits into
p10node:mainfrom
mkroemer:fix/large-markdown-import-scroll
Open

mkroemer wants to merge 2 commits into
p10node:mainfrom
mkroemer:fix/large-markdown-import-scroll

Conversation

@mkroemer

Copy link
Copy Markdown
Contributor

Opening a 50 MB Markdown file fails with Maximum call stack size exceeded because the importer passes all parsed paragraphs to one push(...items) call. After resolving that, the resulting 21,112-page document exceeds the browser’s element-height limit, leaving its final pages unreachable.

This PR fixes both:

  • Uses append loops for document-sized arrays in Markdown and HTML imports.
  • Bounds the document’s physical scroll height and maps it to logical page positions, following the existing spreadsheet viewer’s approach.
  • Applies that mapping to caret, find, outline and comment navigation, waiting for pages to reposition before measuring a navigation target.

The existing DOM rendering and visual editing path are retained, with no new runtime dependencies.

Regression coverage includes 150,000-paragraph Markdown and HTML inputs, a 150,000-line code block, and a document beyond the browser’s scroll-height limit whose final page is reached, searched, edited and saved. The original 50 MB fixture now loads completely, and its final marker is reachable and searchable.

Validation

  • All four new Chromium regression tests fail on upstream main and pass with this change.
  • 697 unit tests and 35 browser tests passed on this PR’s commit.
  • Typecheck and production build passed.
  • git diff --check passed.

The browser checks cover the new regressions plus existing document editing, selection, page setup and comments.

A caret whose paragraph moved to another page is repaired first; a caret on a page out of view then scrolls it into view.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@pierreneter pierreneter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, both fixes look right to me. The append loops remove every document-sized spread in the Markdown and HTML import paths. The scroll mapping follows XlsxView (same 12,000,000 px cap). It changes nothing for documents under the cap, where scrollScale is 1 and scrollOffset is 0. Find, outline, comment and caret navigation all go through scrollToY / scrollByY, and the only raw scrollTop left (imgBox, DocxView.tsx:596) is a physical measurement, so it is correct as it is. This is from reading the code; I have not run it.

About merge order (not a defect in this PR): this conflicts with #8 in src/react/docx/DocxView.tsx, and the stack adds a scroll write of its own. The controller's gotoPage (DocxView.tsx:836 at #9's head fef4db0) does sc.scrollTop = Math.max(0, top - 16), a logical position written to the physical scrollTop. Past the cap, ctl.goto to a page lands about scrollScale times too far down. The stack is close to merging, so I'd rebase this onto main after it and send gotoPage (and the published cur page) through scrollToY / scrollY while resolving the conflict.

Non-blocking, worth a follow-up issue rather than more work here:

  • Past the cap, the pages sit in the scrolled content and are moved by top={tops[i] - scrollOffset}. A native scroll first moves them by the physical distance, and the next frame moves them the rest of the way. The same applies to scrolls the browser makes by itself in the contenteditable: caret into view on arrow keys or Enter at the bottom edge, and autoscroll while drag-selecting. The browser scrolls by the visual distance and the next render moves the content scrollScale times as far, so the caret overshoots by (scale − 1) times the step. At the 50 MB file's scale (about 2) this is mild. It also re-renders every mounted page on each scroll frame. The spreadsheet avoids both because .de-grid-view stays fixed in the viewport while the spacer scrolls. Moving one wrapper instead of each page would at least remove the re-renders.
  • MAX_SCROLL_PX is now defined in both DocxView.tsx and XlsxView.tsx. One shared constant would keep them from drifting apart.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants