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