docx 9.9.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.
@@ -11035,7 +11035,7 @@ DOT: "dot" };
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 @@ DOT: "dot" };
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) {
@@ -12789,7 +12789,7 @@ DOT: "dot" };
12789
12789
  hash.sha512 = hash.sha.sha512;
12790
12790
  hash.ripemd160 = hash.ripemd.ripemd160;
12791
12791
  })))(), 1);
12792
- var urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
12792
+ var urlAlphabet = "57X6y3wQZNzYjJB2L_10pxbackgroundhimselfUTF-8DEMOGRAPHICSVqvt4KW9";
12793
12793
  //#endregion
12794
12794
  //#region node_modules/nanoid/non-secure/index.js
12795
12795
  var customAlphabet = (alphabet, defaultSize = 21) => {
@@ -14586,48 +14586,6 @@ DOT: "dot" };
14586
14586
  }
14587
14587
  };
14588
14588
  //#endregion
14589
- //#region src/file/drawing/doc-properties/doc-properties-children.ts
14590
- /**
14591
- * Child elements for document properties.
14592
- *
14593
- * This module provides hyperlink elements that can be attached to
14594
- * drawing elements for interactive behavior.
14595
- *
14596
- * @module
14597
- */
14598
- /**
14599
- * Creates a click hyperlink element for a drawing.
14600
- *
14601
- * This element defines what happens when a user clicks on a drawing element.
14602
- *
14603
- * ## XSD Schema
14604
- * ```xml
14605
- * <xsd:complexType name="CT_Hyperlink">
14606
- * <xsd:group ref="EG_PContent" minOccurs="0" maxOccurs="unbounded"/>
14607
- * <xsd:attribute name="tgtFrame" type="s:ST_String"/>
14608
- * <xsd:attribute name="tooltip" type="s:ST_String"/>
14609
- * <xsd:attribute name="docLocation" type="s:ST_String"/>
14610
- * <xsd:attribute name="history" type="s:ST_OnOff"/>
14611
- * <xsd:attribute name="anchor" type="s:ST_String"/>
14612
- * <xsd:attribute ref="r:id"/>
14613
- * </xsd:complexType>
14614
- * ```
14615
- *
14616
- * @param linkId - The relationship ID for the hyperlink target
14617
- * @param hasXmlNs - Whether to include the XML namespace declaration
14618
- * @returns An XML component representing the click hyperlink
14619
- */
14620
- var createHyperlinkClick = (linkId, hasXmlNs) => new BuilderElement({
14621
- name: "a:hlinkClick",
14622
- attributes: _objectSpread2(_objectSpread2({}, hasXmlNs ? { xmlns: {
14623
- key: "xmlns:a",
14624
- value: "http://schemas.openxmlformats.org/drawingml/2006/main"
14625
- } } : {}), {}, { id: {
14626
- key: "r:id",
14627
- value: `rId${linkId}`
14628
- } })
14629
- });
14630
- //#endregion
14631
14589
  //#region src/file/drawing/inline/graphic/graphic-data/pic/non-visual-pic-properties/non-visual-properties/non-visual-properties-attributes.ts
14632
14590
  /**
14633
14591
  * Non-visual properties attributes module.
@@ -14708,7 +14666,7 @@ DOT: "dot" };
14708
14666
  for (let i = context.stack.length - 1; i >= 0; i--) {
14709
14667
  const element = context.stack[i];
14710
14668
  if (!(element instanceof ConcreteHyperlink)) continue;
14711
- this.root.push(createHyperlinkClick(element.linkId, false));
14669
+ this.root.push(element.createDrawingClick(context, false));
14712
14670
  break;
14713
14671
  }
14714
14672
  const result = super.prepForXml(context);
@@ -15378,6 +15336,48 @@ EXTERNAL: "External" };
15378
15336
  }
15379
15337
  });
15380
15338
  //#endregion
15339
+ //#region src/file/drawing/doc-properties/doc-properties-children.ts
15340
+ /**
15341
+ * Child elements for document properties.
15342
+ *
15343
+ * This module provides hyperlink elements that can be attached to
15344
+ * drawing elements for interactive behavior.
15345
+ *
15346
+ * @module
15347
+ */
15348
+ /**
15349
+ * Creates a click hyperlink element for a drawing.
15350
+ *
15351
+ * This element defines what happens when a user clicks on a drawing element.
15352
+ *
15353
+ * ## XSD Schema
15354
+ * ```xml
15355
+ * <xsd:complexType name="CT_Hyperlink">
15356
+ * <xsd:group ref="EG_PContent" minOccurs="0" maxOccurs="unbounded"/>
15357
+ * <xsd:attribute name="tgtFrame" type="s:ST_String"/>
15358
+ * <xsd:attribute name="tooltip" type="s:ST_String"/>
15359
+ * <xsd:attribute name="docLocation" type="s:ST_String"/>
15360
+ * <xsd:attribute name="history" type="s:ST_OnOff"/>
15361
+ * <xsd:attribute name="anchor" type="s:ST_String"/>
15362
+ * <xsd:attribute ref="r:id"/>
15363
+ * </xsd:complexType>
15364
+ * ```
15365
+ *
15366
+ * @param linkId - The relationship ID for the hyperlink target
15367
+ * @param hasXmlNs - Whether to include the XML namespace declaration
15368
+ * @returns An XML component representing the click hyperlink
15369
+ */
15370
+ var createHyperlinkClick = (linkId, hasXmlNs) => new BuilderElement({
15371
+ name: "a:hlinkClick",
15372
+ attributes: _objectSpread2(_objectSpread2({}, hasXmlNs ? { xmlns: {
15373
+ key: "xmlns:a",
15374
+ value: "http://schemas.openxmlformats.org/drawingml/2006/main"
15375
+ } } : {}), {}, { id: {
15376
+ key: "r:id",
15377
+ value: `rId${linkId}`
15378
+ } })
15379
+ });
15380
+ //#endregion
15381
15381
  //#region src/file/drawing/doc-properties/non-visual-drawing-properties.ts
