@json-to-office/core-docx 4.1.0 → 4.2.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.
package/dist/index.js CHANGED
@@ -12398,6 +12398,12 @@ init_defaults();
12398
12398
  init_widthUtils();
12399
12399
 
12400
12400
  // src/quality/text-inventory.ts
12401
+ function tocDepth(depth) {
12402
+ const range = asRecord(depth) ?? {};
12403
+ const from = typeof range.from === "number" ? range.from : 1;
12404
+ const to = typeof range.to === "number" ? range.to : 3;
12405
+ return { from, to };
12406
+ }
12401
12407
  function asRecord(value) {
12402
12408
  return typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
12403
12409
  }
@@ -12418,10 +12424,14 @@ function collectDocxTextInventory(children, basePath = "/children") {
12418
12424
  path: path4,
12419
12425
  text,
12420
12426
  role,
12421
- order: entries.length,
12427
+ // Renumbered once the walk is done: contents entries are spliced in.
12428
+ order: -1,
12422
12429
  ...extra
12423
12430
  });
12424
12431
  };
12432
+ const headings = [];
12433
+ const tocs = [];
12434
+ let sectionCount = 0;
12425
12435
  const visitCell = (cell, path4, role, inherited) => {
12426
12436
  const cellRole = inherited.repeats ? "chrome" : role;
12427
12437
  const extra = inherited.repeats ? { repeats: true } : {};
@@ -12436,11 +12446,13 @@ function collectDocxTextInventory(children, basePath = "/children") {
12436
12446
  add(`${path4}/content`, rec.content, cellRole, extra);
12437
12447
  else {
12438
12448
  const inner = asRecord(rec.content);
12439
- if (inner) visitNode(inner, `${path4}/content`, inherited);
12449
+ if (inner)
12450
+ visitNode(inner, `${path4}/content`, { ...inherited, inCell: true });
12440
12451
  }
12441
12452
  return;
12442
12453
  }
12443
- if (typeof rec.name === "string") visitNode(rec, path4, inherited);
12454
+ if (typeof rec.name === "string")
12455
+ visitNode(rec, path4, { ...inherited, inCell: true });
12444
12456
  };
12445
12457
  const visitNode = (node, path4, inherited) => {
12446
12458
  if (node.enabled === false) return;
@@ -12449,6 +12461,7 @@ function collectDocxTextInventory(children, basePath = "/children") {
12449
12461
  const extra = {
12450
12462
  ...inherited.repeats && { repeats: true }
12451
12463
  };
12464
+ const scope = name === "section" ? { ...inherited, section: ++sectionCount } : inherited;
12452
12465
  switch (name) {
12453
12466
  case "heading": {
12454
12467
  if (inherited.repeats) {
@@ -12457,6 +12470,27 @@ function collectDocxTextInventory(children, basePath = "/children") {
12457
12470
  }
12458
12471
  const level = typeof props.level === "number" && Number.isFinite(props.level) ? props.level : 1;
12459
12472
  add(`${path4}/props/text`, props.text, "heading", { ...extra, level });
12473
+ if (typeof props.text === "string" && props.text.trim() !== "" && !scope.inCell) {
12474
+ headings.push({ text: props.text, level, section: scope.section });
12475
+ }
12476
+ break;
12477
+ }
12478
+ case "toc": {
12479
+ add(`${path4}/props/title`, props.title, "toc-entry", extra);
12480
+ const styles = /* @__PURE__ */ new Set();
12481
+ if (Array.isArray(props.styles)) {
12482
+ for (const mapping of props.styles) {
12483
+ const id = asRecord(mapping)?.styleId;
12484
+ if (typeof id === "string") styles.add(id);
12485
+ }
12486
+ }
12487
+ tocs.push({
12488
+ at: entries.length,
12489
+ path: path4,
12490
+ depth: tocDepth(props.depth),
12491
+ styles,
12492
+ section: props.scope === "document" ? void 0 : scope.section
12493
+ });
12460
12494
  break;
12461
12495
  }
12462
12496
  case "paragraph": {
@@ -12470,6 +12504,13 @@ function collectDocxTextInventory(children, basePath = "/children") {
12470
12504
  ...frame && { frame }
12471
12505
  }
12472
12506
  );
12507
+ if (typeof props.themeStyle === "string" && typeof props.text === "string" && props.text.trim() !== "" && !scope.inCell) {
12508
+ headings.push({
12509
+ text: props.text,
12510
+ themeStyle: props.themeStyle,
12511
+ section: scope.section
12512
+ });
12513
+ }
12473
12514
  break;
12474
12515
  }
12475
12516
  case "list": {
@@ -12522,8 +12563,28 @@ function collectDocxTextInventory(children, basePath = "/children") {
12522
12563
  add(`${path4}/props/description`, props.description, "statistic", extra);
12523
12564
  break;
12524
12565
  }
12566
+ case "chart": {
12567
+ if (props.showTitle !== false) {
12568
+ add(`${path4}/props/title`, props.title, "caption", extra);
12569
+ }
12570
+ if (props.type !== "pie" && props.type !== "doughnut") {
12571
+ add(
12572
+ `${path4}/props/catAxisTitle`,
12573
+ props.catAxisTitle,
12574
+ "caption",
12575
+ extra
12576
+ );
12577
+ add(
12578
+ `${path4}/props/valAxisTitle`,
12579
+ props.valAxisTitle,
12580
+ "caption",
12581
+ extra
12582
+ );
12583
+ }
12584
+ add(`${path4}/props/caption`, props.caption, "caption", extra);
12585
+ break;
12586
+ }
12525
12587
  case "image":
12526
- case "chart":
12527
12588
  case "highcharts":
12528
12589
  case "visual":
12529
12590
  case "visual-native": {
@@ -12551,7 +12612,7 @@ function collectDocxTextInventory(children, basePath = "/children") {
12551
12612
  if (Array.isArray(node.children)) {
12552
12613
  node.children.forEach((child, index) => {
12553
12614
  const rec = asRecord(child);
12554
- if (rec) visitNode(rec, `${path4}/children/${index}`, inherited);
12615
+ if (rec) visitNode(rec, `${path4}/children/${index}`, scope);
12555
12616
  });
12556
12617
  }
12557
12618
  };
@@ -12559,6 +12620,29 @@ function collectDocxTextInventory(children, basePath = "/children") {
12559
12620
  const rec = asRecord(child);
12560
12621
  if (rec) visitNode(rec, `${basePath}/${index}`, {});
12561
12622
  });
12623
+ for (const toc of [...tocs].reverse()) {
12624
+ const collected = headings.filter(
12625
+ (heading) => (toc.section === void 0 || heading.section === toc.section) && (heading.level !== void 0 ? heading.level >= toc.depth.from && heading.level <= toc.depth.to : heading.themeStyle !== void 0 && toc.styles.has(heading.themeStyle))
12626
+ );
12627
+ entries.splice(
12628
+ toc.at,
12629
+ 0,
12630
+ ...collected.map(
12631
+ (heading, index) => ({
12632
+ id: `docx:text:${toc.path}:entry:${index}`,
12633
+ kind: "docx/text",
12634
+ path: toc.path,
12635
+ text: heading.text,
12636
+ role: "toc-entry",
12637
+ order: 0,
12638
+ optional: true
12639
+ })
12640
+ )
12641
+ );
12642
+ }
12643
+ entries.forEach((entry, index) => {
12644
+ entry.order = index;
12645
+ });
12562
12646
  return entries;
12563
12647
  }
12564
12648