docx 9.10.0 → 9.11.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.
@@ -11033,7 +11033,7 @@ DOT: "dot" };
11033
11033
  if (options.imprint !== void 0) this.push(new OnOffElement("w:imprint", options.imprint));
11034
11034
  if (options.noProof !== void 0) this.push(new OnOffElement("w:noProof", options.noProof));
11035
11035
  if (options.snapToGrid !== void 0) this.push(new OnOffElement("w:snapToGrid", options.snapToGrid));
11036
- if (options.vanish) this.push(new OnOffElement("w:vanish", options.vanish));
11036
+ if (options.vanish !== void 0) this.push(new OnOffElement("w:vanish", options.vanish));
11037
11037
  if (options.color) this.push(new Color(options.color));
11038
11038
  if (options.characterSpacing) this.push(new CharacterSpacing(options.characterSpacing));
11039
11039
  if (options.scale !== void 0) this.push(new NumberValueElement("w:w", options.scale));
@@ -11052,8 +11052,8 @@ DOT: "dot" };
11052
11052
  if (options.rightToLeft !== void 0) this.push(new OnOffElement("w:rtl", options.rightToLeft));
11053
11053
  if (options.emphasisMark) this.push(createEmphasisMark(options.emphasisMark.type));
11054
11054
  if (options.language) this.push(createLanguageComponent(options.language));
11055
- if (options.specVanish) this.push(new OnOffElement("w:specVanish", options.vanish));
11056
- if (options.math) this.push(new OnOffElement("w:oMath", options.math));
11055
+ if (options.specVanish !== void 0) this.push(new OnOffElement("w:specVanish", options.specVanish));
11056
+ if (options.math !== void 0) this.push(new OnOffElement("w:oMath", options.math));
11057
11057
  if (options.revision) this.push(new RunPropertiesChange(options.revision));
11058
11058
  }
11059
11059
  push(item) {
@@ -16885,14 +16885,14 @@ EXTERNAL: "External" };
16885
16885
  }
16886
16886
  };
16887
16887
  /** The name of a formatted element, or `_attr` for its parent's attributes */
16888
- var nameOf$2 = (element) => typeof element === "object" && element !== null ? Object.keys(element)[0] : void 0;
16888
+ var nameOf$3 = (element) => typeof element === "object" && element !== null ? Object.keys(element)[0] : void 0;
16889
16889
  /** The children of a formatted element. An element with only attributes has them as its one child */
16890
16890
  var childrenOf = (element) => {
16891
- const name = nameOf$2(element);
16891
+ const name = nameOf$3(element);
16892
16892
  const content = name === void 0 ? void 0 : element[name];
16893
16893
  return Array.isArray(content) ? content : content === void 0 ? [] : [content];
16894
16894
  };
16895
- var childOf = (element, name) => childrenOf(element).find((child) => nameOf$2(child) === name);
16895
+ var childOf = (element, name) => childrenOf(element).find((child) => nameOf$3(child) === name);
16896
16896
  var attributeOf = (element, attribute) => {
16897
16897
  var _childOf;
16898
16898
  return (_childOf = childOf(element, "_attr")) === null || _childOf === void 0 || (_childOf = _childOf._attr) === null || _childOf === void 0 ? void 0 : _childOf[attribute];
@@ -16916,7 +16916,7 @@ EXTERNAL: "External" };
16916
16916
  * so the paragraphs in it aren't taken for headings. Nor are paragraphs in text boxes, as in Word.
16917
16917
  */
16918
16918
  var blocksOf = (elements) => elements.flatMap((element) => {
16919
- const name = nameOf$2(element);
16919
+ const name = nameOf$3(element);
16920
16920
  if (name === "w:p" || writtenTables.has(element)) return [element];
16921
16921
  return name !== void 0 && BLOCK_CONTAINERS.has(name) ? blocksOf(childrenOf(element)) : [];
16922
16922
  });
@@ -16936,7 +16936,7 @@ EXTERNAL: "External" };
16936
16936
  ]);
16937
16937
  /** The text an element in a run stands for, such as `\t` for a tab. A page or column break isn't text */