15382
15382
  /**
15383
15383
  * Non-visual drawing properties (`CT_NonVisualDrawingProps`) of the shapes, pictures and groups inside a drawing:
@@ -15572,7 +15572,7 @@ EXTERNAL: "External" };
15572
15572
  } else for (let i = context.stack.length - 1; i >= 0; i--) {
15573
15573
  const element = context.stack[i];
15574
15574
  if (!(element instanceof ConcreteHyperlink)) continue;
15575
- this.root.push(createHyperlinkClick(element.linkId, true));
15575
+ this.root.push(element.createDrawingClick(context, true));
15576
15576
  break;
15577
15577
  }
15578
15578
  if (this.decorative) this.root.push(createDecorativeExtensionList(true));
@@ -16887,14 +16887,14 @@ EXTERNAL: "External" };
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 @@ EXTERNAL: "External" };
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 @@ EXTERNAL: "External" };
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 @@ EXTERNAL: "External" };
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 @@ EXTERNAL: "External" };
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 @@ EXTERNAL: "External" };
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 @@ EXTERNAL: "External" };
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 @@ EXTERNAL: "External" };
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 @@ EXTERNAL: "External" };
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 @@ EXTERNAL: "External" };
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 @@ EXTERNAL: "External" };
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"
@@ -18960,20 +18960,44 @@ MAX: 9026 };
18960
18960
  * ```
18961
18961
  */
