@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.cjs CHANGED
@@ -9818,27 +9818,97 @@ async function parse(file, options = {}) {
9818
9818
  }
9819
9819
  };
9820
9820
  }
9821
+ function asNodeArray(node) {
9822
+ if (!node) return [];
9823
+ return node.constructor === Array ? node : [
9824
+ node
9825
+ ];
9826
+ }
9827
+ function sortSlideXml(p1, p2) {
9828
+ const n1 = +(/(\d+)\.xml/.exec(p1)?.[1] || 0);
9829
+ const n2 = +(/(\d+)\.xml/.exec(p2)?.[1] || 0);
9830
+ return n1 - n2;
9831
+ }
9832
+ function resolvePresentationTarget(target) {
9833
+ let relTarget = String(target || '').replace(/\\/g, '/');
9834
+ if (!relTarget) return '';
9835
+ if (0 === relTarget.indexOf('/ppt/')) return relTarget.substr(1);
9836
+ if (-1 !== relTarget.indexOf('../')) return relTarget.replace('../', 'ppt/');
9837
+ if (0 === relTarget.indexOf('ppt/')) return relTarget;
9838
+ return 'ppt/' + relTarget.replace(/^\//, '');
9839
+ }
9840
+ function relationshipRid(attrs) {
9841
+ if (!attrs) return '';
9842
+ return attrs['r:id'] || attrs.rId || '';
9843
+ }
9844
+ async function getSlidesFromPresentation(zip) {
9845
+ const relsContent = await readXmlFile(zip, 'ppt/_rels/presentation.xml.rels');
9846
+ const relItems = asNodeArray(getTextByPathList(relsContent, [
9847
+ 'Relationships',
9848
+ 'Relationship'
9849
+ ]));
9850
+ const relsMap = {};
9851
+ const slideTargets = [];
9852
+ for (const rel of relItems){
9853
+ const id = getTextByPathList(rel, [
9854
+ 'attrs',
9855
+ 'Id'
9856
+ ]);
9857
+ const type = getTextByPathList(rel, [
9858
+ 'attrs',
9859
+ 'Type'
9860
+ ]);
9861
+ const target = getTextByPathList(rel, [
9862
+ 'attrs',
9863
+ 'Target'
9864
+ ]);
9865
+ if (!id || !target) continue;
9866
+ const loc = resolvePresentationTarget(target);
9867
+ if (loc) {
9868
+ relsMap[id] = loc;
9869
+ if ('slide' === getRelTypeName(type)) slideTargets.push(loc);
9870
+ }
9871
+ }
9872
+ const presentation = await readXmlFile(zip, 'ppt/presentation.xml');
9873
+ const sldIds = asNodeArray(getTextByPathList(presentation, [
9874
+ 'p:presentation',
9875
+ 'p:sldIdLst',
9876
+ 'p:sldId'
9877
+ ]));
9878
+ const ordered = [];
9879
+ for (const sldId of sldIds){
9880
+ const rid = relationshipRid(sldId?.attrs);
9881
+ if (rid && relsMap[rid]) ordered.push(relsMap[rid]);
9882
+ }
9883
+ if (ordered.length) return ordered;
9884
+ return slideTargets.sort(sortSlideXml);
9885
+ }
9821
9886
  async function getContentTypes(zip) {
9822
9887
  const ContentTypesJson = await readXmlFile(zip, '[Content_Types].xml');
9823
- const subObj = ContentTypesJson['Types']['Override'];
9888
+ const overrides = asNodeArray(getTextByPathList(ContentTypesJson, [
9889
+ 'Types',
9890
+ 'Override'
9891
+ ]));
9824
9892
  let slidesLocArray = [];
9825
9893
  let slideLayoutsLocArray = [];
9826
- for (const item of subObj)switch(item['attrs']['ContentType']){
9827
- case 'application/vnd.openxmlformats-officedocument.presentationml.slide+xml':
9828
- slidesLocArray.push(item['attrs']['PartName'].substr(1));
9829
- break;
9830
- case 'application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml':
9831
- slideLayoutsLocArray.push(item['attrs']['PartName'].substr(1));
9832
- break;
9833
- default:
9894
+ for (const item of overrides){
9895
+ const contentType = item?.attrs?.ContentType;
9896
+ const partName = item?.attrs?.PartName;
9897
+ if (!contentType || !partName) continue;
9898
+ const loc = String(partName).replace(/^\//, '');
9899
+ switch(contentType){
9900
+ case 'application/vnd.openxmlformats-officedocument.presentationml.slide+xml':
9901
+ slidesLocArray.push(loc);
9902
+ break;
9903
+ case 'application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml':
9904
+ slideLayoutsLocArray.push(loc);
9905
+ break;
9906
+ default:
9907
+ }
9834
9908
  }
9835
- const sortSlideXml = (p1, p2)=>{
9836
- const n1 = +/(\d+)\.xml/.exec(p1)[1];
9837
- const n2 = +/(\d+)\.xml/.exec(p2)[1];
9838
- return n1 - n2;
9839
- };
9840
9909
  slidesLocArray = slidesLocArray.sort(sortSlideXml);
9841
9910
  slideLayoutsLocArray = slideLayoutsLocArray.sort(sortSlideXml);
9911
+ if (!slidesLocArray.length) slidesLocArray = await getSlidesFromPresentation(zip);
9842
9912
  return {
9843
9913
  slides: slidesLocArray,
9844
9914
  slideLayouts: slideLayoutsLocArray