@openpresentation/opf-pptx 0.9.1 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,9 @@ const NS = 'http://schemas.openxmlformats.org/presentationml/2006/main';
8
8
  const enc = new TextEncoder(), dec = new TextDecoder('utf-8', {fatal:true});
9
9
  const parser = new XMLParser({ignoreAttributes:false, attributeNamePrefix:'', parseTagValue:false, trimValues:false});
10
10
  const ordered = new XMLParser({ignoreAttributes:false, attributeNamePrefix:'', parseTagValue:false, trimValues:false, preserveOrder:true});
11
+ // Numeric XML character references carry native CR without XML line-ending
12
+ // normalization. Decode once while parsing notes, never again on parsed text.
13
+ const orderedNotes = new XMLParser({ignoreAttributes:false, attributeNamePrefix:'', parseTagValue:false, trimValues:false, preserveOrder:true, htmlEntities:true});
11
14
  const array = value => value === undefined ? [] : Array.isArray(value) ? value : [value];
12
15
  const hex = value => [...enc.encode(JSON.stringify(value))].map(byte=>byte.toString(16).padStart(2,'0')).join('').toUpperCase();
13
16
  function unhex(value) {
@@ -64,13 +67,16 @@ export const nativeTextShapes = tree => [...array(tree?.['p:sp']),...array(tree?
64
67
  const orderedShapes = tree => [...children(tree,'p:sp'),...children(tree,'p:grpSp').flatMap(orderedShapes)];
65
68
  // Preserve the order of runs, fields and explicit line breaks. A keyed XML
66
69
  // object groups all a:r before a:fld and cannot represent their original order.
67
- export function nativeShapeParagraphs(xml) {
68
- const root = children(ordered.parse(xml),'p:sld')[0];
70
+ export function nativeShapeParagraphs(xml, rootElement = 'p:sld') {
71
+ const reader = rootElement === 'p:notes' ? orderedNotes : ordered;
72
+ const root = children(reader.parse(xml),rootElement)[0];
69
73
  const tree = children(children(root,'p:cSld')[0],'p:spTree')[0];
70
74
  return orderedShapes(tree).map(shape=>children(children(shape,'p:txBody')[0],'a:p').map(paragraph=>{
71
75
  let text = '', maxFontSize = 0, bullet = false, level = 0;
76
+ const fields = [];
72
77
  for (const child of paragraph) {
73
78
  if (child['a:br'] !== undefined) text += '\n';
79
+ if (child['a:fld'] !== undefined) fields.push({type: String(child[':@']?.type ?? ''), text: children(child['a:fld'],'a:t').map(plainText).join('')});
74
80
  for (const key of ['a:r','a:fld']) if (child[key] !== undefined) {
75
81
  text += children(child[key],'a:t').map(plainText).join('');
76
82
  for (const run of child[key]) {
@@ -83,7 +89,7 @@ export function nativeShapeParagraphs(xml) {
83
89
  bullet = child['a:pPr'].some(node=>node['a:buChar'] !== undefined || node['a:buAutoNum'] !== undefined);
84
90
  }
85
91
  }
86
- return {text,maxFontSize,bullet,level};
92
+ return {text,maxFontSize,bullet,level,fields};
87
93
  }));
88
94
  }
89
95