18962
18962
  var ConcreteHyperlink = class extends XmlComponent {
18963
- constructor(children, relationshipId, anchor) {
18963
+ /**
18964
+ * @param children - Inline content of the hyperlink
18965
+ * @param relationshipId - Id of the external relationship, without the rId prefix
18966
+ * @param anchor - Bookmark name or external URL fragment
18967
+ * @param externalLink - The full address of an external hyperlink, so the relationship id is kept alongside its anchor
18968
+ */
18969
+ constructor(children, relationshipId, anchor, externalLink) {
18964
18970
  super("w:hyperlink");
18965
18971
  _defineProperty(this, "linkId", void 0);
18972
+ _defineProperty(this, "drawingLink", void 0);
18966
18973
  this.linkId = relationshipId;
18974
+ this.drawingLink = anchor && externalLink !== void 0 ? new DrawingLink(externalLink) : void 0;
18967
18975
  const attributes = new HyperlinkAttributes({
18968
18976
  history: 1,
18969
18977
  anchor: anchor ? anchor : void 0,
18970
- id: !anchor ? `rId${this.linkId}` : void 0
18978
+ id: !anchor || externalLink !== void 0 ? `rId${this.linkId}` : void 0
18971
18979
  });
18972
18980
  this.root.push(attributes);
18973
18981
  children.forEach((child) => {
18974
18982
  this.root.push(child);
18975
18983
  });
18976
18984
  }
18985
+ /**
18986
+ * Creates the `a:hlinkClick` of a picture in the hyperlink, which links to the same address.
18987
+ *
18988
+ * The hyperlink decides which relationship its pictures use, rather than the pictures reading `linkId`, because only it
18989
+ * knows whether that relationship has lost the fragment to `w:anchor`. The picture's relationship is added as the
18990
+ * picture is written, through the context's relationships, so it lands in the part the picture is in, both when
18991
+ * packing and when patching.
18992
+ *
18993
+ * @param context - The context the picture is written in, whose relationships get the picture's link if it has its own
18994
+ * @param declareNamespace - Declares the DrawingML namespace, for elements outside `a:graphic` such as `wp:docPr`
18995
+ */
18996
+ createDrawingClick(context, declareNamespace) {
18997
+ if (!this.drawingLink) return createHyperlinkClick(this.linkId, declareNamespace);
18998
+ this.drawingLink.addRelationship(context);
18999
+ return this.drawingLink.createClick(declareNamespace);
19000
+ }
18977
19001
  };
18978
19002
  /**
18979
19003
  * Represents an internal hyperlink to a bookmark within the document.
@@ -19022,6 +19046,11 @@ MAX: 9026 };
19022
19046
  * External hyperlinks create a relationship to an external resource (URL).
19023
19047
  * The relationship is created during document preparation and the hyperlink
19024
19048
  * is converted to a ConcreteHyperlink with the relationship ID.
19049
+ * URL fragments of at most 255 UTF-16 units are written as anchors, preserving any additional # characters.
19050
+ * Longer fragments stay in the original relationship URI to avoid exceeding Word's anchor limit;
19051
+ * those links retain the existing limitations for fragments with multiple # characters.
19052
+ * Word appends the anchor to the relationship target as described in MS-OI29500 §17.16.22:
19053
+ * https://learn.microsoft.com/en-us/openspecs/office_standards/ms-oi29500/df06e423-11a6-4a36-bfb3-82139e531781
19025
19054
  *
19026
19055
  * Reference: http://officeopenxml.com/WPhyperlink.php
19027
19056
  *
@@ -19035,6 +19064,7 @@ MAX: 9026 };
19035
19064
  * <xsd:group ref="EG_PContent" minOccurs="0" maxOccurs="unbounded"/>
19036
19065
  * <xsd:attribute ref="r:id"/>
19037
19066
  * <xsd:attribute name="history" type="s:ST_OnOff" use="optional"/>
19067
+ * <xsd:attribute name="anchor" type="s:ST_String" use="optional"/>
19038
19068
  * </xsd:complexType>
19039
19069
  * ```
19040
19070
  *
@@ -19483,6 +19513,29 @@ MAX: 9026 };
19483
19513
  }
19484
19514
  };
19485
19515
  //#endregion
19516
+ //#region src/file/paragraph/links/external-hyperlink-target.ts
19517
+ /**
19518
+ * External hyperlink targets and fragments for WordprocessingML.
19519
+ *
19520
+ * Word appends the anchor to the relationship target, separated by a # character.
19521
+ * Reference: https://learn.microsoft.com/en-us/openspecs/office_standards/ms-oi29500/df06e423-11a6-4a36-bfb3-82139e531781
19522
+ *
19523
+ * @module
19524
+ */
19525
+ var MAX_ANCHOR_LENGTH = 255;
19526
+ /**
19527
+ * Splits a nonempty fragment that fits Word's anchor limit without decoding or normalizing either part.
19528
+ * Longer fragments stay in the original relationship URI, retaining its existing multi-hash limitations.
19529
+ */
19530
+ var externalHyperlinkTarget = (link) => {
19531
+ const index = link.indexOf("#");
19532
+ const fragmentLength = link.length - index - 1;
19533
+ return index > 0 && fragmentLength > 0 && fragmentLength <= MAX_ANCHOR_LENGTH ? {
19534
+ target: link.slice(0, index),
19535
+ anchor: link.slice(index + 1)
19536
+ } : { target: link };
19537
+ };
19538
+ //#endregion
19486
19539
  //#region src/file/fonts/font.ts
19487
19540
  /**
19488
19541
  * Font module for WordprocessingML documents.
@@ -19621,32 +19674,26 @@ MAX: 9026 };
19621
19674
  ]
19622
19675
  });
19623
19676
  //#endregion
19624
- //#region src/file/fonts/create-regular-font.ts
19677
+ //#region src/file/fonts/create-embedded-font.ts
19625
19678
  /**
19626
- * Creates a regular embedded font with default settings.
19679
+ * Creates an embedded font with default settings.
19627
19680
  *
19628
19681
  * This helper function creates a font definition with standard font signature
19629
19682
  * values that work for most common fonts. The signature specifies Unicode
19630
19683
  * and code page ranges supported by the font.
19631
19684
  *
19632
- * @param options - Font creation options
19633
- * @param options.name - Font name
19634
- * @param options.index - Font relationship index
19635
- * @param options.fontKey - Unique font key (GUID) for obfuscation
19636
- * @param options.characterSet - Optional character set identifier
19637
- *
19638
19685
  * @returns XmlComponent representing the font definition
19639
19686
  *
19640
19687
  * @example
19641
19688
  * ```typescript
19642
- * const font = createRegularFont({
19689
+ * const font = createEmbeddedFont({
19643
19690
  * name: "Arial",
19644
- * index: 1,
19645
- * fontKey: "12345678-1234-1234-1234-123456789012"
19691
+ * embedRegular: { id: "rId1", fontKey: "12345678-1234-1234-1234-123456789012" },
19692
+ * embedBold: { id: "rId2", fontKey: "12345678-1234-1234-1234-123456789013" },
19646
19693
  * });
19647
19694
  * ```
19648
19695
  */
19649
- var createRegularFont = ({ name, index, fontKey, characterSet }) => createFont({
19696
+ var createEmbeddedFont = ({ name, characterSet, embedRegular, embedBold, embedItalic, embedBoldItalic }) => createFont({
19650
19697
  name,
19651
19698
  sig: {
19652
19699
  usb0: "E0002AFF",
@@ -19659,10 +19706,10 @@ MAX: 9026 };
19659
19706
  charset: characterSet,
19660
19707
  family: "auto",
19661
19708
  pitch: "variable",
19662
- embedRegular: {
19663
- fontKey,
19664
- id: `rId${index}`
19665
- }
19709
+ embedRegular,
19710
+ embedBold,
19711
+ embedItalic,
19712
+ embedBoldItalic
19666
19713
  });
19667
19714
  //#endregion
19668
19715
  //#region src/file/fonts/font-table.ts
@@ -19740,25 +19787,47 @@ MAX: 9026 };
19740
19787
  value: "w14 w15 w16se w16cid w16 w16cex w16sdtdh"
19741
19788
  }
19742
19789
  },
19743
- children: fonts.map((font, i) => createRegularFont({
19744
- name: font.name,
19745
- index: i + 1,
19746
- fontKey: font.fontKey,
19747
- characterSet: font.characterSet
19748
- }))
19790
+ children: fonts.map(createEmbeddedFont)
19749
19791
  });
19750
19792
  //#endregion
19751
19793
  //#region src/file/fonts/font-wrapper.ts
19794
+ var FACES = [
19795
+ {
19796
+ option: "data",
19797
+ element: "embedRegular",
19798
+ bold: false,
19799
+ italic: false
19800
+ },
19801
+ {
19802
+ option: "bold",
19803
+ element: "embedBold",
19804
+ bold: true,
19805
+ italic: false
19806
+ },
19807
+ {
19808
+ option: "italic",
19809
+ element: "embedItalic",
19810
+ bold: false,
19811
+ italic: true
19812
+ },
19813
+ {
19814
+ option: "boldItalic",
19815
+ element: "embedBoldItalic",
19816
+ bold: true,
19817
+ italic: true
19818
+ }
19819
+ ];
19752
19820
  /**
19753
19821
  * Wrapper class for managing the font table and its relationships.
19754
19822
  *
19755
19823
  * Creates a font table with embedded font files and manages the relationships
19756
- * required for font embedding. Each font is assigned a unique key for obfuscation.
19824
+ * required for font embedding. Each face of a font is embedded as a file of its own,
19825
+ * with a unique key for obfuscation.
19757
19826
  *
19758
19827
  * @example
19759
19828
  * ```typescript
19760
19829
  * const fontWrapper = new FontWrapper([
19761
- * { name: "CustomFont", data: fontBuffer }
19830
+ * { name: "CustomFont", data: fontBuffer, bold: boldFontBuffer }
19762
19831
  * ]);
19763
19832
  * ```
19764
19833
  */
