@json-to-office/core-docx 7.3.1 → 7.5.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.
@@ -1 +1 @@
1
- {"version":3,"file":"compiler.d.ts","sourceRoot":"","sources":["../../src/ir/compiler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAGL,KAAK,kBAAkB,EACxB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,KAAK,EACV,iBAAiB,EAGlB,MAAM,wBAAwB,CAAC;AAGhC,OAAO,KAAK,EAAE,UAAU,EAAiB,MAAM,gBAAgB,CAAC;AAChE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAqD3D,OAAO,KAAK,EAAE,cAAc,EAAe,MAAM,wBAAwB,CAAC;AAuC1E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,OAAO,EAEL,KAAK,MAAM,EAuCZ,MAAM,SAAS,CAAC;AAyBjB,2DAA2D;AAC3D,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,kEAAkE;IAClE,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,2EAA2E;IAC3E,QAAQ,EAAE,SAAS,kBAAkB,CAAC,WAAW,CAAC,EAAE,CAAC;IACrD,QAAQ,EAAE,iBAAiB,EAAE,CAAC;IAC9B;;;;OAIG;IACH,WAAW,EAAE,oBAAoB,EAAE,CAAC;CACrC;AAoGD,wBAAgB,eAAe,CAC7B,SAAS,EAAE,iBAAiB,EAC5B,MAAM,EAAE,UAAU,EAClB,QAAQ,GAAE,iBAAiB,EAAO,EAClC,MAAM,GAAE,cAA0B,EAClC,OAAO,GAAE;IAAE,YAAY,CAAC,EAAE,OAAO,CAAA;CAAO,GACvC,iBAAiB,CAkHnB"}
1
+ {"version":3,"file":"compiler.d.ts","sourceRoot":"","sources":["../../src/ir/compiler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAGL,KAAK,kBAAkB,EACxB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,KAAK,EACV,iBAAiB,EAGlB,MAAM,wBAAwB,CAAC;AAGhC,OAAO,KAAK,EAAE,UAAU,EAAiB,MAAM,gBAAgB,CAAC;AAChE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAqD3D,OAAO,KAAK,EAAE,cAAc,EAAe,MAAM,wBAAwB,CAAC;AAuC1E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,OAAO,EAEL,KAAK,MAAM,EAwCZ,MAAM,SAAS,CAAC;AAyBjB,2DAA2D;AAC3D,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,kEAAkE;IAClE,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,2EAA2E;IAC3E,QAAQ,EAAE,SAAS,kBAAkB,CAAC,WAAW,CAAC,EAAE,CAAC;IACrD,QAAQ,EAAE,iBAAiB,EAAE,CAAC;IAC9B;;;;OAIG;IACH,WAAW,EAAE,oBAAoB,EAAE,CAAC;CACrC;AAoGD,wBAAgB,eAAe,CAC7B,SAAS,EAAE,iBAAiB,EAC5B,MAAM,EAAE,UAAU,EAClB,QAAQ,GAAE,iBAAiB,EAAO,EAClC,MAAM,GAAE,cAA0B,EAClC,OAAO,GAAE;IAAE,YAAY,CAAC,EAAE,OAAO,CAAA;CAAO,GACvC,iBAAiB,CAkHnB"}
@@ -2902,7 +2902,7 @@ var init_chartText = __esm({
2902
2902
  });
2903
2903
 
2904
2904
  // src/renderers/docxjs/docxSubpath.ts
2905
- function docxSubpath(specifier, component, importer) {
2905
+ function docxSubpath(specifier, component, importer, since = "9.8.0") {
2906
2906
  let module;
2907
2907
  let failure;
2908
2908
  let loading;
@@ -2926,7 +2926,7 @@ function docxSubpath(specifier, component, importer) {
2926
2926
  const { reason } = failure;
2927
2927
  const detail = reason instanceof Error ? reason.message : String(reason);
2928
2928
  throw new Error(
2929
- `The docx \`${component}\` component needs docx 9.8.0 or later: the \`${specifier}\` entry could not be loaded (${detail})`
2929
+ `The docx \`${component}\` component needs docx ${since} or later: the \`${specifier}\` entry could not be loaded (${detail})`
2930
2930
  );
2931
2931
  }
2932
2932
  throw new Error(
@@ -3523,8 +3523,19 @@ function emitBlock(block2, resources = /* @__PURE__ */ new Map()) {
3523
3523
  return assertNever2(block2, "DocxIrBlock");
3524
3524
  }
3525
3525
  }
3526
+ function emitOutsideBody(emit) {
3527
+ const was = outsideBody;
3528
+ outsideBody = true;
3529
+ try {
3530
+ return emit();
3531
+ } finally {
3532
+ outsideBody = was;
3533
+ }
3534
+ }
3526
3535
  function emitToc(block2) {
3527
- return new ThemedTableOfContents(block2.alias ?? "Table of Contents", {
3536
+ const cached = outsideBody && block2.cachedEntries?.length;
3537
+ const Field = cached ? ThemedTableOfContents : TableOfContents;
3538
+ return new Field(block2.alias ?? "Table of Contents", {
3528
3539
  ...block2.hyperlink !== void 0 ? { hyperlink: block2.hyperlink } : {},
3529
3540
  ...block2.headingRange ? {
3530
3541
  headingStyleRange: `${block2.headingRange.from}-${block2.headingRange.to}`
@@ -3537,7 +3548,7 @@ function emitToc(block2) {
3537
3548
  pageNumbersEntryLevelsRange: block2.omitPageNumbersForLevels.map((range) => `${range.from}-${range.to}`).join(",")
3538
3549
  } : {},
3539
3550
  ...block2.entrySeparator !== void 0 ? { entryAndPageNumberSeparator: block2.entrySeparator } : {},
3540
- ...block2.cachedEntries?.length ? {
3551
+ ...cached ? {
3541
3552
  cachedEntries: block2.cachedEntries.map((entry) => ({
3542
3553
  title: entry.text,
3543
3554
  level: entry.level
@@ -3660,7 +3671,7 @@ function emitBorder(border3) {
3660
3671
  ...border3.spacePoints !== void 0 ? { space: border3.spacePoints } : {}
3661
3672
  };
3662
3673
  }
3663
- var ALIGNMENT2, PAGE_FIELDS, isSequenceField, WRAP_TYPE, ThemedTableOfContents, VERTICAL_ALIGN, TABLE_ANCHOR, HORIZONTAL_POSITION, VERTICAL_POSITION;
3674
+ var ALIGNMENT2, PAGE_FIELDS, isSequenceField, WRAP_TYPE, ThemedTableOfContents, outsideBody, VERTICAL_ALIGN, TABLE_ANCHOR, HORIZONTAL_POSITION, VERTICAL_POSITION;
3664
3675
  var init_emit = __esm({
3665
3676
  "src/renderers/docxjs/emit.ts"() {
3666
3677
  "use strict";
@@ -3686,6 +3697,7 @@ var init_emit = __esm({
3686
3697
  Object.defineProperty(ThemedTableOfContents.prototype, "getTabStopsForLevel", {
3687
3698
  value: () => []
3688
3699
  });
3700
+ outsideBody = false;
3689
3701
  VERTICAL_ALIGN = {
3690
3702
  top: VerticalAlign.TOP,
3691
3703
  center: VerticalAlign.CENTER,
@@ -3873,6 +3885,318 @@ var init_styles2 = __esm({
3873
3885
  }
3874
3886
  });
3875
3887
 
3888
+ // src/renderers/docxjs/pageNumbers.ts
3889
+ import {
3890
+ Tab as Tab2
3891
+ } from "docx";
3892
+ function needsPageNumbers(ir) {
3893
+ const seen = /* @__PURE__ */ new Set();
3894
+ const visit = (value) => {
3895
+ if (typeof value !== "object" || value === null) return false;
3896
+ if (ArrayBuffer.isView(value) || seen.has(value)) return false;
3897
+ seen.add(value);
3898
+ if (Array.isArray(value)) return value.some(visit);
3899
+ if (value.kind === "toc") return true;
3900
+ return Object.values(value).some(visit);
3901
+ };
3902
+ return ir.sections.some((section2) => visit(section2.children));
3903
+ }
3904
+ async function createPageNumberEstimator(ir) {
3905
+ await layout.load();
3906
+ const cached = cachedContentsEntries(ir);
3907
+ const separators = contentsSeparators(ir);
3908
+ const estimate = layout.loaded() ? layout.get().estimatePageNumbersWith({ guess: true }) : void 0;
3909
+ return (body, context) => {
3910
+ renumberContentsBookmarks(body);
3911
+ restoreEntryNumbers(body, cached, separators);
3912
+ dropContentsEntryTabs(body);
3913
+ writeSeparatorTabs(body, context);
3914
+ endContentsInLastEntry(body);
3915
+ if (!estimate) {
3916
+ unavailable(
3917
+ `the \`docx/layout\` entry could not be loaded (${failureOf(() => layout.get())})`
3918
+ );
3919
+ return { bookmarks: /* @__PURE__ */ new Map() };
3920
+ }
3921
+ try {
3922
+ const estimated = estimate(body, context);
3923
+ if (complete(estimated)) clearUpdateFields(context.file);
3924
+ return estimated;
3925
+ } catch (error) {
3926
+ unavailable(error instanceof Error ? error.message : String(error));
3927
+ return { bookmarks: /* @__PURE__ */ new Map() };
3928
+ }
3929
+ };
3930
+ }
3931
+ function unavailable(reason) {
3932
+ reportWarning(
3933
+ "toc",
3934
+ PAGE_NUMBERS_UNAVAILABLE,
3935
+ `The table of contents is written without page numbers: laying the pages out failed (${reason}). Word fills them in when it updates the fields on open.`
3936
+ );
3937
+ }
3938
+ function failureOf(get) {
3939
+ try {
3940
+ get();
3941
+ return "unknown";
3942
+ } catch (error) {
3943
+ return error instanceof Error ? error.message : String(error);
3944
+ }
3945
+ }
3946
+ function complete(estimated) {
3947
+ return estimated.pageCount !== void 0 && (estimated.guesses ?? []).length === 0;
3948
+ }
3949
+ function clearUpdateFields(file) {
3950
+ const root = file?.Settings?.root;
3951
+ if (!root) return;
3952
+ const at = root.findIndex(
3953
+ (element) => element?.rootKey === "w:updateFields"
3954
+ );
3955
+ if (at >= 0) root.splice(at, 1);
3956
+ }
3957
+ function isContentsField(sdt) {
3958
+ let found = false;
3959
+ const visit = (element) => {
3960
+ if (found) return;
3961
+ if (nameOf(element) === "w:instrText") {
3962
+ found = childrenOf(element).some(
3963
+ (child) => typeof child === "string" && /^\s*TOC\b/.test(child)
3964
+ );
3965
+ return;
3966
+ }
3967
+ childrenOf(element).forEach(visit);
3968
+ };
3969
+ visit(sdt);
3970
+ return found;
3971
+ }
3972
+ function contentsFields(body) {
3973
+ const found = [];
3974
+ const visit = (element) => {
3975
+ const name = nameOf(element);
3976
+ if (name === void 0 || name === "_attr") return;
3977
+ if (name === "w:sdt" && isContentsField(element)) {
3978
+ found.push(element);
3979
+ return;
3980
+ }
3981
+ childrenOf(element).forEach(visit);
3982
+ };
3983
+ visit(body);
3984
+ return found;
3985
+ }
3986
+ function entryParagraphs(field) {
3987
+ return childrenOf(field).filter((content) => nameOf(content) === "w:sdtContent").flatMap(
3988
+ (content) => childrenOf(content).filter((paragraph2) => nameOf(paragraph2) === "w:p")
3989
+ );
3990
+ }
3991
+ function dropContentsEntryTabs(body) {
3992
+ for (const field of contentsFields(body)) {
3993
+ for (const paragraph2 of entryParagraphs(field)) {
3994
+ for (const properties of childrenOf(paragraph2)) {
3995
+ if (nameOf(properties) !== "w:pPr") continue;
3996
+ properties["w:pPr"] = childrenOf(properties).filter(
3997
+ (child) => nameOf(child) !== "w:tabs"
3998
+ );
3999
+ }
4000
+ }
4001
+ }
4002
+ }
4003
+ function endContentsInLastEntry(body) {
4004
+ for (const field of contentsFields(body)) {
4005
+ for (const content of childrenOf(field)) {
4006
+ if (nameOf(content) !== "w:sdtContent") continue;
4007
+ const paragraphs = childrenOf(content);
4008
+ const last = paragraphs[paragraphs.length - 1];
4009
+ const entry = paragraphs[paragraphs.length - 2];
4010
+ if (nameOf(last) !== "w:p" || nameOf(entry) !== "w:p") continue;
4011
+ const runs = childrenOf(last);
4012
+ const onlyTheEnd = runs.length === 1 && nameOf(runs[0]) === "w:r" && childrenOf(runs[0]).every(
4013
+ (child) => nameOf(child) === "w:fldChar" && attributesOf(child)?.["w:fldCharType"] === "end"
4014
+ );
4015
+ const entries = paragraphs.filter(
4016
+ (paragraph2) => textNodes(paragraph2).length > 0
4017
+ );
4018
+ if (!onlyTheEnd || entries.length < 2 || textNodes(entry).length === 0)
4019
+ continue;
4020
+ childrenOf(entry).push(runs[0]);
4021
+ paragraphs.pop();
4022
+ }
4023
+ }
4024
+ }
4025
+ function writeSeparatorTabs(body, context) {
4026
+ const tab = () => new Tab2().prepForXml(context);
4027
+ const visit = (element) => {
4028
+ const name = nameOf(element);
4029
+ if (name === void 0 || name === "_attr") return;
4030
+ if (name === "w:r") {
4031
+ const children = childrenOf(element);
4032
+ children.forEach((child, index) => {
4033
+ if (nameOf(child) !== "w:t") return;
4034
+ const text = childrenOf(child).filter(
4035
+ (part) => typeof part === "string"
4036
+ );
4037
+ if (text.length === 1 && text[0] === " ") children[index] = tab();
4038
+ });
4039
+ return;
4040
+ }
4041
+ childrenOf(element).forEach(visit);
4042
+ };
4043
+ for (const field of contentsFields(body))
4044
+ entryParagraphs(field).forEach(visit);
4045
+ }
4046
+ function textNodes(element) {
4047
+ const name = nameOf(element);
4048
+ if (name === void 0 || name === "_attr" || name === "w:instrText")
4049
+ return [];
4050
+ const children = childrenOf(element);
4051
+ if (name === "w:t")
4052
+ return children.flatMap(
4053
+ (child, index) => typeof child === "string" ? [{ parent: children, index }] : []
4054
+ );
4055
+ return children.flatMap(textNodes);
4056
+ }
4057
+ function contentsSeparators(ir) {
4058
+ const separators = [];
4059
+ const visit = (value) => {
4060
+ if (typeof value !== "object" || value === null) return;
4061
+ if (ArrayBuffer.isView(value)) return;
4062
+ if (Array.isArray(value)) {
4063
+ value.forEach(visit);
4064
+ return;
4065
+ }
4066
+ if (value.kind === "toc") {
4067
+ separators.push(value.entrySeparator);
4068
+ return;
4069
+ }
4070
+ Object.values(value).forEach(visit);
4071
+ };
4072
+ for (const section2 of ir.sections) visit(section2.children);
4073
+ return separators;
4074
+ }
4075
+ function cachedContentsEntries(ir) {
4076
+ const fields = [];
4077
+ const visit = (value) => {
4078
+ if (typeof value !== "object" || value === null) return;
4079
+ if (ArrayBuffer.isView(value)) return;
4080
+ if (Array.isArray(value)) {
4081
+ value.forEach(visit);
4082
+ return;
4083
+ }
4084
+ if (value.kind === "toc") {
4085
+ const toc = value;
4086
+ fields.push(toc.cachedEntries?.map((entry) => entry.text));
4087
+ return;
4088
+ }
4089
+ Object.values(value).forEach(visit);
4090
+ };
4091
+ for (const section2 of ir.sections) visit(section2.children);
4092
+ return fields;
4093
+ }
4094
+ function restoreEntryNumbers(body, cached, separators = []) {
4095
+ contentsFields(body).forEach((field, index) => {
4096
+ const texts = cached[index];
4097
+ const paragraphs = entryParagraphs(field).filter(
4098
+ (paragraph2) => textNodes(paragraph2).length > 0
4099
+ );
4100
+ if (!texts || texts.length !== paragraphs.length) return;
4101
+ paragraphs.forEach((paragraph2, entry) => {
4102
+ const nodes = textNodes(paragraph2);
4103
+ let title = nodes.map(({ parent, index: at }) => parent[at]).join("");
4104
+ const separator = separators[index];
4105
+ if (separator && title.endsWith(separator))
4106
+ title = title.slice(0, -separator.length);
4107
+ const text = texts[entry];
4108
+ if (title === "" || text === title || !text.endsWith(title)) return;
4109
+ const [first] = nodes;
4110
+ first.parent[first.index] = text.slice(0, text.length - title.length) + first.parent[first.index];
4111
+ });
4112
+ });
4113
+ }
4114
+ function renumberContentsBookmarks(body) {
4115
+ const names = /* @__PURE__ */ new Map();
4116
+ const paragraphs = (element) => {
4117
+ const name = nameOf(element);
4118
+ if (name === void 0 || name === "_attr") return;
4119
+ if (name !== "w:p") {
4120
+ childrenOf(element).forEach(paragraphs);
4121
+ return;
4122
+ }
4123
+ const children = childrenOf(element);
4124
+ for (const start of children) {
4125
+ if (nameOf(start) !== "w:bookmarkStart") continue;
4126
+ const attributes = attributesOf(start);
4127
+ const old = attributes?.["w:name"];
4128
+ if (typeof old !== "string" || !CONTENTS_BOOKMARK.test(old)) continue;
4129
+ const ordinal = names.size + 1;
4130
+ const renamed2 = `_Toc${ordinal}`;
4131
+ const id = CONTENTS_BOOKMARK_ID_BASE + ordinal;
4132
+ names.set(old, renamed2);
4133
+ const oldId = attributes["w:id"];
4134
+ attributes["w:name"] = renamed2;
4135
+ attributes["w:id"] = id;
4136
+ const end = [...children].reverse().find(
4137
+ (child) => nameOf(child) === "w:bookmarkEnd" && String(attributesOf(child)?.["w:id"]) === String(oldId)
4138
+ );
4139
+ if (end) attributesOf(end)["w:id"] = id;
4140
+ }
4141
+ };
4142
+ paragraphs(body);
4143
+ if (names.size === 0) return;
4144
+ const references = (element) => {
4145
+ const name = nameOf(element);
4146
+ if (name === void 0) return;
4147
+ if (name === "w:hyperlink") {
4148
+ const attributes = attributesOf(element);
4149
+ const anchor = attributes?.["w:anchor"];
4150
+ if (typeof anchor === "string" && names.has(anchor))
4151
+ attributes["w:anchor"] = names.get(anchor);
4152
+ }
4153
+ if (name === "w:instrText") {
4154
+ const children = childrenOf(element);
4155
+ children.forEach((child, index) => {
4156
+ if (typeof child !== "string") return;
4157
+ children[index] = child.replace(
4158
+ /\b(PAGEREF\s+)(_Toc\d+)\b/g,
4159
+ (match, field, bookmark) => names.has(bookmark) ? `${field}${names.get(bookmark)}` : match
4160
+ );
4161
+ });
4162
+ element[name] = children;
4163
+ return;
4164
+ }
4165
+ if (name === "_attr") return;
4166
+ childrenOf(element).forEach(references);
4167
+ };
4168
+ references(body);
4169
+ }
4170
+ function attributesOf(element) {
4171
+ const attributes = childrenOf(element).find(
4172
+ (child) => nameOf(child) === "_attr"
4173
+ );
4174
+ return attributes?._attr;
4175
+ }
4176
+ var PAGE_NUMBERS_UNAVAILABLE, layout, nameOf, childrenOf, CONTENTS_BOOKMARK_ID_BASE, CONTENTS_BOOKMARK;
4177
+ var init_pageNumbers = __esm({
4178
+ "src/renderers/docxjs/pageNumbers.ts"() {
4179
+ "use strict";
4180
+ init_generationContext();
4181
+ init_docxSubpath();
4182
+ PAGE_NUMBERS_UNAVAILABLE = "W_DOCX_PAGE_NUMBERS_UNAVAILABLE";
4183
+ layout = docxSubpath(
4184
+ "docx/layout",
4185
+ "toc",
4186
+ () => import("docx/layout"),
4187
+ "9.9.0"
4188
+ );
4189
+ nameOf = (element) => typeof element === "object" && element !== null && !Array.isArray(element) ? Object.keys(element)[0] : void 0;
4190
+ childrenOf = (element) => {
4191
+ const name = nameOf(element);
4192
+ const content = name === void 0 ? void 0 : element[name];
4193
+ return Array.isArray(content) ? content : content === void 0 ? [] : [content];
4194
+ };
4195
+ CONTENTS_BOOKMARK_ID_BASE = 3e6;
4196
+ CONTENTS_BOOKMARK = /^_Toc\d+$/;
4197
+ }
4198
+ });
4199
+
3876
4200
  // src/utils/normalizeTextCase.ts
3877
4201
  function normalizeTextCase(zip) {
3878
4202
  let changed = false;
@@ -4654,7 +4978,8 @@ function createDocxJsRenderer() {
4654
4978
  )
4655
4979
  });
4656
4980
  }
4657
- const document = buildDocument(ir, resources);
4981
+ const pageNumbers = needsPageNumbers(ir) ? await createPageNumberEstimator(ir) : void 0;
4982
+ const document = buildDocument(ir, resources, pageNumbers);
4658
4983
  const packed = await Packer.toBuffer(document);
4659
4984
  const fixed = fixFloatingImageIdsInBuffer(packed);
4660
4985
  const repaired = markers ? repairDrawingGroupsInBuffer(fixed, markers) : fixed;
@@ -4672,13 +4997,12 @@ function createDocxJsRenderer() {
4672
4997
  }
4673
4998
  };
4674
4999
  }
4675
- function buildDocument(ir, resources = /* @__PURE__ */ new Map()) {
5000
+ function buildDocument(ir, resources = /* @__PURE__ */ new Map(), pageNumbers) {
4676
5001
  return new Document({
5002
+ ...pageNumbers ? { pageNumbers } : {},
4677
5003
  styles: emitStyles(ir.styles),
4678
5004
  ...ir.theme ? { theme: themeOptions(ir.theme) } : {},
4679
- sections: ir.sections.map(
4680
- (section2, index) => sectionOptions(section2, resources, index === ir.sections.length - 1)
4681
- ),
5005
+ sections: ir.sections.map((section2) => sectionOptions(section2, resources)),
4682
5006
  ...coreProperties(ir),
4683
5007
  features: {
4684
5008
  updateFields: ir.settings.updateFields,
@@ -4697,8 +5021,8 @@ function buildDocument(ir, resources = /* @__PURE__ */ new Map()) {
4697
5021
  author: comment.author,
4698
5022
  ...comment.initials ? { initials: comment.initials } : {},
4699
5023
  date: new Date(comment.date),
4700
- children: comment.children.map(
4701
- (block2) => emitBlock(block2, resources)
5024
+ children: emitOutsideBody(
5025
+ () => comment.children.map((block2) => emitBlock(block2, resources))
4702
5026
  ),
4703
5027
  ...comment.parentId !== void 0 ? { parentId: comment.parentId } : {},
4704
5028
  ...comment.resolved !== void 0 ? { resolved: comment.resolved } : {}
@@ -4726,8 +5050,8 @@ function noteBodies(notes, resources) {
4726
5050
  const bodies = {};
4727
5051
  for (const note of notes) {
4728
5052
  bodies[String(note.id)] = {
4729
- children: note.children.map(
4730
- (block2) => emitBlock(block2, resources)
5053
+ children: emitOutsideBody(
5054
+ () => note.children.map((block2) => emitBlock(block2, resources))
4731
5055
  )
4732
5056
  };
4733
5057
  }
@@ -4888,7 +5212,7 @@ function collectImagePlacements(ir) {
4888
5212
  }
4889
5213
  return { placements, drawingGroups };
4890
5214
  }
4891
- function sectionOptions(section2, resources, closesDocument = false) {
5215
+ function sectionOptions(section2, resources) {
4892
5216
  const { page, columns } = section2.properties;
4893
5217
  const options = {
4894
5218
  properties: {
@@ -4927,7 +5251,7 @@ function sectionOptions(section2, resources, closesDocument = false) {
4927
5251
  }
4928
5252
  } : {}
4929
5253
  },
4930
- children: sectionChildren(section2, resources, closesDocument)
5254
+ children: sectionChildren(section2, resources)
4931
5255
  };
4932
5256
  const headers = chromeSlots(section2.headers, Header, resources);
4933
5257
  if (headers) options.headers = headers;
@@ -4941,7 +5265,9 @@ function chromeSlots(set, Part, resources) {
4941
5265
  for (const slot of ["default", "first", "even"]) {
4942
5266
  const part = set[slot];
4943
5267
  if (part) {
4944
- out[slot] = new Part({ children: partChildren(part, resources) });
5268
+ out[slot] = new Part({
5269
+ children: emitOutsideBody(() => partChildren(part, resources))
5270
+ });
4945
5271
  }
4946
5272
  }
4947
5273
  return Object.keys(out).length > 0 ? out : void 0;
@@ -4949,10 +5275,10 @@ function chromeSlots(set, Part, resources) {
4949
5275
  function partChildren(part, resources) {
4950
5276
  return part.children.map((block2) => emitBlock(block2, resources));
4951
5277
  }
4952
- function sectionChildren(section2, resources, closesDocument = false) {
5278
+ function sectionChildren(section2, resources) {
4953
5279
  const blocks = section2.children.map((block2) => emitBlock(block2, resources));
4954
5280
  const bookmark = section2.bookmark;
4955
- const endsInFrame = closesDocument && lastBlockIsFrame(section2);
5281
+ const endsInFrame = lastBlockIsFrame(section2);
4956
5282
  if (!bookmark && !endsInFrame) return blocks;
4957
5283
  const out = [...blocks];
4958
5284
  if (bookmark?.opens) {
@@ -5005,6 +5331,7 @@ var init_docxjs = __esm({
5005
5331
  "use strict";
5006
5332
  init_styles2();
5007
5333
  init_charts();
5334
+ init_pageNumbers();
5008
5335
  init_imageUtils();
5009
5336
  init_packageDocument();
5010
5337
  init_fixFloatingImageIds();
@@ -6191,7 +6518,7 @@ function sectionChildren2(value, ctx, closesDocument = false) {
6191
6518
  const blocks = value.children.map((child) => block(child, ctx));
6192
6519
  const last = value.children[value.children.length - 1];
6193
6520
  const bookmark = value.bookmark;
6194
- if (bookmark?.closes && last?.kind !== "paragraph" || closesDocument && last?.kind === "paragraph" && last.frame !== void 0)
6521
+ if (bookmark?.closes && last?.kind !== "paragraph" || last?.kind === "paragraph" && last.frame !== void 0 && (closesDocument || !bookmark?.closes))
6195
6522
  blocks.push({
6196
6523
  paragraph: {
6197
6524
  children: [],
@@ -7455,7 +7782,8 @@ import {
7455
7782
  resolveServiceUrl as resolveServiceUrl3,
7456
7783
  postJsonToService as postJsonToService3,
7457
7784
  recordChartRetry,
7458
- sendChartRequest
7785
+ sendChartRequest,
7786
+ withChartPresets
7459
7787
  } from "@json-to-office/shared";
7460
7788
  var DEFAULT_EXPORT_SERVER_URL = "http://localhost:7801";
7461
7789
  function effectiveChartServerUrl(props, servicesConfig) {
@@ -7534,6 +7862,12 @@ function toChartColor(value, theme) {
7534
7862
  return void 0;
7535
7863
  }
7536
7864
  }
7865
+ function withThemePresets(config, theme) {
7866
+ const presets = theme?.componentDefaults?.highcharts;
7867
+ if (!presets) return config;
7868
+ const options = withChartPresets(config.options, presets);
7869
+ return options === config.options ? config : { ...config, options };
7870
+ }
7537
7871
  function withThemeColors(config, theme) {
7538
7872
  const options = config.options;
7539
7873
  if (!options || options.colors || !theme?.colors) return config;
@@ -7609,7 +7943,10 @@ function withChartFontFaces(config, theme, faces) {
7609
7943
  }
7610
7944
  async function renderChartToImageProps(props, theme, servicesConfig, chartFonts, warnings, cache) {
7611
7945
  const config = withChartFontFaces(
7612
- withThemeTypography(withThemeColors(props, theme), theme),
7946
+ withThemeTypography(
7947
+ withThemeColors(withThemePresets(props, theme), theme),
7948
+ theme
7949
+ ),
7613
7950
  theme,
7614
7951
  chartFonts
7615
7952
  );
@@ -10557,9 +10894,9 @@ function schemeHex(token, theme) {
10557
10894
  // src/ir/compiler.ts
10558
10895
  var CONTENT_BOOKMARK_ID_BASE = 2e6;
10559
10896
  var SINGLE_LINE_TWIPS = 240;
10560
- function compileDocument(structure, layout, warnings = [], images = /* @__PURE__ */ new Map(), options = {}) {
10897
+ function compileDocument(structure, layout2, warnings = [], images = /* @__PURE__ */ new Map(), options = {}) {
10561
10898
  const styles = createDocumentStyles(structure.theme, structure.language);
10562
- const outline = collectOutline(layout, warnings);
10899
+ const outline = collectOutline(layout2, warnings);
10563
10900
  const ctx = {
10564
10901
  theme: structure.theme,
10565
10902
  themeName: structure.themeName,
@@ -10594,10 +10931,10 @@ function compileDocument(structure, layout, warnings = [], images = /* @__PURE__
10594
10931
  };
10595
10932
  ctx.features.require("paragraphs", "sections");
10596
10933
  ctx.features.require("styles", "styles");
10597
- if (layout.sections.length > 1) ctx.features.require("sections", "sections");
10598
- const ordinals = computeSectionOrdinals(layout.sections);
10599
- const chrome = resolveSectionChrome(layout.sections);
10600
- const sections = layout.sections.map(
10934
+ if (layout2.sections.length > 1) ctx.features.require("sections", "sections");
10935
+ const ordinals = computeSectionOrdinals(layout2.sections);
10936
+ const chrome = resolveSectionChrome(layout2.sections);
10937
+ const sections = layout2.sections.map(
10601
10938
  (section2, index) => compileSection(section2, index, ordinals[index], chrome[index], ctx)
10602
10939
  );
10603
10940
  if (ctx.hasResolvedComment && !ctx.comments.some((comment) => comment.parentId !== void 0)) {
@@ -10675,9 +11012,9 @@ function separateFromTables(blocks, bodyAfterTwips) {
10675
11012
  }
10676
11013
  }
10677
11014
  }
10678
- function collectOutline(layout, warnings) {
11015
+ function collectOutline(layout2, warnings) {
10679
11016
  try {
10680
- return collectDocumentOutline(layout.sections);
11017
+ return collectDocumentOutline(layout2.sections);
10681
11018
  } catch (error) {
10682
11019
  warnings.push({
10683
11020
  component: "document",
@@ -13223,13 +13560,19 @@ function compileTableRow(row, columnTwips, spacing, baseStyle, scope) {
13223
13560
  ...scope.cantSplit !== void 0 ? { cantSplit: scope.cantSplit } : {}
13224
13561
  };
13225
13562
  }
13563
+ var NORMAL_TABLE_CELL_MARGINS = {
13564
+ topTwips: 0,
13565
+ bottomTwips: 0,
13566
+ leftTwips: 108,
13567
+ rightTwips: 108
13568
+ };
13226
13569
  function compileTableCell(cell, columnTwips, spacing, baseStyle, keepNext, partScope, rowRevision, markRevision) {
13227
- const margins = !cell.missing && cell.padding ? {
13570
+ const margins = cell.missing ? void 0 : cell.padding ? {
13228
13571
  topTwips: pointsToTwips2(cell.padding.top),
13229
13572
  bottomTwips: pointsToTwips2(cell.padding.bottom),
13230
13573
  leftTwips: pointsToTwips2(cell.padding.left),
13231
13574
  rightTwips: pointsToTwips2(cell.padding.right)
13232
- } : void 0;
13575
+ } : NORMAL_TABLE_CELL_MARGINS;
13233
13576
  const scope = {
13234
13577
  ...partScope,
13235
13578
  measureTwips: cellMeasureTwips(columnTwips, margins)
@@ -13742,8 +14085,8 @@ function hasPageBreak(component) {
13742
14085
  }
13743
14086
  return false;
13744
14087
  }
13745
- function getColumnSettings(layout) {
13746
- switch (layout) {
14088
+ function getColumnSettings(layout2) {
14089
+ switch (layout2) {
13747
14090
  case "multi-column":
13748
14091
  return {
13749
14092
  count: 2,
@@ -15969,7 +16312,7 @@ async function compileDocumentScoped(document, options, warnings) {
15969
16312
  const visualFonts = hasVisual ? toRasterizeFontFaces(resolvedFonts, warnings) : [];
15970
16313
  const chartFonts = hasHighcharts ? toChartFontFaces(resolvedFonts) : [];
15971
16314
  const desugared = await desugarExternals(context.document, {
15972
- theme: context.theme,
16315
+ theme: prepared.model.document.theme,
15973
16316
  ...options.services ? { services: options.services } : {},
15974
16317
  ...options.baseDir !== void 0 ? { baseDir: options.baseDir } : {},
15975
16318
  ...visualFonts.length > 0 ? { visualFonts } : {},
@@ -15983,19 +16326,19 @@ async function compileDocumentScoped(document, options, warnings) {
15983
16326
  context.themeName,
15984
16327
  resolveGenerationDate(options)
15985
16328
  );
15986
- const layout = applyLayout(
16329
+ const layout2 = applyLayout(
15987
16330
  structure.sections,
15988
16331
  context.theme,
15989
16332
  context.themeName
15990
16333
  );
15991
16334
  const images = await loadImageResources(
15992
- layout.sections.flatMap((section2) => [
16335
+ layout2.sections.flatMap((section2) => [
15993
16336
  ...section2.components,
15994
16337
  ...Array.isArray(section2.header) ? section2.header : [],
15995
16338
  ...Array.isArray(section2.footer) ? section2.footer : []
15996
16339
  ])
15997
16340
  );
15998
- const compiled = compileDocument(structure, layout, warnings, images, {
16341
+ const compiled = compileDocument(structure, layout2, warnings, images, {
15999
16342
  // A caller that collects nothing still needs to hear about a dropped
16000
16343
  // value, so warnings go to the console when there is no collector.
16001
16344
  echoWarnings: options.warnings === void 0