Skip to content

fix(epub): optimize spine membership lookup and avoid leading blank lines on missing metadata - #2552

Open
Chau Vu (chauvuusvn) wants to merge 1 commit into
microsoft:mainfrom
chauvuusvn:fix/epub-spine-membership-and-empty-metadata
Open

Chau Vu (chauvuusvn) wants to merge 1 commit into
microsoft:mainfrom
chauvuusvn:fix/epub-spine-membership-and-empty-metadata

Conversation

@chauvuusvn

Copy link
Copy Markdown

Description

This PR addresses two improvements in EpubConverter:

  1. O(1) Set Membership Lookup: In EpubConverter.convert(), the loop over spine called if file in z.namelist(): on every chapter iteration, repeatedly reconstructing the archive entry list ((N \times M)$). We now utilize the precomputed zip_names set ((1)$).
  2. Avoid Leading Blank Lines when Metadata is Missing: When an EPUB document lacks metadata tags (dc:title, dc:creator, etc.), metadata_markdown is empty, but markdown_content.insert(0, "\n".join(metadata_markdown)) still inserted an empty string into index 0, producing leading blank lines in the final markdown output. We now only prepend metadata when non-empty.
  3. Filter Empty Chapter Items: Ensures empty converted spine items don't accumulate excessive line breaks.

Testing

  • Added regression test test_epub_without_metadata_has_no_leading_blank_lines() in packages/markitdown/tests/test_epub_converter.py.
  • All 5/5 tests in test_epub_converter.py pass.

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