@@ -19767,12 +19836,43 @@ MAX: 9026 };
19767
19836
  _defineProperty(this, "options", void 0);
19768
19837
  _defineProperty(this, "fontTable", void 0);
19769
19838
  _defineProperty(this, "relationships", void 0);
19770
- _defineProperty(this, "fontOptionsWithKey", []);
19839
+ _defineProperty(
19840
+ this,
19841
+ /** @deprecated Use `files`, which has the file and key of each face a font embeds, not only the regular face's key */
19842
+ "fontOptionsWithKey",
19843
+ []
19844
+ );
19845
+ _defineProperty(
19846
+ this,
19847
+ /** The files of the faces the fonts embed, in order: the Nth is `fonts/font<N>.odttf`, with the relationship `rId<N>` */
19848
+ "files",
19849
+ []
19850
+ );
19771
19851
  this.options = options;
19772
- this.fontOptionsWithKey = options.map((o) => _objectSpread2(_objectSpread2({}, o), {}, { fontKey: uniqueUuid() }));
19773
- this.fontTable = createFontTable(this.fontOptionsWithKey);
19852
+ const faces = options.map((font) => FACES.flatMap(({ option, element, bold, italic }) => {
19853
+ const data = font[option];
19854
+ return data ? [{
19855
+ element,
19856
+ file: {
19857
+ name: font.name,
19858
+ data,
19859
+ bold,
19860
+ italic,
19861
+ fontKey: uniqueUuid()
19862
+ }
19863
+ }] : [];
19864
+ }));
19865
+ this.files = faces.flatMap((ofFont) => ofFont.map(({ file }) => file));
19866
+ this.fontOptionsWithKey = options.map((font, index) => _objectSpread2(_objectSpread2({}, font), {}, { fontKey: faces[index][0].file.fontKey }));
19867
+ this.fontTable = createFontTable(options.map((font, index) => _objectSpread2({
19868
+ name: font.name,
19869
+ characterSet: font.characterSet
19870
+ }, Object.fromEntries(faces[index].map(({ element, file }) => [element, {
19871
+ id: `rId${this.files.indexOf(file) + 1}`,
19872
+ fontKey: file.fontKey
19873
+ }])))));
19774
19874
  this.relationships = new Relationships();
19775
- for (let i = 0; i < options.length; i++) this.relationships.addRelationship(i + 1, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/font", `fonts/font${i + 1}.odttf`);
19875
+ for (let i = 0; i < this.files.length; i++) this.relationships.addRelationship(i + 1, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/font", `fonts/font${i + 1}.odttf`);
19776
19876
  }
19777
19877
  get View() {
19778
19878
  return this.fontTable;
@@ -20132,12 +20232,14 @@ MAX: 9026 };
20132
20232
  if (options.widowControl !== void 0) this.push(new OnOffElement("w:widowControl", options.widowControl));
20133
20233
  if (options.bullet) this.push(new NumberProperties(1, options.bullet.level));
20134
20234
  if (options.numbering) {
20135
- 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;
20136
20237
  this.numberingReferences.push({
20137
- reference: options.numbering.reference,
20138
- 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
20139
20241
  });
20140
- 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));
20141
20243
  } else if (options.numbering === false) this.push(new NumberProperties(0, 0));
20142
20244
  if (options.border) this.push(new Border(options.border));
20143
20245
  if (options.thematicBreak) this.push(new ThematicBreak());
@@ -20217,7 +20319,7 @@ MAX: 9026 };
20217
20319
  * @returns The prepared XML object, or undefined if the component should be ignored
20218
20320
  */
20219
20321
  prepForXml(context) {
20220
- 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);
20221
20323
  return super.prepForXml(context);
20222
20324
  }
20223
20325
  };
@@ -20295,8 +20397,9 @@ MAX: 9026 };
20295
20397
  prepForXml(context) {
20296
20398
  for (const element of this.root) if (element instanceof ExternalHyperlink) {
20297
20399
  const index = this.root.indexOf(element);
20298
- const concreteHyperlink = new ConcreteHyperlink(element.options.children, uniqueId());
20299
- context.viewWrapper.Relationships.addRelationship(concreteHyperlink.linkId, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink", element.options.link, TargetModeType.EXTERNAL);
20400
+ const { target, anchor } = externalHyperlinkTarget(element.options.link);
20401
+ const concreteHyperlink = new ConcreteHyperlink(element.options.children, uniqueId(), anchor, element.options.link);
20402
+ context.viewWrapper.Relationships.addRelationship(concreteHyperlink.linkId, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink", target, TargetModeType.EXTERNAL);
20300
20403
  this.root[index] = concreteHyperlink;
20301
20404
  }
20302
20405
  return super.prepForXml(context);
@@ -26550,6 +26653,35 @@ MAX: 9026 };
26550
26653
  }
26551
26654
  };
26552
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
26553
26685
  //#region src/file/endnotes/endnote/endnote-attributes.ts
26554
26686
  var EndnoteAttributes = class extends XmlAttributeComponent {
26555
26687
  constructor(..._args) {
@@ -26581,11 +26713,7 @@ MAX: 9026 };
26581
26713
  type: options.type,
26582
26714
  id: options.id
26583
26715
  }));
