diff --git a/packages/file-parse/src/pptx.ts b/packages/file-parse/src/pptx.ts
index fba2f61..5aadefa 100644
--- a/packages/file-parse/src/pptx.ts
+++ b/packages/file-parse/src/pptx.ts
@@ -2,56 +2,60 @@ import JSZip from 'jszip'
import { XMLParser } from 'fast-xml-parser'
// Text fidelity: no trim (xml:space="preserve" runs carry the spaces between words),
-// no numeric coercion of tag values (otherwise 02139 becomes a number and loses characters)
-const parser = new XMLParser({ ignoreAttributes: true, trimValues: false, parseTagValue: false })
+// no numeric coercion of tag values (otherwise 02139 becomes a number and loses characters).
+// preserveOrder keeps and in sequence with the runs around them; grouped by
+// tag name they lose that position, and a deck's soft breaks and field text land in the wrong place.
+const parser = new XMLParser({
+ ignoreAttributes: true,
+ trimValues: false,
+ parseTagValue: false,
+ preserveOrder: true,
+})
function slideNumber(path: string): number {
const m = /slide(\d+)\.xml$/.exec(path)
return m ? Number(m[1]) : 0
}
-/** collect the text of all a:t descendants of one node */
-function collectText(node: unknown, out: string[], isText = false): void {
- if (node == null) return
- if (typeof node === 'string' || typeof node === 'number') {
- // untrimmed, a non-a:t element written across lines carries its whitespace as a value
- if (isText) out.push(String(node))
- return
- }
- if (Array.isArray(node)) {
- for (const item of node) collectText(item, out, isText)
- return
- }
- if (typeof node === 'object') {
+/**
+ * One paragraph's text in document order. Only #text directly under a:t counts: untrimmed, the
+ * whitespace laying out any other element is a value too. is a soft line break, and
+ * (slide number, date) contributes its own a:t where it sits.
+ */
+function collectText(nodes: readonly unknown[], out: string[], isText = false): void {
+ for (const node of nodes) {
+ if (node == null || typeof node !== 'object') continue
for (const [key, value] of Object.entries(node)) {
- if (key === 'a:t') collectText(value, out, true)
- else if (typeof value === 'object') collectText(value, out)
+ if (key === '#text') {
+ if (isText) out.push(String(value))
+ } else if (key === 'a:br') {
+ out.push('\n')
+ } else if (Array.isArray(value)) {
+ collectText(value, out, key === 'a:t')
+ }
}
}
}
-/** walk the slide tree; each a:p paragraph becomes one output line */
-function collectParagraphs(node: unknown, out: string[]): void {
- if (node == null || typeof node !== 'object') return
- if (Array.isArray(node)) {
- for (const item of node) collectParagraphs(item, out)
- return
- }
- for (const [key, value] of Object.entries(node)) {
- if (key === 'a:p') {
- for (const para of Array.isArray(value) ? value : [value]) {
+/** walk the slide tree; each a:p paragraph becomes one output entry (a:br splits it further) */
+function collectParagraphs(nodes: readonly unknown[], out: string[]): void {
+ for (const node of nodes) {
+ if (node == null || typeof node !== 'object') continue
+ for (const [key, value] of Object.entries(node)) {
+ if (!Array.isArray(value)) continue
+ if (key === 'a:p') {
const texts: string[] = []
- collectText(para, texts)
+ collectText(value, texts)
const line = texts.join('')
if (line.trim()) out.push(line)
+ } else {
+ collectParagraphs(value, out)
}
- } else {
- collectParagraphs(value, out)
}
}
}
-/** extract slide text from a pptx: one "## Slide N" section per slide, one line per paragraph */
+/** extract slide text from a pptx: one "## Slide N" section per slide, a line per paragraph */
export async function pptxToText(bytes: Uint8Array): Promise {
const zip = await JSZip.loadAsync(bytes)
const slidePaths = Object.keys(zip.files)
diff --git a/packages/file-parse/tests/helpers/fixtures.ts b/packages/file-parse/tests/helpers/fixtures.ts
index 4ffd8a0..5c718e3 100644
--- a/packages/file-parse/tests/helpers/fixtures.ts
+++ b/packages/file-parse/tests/helpers/fixtures.ts
@@ -98,8 +98,16 @@ export async function buildPptxFixture(): Promise {
'ppt/slides/slide3.xml',
`${XML_DECL}' +
- '' +
+ '' +
+ // a comment naming the very tags the walker looks for: rewriting markup as text
+ // instead of parsing it consumes the terminator and the whole slide fails to parse
+ '' +
+ '' +
'Before\n \n After' +
+ '\n \n ' +
+ 'Page \n ' +
+ '3\n ' +
+ ' of 10\n ' +
'',
)
zip.file('ppt/slides/slide10.xml', slideXml([['Summary Slide']]))
diff --git a/packages/file-parse/tests/office-formats.test.ts b/packages/file-parse/tests/office-formats.test.ts
index 2c618b2..3719fc2 100644
--- a/packages/file-parse/tests/office-formats.test.ts
+++ b/packages/file-parse/tests/office-formats.test.ts
@@ -38,10 +38,22 @@ describe('parseFileToText: pptx', () => {
expect(result.text).toContain('Order 0042')
})
+ it('keeps a:br as a line break and a:fld in document order', async () => {
+ const path = writeFixture('deck.pptx', await buildPptxFixture())
+ const result = await parseFileToText(path)
+ // two breaks, so the run text is not concatenated and the field lands between its runs;
+ // the slide also carries a comment naming those tags, which must not reach the walker
+ expect(result.text).toContain('## Slide 3\nBefore\n\nAfter\nPage 3 of 10')
+ })
+
it('takes text from a:t only, not from whitespace inside sibling elements', async () => {
const path = writeFixture('deck.pptx', await buildPptxFixture())
const result = await parseFileToText(path)
- expect(result.text!.split('\n\n')).toContain('## Slide 3\nBeforeAfter')
+ // the a:br elements are written across lines; that layout whitespace is a value too
+ expect(result.text).toContain('Before')
+ expect(result.text).not.toMatch(/Before\n[^\S\n]/)
+ // and the comment the slide carries is markup, not text
+ expect(result.text).not.toContain('authoring note')
})
})