16938
16938
  var textOfRunContent = (element) => {
16939
- switch (nameOf$2(element)) {
16939
+ switch (nameOf$3(element)) {
16940
16940
  case "w:t": return childrenOf(element).filter((child) => typeof child === "string").join("");
16941
16941
  case "w:tab": return " ";
16942
16942
  case "w:br": return [void 0, "textWrapping"].includes(attributeOf(element, "w:type")) ? "\n" : "";
@@ -16949,7 +16949,7 @@ EXTERNAL: "External" };
16949
16949
  var textOf = (paragraph) => {
16950
16950
  const fields = [];
16951
16951
  const read = (element) => {
16952
- const name = nameOf$2(element);
16952
+ const name = nameOf$3(element);
16953
16953
  if (name !== void 0 && TEXT_CONTAINERS.has(name)) return childrenOf(element).map(read).join("");
16954
16954
  if (name === "w:fldChar") {
16955
16955
  const type = attributeOf(element, "w:fldCharType");
@@ -16964,7 +16964,7 @@ EXTERNAL: "External" };
16964
16964
  };
16965
16965
  /** The bookmarks started and ended in an element, in order */
16966
16966
  var bookmarkMarksOf = (element) => {
16967
- const name = nameOf$2(element);
16967
+ const name = nameOf$3(element);
16968
16968
  if (name === "w:bookmarkStart") return [{
16969
16969
  start: true,
16970
16970
  id: attributeOf(element, "w:id"),
@@ -17009,7 +17009,7 @@ EXTERNAL: "External" };
17009
17009
  var stylesOf = (context) => {
17010
17010
  var _context$file;
17011
17011
  const styles = (_context$file = context.file) === null || _context$file === void 0 || (_context$file = _context$file.Styles) === null || _context$file === void 0 ? void 0 : _context$file.prepForXml(context);
17012
- return new Map(childrenOf(styles).filter((style) => nameOf$2(style) === "w:style" && ["paragraph", void 0].includes(attributeOf(style, "w:type"))).map((style) => [attributeOf(style, "w:styleId"), {
17012
+ return new Map(childrenOf(styles).filter((style) => nameOf$3(style) === "w:style" && ["paragraph", void 0].includes(attributeOf(style, "w:type"))).map((style) => [attributeOf(style, "w:styleId"), {
17013
17013
  name: attributeOf(childOf(style, "w:name"), "w:val"),
17014
17014
  basedOn: attributeOf(childOf(style, "w:basedOn"), "w:val"),
17015
17015
  outlineLevel: numberAttributeOf(childOf(childOf(style, "w:pPr"), "w:outlineLvl"), "w:val")
@@ -17137,7 +17137,7 @@ EXTERNAL: "External" };
17137
17137
  /** Puts a bookmark around the content of a formatted paragraph */
17138
17138
  var bookmark = (paragraph, name, id, context) => {
17139
17139
  const children = childrenOf(paragraph);
17140
- const start = children.findIndex((child) => nameOf$2(child) === "w:pPr") + 1;
17140
+ const start = children.findIndex((child) => nameOf$3(child) === "w:pPr") + 1;
17141
17141
  paragraph["w:p"] = [
17142
17142
  ...children.slice(0, start),
17143
17143
  new BookmarkStart(name, id).prepForXml(context),
@@ -17158,7 +17158,7 @@ EXTERNAL: "External" };
17158
17158
  if (tables.length === 0) return;
17159
17159
  const styles = stylesOf(context);
17160
17160
  const followBookmarks = tables.some(([, { properties }]) => Boolean(properties.entriesFromBookmark));
17161
- const headings = paragraphDetailsOf(blocks.filter((block) => nameOf$2(block) === "w:p"), followBookmarks).map((paragraph) => ({
17161
+ const headings = paragraphDetailsOf(blocks.filter((block) => nameOf$3(block) === "w:p"), followBookmarks).map((paragraph) => ({
17162
17162
  paragraph,
17163
17163
  levels: tables.map(([, { properties }]) => levelIn(properties, paragraph, styles))
17164
17164
  })).filter(({ levels }) => levels.some((level) => level !== void 0)).map((heading, index) => _objectSpread2(_objectSpread2({}, heading), {}, { id: bookmarkIds.get(index) }));
@@ -17173,7 +17173,7 @@ EXTERNAL: "External" };
17173
17173
  }];
17174
17174
  });
17175
17175
  if (entries.length === 0) return;
17176
- table["w:sdt"] = childrenOf(table).map((child) => nameOf$2(child) === "w:sdtContent" ? contentOf(options, entries, styles, context) : child);
17176
+ table["w:sdt"] = childrenOf(table).map((child) => nameOf$3(child) === "w:sdtContent" ? contentOf(options, entries, styles, context) : child);
17177
17177
  });
17178
17178
  };
17179
17179
  //#endregion
@@ -17353,21 +17353,21 @@ EXTERNAL: "External" };
17353
17353
  *
17354
17354
  * @module
17355
17355
  */
17356
- var nameOf$1 = (element) => {
17356
+ var nameOf$2 = (element) => {
17357
17357
  const name = typeof element === "object" && element !== null && !Array.isArray(element) ? Object.keys(element)[0] : void 0;
17358
17358
  return name === "_attr" ? void 0 : name;
17359
17359
  };
17360
17360
  var textElement = (text) => ({ "w:t": [{ _attr: { "xml:space": "preserve" } }, text] });
17361
17361
  /** The elements docx formats to write a document */
17362
17362
  var FORMATTED = {
17363
- nameOf: nameOf$1,
17363
+ nameOf: nameOf$2,
17364
17364
  contentOf: (element) => {
17365
- const content = element[nameOf$1(element)];
17365
+ const content = element[nameOf$2(element)];
17366
17366
  return Array.isArray(content) ? content : void 0;
17367
17367
  },
17368
17368
  attributeOf: (element, attribute) => {
17369
17369
  var _holder$_attr;
17370
- const content = element[nameOf$1(element)];
17370
+ const content = element[nameOf$2(element)];
17371
17371
  const holder = Array.isArray(content) ? content.find((child) => typeof child === "object" && child !== null && "_attr" in child) : content;
17372
17372
  return holder === null || holder === void 0 || (_holder$_attr = holder._attr) === null || _holder$_attr === void 0 ? void 0 : _holder$_attr[attribute];
17373
17373
  },
@@ -18496,9 +18496,9 @@ EXTERNAL: "External" };
18496
18496
  var LineRuleType = {
18497
18497
  /** Line spacing is at least the specified value */
18498
18498
  AT_LEAST: "atLeast",
18499
- /** Line spacing is exactly the specified value */
18499
+ /** Writes `exactly`, which isn't a value the schema allows, so Word ignores it. Use {@link LineRuleType.EXACT} for an exact line height. */
18500
18500
  EXACTLY: "exactly",
18501
- /** Line spacing is exactly the specified value (alias for EXACTLY) */
18501
+ /** Line spacing is exactly the specified value */
18502
18502
  EXACT: "exact",
18503
18503
  /** Line spacing is automatically determined based on content */
18504
18504
  AUTO: "auto"
@@ -20230,12 +20230,14 @@ MAX: 9026 };
20230
20230
  if (options.widowControl !== void 0) this.push(new OnOffElement("w:widowControl", options.widowControl));
20231
20231
  if (options.bullet) this.push(new NumberProperties(1, options.bullet.level));
20232
20232
  if (options.numbering) {
20233
- var _options$numbering$in, _options$numbering$in2;
20233
+ const { reference: list, instance = 0, level } = options.numbering;
20234
+ const reference = typeof list === "string" ? list : list.reference;
20234
20235
  this.numberingReferences.push({
20235
- reference: options.numbering.reference,
20236
- instance: (_options$numbering$in = options.numbering.instance) !== null && _options$numbering$in !== void 0 ? _options$numbering$in : 0
20236
+ reference,
20237
+ instance,
20238
+ definition: typeof list === "string" ? void 0 : list
20237
20239
  });
20238
- this.push(new NumberProperties(`${options.numbering.reference}-${(_options$numbering$in2 = options.numbering.instance) !== null && _options$numbering$in2 !== void 0 ? _options$numbering$in2 : 0}`, options.numbering.level));
20240
+ this.push(new NumberProperties(`${reference}-${instance}`, level));
20239
20241
  } else if (options.numbering === false) this.push(new NumberProperties(0, 0));
20240
20242
  if (options.border) this.push(new Border(options.border));
20241
20243
  if (options.thematicBreak) this.push(new ThematicBreak());
@@ -20315,7 +20317,7 @@ MAX: 9026 };
20315
20317
  * @returns The prepared XML object, or undefined if the component should be ignored
20316
20318
  */
20317
20319
  prepForXml(context) {
20318
- if (!(context.viewWrapper instanceof FontWrapper)) for (const reference of this.numberingReferences) context.file.Numbering.createConcreteNumberingInstance(reference.reference, reference.instance);
20320
+ if (!(context.viewWrapper instanceof FontWrapper)) for (const { reference, instance, definition } of this.numberingReferences) context.file.Numbering.createConcreteNumberingInstance(reference, instance, definition);
20319
20321
  return super.prepForXml(context);
20320
20322
  }
20321
20323
  };
@@ -26649,6 +26651,35 @@ MAX: 9026 };
26649
26651
  }
26650
26652
  };
26651
26653
  //#endregion
26654
+ //#region src/file/footnotes/footnote/note-paragraph.ts
26655
+ /**
26656
+ * The first paragraph of a footnote or endnote, written with the run that shows the note's number before its content.
26657
+ *
26658
+ * The run is added to what the paragraph is written as, rather than to the paragraph, so a paragraph in more than one
26659
+ * note, such as the notes of two documents, or two notes with the same content, has one number in each.
26660
+ *
26661
+ * @internal
26662
+ */
26663
+ var FirstNoteParagraph = class extends XmlComponent {
26664
+ constructor(paragraph, mark) {
26665
+ super("w:p");
26666
+ _defineProperty(this, "paragraph", void 0);
26667
+ _defineProperty(this, "mark", void 0);
26668
+ this.paragraph = paragraph;
26669
+ this.mark = mark;
26670
+ }
26671
+ prepForXml(context) {
26672
+ const content = this.paragraph.prepForXml(context)["w:p"];
26673
+ const children = Array.isArray(content) ? content : [];
26674
+ const index = children.length > 0 && "w:pPr" in children[0] ? 1 : 0;
26675
+ return { "w:p": [
26676
+ ...children.slice(0, index),
26677
+ this.mark.prepForXml(context),
26678
+ ...children.slice(index)
26679
+ ] };
26680
+ }
26681
+ };
26682
+ //#endregion
26652
26683
  //#region src/file/endnotes/endnote/endnote-attributes.ts
26653
26684
  var EndnoteAttributes = class extends XmlAttributeComponent {
26654
26685
  constructor(..._args) {
@@ -26680,11 +26711,7 @@ MAX: 9026 };
26680
26711
  type: options.type,
26681
26712
  id: options.id
26682
26713
  }));
26683
- for (let i = 0; i < options.children.length; i++) {
26684
- const child = options.children[i];
26685
- if (i === 0) child.addRunToFront(new EndnoteRefRun());
26686
- this.root.push(child);
26687
- }
26714
+ for (const [index, child] of options.children.entries()) this.root.push(index === 0 ? new FirstNoteParagraph(child, new EndnoteRefRun()) : child);
26688
26715
  }
26689
26716
  };
26690
26717
  //#endregion
@@ -26803,6 +26830,8 @@ MAX: 9026 };
26803
26830
  var Endnotes = class extends XmlComponent {
26804
26831
  constructor() {
26805
26832
  super("w:endnotes");
26833
+ _defineProperty(this, "ownNotes", /* @__PURE__ */ new Map());
26834
+ _defineProperty(this, "highestId", 0);
26806
26835
  this.root.push(new EndnotesAttributes({
26807
26836
  wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
26808
26837
  mc: "http://schemas.openxmlformats.org/markup-compatibility/2006",
@@ -26855,6 +26884,27 @@ MAX: 9026 };
26855
26884
  children: paragraph
26856
26885
  });
26857
26886
  this.root.push(endnote);
26887
+ if (Number.isFinite(id)) this.highestId = Math.max(this.highestId, Math.floor(id));
26888
+ }
26889
+ /**
26890
+ * The id of the endnote that a reference run brings with it, which is added to the endnotes the first time the run
26891
+ * is written, with an id no other endnote has. Word has endnotes only in a document's body, so a run written
26892
+ * anywhere else, such as in a header, has none, and its endnote isn't added.
26893
+ *
26894
+ * @internal
26895
+ * @param reference - The run, which has the same endnote each time it is written
26896
+ * @param endnote - The endnote's content
26897
+ * @param context - The context the run is written in
26898
+ * @returns The endnote's id, or undefined outside the document's body
26899
+ */
26900
+ idOf(reference, endnote, context) {
26901
+ if (context.viewWrapper !== context.file.Document) return;
26902
+ const existing = this.ownNotes.get(reference);
26903
+ if (existing !== void 0) return existing;
26904
+ const id = this.highestId + 1;
26905
+ this.createEndnote(id, endnote.children);
26906
+ this.ownNotes.set(reference, id);
26907
+ return id;
26858
26908
  }
26859
26909
  /**
26860
26910
  * Formats the endnotes, with the page numbers worked out for their document written into their fields, when the
@@ -27199,11 +27249,7 @@ MAX: 9026 };
27199
27249
  type: options.type,
27200
27250
  id: options.id
27201
27251
  }));
27202
- for (let i = 0; i < options.children.length; i++) {
27203
- const child = options.children[i];
27204
- if (i === 0) child.addRunToFront(new FootnoteRefRun());
27205
- this.root.push(child);
27206
- }
27252
+ for (const [index, child] of options.children.entries()) this.root.push(index === 0 ? new FirstNoteParagraph(child, new FootnoteRefRun()) : child);
27207
27253
  }
27208
27254
  };
27209
27255
  //#endregion
@@ -27306,6 +27352,8 @@ MAX: 9026 };
27306
27352
  var FootNotes = class extends XmlComponent {
27307
27353
  constructor() {
27308
27354
  super("w:footnotes");
27355
+ _defineProperty(this, "ownNotes", /* @__PURE__ */ new Map());
27356
+ _defineProperty(this, "highestId", 0);
27309
27357
  this.root.push(new FootnotesAttributes({
27310
27358
  wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
27311
27359
  mc: "http://schemas.openxmlformats.org/markup-compatibility/2006",
@@ -27364,6 +27412,27 @@ MAX: 9026 };
27364
27412
  children: paragraph
27365
27413
  });