26584
- for (let i = 0; i < options.children.length; i++) {
26585
- const child = options.children[i];
26586
- if (i === 0) child.addRunToFront(new EndnoteRefRun());
26587
- this.root.push(child);
26588
- }
26716
+ for (const [index, child] of options.children.entries()) this.root.push(index === 0 ? new FirstNoteParagraph(child, new EndnoteRefRun()) : child);
26589
26717
  }
26590
26718
  };
26591
26719
  //#endregion
@@ -26704,6 +26832,8 @@ MAX: 9026 };
26704
26832
  var Endnotes = class extends XmlComponent {
26705
26833
  constructor() {
26706
26834
  super("w:endnotes");
26835
+ _defineProperty(this, "ownNotes", /* @__PURE__ */ new Map());
26836
+ _defineProperty(this, "highestId", 0);
26707
26837
  this.root.push(new EndnotesAttributes({
26708
26838
  wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
26709
26839
  mc: "http://schemas.openxmlformats.org/markup-compatibility/2006",
@@ -26756,6 +26886,27 @@ MAX: 9026 };
26756
26886
  children: paragraph
26757
26887
  });
26758
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;
26759
26910
  }
26760
26911
  /**
26761
26912
  * Formats the endnotes, with the page numbers worked out for their document written into their fields, when the
@@ -27100,11 +27251,7 @@ MAX: 9026 };
27100
27251
  type: options.type,
27101
27252
  id: options.id
27102
27253
  }));
27103
- for (let i = 0; i < options.children.length; i++) {
27104
- const child = options.children[i];
27105
- if (i === 0) child.addRunToFront(new FootnoteRefRun());
27106
- this.root.push(child);
27107
- }
27254
+ for (const [index, child] of options.children.entries()) this.root.push(index === 0 ? new FirstNoteParagraph(child, new FootnoteRefRun()) : child);
27108
27255
  }
27109
27256
  };
27110
27257
  //#endregion
@@ -27207,6 +27354,8 @@ MAX: 9026 };
27207
27354
  var FootNotes = class extends XmlComponent {
27208
27355
  constructor() {
27209
27356
  super("w:footnotes");
27357
+ _defineProperty(this, "ownNotes", /* @__PURE__ */ new Map());
27358
+ _defineProperty(this, "highestId", 0);
27210
27359
  this.root.push(new FootnotesAttributes({
27211
27360
  wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
27212
27361
  mc: "http://schemas.openxmlformats.org/markup-compatibility/2006",
@@ -27265,6 +27414,27 @@ MAX: 9026 };
27265
27414
  children: paragraph
27266
27415
  });
27267
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;
27268
27438
  }
27269
27439
  /**
27270
27440
  * Formats the footnotes, with the page numbers worked out for their document written into their fields, when the
@@ -28473,8 +28643,15 @@ MAX: 9026 };
28473
28643
  *
28474
28644
  * @param reference - The reference name of the abstract numbering definition
28475
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
28476
28649
  */
28477
- 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
+ }
28478
28655
  const abstractNumbering = this.abstractNumberingMap.get(reference);
28479
28656
  if (!abstractNumbering) return;
28480
28657
  const fullReference = `${reference}-${instance}`;
@@ -29574,7 +29751,7 @@ MAX: 9026 };
29574
29751
  //#endregion
29575
29752
  //#region src/file/styles/styles.ts
29576
29753
  /** The name of a formatted element, such as `w:style`, or `_attr` for its parent's attributes */
29577
- 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;
29578
29755
  /** The attributes of a formatted `w:style` */
29579
29756
  var attributesOf = (style) => {
29580
29757
  var _flat$find;
@@ -29654,16 +29831,16 @@ MAX: 9026 };
29654
29831
  const xml = super.prepForXml(context);
29655
29832
  const children = xml["w:styles"];
29656
29833
  if (!Array.isArray(children)) return xml;
29657
- const named = (name) => children.filter((child) => nameOf(child) === name);
29658
- 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);
29659
29836
  const kept = children.filter((child, index) => ![
29660
29837
  "_attr",
29661
29838
  "w:docDefaults",
29662
29839
  "w:latentStyles"
29663
- ].includes(nameOf(child)) && (ids[index] === void 0 || ids.lastIndexOf(ids[index]) === index));
29664
- 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) => {
29665
29842
  var _attributesOf$wType, _attributesOf2;
29666
- 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;
29667
29844
  });
29668
29845
  return { "w:styles": [
29669
29846
  ...named("_attr"),
@@ -31056,16 +31233,6 @@ MAX: 9026 };
31056
31233
  //#endregion
31057
31234
  //#region src/file/footnotes/footnote/run/reference-run.ts
31058
31235
  /**
31059
- * Footnote reference run module for WordprocessingML documents.
31060
- *
31061
- * This module provides the footnote reference marker that appears in the
31062
- * main document text to indicate a footnote.
31063
- *
31064
- * Reference: http://officeopenxml.com/WPfootnotes.php
31065
- *
31066
- * @module
31067
- */
31068
- /**
31069
31236
  * Represents the attributes for a footnote reference element.
31070
31237
  *
31071
31238
  * @internal
@@ -31105,6 +31272,10 @@ MAX: 9026 };
31105
31272
  * (typically a superscript number) that appears in the main document text.
31106
31273
  * Clicking this marker navigates to the corresponding footnote content.
31107
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
+ *
31108
31279
  * Reference: http://officeopenxml.com/WPfootnotes.php
31109
31280
  *
31110
31281
  * @publicApi
@@ -31118,17 +31289,44 @@ MAX: 9026 };
31118
31289
  * new FootnoteReferenceRun(1),
31119
31290
  * ],
31120
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
+ * });
31121
31300
  * ```
31122
31301
  */
31123
31302
  var FootnoteReferenceRun = class extends Run {
31124
31303
  /**
31125
31304
  * Creates a new footnote reference run.
31126
31305
  *
31127
- * @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
31128
31309
  */
31129
- constructor(id) {
31310
+ constructor(footnote) {
31130
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;
31131
31324
  this.root.push(new FootnoteReference(id));
31325
+ try {
31326
+ return super.prepForXml(context);
31327
+ } finally {
31328
+ this.root.pop();
31329
+ }
31132
31330
  }
31133
31331
  };
31134
31332
  //#endregion
@@ -31145,10 +31343,47 @@ MAX: 9026 };
31145
31343
  this.root.push(new EndnoteReferenceRunAttributes({ id }));
31146
31344
  }
