@lofcz/pptxtojson 2.2.3 → 2.2.4

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.
package/dist/index.js CHANGED
@@ -9786,27 +9786,97 @@ async function parse(file, options = {}) {
9786
9786
  }
9787
9787
  };
9788
9788
  }
9789
+ function asNodeArray(node) {
9790
+ if (!node) return [];
9791
+ return node.constructor === Array ? node : [
9792
+ node
9793
+ ];
9794
+ }
9795
+ function sortSlideXml(p1, p2) {
9796
+ const n1 = +(/(\d+)\.xml/.exec(p1)?.[1] || 0);
9797
+ const n2 = +(/(\d+)\.xml/.exec(p2)?.[1] || 0);
9798
+ return n1 - n2;
9799
+ }
9800
+ function resolvePresentationTarget(target) {
9801
+ let relTarget = String(target || '').replace(/\\/g, '/');
9802
+ if (!relTarget) return '';
9803
+ if (0 === relTarget.indexOf('/ppt/')) return relTarget.substr(1);
9804
+ if (-1 !== relTarget.indexOf('../')) return relTarget.replace('../', 'ppt/');
9805
+ if (0 === relTarget.indexOf('ppt/')) return relTarget;
9806
+ return 'ppt/' + relTarget.replace(/^\//, '');
9807
+ }
9808
+ function relationshipRid(attrs) {
9809
+ if (!attrs) return '';
9810
+ return attrs['r:id'] || attrs.rId || '';
9811
+ }
9812
+ async function getSlidesFromPresentation(zip) {
9813
+ const relsContent = await readXmlFile(zip, 'ppt/_rels/presentation.xml.rels');
9814
+ const relItems = asNodeArray(getTextByPathList(relsContent, [
9815
+ 'Relationships',
9816
+ 'Relationship'
9817
+ ]));
9818
+ const relsMap = {};
9819
+ const slideTargets = [];
9820
+ for (const rel of relItems){
9821
+ const id = getTextByPathList(rel, [
9822
+ 'attrs',
9823
+ 'Id'
9824
+ ]);
9825
+ const type = getTextByPathList(rel, [
9826
+ 'attrs',
9827
+ 'Type'
9828
+ ]);
9829
+ const target = getTextByPathList(rel, [
9830
+ 'attrs',
9831
+ 'Target'
9832
+ ]);
9833
+ if (!id || !target) continue;
9834
+ const loc = resolvePresentationTarget(target);
9835
+ if (loc) {
9836
+ relsMap[id] = loc;
9837
+ if ('slide' === getRelTypeName(type)) slideTargets.push(loc);
9838
+ }
9839
+ }
9840
+ const presentation = await readXmlFile(zip, 'ppt/presentation.xml');
9841
+ const sldIds = asNodeArray(getTextByPathList(presentation, [
9842
+ 'p:presentation',
9843
+ 'p:sldIdLst',
9844
+ 'p:sldId'
9845
+ ]));
9846
+ const ordered = [];
9847
+ for (const sldId of sldIds){
9848
+ const rid = relationshipRid(sldId?.attrs);
9849
+ if (rid && relsMap[rid]) ordered.push(relsMap[rid]);
9850
+ }
9851
+ if (ordered.length) return ordered;
9852
+ return slideTargets.sort(sortSlideXml);
9853
+ }
9789
9854
  async function getContentTypes(zip) {
9790
9855
  const ContentTypesJson = await readXmlFile(zip, '[Content_Types].xml');
9791
- const subObj = ContentTypesJson['Types']['Override'];
9856
+ const overrides = asNodeArray(getTextByPathList(ContentTypesJson, [
9857
+ 'Types',
9858
+ 'Override'
9859
+ ]));
9792
9860
  let slidesLocArray = [];
9793
9861
  let slideLayoutsLocArray = [];
9794
- for (const item of subObj)switch(item['attrs']['ContentType']){
9795
- case 'application/vnd.openxmlformats-officedocument.presentationml.slide+xml':
9796
- slidesLocArray.push(item['attrs']['PartName'].substr(1));
9797
- break;
9798
- case 'application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml':
9799
- slideLayoutsLocArray.push(item['attrs']['PartName'].substr(1));
9800
- break;
9801
- default:
9862
+ for (const item of overrides){
9863
+ const contentType = item?.attrs?.ContentType;
9864
+ const partName = item?.attrs?.PartName;
9865
+ if (!contentType || !partName) continue;
9866
+ const loc = String(partName).replace(/^\//, '');
9867
+ switch(contentType){
9868
+ case 'application/vnd.openxmlformats-officedocument.presentationml.slide+xml':
9869
+ slidesLocArray.push(loc);
9870
+ break;
9871
+ case 'application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml':
9872
+ slideLayoutsLocArray.push(loc);
9873
+ break;
9874
+ default:
9875
+ }
9802
9876
  }
9803
- const sortSlideXml = (p1, p2)=>{
9804
- const n1 = +/(\d+)\.xml/.exec(p1)[1];
9805
- const n2 = +/(\d+)\.xml/.exec(p2)[1];
9806
- return n1 - n2;
9807
- };
9808
9877
  slidesLocArray = slidesLocArray.sort(sortSlideXml);
9809
9878
  slideLayoutsLocArray = slideLayoutsLocArray.sort(sortSlideXml);
9879
+ if (!slidesLocArray.length) slidesLocArray = await getSlidesFromPresentation(zip);
9810
9880
  return {
9811
9881
  slides: slidesLocArray,
9812
9882
  slideLayouts: slideLayoutsLocArray