fix(file-parse): keep pptx line breaks and fields in document order (#66)
This commit is contained in:
@@ -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 <a:t>02139</a:t> becomes a number and loses characters)
|
||||
const parser = new XMLParser({ ignoreAttributes: true, trimValues: false, parseTagValue: false })
|
||||
// no numeric coercion of tag values (otherwise <a:t>02139</a:t> becomes a number and loses characters).
|
||||
// preserveOrder keeps <a:br> and <a:fld> in sequence with the <a:r> 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. <a:br> is a soft line break, and
|
||||
* <a:fld> (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<string> {
|
||||
const zip = await JSZip.loadAsync(bytes)
|
||||
const slidePaths = Object.keys(zip.files)
|
||||
|
||||
@@ -98,8 +98,16 @@ export async function buildPptxFixture(): Promise<Uint8Array> {
|
||||
'ppt/slides/slide3.xml',
|
||||
`${XML_DECL}<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" ` +
|
||||
'xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">' +
|
||||
'<p:cSld><p:spTree><p:sp><p:txBody><a:p>' +
|
||||
'<p:cSld><p:spTree><p:sp><p:txBody>' +
|
||||
// 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
|
||||
'<!-- authoring note: <a:br/> and <a:fld> -->' +
|
||||
'<a:p>' +
|
||||
'<a:r><a:t>Before</a:t></a:r><a:br>\n </a:br><a:br>\n </a:br><a:r><a:t>After</a:t></a:r>' +
|
||||
'</a:p>\n <a:p>\n ' +
|
||||
'<a:r><a:t xml:space="preserve">Page </a:t></a:r>\n ' +
|
||||
'<a:fld id="{G}" type="slidenum"><a:t>3</a:t></a:fld>\n ' +
|
||||
'<a:r><a:t xml:space="preserve"> of 10</a:t></a:r>\n ' +
|
||||
'</a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>',
|
||||
)
|
||||
zip.file('ppt/slides/slide10.xml', slideXml([['Summary Slide']]))
|
||||
|
||||
@@ -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')
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user