Skip to content

fix: drop PDF page markers and stop chunks from splitting words - #540

Merged
RobertLD merged 1 commit into
mainfrom
fix/pdf-text-cleanup
Nov 27, 2026
Merged

RobertLD merged 1 commit into
mainfrom
fix/pdf-text-cleanup

Conversation

@RobertLD

Copy link
Copy Markdown
Owner

Problem

Chunks from indexed PDFs had two defects that hurt search:

  • Page markers were indexed as text: -- 12 of 92 --, sometimes twice in a row.
  • Chunks and overlap prefixes started or ended mid-word, for example a chunk ending ...the Enterprise Message API r and the next starting esorts to, or an overlap starting ction within an application.

Causes

  • pdf-parse 2.x getText() defaults pageJoiner to '\n-- page_number of total_number --' (src/core/parsers/pdf.ts).
  • splitAtParagraphs hard-split an oversized paragraph every maxSize characters. PDF text has few blank lines, so most PDF chunks went through this path.
  • addChunkOverlap took prev.slice(-overlapChars) at a raw offset.
  • The streaming chunker (content over 1 MB) started each window at windowEnd - overlap, an arbitrary character, and findWindowBoundary accepted any . (including 3.10.0) or cut at end.

Fix

  • getText({ pageJoiner: "" }). Pages are still joined by a blank line, so words do not fuse across pages.
  • splitAtParagraphs splits at the last whitespace within the limit. It hard-splits only a single token longer than the limit.
  • addChunkOverlap drops a leading partial word from the overlap. Han, Hiragana and Katakana are exempt, so CJK text keeps its overlap.
  • Streaming chunker: window starts move forward to the next whitespace. When there is none, the next window starts at windowEnd, which the previous window already covered. findWindowBoundary takes a . only when whitespace follows it, then a newline, then whitespace, then the end of the text, within 200 characters.

Effect on other documents

Any document with a paragraph larger than maxChunkSize gets slightly different chunk boundaries. Nothing depends on chunk boundaries: document dedup and change detection hash the whole content. Already-indexed documents keep their chunks until re-indexed. A PDF's content hash changes because the markers are gone, so it re-indexes once when next seen.

Tests

New tests in tests/unit/indexing.test.ts and tests/unit/parsers.test.ts (a two-page PDF built in the test). Each fails without its fix, except two guards that pin unchanged behaviour: the long-token hard split and pure CJK overlap.

Verification

  • npm run format:check, lint (0 errors), typecheck, npm test: 2154 tests pass.
  • Fuzzed chunkContent and chunkContentStreaming (10,000 random texts each, with spaces, tabs, CRLF, NBSP, CJK, dotted tokens and over-long words). No empty chunks, no lost or reordered words, no oversized chunks, no non-terminating runs, and no mid-word cut where whitespace was available.
  • A 3 MB single paragraph through splitIntoChunks: 0 fragments and 0 lost tokens, against 3,103 fragments and 1,506 lost tokens on main.

Not verified

  • Real PDFs, beyond the synthetic test PDF and reading the pdf-parse source.
  • Thai, Lao and Khmer: they follow the space-delimited rule.
  • test:coverage, build and docs:check were not run on this branch.

pdf-parse 2.x inserts "-- N of M --" between pages by default; pass an empty pageJoiner so pages are joined by a blank line only.

Oversized paragraphs now split at the last whitespace within the limit, overlap prefixes start at a word boundary (keeping CJK text, which has no spaces), and the streaming chunker for content over 1 MB snaps window starts and ends to whitespace and only treats a period followed by whitespace as a sentence end.
@vercel

vercel Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
libscope Ignored Ignored Preview Nov 27, 2026 3:22pm UTC

@sonarqubecloud

Copy link
Copy Markdown

@RobertLD
RobertLD merged commit 8ec921d into main Nov 27, 2026
10 checks passed
@RobertLD
RobertLD deleted the fix/pdf-text-cleanup branch November 27, 2026 15:26
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.

1 participant