27366
27414
  this.root.push(footnote);
27415
+ if (Number.isFinite(id)) this.highestId = Math.max(this.highestId, Math.floor(id));
27416
+ }
27417
+ /**
27418
+ * The id of the footnote that a reference run brings with it, which is added to the footnotes the first time the
27419
+ * run is written, with an id no other footnote has. Word has footnotes only in a document's body, so a run written
27420
+ * anywhere else, such as in a header, has none, and its footnote isn't added.
27421
+ *
27422
+ * @internal
27423
+ * @param reference - The run, which has the same footnote each time it is written
27424
+ * @param footnote - The footnote's content
27425
+ * @param context - The context the run is written in
27426
+ * @returns The footnote's id, or undefined outside the document's body
27427
+ */
27428
+ idOf(reference, footnote, context) {
27429
+ if (context.viewWrapper !== context.file.Document) return;
27430
+ const existing = this.ownNotes.get(reference);
27431
+ if (existing !== void 0) return existing;
27432
+ const id = this.highestId + 1;
27433
+ this.createFootNote(id, footnote.children);
27434
+ this.ownNotes.set(reference, id);
27435
+ return id;
27367
27436
  }
27368
27437
  /**
27369
27438
  * Formats the footnotes, with the page numbers worked out for their document written into their fields, when the
@@ -28572,8 +28641,15 @@ MAX: 9026 };
28572
28641
  *
28573
28642
  * @param reference - The reference name of the abstract numbering definition
28574
28643
  * @param instance - The instance number for this concrete numbering
28644
+ * @param definition - The list's definition, for a list a paragraph gives its own definition of. It is added with
28645
+ * its reference, unless the numbering already has a list with that reference, such as one of the document's
28646
+ * `numbering` option, which is kept
28575
28647
  */
28576
- createConcreteNumberingInstance(reference, instance) {
28648
+ createConcreteNumberingInstance(reference, instance, definition) {
28649
+ if (definition && !this.abstractNumberingMap.has(definition.reference)) {
28650
+ this.abstractNumberingMap.set(definition.reference, new AbstractNumbering(this.abstractNumUniqueNumericId(), definition.levels));
28651
+ this.referenceConfigMap.set(definition.reference, definition.levels);
28652
+ }
28577
28653
  const abstractNumbering = this.abstractNumberingMap.get(reference);
28578
28654
  if (!abstractNumbering) return;
28579
28655
  const fullReference = `${reference}-${instance}`;
@@ -29673,7 +29749,7 @@ MAX: 9026 };
29673
29749
  //#endregion
29674
29750
  //#region src/file/styles/styles.ts
29675
29751
  /** The name of a formatted element, such as `w:style`, or `_attr` for its parent's attributes */
29676
- var nameOf = (child) => typeof child === "object" ? Object.keys(child)[0] : void 0;
29752
+ var nameOf$1 = (child) => typeof child === "object" ? Object.keys(child)[0] : void 0;
29677
29753
  /** The attributes of a formatted `w:style` */
29678
29754
  var attributesOf = (style) => {
29679
29755
  var _flat$find;
@@ -29753,16 +29829,16 @@ MAX: 9026 };
29753
29829
  const xml = super.prepForXml(context);
29754
29830
  const children = xml["w:styles"];
29755
29831
  if (!Array.isArray(children)) return xml;
29756
- const named = (name) => children.filter((child) => nameOf(child) === name);
29757
- const ids = children.map((child) => nameOf(child) === "w:style" ? styleIdOf(child) : void 0);
29832
+ const named = (name) => children.filter((child) => nameOf$1(child) === name);
29833
+ const ids = children.map((child) => nameOf$1(child) === "w:style" ? styleIdOf(child) : void 0);
29758
29834
  const kept = children.filter((child, index) => ![
29759
29835
  "_attr",
29760
29836
  "w:docDefaults",
29761
29837
  "w:latentStyles"
29762
- ].includes(nameOf(child)) && (ids[index] === void 0 || ids.lastIndexOf(ids[index]) === index));
29763
- const styles = kept.some((child) => nameOf(child) === "w:style" && isDefaultParagraphStyle(child)) ? kept : kept.map((child) => {
29838
+ ].includes(nameOf$1(child)) && (ids[index] === void 0 || ids.lastIndexOf(ids[index]) === index));
29839
+ const styles = kept.some((child) => nameOf$1(child) === "w:style" && isDefaultParagraphStyle(child)) ? kept : kept.map((child) => {
29764
29840
  var _attributesOf$wType, _attributesOf2;
29765
- return nameOf(child) === "w:style" && styleIdOf(child) === "Normal" && ((_attributesOf$wType = (_attributesOf2 = attributesOf(child)) === null || _attributesOf2 === void 0 ? void 0 : _attributesOf2["w:type"]) !== null && _attributesOf$wType !== void 0 ? _attributesOf$wType : "paragraph") === "paragraph" ? markedAsDefault(child) : child;
29841
+ return nameOf$1(child) === "w:style" && styleIdOf(child) === "Normal" && ((_attributesOf$wType = (_attributesOf2 = attributesOf(child)) === null || _attributesOf2 === void 0 ? void 0 : _attributesOf2["w:type"]) !== null && _attributesOf$wType !== void 0 ? _attributesOf$wType : "paragraph") === "paragraph" ? markedAsDefault(child) : child;
29766
29842
  });
29767
29843
  return { "w:styles": [
29768
29844
  ...named("_attr"),
@@ -31155,16 +31231,6 @@ MAX: 9026 };
31155
31231
  //#endregion
31156
31232
  //#region src/file/footnotes/footnote/run/reference-run.ts
31157
31233
  /**
31158
- * Footnote reference run module for WordprocessingML documents.
31159
- *
31160
- * This module provides the footnote reference marker that appears in the
31161
- * main document text to indicate a footnote.
31162
- *
31163
- * Reference: http://officeopenxml.com/WPfootnotes.php
31164
- *
31165
- * @module
31166
- */
31167
- /**
31168
31234
  * Represents the attributes for a footnote reference element.
31169
31235
  *
31170
31236
  * @internal
@@ -31204,6 +31270,10 @@ MAX: 9026 };
31204
31270
  * (typically a superscript number) that appears in the main document text.
31205
31271
  * Clicking this marker navigates to the corresponding footnote content.
31206
31272
  *
31273
+ * It refers to one of the document's `footnotes` by its id, or is given its footnote's content, for a footnote of its
31274
+ * own, which is added to the document's footnotes when the run is written. Content made before the document it goes
31275
+ * in, such as by a function that makes paragraphs, then brings its footnotes with it.
31276
+ *
31207
31277
  * Reference: http://officeopenxml.com/WPfootnotes.php
31208
31278
  *
31209
31279
  * @publicApi
@@ -31217,17 +31287,44 @@ MAX: 9026 };
31217
31287
  * new FootnoteReferenceRun(1),
31218
31288
  * ],
31219
31289
  * });
31290
+ *
31291
+ * // Or with the footnote's content
31292
+ * new Paragraph({
31293
+ * children: [
31294
+ * new TextRun("This text has a footnote of its own"),
31295
+ * new FootnoteReferenceRun({ children: [new Paragraph("The footnote.")] }),
31296
+ * ],
31297
+ * });
31220
31298
  * ```
31221
31299
  */
31222
31300
  var FootnoteReferenceRun = class extends Run {
31223
31301
  /**
31224
31302
  * Creates a new footnote reference run.
31225
31303
  *
31226
- * @param id - Unique identifier linking to the footnote content
31304
+ * @param footnote - The id of one of the document's `footnotes`, or the footnote's content, for a footnote of the
31305
+ * run's own. Word has footnotes only in a document's body, so a run with its footnote's content is left out of
31306
+ * headers, footers, comments and notes
31227
31307
  */
31228
- constructor(id) {
31308
+ constructor(footnote) {
31229
31309
  super({ style: "FootnoteReference" });
31310
+ _defineProperty(this, "footnote", void 0);
31311
+ if (typeof footnote === "number") this.root.push(new FootnoteReference(footnote));
31312
+ else this.footnote = footnote;
31313
+ }
31314
+ /**
31315
+ * Formats the run, with its own footnote added to the document's footnotes, and referred to by the id it has there.
31316
+ */
31317
+ prepForXml(context) {
31318
+ var _context$file;
31319
+ if (this.footnote === void 0) return super.prepForXml(context);
31320
+ const id = (_context$file = context.file) === null || _context$file === void 0 ? void 0 : _context$file.FootNotes.View.idOf(this, this.footnote, context);
31321
+ if (id === void 0) return;
31230
31322
  this.root.push(new FootnoteReference(id));
31323
+ try {
31324
+ return super.prepForXml(context);
31325
+ } finally {
31326
+ this.root.pop();
31327
+ }
31231
31328
  }
31232
31329
  };
31233
31330
  //#endregion
@@ -31244,10 +31341,47 @@ MAX: 9026 };
31244
31341
  this.root.push(new EndnoteReferenceRunAttributes({ id }));
31245
31342
  }
31246
31343
  };
