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