31147
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
+ */
31148
31361
  var EndnoteReferenceRun = class extends Run {
31149
- 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) {
31150
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;
31151
31381
  this.root.push(new EndnoteIdReference(id));
31382
+ try {
31383
+ return super.prepForXml(context);
31384
+ } finally {
31385
+ this.root.pop();
31386
+ }
31152
31387
  }
31153
31388
  };
31154
31389
  //#endregion
@@ -35350,11 +35585,11 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
35350
35585
  *
35351
35586
  * @module
35352
35587
  */
35353
- var formatter$3 = new Formatter();
35588
+ var formatter$4 = new Formatter();
35354
35589
  /**
35355
35590
  * Formats a part's XML, with the relationships that anything in it that refers to other parts adds to.
35356
35591
  */
35357
- 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, {
35358
35593
  viewWrapper: {
35359
35594
  View: content,
35360
35595
  Relationships: relationships
@@ -35471,7 +35706,7 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
35471
35706
  zip.file(`word/media/${data.fileName}`, data.data);
35472
35707
  zip.file(`word/media/${data.fallback.fileName}`, data.fallback.data);
35473
35708
  }
35474
- for (const [i, { data: buffer, fontKey }] of file.FontTable.fontOptionsWithKey.entries()) zip.file(`word/fonts/font${i + 1}.odttf`, obfuscate(buffer, fontKey));
35709
+ for (const [i, { data: buffer, fontKey }] of file.FontTable.files.entries()) zip.file(`word/fonts/font${i + 1}.odttf`, obfuscate(buffer, fontKey));
35475
35710
  return zip;
35476
35711
  }
35477
35712
  xmlifyFile(file, prettify) {
@@ -36124,7 +36359,7 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36124
36359
  *
36125
36360
  * @module
36126
36361
  */
36127
- var formatter$2 = new Formatter();
36362
+ var formatter$3 = new Formatter();
36128
36363
  /**
36129
36364
  * Converts XML string to JSON element structure.
36130
36365
  *
@@ -36164,7 +36399,7 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36164
36399
  */
36165
36400
  var createTextElementContents = (text) => {
36166
36401
  var _textJson$elements$0$;
36167
- 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$ : [];
36168
36403
  };
36169
36404
  /**
36170
36405
  * Adds xml:space="preserve" attribute to an element.
@@ -36280,6 +36515,81 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36280
36515
  });
36281
36516
  };
36282
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
36283
36593
  //#region src/patcher/patch-type.ts
36284
36594
  /**
36285
36595
  * The types of patch `patchDocument` applies.
@@ -36623,8 +36933,8 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36623
36933
  *
36624
36934
  * @module
36625
36935
  */
36626
- var formatter$1 = new Formatter();
36627
- var DOCUMENT_PATH = "word/document.xml";
36936
+ var formatter$2 = new Formatter();
36937
+ var DOCUMENT_PATH$1 = "word/document.xml";
36628
36938
  var FOOTNOTES = {
36629
36939
  path: "word/footnotes.xml",
36630
36940
  rootName: "w:footnotes",
@@ -36659,7 +36969,7 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36659
36969
  ]
36660
36970
  }
36661
36971
  };
36662
- 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];
36663
36973
  var findIds = (element, names) => {
36664
36974
  var _element$name, _element$attributes, _element$elements;
36665
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;
@@ -36675,8 +36985,10 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36675
36985
  ...elements.slice(index)
36676
36986
  ] });
36677
36987
  };
36678
- var patchNotesOfKind = (kind, notes, parts, { createContext, addContentTypeOverride, renumberBookmarks }) => {
36988
+ var patchNotesOfKind = (kind, notes, parts, { createContext, addContentTypeOverride, renumberBookmarks, renumberDrawings, renumberLists, useStyles }) => {
36679
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);
36680
36992
  const noteOfId = /* @__PURE__ */ new Map();
36681
36993
  let highestId;
36682
36994
  const renumber = (element) => {
@@ -36689,11 +37001,11 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36689
37001
  };
36690
37002
  const findOrAddPart = () => {
36691
37003
  var _parts$get;
36692
- 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) => {
36693
37005
  var _element$attributes3;
36694
37006
  return ((_element$attributes3 = element.attributes) === null || _element$attributes3 === void 0 ? void 0 : _element$attributes3.Type) === kind.relationshipType;
36695
37007
  });
36696
- 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;
36697
37009
  if (!parts.has(path)) {
36698
37010
  parts.set(path, {
36699
37011
  declaration: { attributes: {
@@ -36701,11 +37013,11 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36701
37013
  encoding: "UTF-8",
36702
37014
  standalone: "yes"
36703
37015
  } },
36704
- elements: [formatElement(kind.create.part(), createContext(path))]
37016
+ elements: [formatElement$1(kind.create.part(), createContext(path))]
36705
37017
  });
36706
37018
  addContentTypeOverride(kind.contentType, `/${path}`);
36707
37019
  }
36708
- 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));
36709
37021
  return path;
36710
37022
  };
