Skip to content

fix(parser): slice PDF TOC prefix by characters, not bytes - #19745

Closed
coderdailyone wants to merge 1 commit into
infiniflow:mainfrom
coderdailyone:fix/pdf-toc-cjk-prefix
Closed

coderdailyone wants to merge 1 commit into
infiniflow:mainfrom
coderdailyone:fix/pdf-toc-cjk-prefix

Conversation

@coderdailyone

Copy link
Copy Markdown
Contributor

Summary

removePDFTOC ports Python's remove_contents_table (rag/nlp/init.py): after dropping the "目录"/"contents" heading and the first entry, it deletes all sections up to the next one sharing that entry's 3-character prefix — the matching body heading.

sectionTextPrefix computed the prefix as text[:3] (bytes) instead of the first 3 characters. For the standard Chinese TOC entry "第一章 绪论" the byte slice yields only "第", so the terminator scan matched the very next "第..." chapter entry, deleted nothing, and the whole remaining TOC entry list survived into chunks. A leading multi-byte rune whose width is not a multiple of 3 could also produce an invalid UTF-8 prefix matching nothing.

Fix: convert to []rune and slice the first n runes, matching Python's get(i)[:3] and the sibling tocPrefix in text_toc.go.

Regression test: TestApplyPDFPostProcess_RemoveTOCPrefixScanCJKEntries in internal/parser/parser/pdf_postprocess_test.go — fails with the source change reverted (kept 第二章 背景, 第三章 方法), passes with the fix.

Type of change

  • Bug Fix (non-breaking change which fixes an issue)

Fixes #19744

🤖 Generated with Claude Code

https://claude.ai/code/session_01CRrBenkCCr4pacMNwR4NuG

removePDFTOC ports Python's remove_contents_table (rag/nlp/__init__.py):
after dropping the TOC heading and first entry it deletes all sections up
to the next one sharing that entry's 3-character prefix. sectionTextPrefix
computed the prefix as text[:3] (bytes), so a Chinese entry like
"第一章 绪论" yielded only "第" — the terminator scan then matched the
very next 第-prefixed chapter entry, deleted nothing, and the whole
remaining TOC list survived into chunks. Slice runes instead, matching
Python's get(i)[:3].

Regression test: TestApplyPDFPostProcess_RemoveTOCPrefixScanCJKEntries
fails with the source change reverted.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CRrBenkCCr4pacMNwR4NuG
@coderabbitai

coderabbitai Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 13131140-4330-4c85-ab05-41e8f9c3891b

📥 Commits

Reviewing files that changed from the base of the PR and between 701b82a and 72adaad.

📒 Files selected for processing (2)
  • internal/parser/parser/pdf_postprocess.go
  • internal/parser/parser/pdf_postprocess_test.go

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

The PDF TOC prefix helper now slices Unicode characters instead of bytes. A regression test covers CJK chapter entries during TOC removal.

Changes

PDF TOC prefix handling

Layer / File(s) Summary
Rune-based prefix and CJK regression
internal/parser/parser/pdf_postprocess.go, internal/parser/parser/pdf_postprocess_test.go
sectionTextPrefix now uses rune slicing. The test verifies that CJK TOC entries are removed and body content remains.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: euvre

Merge Risk: ⚪ Minimal · up to 72ada

The PDF TOC removal update preserves complete Unicode characters and includes coverage for CJK entries; no merge-blocking issue remains.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: updating PDF TOC prefix slicing to use characters instead of bytes.
Description check ✅ Passed The description includes the required Summary section and clearly explains the bug, root cause, fix, regression test, change type, and linked issue.
Linked Issues check ✅ Passed The change satisfies issue #19744. sectionTextPrefix now converts text to []rune and returns the first n runes, so 2-byte and 4-byte runes do not produce invalid UTF-8 prefixes. The new `TestApp…
Out of Scope Changes check ✅ Passed The reviewed changes are limited to the sectionTextPrefix fix and regression coverage in the related PDF post-processing test. Both changes directly support issue #19744. No unrelated implementation…
  • Fix all pre-merge checks with AI

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.

❤️ Share

A rabbit found three runes in line
The chapter trail now reads just fine
No broken bytes leap through
The TOC fades from view
And正文 stays safe to shine

Comment @coderabbitai help to get the list of available commands.

@JinHai-CN JinHai-CN added the ci Continue Integration label Sep 17, 2026
@xugangqiang

Copy link
Copy Markdown
Contributor

LGTM

@yuzhichang

Copy link
Copy Markdown
Member

#19779 is better than this.

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

Labels

ci Continue Integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PDF TOC removal: sectionTextPrefix slices bytes, not characters, so CJK TOC entries leak into chunks

4 participants