[Bug] PPTX loader skips every other slide when the slide parts are stored consecutively
Client Type
Other: server-side file parsing in packages/file-loaders, so not client specific.
Operating System
macOS (not OS specific)
Deployment Platform
Other (not deployment specific)
Version
canary at f5931b12b53
Browser
Other (not browser related)
What happened?
PptxLoader.loadPages (packages/file-loaders/src/loaders/pptx/index.ts) drops every second slide of many real presentations, and reports nothing.
The slide XML files are picked with a regex that has the g flag, reused across test() calls:
const slidesRegex = /ppt\/slides\/slide\d+\.xml/g;
const slideFiles = await extractFiles(filePath, (fileName) => slidesRegex.test(fileName));A global regex keeps lastIndex between test() calls. After ppt/slides/slide1.xml matches, lastIndex is 21. The next entry, ppt/slides/slide2.xml, is searched from offset 21, which is its end, so it does not match. That failure resets lastIndex to 0, so slide3.xml matches again, and so on.
Whether a file is hit depends on its zip entry order. When each slide is followed by its _rels entry, the failed test on the rels entry resets lastIndex and nothing is lost. The bundled test.pptx is laid out that way, which is why the snapshot test passes. When the slide parts are stored next to each other, as PowerPoint writes them, half the slides disappear.
How to reproduce it?
Replaying the loader's filter over the entry names of .pptx test files from other open source projects:
| file | slides | loaded |
|---|---|---|
microsoft/markitdown tests/test_files/test.pptx |
6 | 3 |
Unstructured-IO/unstructured example-docs/science-exploration-369p.pptx |
368 | 185 |
Unstructured-IO/unstructured example-docs/simple.pptx |
2 | 1 |
docling-project/docling tests/data/pptx/sources/pptx_chart.pptx |
2 | 1 |
docling-project/docling tests/data/pptx/sources/powerpoint_comments.pptx |
3 | 2 |
lobehub src/loaders/pptx/fixtures/corrupted-slides.pptx |
2 | 1 |
The corrupted fixture loses slide1.xml. Its test still passes because both of its slides are malformed.
// node replay.js <file.pptx> (entry names in central directory order, as yauzl reads them)
const names = require('child_process')
.execFileSync('unzip', ['-Z1', process.argv[2]]).toString().trim().split('\n');
const slidesRegex = /ppt\/slides\/slide\d+\.xml/g; // as in the loader
console.log(names.filter((n) => /ppt\/slides\/slide\d+\.xml/.test(n)).length, 'slides');
console.log(names.filter((n) => slidesRegex.test(n)).length, 'loaded');I also checked it end to end with the loader. A 4-slide deck built with python-pptx, re-zipped so that slide1.xml to slide4.xml are consecutive entries, gives pages for slides 1 and 3 only. It gives all four once the flag is removed.
What it should be?
Every slide is loaded, whatever the entry order.
Additional Information
The fix is to drop the g flag. The regex is only used with test():
const slidesRegex = /ppt\/slides\/slide\d+\.xml/;With that change, the existing pptx/index.test.ts still passes (7/7 with the test below), including both corrupted-fixture tests. A regression test needs a fixture whose slide parts are consecutive entries:
it('should load every slide when the slide parts are stored next to each other', async () => {
const pages = await loader.loadPages(fixturePath('consecutive-slides.pptx'));
expect(pages.map((page) => page.metadata.slideNumber)).toEqual([1, 2, 3, 4]);
});The other /g regexes in file-loaders (in the ipynb loader) are used with replaceAll or with an explicit lastIndex, so they are fine.
I searched the existing issues and pull requests and found no report of this.
️ Willing to Submit a PR?
No, but I am happy to help test the fix.
Source: lobehub/lobehub