31344
+ /**
31345
+ * A run with an endnote's reference mark, its number, in the document's text.
31346
+ *
31347
+ * It refers to one of the document's `endnotes` by its id, or is given its endnote's content, for an endnote of its
31348
+ * own, which is added to the document's endnotes when the run is written.
31349
+ *
31350
+ * @publicApi
31351
+ *
31352
+ * @example
31353
+ * ```typescript
31354
+ * new Paragraph({
31355
+ * children: [new TextRun("A claim"), new EndnoteReferenceRun({ children: [new Paragraph("Its source.")] })],
31356
+ * });
31357
+ * ```
31358
+ */
31247
31359
  var EndnoteReferenceRun = class extends Run {
31248
- constructor(id) {
31360
+ /**
31361
+ * @param endnote - The id of one of the document's `endnotes`, or the endnote's content, for an endnote of the run's
31362
+ * own. Word has endnotes only in a document's body, so a run with its endnote's content is left out of headers,
31363
+ * footers, comments and notes
31364
+ */
31365
+ constructor(endnote) {
31249
31366
  super({ style: "EndnoteReference" });
31367
+ _defineProperty(this, "endnote", void 0);
31368
+ if (typeof endnote === "number") this.root.push(new EndnoteIdReference(endnote));
31369
+ else this.endnote = endnote;
31370
+ }
31371
+ /**
31372
+ * Formats the run, with its own endnote added to the document's endnotes, and referred to by the id it has there.
31373
+ */
31374
+ prepForXml(context) {
31375
+ var _context$file;
31376
+ if (this.endnote === void 0) return super.prepForXml(context);
31377
+ const id = (_context$file = context.file) === null || _context$file === void 0 ? void 0 : _context$file.Endnotes.View.idOf(this, this.endnote, context);
31378
+ if (id === void 0) return;
31250
31379
  this.root.push(new EndnoteIdReference(id));
31380
+ try {
31381
+ return super.prepForXml(context);
31382
+ } finally {
31383
+ this.root.pop();
31384
+ }
31251
31385
  }
31252
31386
  };
31253
31387
  //#endregion
@@ -35449,11 +35583,11 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
35449
35583
  *
35450
35584
  * @module
35451
35585
  */
35452
- var formatter$3 = new Formatter();
35586
+ var formatter$4 = new Formatter();
35453
35587
  /**
35454
35588
  * Formats a part's XML, with the relationships that anything in it that refers to other parts adds to.
35455
35589
  */
35456
- var xmlifyPart = (file, content, prettify, relationships, standalone = true) => (0, import_xml.default)(formatter$3.format(content, {
35590
+ var xmlifyPart = (file, content, prettify, relationships, standalone = true) => (0, import_xml.default)(formatter$4.format(content, {
35457
35591
  viewWrapper: {
35458
35592
  View: content,
35459
35593
  Relationships: relationships
@@ -36223,7 +36357,7 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36223
36357
  *
36224
36358
  * @module
36225
36359
  */
36226
- var formatter$2 = new Formatter();
36360
+ var formatter$3 = new Formatter();
36227
36361
  /**
36228
36362
  * Converts XML string to JSON element structure.
36229
36363
  *
@@ -36263,7 +36397,7 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36263
36397
  */
36264
36398
  var createTextElementContents = (text) => {
36265
36399
  var _textJson$elements$0$;
36266
- return (_textJson$elements$0$ = toJson((0, import_xml.default)(formatter$2.format(new Text({ text })))).elements[0].elements) !== null && _textJson$elements$0$ !== void 0 ? _textJson$elements$0$ : [];
36400
+ return (_textJson$elements$0$ = toJson((0, import_xml.default)(formatter$3.format(new Text({ text })))).elements[0].elements) !== null && _textJson$elements$0$ !== void 0 ? _textJson$elements$0$ : [];
36267
36401
  };
36268
36402
  /**
36269
36403
  * Adds xml:space="preserve" attribute to an element.
@@ -36379,6 +36513,81 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36379
36513
  });
36380
36514
  };
36381
36515
  //#endregion
36516
+ //#region src/patcher/drawing-ids.ts
36517
+ var DRAWINGS = /* @__PURE__ */ new Set(["wp:inline", "wp:anchor"]);
36518
+ var ID_ELEMENTS = /* @__PURE__ */ new Set([
36519
+ "wp:docPr",
36520
+ "wps:cNvPr",
36521
+ "wpg:cNvPr"
36522
+ ]);
36523
+ var REFERENCE_ELEMENTS = /* @__PURE__ */ new Set(["a:stCxn", "a:endCxn"]);
36524
+ var idOf$1 = (element, names) => {
36525
+ var _element$name, _element$attributes;
36526
+ if (!names.has((_element$name = element.name) !== null && _element$name !== void 0 ? _element$name : "")) return;
36527
+ const id = Number((_element$attributes = element.attributes) === null || _element$attributes === void 0 ? void 0 : _element$attributes.id);
36528
+ return Number.isInteger(id) ? id : void 0;
36529
+ };
36530
+ /**
36531
+ * Finds the id of every drawing in an element, at any depth, and of the shapes in them, such as in a group.
36532
+ *
36533
+ * @param element - The element to search, such as a part's root
36534
+ * @returns The ids, once for each element that has one
36535
+ */
36536
+ var findDrawingIds = (element) => {
36537
+ var _element$elements;
36538
+ const id = idOf$1(element, ID_ELEMENTS);
36539
+ return [...id === void 0 ? [] : [id], ...((_element$elements = element.elements) !== null && _element$elements !== void 0 ? _element$elements : []).flatMap(findDrawingIds)];
36540
+ };
36541
+ var ownIdsOf = (drawing) => {
36542
+ const idsIn = (element) => {
36543
+ var _element$elements2;
36544
+ const id = idOf$1(element, ID_ELEMENTS);
36545
+ return [...id === void 0 ? [] : [id], ...((_element$elements2 = element.elements) !== null && _element$elements2 !== void 0 ? _element$elements2 : []).flatMap((child) => {
36546
+ var _child$name;
36547
+ return DRAWINGS.has((_child$name = child.name) !== null && _child$name !== void 0 ? _child$name : "") ? [] : idsIn(child);
36548
+ })];
36549
+ };
36550
+ return idsIn(drawing);
36551
+ };
36552
+ /**
36553
+ * Creates a function that renumbers the drawings in content being inserted, so none has an id another drawing in the
36554
+ * document has. Word finds a document whose drawings share an id unreadable, even when they are in different parts,
36555
+ * such as a header and the body. A drawing's id is chosen when it is created, without knowing which ids the template
36556
+ * has, and content inserted more than once would have the same ids each time.
36557
+ *
36558
+ * A drawing keeps its ids if nothing in the document has them yet, and otherwise gets the next unused ones. The
36559
+ * connectors in a drawing then still join the shapes they did.
36560
+ *
36561
+ * @param idsInDocument - The ids of the drawings already in the document, and of the shapes in them
36562
+ * @returns A function that returns the elements with their drawings renumbered
36563
+ */
36564
+ var renumberDrawingsAvoiding = (idsInDocument) => {
36565
+ const usedIds = new Set(idsInDocument);
36566
+ let highestId = idsInDocument.reduce((highest, id) => Math.max(highest, id), 0);
36567
+ const newIdFor = (id) => {
36568
+ const newId = usedIds.has(id) ? highestId + 1 : id;
36569
+ highestId = Math.max(highestId, newId);
36570
+ usedIds.add(newId);
36571
+ return newId;
36572
+ };
36573
+ const renumberDrawing = (drawing) => {
36574
+ const newIds = new Map([...new Set(ownIdsOf(drawing))].map((id) => [id, newIdFor(id)]));
36575
+ const renumberIn = (element) => {
36576
+ var _element$name2, _idOf;
36577
+ if (element !== drawing && DRAWINGS.has((_element$name2 = element.name) !== null && _element$name2 !== void 0 ? _element$name2 : "")) return renumberDrawing(element);
36578
+ const id = (_idOf = idOf$1(element, ID_ELEMENTS)) !== null && _idOf !== void 0 ? _idOf : idOf$1(element, REFERENCE_ELEMENTS);
36579
+ const newId = id === void 0 ? void 0 : newIds.get(id);
36580
+ return _objectSpread2(_objectSpread2(_objectSpread2({}, element), newId === void 0 ? {} : { attributes: _objectSpread2(_objectSpread2({}, element.attributes), {}, { id: String(newId) }) }), element.elements === void 0 ? {} : { elements: element.elements.map(renumberIn) });
36581
+ };
36582
+ return renumberIn(drawing);
36583
+ };
36584
+ const renumber = (element) => {
36585
+ var _element$name3;
36586
+ return DRAWINGS.has((_element$name3 = element.name) !== null && _element$name3 !== void 0 ? _element$name3 : "") ? renumberDrawing(element) : _objectSpread2(_objectSpread2({}, element), element.elements === void 0 ? {} : { elements: element.elements.map(renumber) });
36587
+ };
36588
+ return (elements) => elements.map(renumber);
36589
+ };
36590
+ //#endregion
36382
36591
  //#region src/patcher/patch-type.ts
36383
36592
  /**
36384
36593
  * The types of patch `patchDocument` applies.
@@ -36722,8 +36931,8 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36722
36931
  *
36723
36932
  * @module
36724
36933
  */
36725
- var formatter$1 = new Formatter();
36726
- var DOCUMENT_PATH = "word/document.xml";
36934
+ var formatter$2 = new Formatter();
36935
+ var DOCUMENT_PATH$1 = "word/document.xml";
36727
36936
  var FOOTNOTES = {
36728
36937
  path: "word/footnotes.xml",
36729
36938
  rootName: "w:footnotes",
@@ -36758,7 +36967,7 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36758
36967
  ]
36759
36968
  }
36760
36969
  };
36761
- var formatElement = (component, context) => toJson((0, import_xml.default)(formatter$1.format(component, context))).elements[0];
36970
+ var formatElement$1 = (component, context) => toJson((0, import_xml.default)(formatter$2.format(component, context))).elements[0];
36762
36971
  var findIds = (element, names) => {
36763
36972
  var _element$name, _element$attributes, _element$elements;
36764
36973
  const id = names.includes((_element$name = element.name) !== null && _element$name !== void 0 ? _element$name : "") ? Number((_element$attributes = element.attributes) === null || _element$attributes === void 0 ? void 0 : _element$attributes["w:id"]) : NaN;
@@ -36774,8 +36983,10 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36774
36983
  ...elements.slice(index)
36775
36984
  ] });
36776
36985
  };
36777
- var patchNotesOfKind = (kind, notes, parts, { createContext, addContentTypeOverride, renumberBookmarks }) => {
36986
+ var patchNotesOfKind = (kind, notes, parts, { createContext, addContentTypeOverride, renumberBookmarks, renumberDrawings, renumberLists, useStyles }) => {
36778
36987
  const givenNotes = new Map(Object.entries(notes).map(([id, note]) => [Number(id), note]));
36988
+ const carriedIds = /* @__PURE__ */ new Map();
36989
+ let highestGivenId = [...givenNotes.keys()].filter(Number.isFinite).reduce((highest, id) => Math.max(highest, Math.floor(id)), 0);
36779
36990
  const noteOfId = /* @__PURE__ */ new Map();
36780
36991
  let highestId;
36781
36992
  const renumber = (element) => {
@@ -36788,11 +36999,11 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36788
36999
  };
36789
37000
  const findOrAddPart = () => {
36790
37001
  var _parts$get;
36791
- const relationship = getFirstLevelElements((_parts$get = parts.get(relationshipsPathOf(DOCUMENT_PATH))) !== null && _parts$get !== void 0 ? _parts$get : {}, "Relationships").find((element) => {
37002
+ const relationship = getFirstLevelElements((_parts$get = parts.get(relationshipsPathOf(DOCUMENT_PATH$1))) !== null && _parts$get !== void 0 ? _parts$get : {}, "Relationships").find((element) => {
36792
37003
  var _element$attributes3;
36793
37004
  return ((_element$attributes3 = element.attributes) === null || _element$attributes3 === void 0 ? void 0 : _element$attributes3.Type) === kind.relationshipType;
36794
37005
  });
36795
- const path = relationship ? resolveTarget(DOCUMENT_PATH, String(relationship.attributes.Target)) : kind.path;
37006
+ const path = relationship ? resolveTarget(DOCUMENT_PATH$1, String(relationship.attributes.Target)) : kind.path;
36796
37007
  if (!parts.has(path)) {
36797
37008
  parts.set(path, {
36798
37009
  declaration: { attributes: {
@@ -36800,11 +37011,11 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36800
37011
  encoding: "UTF-8",
36801
37012
  standalone: "yes"
36802
37013
  } },
36803
- elements: [formatElement(kind.create.part(), createContext(path))]
37014
+ elements: [formatElement$1(kind.create.part(), createContext(path))]
36804
37015
  });
36805
37016
  addContentTypeOverride(kind.contentType, `/${path}`);
36806
37017
  }
36807
- if (!relationship) createContext(DOCUMENT_PATH).viewWrapper.Relationships.addRelationship(uniqueId(), kind.relationshipType, relativeTarget(DOCUMENT_PATH, path));
37018
+ if (!relationship) createContext(DOCUMENT_PATH$1).viewWrapper.Relationships.addRelationship(uniqueId(), kind.relationshipType, relativeTarget(DOCUMENT_PATH$1, path));
36808
37019
  return path;
36809
37020
  };
36810
37021
  const write = () => {
@@ -36812,14 +37023,14 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36812
37023
  const path = findOrAddPart();
36813
37024
  const context = createContext(path);
36814
37025
  const contentOfNote = new Map([...new Set(noteOfId.values())].map((note) => {
36815
- const mark = formatElement(kind.create.mark(), context);
36816
- return [note, renumberBookmarks(note.children.map((paragraph) => formatElement(paragraph, context)).map((paragraph, index) => index === 0 ? withMark(paragraph, mark) : paragraph))];
37026
+ const mark = formatElement$1(kind.create.mark(), context);
37027
+ return [note, useStyles(renumberLists(renumberBookmarks(note.children.map((paragraph) => formatElement$1(paragraph, context)).map((paragraph, index) => index === 0 ? withMark(paragraph, mark) : paragraph))))];
36817
37028
  }));
36818
37029
  getFirstLevelElements(parts.get(path), kind.rootName).push(...[...noteOfId].map(([id, note]) => ({
36819
37030
  type: "element",
36820
37031
  name: kind.noteName,
36821
37032
  attributes: { "w:id": String(id) },
36822
- elements: [...contentOfNote.get(note)]
37033
+ elements: [...renumberDrawings(contentOfNote.get(note))]
36823
37034
  })));
36824
37035
  const stylesPart = parts.get("word/styles.xml");
36825
37036
  if (stylesPart) {
@@ -36828,34 +37039,162 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36828
37039
  var _style$attributes;
36829
37040
  return (_style$attributes = style.attributes) === null || _style$attributes === void 0 ? void 0 : _style$attributes["w:styleId"];
36830
37041
  }));
36831
- styles.push(...kind.create.styles().map((style) => formatElement(style, context)).filter((style) => {
37042
+ styles.push(...kind.create.styles().map((style) => formatElement$1(style, context)).filter((style) => {
36832
37043
  var _style$attributes2;
36833
37044
  return !styleIds.has((_style$attributes2 = style.attributes) === null || _style$attributes2 === void 0 ? void 0 : _style$attributes2["w:styleId"]);
36834
37045
  }));
36835
37046
  }
36836
37047
  };
37048
+ const carry = (reference, note) => {
37049
+ const existing = carriedIds.get(reference);
37050
+ if (existing !== void 0) return existing;
37051
+ highestGivenId++;
37052
+ givenNotes.set(highestGivenId, note);
37053
+ carriedIds.set(reference, highestGivenId);
37054
+ return highestGivenId;
37055
+ };
36837
37056
  return {
36838
37057
  renumber: (elements) => elements.map(renumber),
36839
- write
37058
+ write,
37059
+ carry
36840
37060
  };
36841
37061
  };
36842
37062
  /**
36843
37063
  * Creates the writer of the footnotes and endnotes that patches refer to.
36844
37064
  *
36845
37065
  * A patch refers to a note with a reference run, such as `new FootnoteReferenceRun(1)`, whose id is the note's in
36846
- * `footnotes`. Only references to the given notes are renumbered, and only the notes that are referred to are written.
37066
+ * `footnotes`, or with a run that brings its note with it, such as `new FootnoteReferenceRun({ children })`, which is
37067
+ * given an id with `carry` as it is written. Only references to these notes are renumbered, and only the notes that
37068
+ * are referred to are written.
36847
37069
  *
36848
37070
  * @param notes - The footnotes and endnotes, by the id the patches' reference runs are given
36849
37071
  * @param parts - The document's XML parts, parsed, by their paths, which the notes are written to
36850
37072
  * @param helpers - The patcher's helpers for what the notes refer to
36851
37073
  */
36852
37074
  var patchNotes = ({ footnotes = {}, endnotes = {} }, parts, helpers) => {
36853
- const patchers = [patchNotesOfKind(FOOTNOTES, footnotes, parts, helpers), patchNotesOfKind(ENDNOTES, endnotes, parts, helpers)];
37075
+ const patchers = {
37076
+ footnotes: patchNotesOfKind(FOOTNOTES, footnotes, parts, helpers),
37077
+ endnotes: patchNotesOfKind(ENDNOTES, endnotes, parts, helpers)
37078
+ };
36854
37079
  return {
36855
- renumber: (elements) => patchers.reduce((renumbered, patcher) => patcher.renumber(renumbered), elements),
37080
+ carry: (kind, reference, note) => patchers[kind].carry(reference, note),
37081
+ renumber: (elements) => Object.values(patchers).reduce((renumbered, patcher) => patcher.renumber(renumbered), elements),
36856
37082
  write: () => {
36857
- for (const patcher of patchers) patcher.write();
37083
+ for (const patcher of Object.values(patchers)) patcher.write();
37084
+ }
37085
+ };
37086
+ };
37087
+ //#endregion
37088
+ //#region src/patcher/numbering.ts
37089
+ /**
37090
+ * Lists in content inserted into an existing document.
37091
+ *
37092
+ * @module
37093
+ */
37094
+ var formatter$1 = new Formatter();
37095
+ var DOCUMENT_PATH = "word/document.xml";
37096
+ var NUMBERING_PATH = "word/numbering.xml";
37097
+ var NUMBERING_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml";
37098
+ var NUMBERING_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering";
37099
+ var BULLET_REFERENCE = "default-bullet-numbering";
37100
+ var BULLET_NUM_ID = "1";
37101
+ var formatElement = (component, context) => toJson((0, import_xml.default)(formatter$1.format(component, context))).elements[0];
37102
+ var highestOf = (ids) => ids.reduce((highest, id) => Math.max(highest, id), 0);
37103
+ /**
37104
+ * Creates the writer of the lists that patches use.
37105
+ *
37106
+ * A paragraph in a patch is put in a list as in a Document, with `numbering: { reference, level }` for one of the
37107
+ * numbering's references, or with `bullet`. It is written with a placeholder for its list's id, or for a bullet, with
37108
+ * the id a Document's bullet list has, which a template's lists may have too.
37109
+ *
37110
+ * @param numbering - The numbering the patches' paragraphs add their lists to as they are written
37111
+ * @param parts - The document's XML parts, parsed, by their paths, which the lists are written to
37112
+ * @param helpers - The patcher's helpers for the numbering part
37113
+ */
37114
+ var patchNumbering = (numbering, parts, { createContext, addContentTypeOverride }) => {
37115
+ const newNumIds = /* @__PURE__ */ new Map();
37116
+ let highestNumId;
37117
+ const numberingRelationship = () => {
37118
+ var _parts$get;
37119
+ return getFirstLevelElements((_parts$get = parts.get(relationshipsPathOf(DOCUMENT_PATH))) !== null && _parts$get !== void 0 ? _parts$get : {}, "Relationships").find((element) => {
37120
+ var _element$attributes;
37121
+ return ((_element$attributes = element.attributes) === null || _element$attributes === void 0 ? void 0 : _element$attributes.Type) === NUMBERING_RELATIONSHIP;
37122
+ });
37123
+ };
37124
+ const numberingPath = (relationship = numberingRelationship()) => relationship ? resolveTarget(DOCUMENT_PATH, String(relationship.attributes.Target)) : NUMBERING_PATH;
37125
+ const idsInTemplate = (name, attribute) => {
37126
+ var _parts$get2;
37127
+ return getFirstLevelElements((_parts$get2 = parts.get(numberingPath())) !== null && _parts$get2 !== void 0 ? _parts$get2 : {}, "w:numbering").filter((element) => element.name === name).map((element) => Number(element.attributes[attribute]));
37128
+ };
37129
+ const listOf = (numId) => {
37130
+ if (numId === BULLET_NUM_ID) return numbering.ConcreteNumbering.find(({ reference }) => reference === BULLET_REFERENCE);
37131
+ if (!numId.startsWith("{")) return;
37132
+ const list = numbering.ConcreteNumbering.find(({ reference, instance }) => numId === `{${reference}-${instance}}`);
37133
+ if (!list) {
37134
+ const reference = numId.slice(1, -1).replace(/-\d+$/, "");
37135
+ throw new Error(`A paragraph in a patch uses the numbering "${reference}", which isn't in the numbering option of patchDocument. Add it there with its levels, as in a Document`);
37136
+ }
37137
+ return list;
37138
+ };
37139
+ const newNumIdOf = (list) => {
37140
+ var _highestNumId;
37141
+ const knownId = newNumIds.get(list.numId);
37142
+ if (knownId !== void 0) return knownId;
37143
+ highestNumId = ((_highestNumId = highestNumId) !== null && _highestNumId !== void 0 ? _highestNumId : highestOf(idsInTemplate("w:num", "w:numId"))) + 1;
37144
+ newNumIds.set(list.numId, highestNumId);
37145
+ return highestNumId;
37146
+ };
37147
+ const renumber = (element) => {
37148
+ const list = element.name === "w:numId" ? listOf(String(element.attributes["w:val"])) : void 0;
37149
+ if (list === void 0) return element.elements === void 0 ? element : _objectSpread2(_objectSpread2({}, element), {}, { elements: element.elements.map(renumber) });
37150
+ return _objectSpread2(_objectSpread2({}, element), {}, { attributes: _objectSpread2(_objectSpread2({}, element.attributes), {}, { "w:val": String(newNumIdOf(list)) }) });
37151
+ };
37152
+ const findOrAddPart = (written) => {
37153
+ const relationship = numberingRelationship();
37154
+ const path = numberingPath(relationship);
37155
+ if (!parts.has(path)) {
37156
+ parts.set(path, {
37157
+ declaration: { attributes: {
37158
+ version: "1.0",
37159
+ encoding: "UTF-8",
37160
+ standalone: "yes"
37161
+ } },
37162
+ elements: [_objectSpread2(_objectSpread2({}, written), {}, { elements: [] })]
37163
+ });
37164
+ addContentTypeOverride(NUMBERING_CONTENT_TYPE, `/${path}`);
36858
37165
  }
37166
+ if (!relationship) createContext(DOCUMENT_PATH).viewWrapper.Relationships.addRelationship(uniqueId(), NUMBERING_RELATIONSHIP, relativeTarget(DOCUMENT_PATH, path));
37167
+ return path;
37168
+ };
37169
+ const write = () => {
37170
+ var _root$attributes$mcI, _root$attributes;
37171
+ if (newNumIds.size === 0) return;
37172
+ const written = formatElement(numbering, createContext(numberingPath()));
37173
+ const abstractNumIdOf = (list) => Number(list.elements.find((element) => element.name === "w:abstractNumId").attributes["w:val"]);
37174
+ const lists = written.elements.filter((element) => element.name === "w:num" && newNumIds.has(Number(element.attributes["w:numId"])));
37175
+ const highestAbstractNumId = highestOf(idsInTemplate("w:abstractNum", "w:abstractNumId"));
37176
+ const newAbstractNumIds = new Map([...new Set(lists.map(abstractNumIdOf))].map((id, index) => [id, highestAbstractNumId + index + 1]));
37177
+ const abstractLists = written.elements.flatMap((element) => {
37178
+ const newId = element.name === "w:abstractNum" ? newAbstractNumIds.get(Number(element.attributes["w:abstractNumId"])) : void 0;
37179
+ return newId === void 0 ? [] : [_objectSpread2(_objectSpread2({}, element), {}, { attributes: _objectSpread2(_objectSpread2({}, element.attributes), {}, { "w:abstractNumId": String(newId) }) })];
37180
+ });
37181
+ const path = findOrAddPart(written);
37182
+ const root = parts.get(path).elements.find((element) => element.name === "w:numbering");
37183
+ root.attributes = _objectSpread2(_objectSpread2(_objectSpread2({}, Object.fromEntries(Object.entries(written.attributes).filter(([name]) => name.startsWith("xmlns:")))), root.attributes), {}, { "mc:Ignorable": [.../* @__PURE__ */ new Set([...String((_root$attributes$mcI = (_root$attributes = root.attributes) === null || _root$attributes === void 0 ? void 0 : _root$attributes["mc:Ignorable"]) !== null && _root$attributes$mcI !== void 0 ? _root$attributes$mcI : "").split(" "), ...String(written.attributes["mc:Ignorable"]).split(" ")])].filter((prefix) => prefix !== "").join(" ") });
37184
+ const elements = getFirstLevelElements(parts.get(path), "w:numbering");
37185
+ const firstIndexOf = (names) => {
37186
+ const index = elements.findIndex((element) => names.includes(element.name));
37187
+ return index === -1 ? elements.length : index;
37188
+ };
37189
+ elements.splice(firstIndexOf(["w:num", "w:numIdMacAtCleanup"]), 0, ...abstractLists);
37190
+ elements.splice(firstIndexOf(["w:numIdMacAtCleanup"]), 0, ...lists.map((list) => _objectSpread2(_objectSpread2({}, list), {}, {
37191
+ attributes: _objectSpread2(_objectSpread2({}, list.attributes), {}, { "w:numId": String(newNumIds.get(Number(list.attributes["w:numId"]))) }),
37192
+ elements: list.elements.map((element) => element.name === "w:abstractNumId" ? _objectSpread2(_objectSpread2({}, element), {}, { attributes: { "w:val": String(newAbstractNumIds.get(abstractNumIdOf(list))) } }) : element)
37193
+ })));
37194
+ };
37195
+ return {
37196
+ renumber: (elements) => elements.map(renumber),
37197
+ write
36859
37198
  };
36860
37199
  };
36861
37200
  //#endregion
@@ -37286,7 +37625,6 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37286
37625
  *
37287
37626
  * @module
37288
37627
  */
37289
- var formatter = new Formatter();
37290
37628
  var SPLIT_TOKEN = "￿";
37291
37629
  /**
37292
37630
  * Replaces placeholder text in XML with new content from a patch.
@@ -37303,7 +37641,7 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37303
37641
  * @param keepOriginalStyles - Whether to preserve original text formatting
37304
37642
  * @param recursive - Whether to replace every occurrence in a paragraph, rather than only the first
37305
37643
  * @param renumberIds - Renumbers the bookmarks and note references the patch inserts, so they don't take an id the
37306
- * document uses
37644
+ * document uses, and records the styles it uses
37307
37645
  * @returns Result containing the modified element and whether a replacement occurred
37308
37646
  */
37309
37647
  var replacer = ({ json, patch, patchText, context, keepOriginalStyles = true, recursive = true, renumberIds = (elements) => elements }) => {
@@ -37359,7 +37697,10 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37359
37697
  var formatChildren = (patch, context, renumberIds) => renumberIds(patch.children.flatMap((c) => {
37360
37698
  var _c$writtenAs;
37361
37699
  return (_c$writtenAs = c.writtenAs) !== null && _c$writtenAs !== void 0 ? _c$writtenAs : c;
37362
- }).map((c) => toJson((0, import_xml.default)(formatter.format(c, context)))).map((c) => c.elements[0]));
37700
+ }).flatMap((c) => {
37701
+ const formatted = c.prepForXml(context);
37702
+ return formatted ? [toJson((0, import_xml.default)(formatted)).elements[0]] : [];
37703
+ }));
37363
37704
  /**
37364
37705
  * Replaces the first occurrence of the placeholder from `fromIndex` on, splitting the run it starts in.
37365
37706
  *
@@ -37483,6 +37824,91 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37483
37824
  var goToParentElementFromPath = (json, path) => goToElementFromPath(json, path.slice(0, path.length - 1));
37484
37825
  var getLastElementIndexFromPath = (path) => path[path.length - 1];
37485
37826
  //#endregion
37827
+ //#region src/patcher/styles.ts
37828
+ /**
37829
+ * Styles of content inserted into an existing document.
37830
+ *
37831
+ * @module
37832
+ */
37833
+ var formatter = new Formatter();
37834
+ var STYLES_PATH = "word/styles.xml";
37835
+ var REFERENCES = /* @__PURE__ */ new Set([
37836
+ "w:pStyle",
37837
+ "w:rStyle",
37838
+ "w:tblStyle",
37839
+ "w:basedOn",
37840
+ "w:next",
37841
+ "w:link"
37842
+ ]);
37843
+ var idOf = (style) => {
37844
+ var _style$attributes;
37845
+ return String((_style$attributes = style.attributes) === null || _style$attributes === void 0 ? void 0 : _style$attributes["w:styleId"]);
37846
+ };
37847
+ var nameOf = (style) => {
37848
+ var _style$elements;
37849
+ const name = (_style$elements = style.elements) === null || _style$elements === void 0 || (_style$elements = _style$elements.find((element) => element.name === "w:name")) === null || _style$elements === void 0 || (_style$elements = _style$elements.attributes) === null || _style$elements === void 0 ? void 0 : _style$elements["w:val"];
37850
+ return name === void 0 ? void 0 : String(name).toLowerCase();
37851
+ };
37852
+ var formatDefaultStyles = (context) => new Map(Object.values(createDefaultStyles()).map((style) => toJson((0, import_xml.default)(formatter.format(style, context))).elements[0]).filter((style) => style.name === "w:style").map((style) => [idOf(style), style]));
37853
+ /**
37854
+ * Creates the writer of the styles that patches use.
37855
+ *
37856
+ * A paragraph in a patch refers to its style by its id, such as `Heading1` for `HeadingLevel.HEADING_1`, which a
37857
+ * template may not have, as Word only saves the styles that a document uses. Word shows a paragraph whose style the
37858
+ * document doesn't have in the Normal style, and LibreOffice leaves out its number if it is in a list, so the styles of
37859
+ * docx's that the content uses are added to the document.
37860
+ *
37861
+ * @param parts - The document's XML parts, parsed, by their paths, whose styles part the styles are added to
37862
+ * @param createContext - Creates a context to format content for a part with
37863
+ */
37864
+ var patchStyles = (parts, createContext) => {
37865
+ var _parts$get;
37866
+ const templateStyles = getFirstLevelElements((_parts$get = parts.get(STYLES_PATH)) !== null && _parts$get !== void 0 ? _parts$get : {}, "w:styles").filter((element) => element.name === "w:style");
37867
+ const templateIds = new Set(templateStyles.map(idOf));
37868
+ let defaultStyles;
37869
+ const defaultStyleOf = (id) => {
37870
+ var _defaultStyles;
37871
+ (_defaultStyles = defaultStyles) !== null && _defaultStyles !== void 0 || (defaultStyles = formatDefaultStyles(createContext(STYLES_PATH)));
37872
+ return defaultStyles.get(id);
37873
+ };
37874
+ const missingIds = /* @__PURE__ */ new Set();
37875
+ const idsInDocument = /* @__PURE__ */ new Map();
37876
+ const findIdInDocument = (id) => {
37877
+ const style = templateIds.has(id) ? void 0 : defaultStyleOf(id);
37878
+ if (style === void 0) return id;
37879
+ const sameStyle = templateStyles.find((templateStyle) => {
37880
+ var _templateStyle$attrib, _style$attributes2;
37881
+ return ((_templateStyle$attrib = templateStyle.attributes) === null || _templateStyle$attrib === void 0 ? void 0 : _templateStyle$attrib["w:type"]) === ((_style$attributes2 = style.attributes) === null || _style$attributes2 === void 0 ? void 0 : _style$attributes2["w:type"]) && nameOf(templateStyle) === nameOf(style);
37882
+ });
37883
+ if (sameStyle !== void 0) return idOf(sameStyle);
37884
+ missingIds.add(id);
37885
+ return id;
37886
+ };
37887
+ const idInDocument = (id) => {
37888
+ if (!idsInDocument.has(id)) idsInDocument.set(id, findIdInDocument(id));
37889
+ return idsInDocument.get(id);
37890
+ };
37891
+ const withIdsInDocument = (element) => {
37892
+ var _element$name;
37893
+ if (REFERENCES.has((_element$name = element.name) !== null && _element$name !== void 0 ? _element$name : "")) return _objectSpread2(_objectSpread2({}, element), {}, { attributes: _objectSpread2(_objectSpread2({}, element.attributes), {}, { "w:val": idInDocument(String(element.attributes["w:val"])) }) });
37894
+ return element.elements === void 0 ? element : _objectSpread2(_objectSpread2({}, element), {}, { elements: element.elements.map(withIdsInDocument) });
37895
+ };
37896
+ const write = () => {
37897
+ const stylesPart = parts.get(STYLES_PATH);
37898
+ if (!stylesPart) return;
37899
+ const styles = getFirstLevelElements(stylesPart, "w:styles");
37900
+ const ids = new Set(styles.map(idOf));
37901
+ for (const id of missingIds) if (!ids.has(id)) {
37902
+ ids.add(id);
37903
+ styles.push(withIdsInDocument(defaultStyleOf(id)));
37904
+ }
37905
+ };
37906
+ return {
37907
+ use: (elements) => elements.map(withIdsInDocument),
37908
+ write
37909
+ };
37910
+ };
37911
+ //#endregion
37486
37912
  //#region src/patcher/table-rows.ts
37487
37913
  var EMPTY = {
37488
37914
  type: PatchType.PARAGRAPH,
@@ -37497,8 +37923,9 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37497
37923
  * @param patch - The rows of data
37498
37924
  * @param delimiters - The placeholders' delimiters
37499
37925
  * @param patchPlaceholder - Patches a field in a copy of the rows, as `patchDocument` patches a placeholder
37926
+ * @param renumberDrawings - Gives the drawings in a copy of the rows ids no other drawing in the document has
37500
37927
  */
37501
- var patchTableRows = ({ json, key, patch, delimiters: { start, end }, patchPlaceholder }) => {
37928
+ var patchTableRows = ({ json, key, patch, delimiters: { start, end }, patchPlaceholder, renumberDrawings }) => {
37502
37929
  const fieldStart = `${start}${key}.`;
37503
37930
  for (const rows of findRowsToRepeat(json, fieldStart)) {
37504
37931
  const fields = fieldsIn(rows.elements, fieldStart, end);
@@ -37506,7 +37933,7 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37506
37933
  const copy = {
37507
37934
  type: "element",
37508
37935
  name: "w:tbl",
37509
- elements: [...index === 0 ? rows.elements.map(copyOf) : withoutIds(rows.elements)]
37936
+ elements: [...index === 0 ? rows.elements.map(copyOf) : renumberDrawings(withoutIds(rows.elements))]
37510
37937
  };
37511
37938
  const fieldPatches = new Map(Object.entries(row).flatMap(([field, p]) => p === void 0 ? [] : [[field, p]]));
37512
37939
  for (const [field, fieldPatch] of fieldPatches) patchPlaceholder(copy, `${key}.${field}`, fieldPatch);
@@ -37756,14 +38183,17 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37756
38183
  var _ref = _asyncToGenerator(function* ({ outputType, data, patches, keepOriginalStyles, placeholderDelimiters = {
37757
38184
  start: "{{",
37758
38185
  end: "}}"
37759
- }, recursive = true, footnotes, endnotes, pageNumbers }) {
38186
+ }, recursive = true, footnotes, endnotes, numbering = { config: [] }, pageNumbers }) {
37760
38187
  const zipContent = data instanceof import_jszip_min.default ? data : yield import_jszip_min.default.loadAsync(data);
37761
38188
  const contexts = /* @__PURE__ */ new Map();
37762
38189
  const themeColors = yield readThemeColors(zipContent);
37763
38190
  const contentTypeOverrides = [];
37764
38191
  const file = {
37765
38192
  Media: new Media(),
38193
+ Numbering: new Numbering(numbering),
37766
38194
  Theme: themeColors && { Colors: themeColors },
38195
+ FootNotes: { View: { idOf: (reference, note, context) => carryNote("footnotes", reference, note, context) } },
38196
+ Endnotes: { View: { idOf: (reference, note, context) => carryNote("endnotes", reference, note, context) } },
37767
38197
  PackageParts: new PackageParts({ addOverride: (contentType, partName) => contentTypeOverrides.push({
37768
38198
  contentType,
37769
38199
  partName
@@ -37806,9 +38236,12 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37806
38236
  map.set(key, json);
37807
38237
  }
37808
38238
  const renumberBookmarks = renumberBookmarksAvoiding([...map.values()].flatMap(findBookmarkIds));
37809
- const createContext = (key) => ({
37810
- file,
37811
- viewWrapper: { Relationships: { addRelationship: (id, type, target, targetMode) => {
38239
+ const renumberDrawings = renumberDrawingsAvoiding([...map.values()].flatMap(findDrawingIds));
38240
+ const viewWrappers = /* @__PURE__ */ new Map();
38241
+ const viewWrapperOf = (key) => {
38242
+ const existing = viewWrappers.get(key);
38243
+ if (existing) return existing;
38244
+ const viewWrapper = { Relationships: { addRelationship: (id, type, target, targetMode) => {
37812
38245
  relationshipAdditions.push({
37813
38246
  key,
37814
38247
  id,
@@ -37816,20 +38249,38 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37816
38249
  target,
37817
38250
  targetMode
37818
38251
  });
37819
- } } },
38252
+ } } };
38253
+ viewWrappers.set(key, viewWrapper);
38254
+ return viewWrapper;
38255
+ };
38256
+ const createContext = (key) => ({
38257
+ file,
38258
+ viewWrapper: viewWrapperOf(key),
37820
38259
  stack: []
37821
38260
  });
38261
+ const addContentTypeOverride = (contentType, partName) => {
38262
+ contentTypeOverrides.push({
38263
+ contentType,
38264
+ partName
38265
+ });
38266
+ };
38267
+ const styles = patchStyles(map, createContext);
38268
+ const lists = patchNumbering(file.Numbering, map, {
38269
+ createContext,
38270
+ addContentTypeOverride
38271
+ });
37822
38272
  const notes = patchNotes({
37823
38273
  footnotes,
37824
38274
  endnotes
37825
38275
  }, map, {
37826
38276
  createContext,
37827
- addContentTypeOverride: (contentType, partName) => contentTypeOverrides.push({
37828
- contentType,
37829
- partName
37830
- }),
37831
- renumberBookmarks
38277
+ addContentTypeOverride,
38278
+ renumberBookmarks,
38279
+ renumberDrawings,
38280
+ renumberLists: lists.renumber,
38281
+ useStyles: styles.use
37832
38282
  });
38283
+ const carryNote = (kind, reference, note, context) => context.viewWrapper === viewWrapperOf("word/document.xml") ? notes.carry(kind, reference, note) : void 0;
37833
38284
  patchDrawings({
37834
38285
  parts: map,
37835
38286
  binaryParts: binaryContentMap,
@@ -37856,7 +38307,8 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37856
38307
  start,
37857
38308
  end
37858
38309
  },
37859
- patchPlaceholder
38310
+ patchPlaceholder,
38311
+ renumberDrawings
37860
38312
  });
37861
38313
  return;
37862
38314
  }
@@ -37881,13 +38333,15 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37881
38333
  context,
37882
38334
  keepOriginalStyles,
37883
38335
  recursive,
37884
- renumberIds: (elements) => notes.renumber(renumberBookmarks(elements))
38336
+ renumberIds: (elements) => styles.use(lists.renumber(notes.renumber(renumberBookmarks(renumberDrawings(elements)))))
37885
38337
  });
37886
38338
  if (didFindOccurrence) relationshipAdditions.push(...hyperlinkRelationships);
37887
38339
  };
37888
38340
  for (const [patchKey, patchValue] of patchesInOrder) patchPlaceholder(json, patchKey, patchValue);
37889
38341
  }
37890
38342
  notes.write();
38343
+ lists.write();
38344
+ styles.write();
37891
38345
  for (const [key, json] of map) {
37892
38346
  if (!key.startsWith("word/") || key.endsWith(".xml.rels")) continue;
37893
38347
  const mediaDatas = imageReplacer.getMediaData(JSON.stringify(json), file.Media);