docxmlater 1.12.0 → 1.13.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.
@@ -356,6 +356,7 @@ class DocumentParser {
356
356
  if (bidi) {
357
357
  (0, diagnostics_1.logTextDirection)(`Paragraph has BiDi enabled`);
358
358
  }
359
+ this.mergeConsecutiveHyperlinks(paragraph);
359
360
  return paragraph;
360
361
  }
361
362
  catch (error) {
@@ -459,6 +460,7 @@ class DocumentParser {
459
460
  }
460
461
  }
461
462
  }
463
+ this.mergeConsecutiveHyperlinks(paragraph);
462
464
  return paragraph;
463
465
  }
464
466
  catch (error) {
@@ -811,16 +813,22 @@ class DocumentParser {
811
813
  const tooltip = hyperlinkObj["@_w:tooltip"];
812
814
  const runs = hyperlinkObj["w:r"];
813
815
  const runChildren = Array.isArray(runs) ? runs : (runs ? [runs] : []);
814
- let parsedRun = null;
816
+ const parsedRuns = [];
815
817
  let text = '';
816
818
  let formatting = {};
817
819
  if (runChildren.length > 0) {
818
- parsedRun = this.parseRunFromObject(runChildren[0]);
819
- if (parsedRun) {
820
- text = parsedRun.getText();
821
- formatting = parsedRun.getFormatting();
820
+ for (const runChild of runChildren) {
821
+ const parsedRun = this.parseRunFromObject(runChild);
822
+ if (parsedRun) {
823
+ parsedRuns.push(parsedRun);
824
+ text += parsedRun.getText();
825
+ }
826
+ }
827
+ if (parsedRuns.length > 0 && parsedRuns[0]) {
828
+ formatting = parsedRuns[0].getFormatting();
822
829
  }
823
830
  }
831
+ const parsedRun = parsedRuns.length > 0 ? parsedRuns[0] : null;
824
832
  let url;
825
833
  if (relationshipId) {
826
834
  const relationship = relationshipManager.getRelationship(relationshipId);
@@ -852,6 +860,72 @@ class DocumentParser {
852
860
  return null;
853
861
  }
854
862
  }
863
+ mergeConsecutiveHyperlinks(paragraph) {
864
+ const content = paragraph.getContent();
865
+ if (!content || content.length < 2)
866
+ return;
867
+ const mergedContent = [];
868
+ let i = 0;
869
+ while (i < content.length) {
870
+ const currentItem = content[i];
871
+ if (currentItem.constructor.name === 'Hyperlink') {
872
+ const currentHyperlink = currentItem;
873
+ const currentUrl = currentHyperlink.getUrl();
874
+ const currentAnchor = currentHyperlink.getAnchor();
875
+ const consecutiveHyperlinks = [currentHyperlink];
876
+ let j = i + 1;
877
+ while (j < content.length) {
878
+ const nextItem = content[j];
879
+ if (nextItem.constructor.name !== 'Hyperlink')
880
+ break;
881
+ const nextHyperlink = nextItem;
882
+ const nextUrl = nextHyperlink.getUrl();
883
+ const nextAnchor = nextHyperlink.getAnchor();
884
+ if (nextUrl !== currentUrl || nextAnchor !== currentAnchor)
885
+ break;
886
+ consecutiveHyperlinks.push(nextHyperlink);
887
+ j++;
888
+ }
889
+ if (consecutiveHyperlinks.length > 1) {
890
+ const mergedText = consecutiveHyperlinks
891
+ .map(h => h.getText())
892
+ .join('');
893
+ const mergedHyperlink = new currentHyperlink.constructor({
894
+ url: currentUrl,
895
+ anchor: currentAnchor,
896
+ text: mergedText,
897
+ formatting: currentHyperlink.getFormatting(),
898
+ tooltip: currentHyperlink.getTooltip(),
899
+ relationshipId: currentHyperlink.getRelationshipId(),
900
+ });
901
+ mergedContent.push(mergedHyperlink);
902
+ i = j;
903
+ }
904
+ else {
905
+ mergedContent.push(currentHyperlink);
906
+ i++;
907
+ }
908
+ }
909
+ else {
910
+ mergedContent.push(currentItem);
911
+ i++;
912
+ }
913
+ }
914
+ if (mergedContent.length !== content.length) {
915
+ paragraph.clearContent();
916
+ for (const item of mergedContent) {
917
+ if (item.constructor.name === 'Hyperlink') {
918
+ paragraph.addHyperlink(item);
919
+ }
920
+ else if (item.constructor.name === 'Run') {
921
+ paragraph.addRun(item);
922
+ }
923
+ else if (item.constructor.name === 'Field') {
924
+ paragraph.addField(item);
925
+ }
926
+ }
927
+ }
928
+ }
855
929
  parseSimpleFieldFromObject(fieldObj) {
856
930
  try {
857
931
  const instruction = fieldObj["@_w:instr"];