36711
37023
  const write = () => {
@@ -36713,14 +37025,14 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36713
37025
  const path = findOrAddPart();
36714
37026
  const context = createContext(path);
36715
37027
  const contentOfNote = new Map([...new Set(noteOfId.values())].map((note) => {
36716
- const mark = formatElement(kind.create.mark(), context);
36717
- 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))))];
36718
37030
  }));
36719
37031
  getFirstLevelElements(parts.get(path), kind.rootName).push(...[...noteOfId].map(([id, note]) => ({
36720
37032
  type: "element",
36721
37033
  name: kind.noteName,
36722
37034
  attributes: { "w:id": String(id) },
36723
- elements: [...contentOfNote.get(note)]
37035
+ elements: [...renumberDrawings(contentOfNote.get(note))]
36724
37036
  })));
36725
37037
  const stylesPart = parts.get("word/styles.xml");
36726
37038
  if (stylesPart) {
@@ -36729,37 +37041,165 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
36729
37041
  var _style$attributes;
36730
37042
  return (_style$attributes = style.attributes) === null || _style$attributes === void 0 ? void 0 : _style$attributes["w:styleId"];
36731
37043
  }));
36732
- 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) => {
36733
37045
  var _style$attributes2;
36734
37046
  return !styleIds.has((_style$attributes2 = style.attributes) === null || _style$attributes2 === void 0 ? void 0 : _style$attributes2["w:styleId"]);
36735
37047
  }));
36736
37048
  }
36737
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
+ };
36738
37058
  return {
36739
37059
  renumber: (elements) => elements.map(renumber),
36740
- write
37060
+ write,
37061
+ carry
36741
37062
  };
36742
37063
  };
36743
37064
  /**
36744
37065
  * Creates the writer of the footnotes and endnotes that patches refer to.
36745
37066
  *
36746
37067
  * A patch refers to a note with a reference run, such as `new FootnoteReferenceRun(1)`, whose id is the note's in
36747
- * `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.
36748
37071
  *
36749
37072
  * @param notes - The footnotes and endnotes, by the id the patches' reference runs are given
36750
37073
  * @param parts - The document's XML parts, parsed, by their paths, which the notes are written to
36751
37074
  * @param helpers - The patcher's helpers for what the notes refer to
36752
37075
  */
36753
37076
  var patchNotes = ({ footnotes = {}, endnotes = {} }, parts, helpers) => {
36754
- 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
+ };
36755
37081
  return {
36756
- 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),
36757
37084
  write: () => {
36758
- for (const patcher of patchers) patcher.write();
37085
+ for (const patcher of Object.values(patchers)) patcher.write();
36759
37086
  }
36760
37087
  };
36761
37088
  };
