feat(pdf): extract bookmarks as markdown headings for hierarchical parsing#403
Merged
MaojiaSheng merged 3 commits intovolcengine:mainfrom Mar 4, 2026
Merged
Conversation
…rsing Addresses Phase 1 of #393 — PDF bookmark/outline extraction. **Problem:** When parsing large structured PDFs (textbooks, pharmacopoeias, standards), the local pdfplumber strategy only outputs `<!-- Page N -->` HTML comment markers. Since MarkdownParser._find_headings() ignores HTML comments, it finds zero headings and falls back to paragraph-based splitting, producing hundreds of flat numbered files with no semantic organization. **Solution:** - Add `_extract_bookmarks()` method that extracts PDF outlines via pdfminer's `doc.get_outlines()` (accessed through pdfplumber) - Map bookmark destinations to page numbers by resolving page object IDs - Inject bookmarks as markdown headings (`#`, `##`, etc.) at the correct page positions before the page's text content - This allows MarkdownParser's existing heading-based splitting to build a hierarchical directory structure naturally **Details:** - Heading levels capped at 1-6 for markdown compatibility - Empty/whitespace-only titles are skipped - Unresolvable page destinations gracefully result in page_num=None (bookmark is logged but not injected at a wrong position) - New `bookmarks_extracted` metadata field tracks extraction count - Fully backward-compatible: PDFs without bookmarks behave identically **Tests:** - 7 unit tests covering: normal extraction, no outlines, missing method, empty titles, level capping, unresolved pages, and exception handling Ref: #393
b91cab2 to
2109834
Compare
1 task
MaojiaSheng
approved these changes
Mar 4, 2026
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
Implements Phase 1 of #393 — PDF bookmark/outline extraction and injection as markdown headings.
Problem
When parsing large structured PDFs (textbooks, pharmacopoeias, standards documents), the local pdfplumber strategy only outputs
<!-- Page N -->HTML comment markers. SinceMarkdownParser._find_headings()ignores HTML comments, it finds zero headings and falls back to paragraph-based splitting. This produces hundreds of flat numbered files (name_1.mdthroughname_538.md) with no semantic organization.Solution
_extract_bookmarks()method that extracts PDF outlines via pdfminer'sdoc.get_outlines()(accessed through pdfplumber)#,##, etc.) at the correct page positions before each page's text contentBefore
After (for PDFs with bookmarks)
Details
page_num=None(not injected at wrong position)bookmarks_extractedmetadata field addedTests
7 unit tests added in
tests/parse/test_pdf_bookmark_extraction.py:get_outlinesmethod → empty listFuture Work (from #393)
MAX_CHILDREN_PER_DIRthreshold)Ref: #393