36762
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}`);
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
37200
+ };
37201
+ };
37202
+ //#endregion
36763
37203
  //#region src/patcher/page-numbers.ts
36764
37204
  /**
36765
37205
  * Page numbers written into a template's fields once `patchDocument` has patched it, from an estimate of its pages, as
@@ -37187,7 +37627,6 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37187
37627
  *
37188
37628
  * @module
37189
37629
  */
37190
- var formatter = new Formatter();
37191
37630
  var SPLIT_TOKEN = "￿";
37192
37631
  /**
37193
37632
  * Replaces placeholder text in XML with new content from a patch.
@@ -37204,7 +37643,7 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37204
37643
  * @param keepOriginalStyles - Whether to preserve original text formatting
37205
37644
  * @param recursive - Whether to replace every occurrence in a paragraph, rather than only the first
37206
37645
  * @param renumberIds - Renumbers the bookmarks and note references the patch inserts, so they don't take an id the
37207
- * document uses
37646
+ * document uses, and records the styles it uses
37208
37647
  * @returns Result containing the modified element and whether a replacement occurred
37209
37648
  */
37210
37649
  var replacer = ({ json, patch, patchText, context, keepOriginalStyles = true, recursive = true, renumberIds = (elements) => elements }) => {
@@ -37260,7 +37699,10 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37260
37699
  var formatChildren = (patch, context, renumberIds) => renumberIds(patch.children.flatMap((c) => {
37261
37700
  var _c$writtenAs;
37262
37701
  return (_c$writtenAs = c.writtenAs) !== null && _c$writtenAs !== void 0 ? _c$writtenAs : c;
37263
- }).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
+ }));
37264
37706
  /**
37265
37707
  * Replaces the first occurrence of the placeholder from `fromIndex` on, splitting the run it starts in.
37266
37708
  *
@@ -37384,6 +37826,91 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37384
37826
  var goToParentElementFromPath = (json, path) => goToElementFromPath(json, path.slice(0, path.length - 1));
37385
37827
  var getLastElementIndexFromPath = (path) => path[path.length - 1];
37386
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
37387
37914
  //#region src/patcher/table-rows.ts
37388
37915
  var EMPTY = {
37389
37916
  type: PatchType.PARAGRAPH,
@@ -37398,8 +37925,9 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37398
37925
  * @param patch - The rows of data
37399
37926
  * @param delimiters - The placeholders' delimiters
37400
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
37401
37929
  */
37402
- var patchTableRows = ({ json, key, patch, delimiters: { start, end }, patchPlaceholder }) => {
37930
+ var patchTableRows = ({ json, key, patch, delimiters: { start, end }, patchPlaceholder, renumberDrawings }) => {
37403
37931
  const fieldStart = `${start}${key}.`;
37404
37932
  for (const rows of findRowsToRepeat(json, fieldStart)) {
37405
37933
  const fields = fieldsIn(rows.elements, fieldStart, end);
@@ -37407,7 +37935,7 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37407
37935
  const copy = {
37408
37936
  type: "element",
37409
37937
  name: "w:tbl",
37410
- elements: [...index === 0 ? rows.elements.map(copyOf) : withoutIds(rows.elements)]
37938
+ elements: [...index === 0 ? rows.elements.map(copyOf) : renumberDrawings(withoutIds(rows.elements))]
37411
37939
  };
37412
37940
  const fieldPatches = new Map(Object.entries(row).flatMap(([field, p]) => p === void 0 ? [] : [[field, p]]));
37413
37941
  for (const [field, fieldPatch] of fieldPatches) patchPlaceholder(copy, `${key}.${field}`, fieldPatch);
@@ -37657,14 +38185,17 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37657
38185
  var _ref = _asyncToGenerator(function* ({ outputType, data, patches, keepOriginalStyles, placeholderDelimiters = {
37658
38186
  start: "{{",
37659
38187
  end: "}}"
37660
- }, recursive = true, footnotes, endnotes, pageNumbers }) {
38188
+ }, recursive = true, footnotes, endnotes, numbering = { config: [] }, pageNumbers }) {
37661
38189
  const zipContent = data instanceof import_jszip_min.default ? data : yield import_jszip_min.default.loadAsync(data);
37662
38190
  const contexts = /* @__PURE__ */ new Map();
37663
38191
  const themeColors = yield readThemeColors(zipContent);
37664
38192
  const contentTypeOverrides = [];
37665
38193
  const file = {
37666
38194
  Media: new Media(),
38195
+ Numbering: new Numbering(numbering),
37667
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) } },
37668
38199
  PackageParts: new PackageParts({ addOverride: (contentType, partName) => contentTypeOverrides.push({
37669
38200
  contentType,
37670
38201
  partName
@@ -37707,9 +38238,12 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37707
38238
  map.set(key, json);
37708
38239
  }
37709
38240
  const renumberBookmarks = renumberBookmarksAvoiding([...map.values()].flatMap(findBookmarkIds));
37710
- const createContext = (key) => ({
37711
- file,
37712
- 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) => {
37713
38247
  relationshipAdditions.push({
37714
38248
  key,
37715
38249
  id,
@@ -37717,20 +38251,38 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37717
38251
  target,
37718
38252
  targetMode
37719
38253
  });
37720
- } } },
38254
+ } } };
38255
+ viewWrappers.set(key, viewWrapper);
38256
+ return viewWrapper;
38257
+ };
38258
+ const createContext = (key) => ({
38259
+ file,
38260
+ viewWrapper: viewWrapperOf(key),
37721
38261
  stack: []
37722
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
+ });
37723
38274
  const notes = patchNotes({
37724
38275
  footnotes,
37725
38276
  endnotes
37726
38277
  }, map, {
37727
38278
  createContext,
37728
- addContentTypeOverride: (contentType, partName) => contentTypeOverrides.push({
37729
- contentType,
37730
- partName
37731
- }),
37732
- renumberBookmarks
38279
+ addContentTypeOverride,
38280
+ renumberBookmarks,
38281
+ renumberDrawings,
38282
+ renumberLists: lists.renumber,
38283
+ useStyles: styles.use
37733
38284
  });
38285
+ const carryNote = (kind, reference, note, context) => context.viewWrapper === viewWrapperOf("word/document.xml") ? notes.carry(kind, reference, note) : void 0;
37734
38286
  patchDrawings({
37735
38287
  parts: map,
37736
38288
  binaryParts: binaryContentMap,
@@ -37757,7 +38309,8 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37757
38309
  start,
37758
38310
  end
37759
38311
  },
37760
- patchPlaceholder
38312
+ patchPlaceholder,
38313
+ renumberDrawings
37761
38314
  });
37762
38315
  return;
37763
38316
  }
@@ -37766,12 +38319,13 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37766
38319
  json: element,
37767
38320
  patch: _objectSpread2(_objectSpread2({}, patchValue), {}, { children: patchValue.children.flatMap((child) => child instanceof Bookmark ? child.writtenAs : [child]).map((child) => {
37768
38321
  if (child instanceof ExternalHyperlink) {
37769
- const concreteHyperlink = new ConcreteHyperlink(child.options.children, uniqueId());
38322
+ const { target, anchor } = externalHyperlinkTarget(child.options.link);
38323
+ const concreteHyperlink = new ConcreteHyperlink(child.options.children, uniqueId(), anchor, child.options.link);
37770
38324
  hyperlinkRelationships.push({
37771
38325
  key,
37772
38326
  id: concreteHyperlink.linkId,
37773
38327
  type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
37774
- target: child.options.link,
38328
+ target,
37775
38329
  targetMode: TargetModeType.EXTERNAL
37776
38330
  });
37777
38331
  return concreteHyperlink;
@@ -37781,13 +38335,15 @@ while (n === a[++i] && n === a[++i] && n === a[++i] && n === a[++i] && n === a[+
37781
38335
  context,
37782
38336
  keepOriginalStyles,
37783
38337
  recursive,
37784
- renumberIds: (elements) => notes.renumber(renumberBookmarks(elements))
38338
+ renumberIds: (elements) => styles.use(lists.renumber(notes.renumber(renumberBookmarks(renumberDrawings(elements)))))
37785
38339
  });
37786
38340
  if (didFindOccurrence) relationshipAdditions.push(...hyperlinkRelationships);
37787
38341
  };
37788
38342
  for (const [patchKey, patchValue] of patchesInOrder) patchPlaceholder(json, patchKey, patchValue);
37789
38343
  }
37790
38344
  notes.write();
38345
+ lists.write();
38346
+ styles.write();
37791
38347
  for (const [key, json] of map) {
37792
38348
  if (!key.startsWith("word/") || key.endsWith(".xml.rels")) continue;
37793
38349
  const mediaDatas = imageReplacer.getMediaData(JSON.stringify(json), file.Media);