docx 9.7.1 → 9.8.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.
@@ -0,0 +1,524 @@
1
+ var docxMath = (function(exports, docx) {
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ //#region src/math/math-elements.ts
4
+ /**
5
+ * What docx/math's components share: the checks on their arguments, and the elements they are made of. Nothing here is
6
+ * exported from docx/math.
7
+ *
8
+ * @module
9
+ */
10
+ /**
11
+ * A count of things, such as "1 column" or "3 columns".
12
+ */
13
+ var plural = (count, thing) => `${count} ${thing}${count === 1 ? "" : "s"}`;
14
+ /**
15
+ * An element whose value is its `m:val`, such as `<m:begChr m:val="("/>`.
16
+ */
17
+ var createValueElement = (name, value) => new docx.BuilderElement({
18
+ name,
19
+ attributes: { value: {
20
+ key: "m:val",
21
+ value
22
+ } }
23
+ });
24
+ /**
25
+ * Throws for what can't go in an argument: a `Math`, which Word won't open inside math, and a paragraph or a run of
26
+ * document text, which the schema doesn't allow there. Only an argument's own children can be checked.
27
+ */
28
+ var checkArgument = (owner, where, children) => {
29
+ for (const child of children) {
30
+ if (child instanceof docx.Math) throw new Error(`${owner}: ${where} holds a Math. Word won't open math inside math, so give the Math's children instead`);
31
+ if (child instanceof docx.Paragraph) throw new Error(`${owner}: ${where} holds a Paragraph. Math goes in a paragraph, not a paragraph in math`);
32
+ if (child instanceof docx.Run) throw new Error(`${owner}: ${where} holds a TextRun or another run of document text, which can't go in math. Use a MathRun`);
33
+ }
34
+ };
35
+ /**
36
+ * Throws unless a bracket or separator is one character, or none. Characters are counted as code points, so `𝒜` is one.
37
+ */
38
+ var checkCharacter = (owner, option, value) => {
39
+ if ([...value].length > 1) throw new Error(`${owner}: ${option} is "${value}", which is more than one character. Give one character, or "" for none`);
40
+ };
41
+ /**
42
+ * Throws unless a value is one of those allowed, for code that isn't type checked.
43
+ */
44
+ var checkOneOf = (owner, option, value, allowed) => {
45
+ if (!allowed.includes(value)) throw new Error(`${owner}: ${option} is "${value}", which isn't one of ${allowed.map((one) => `"${one}"`).join(", ")}`);
46
+ };
47
+ /**
48
+ * An argument (`m:e`). An empty one gets a zero-width space, as LibreOffice can't read an equation with an empty
49
+ * argument, and draws "¿" in its place.
50
+ */
51
+ var createArgument = (children) => (0, docx.createMathBase)({ children: children.length === 0 ? [new docx.MathRun("​")] : children });
52
+ /**
53
+ * Brackets (`m:d`) around arguments, with a separator between them. They grow with their content unless `grow` is
54
+ * false, as Word's do when `m:grow` isn't written.
55
+ */
56
+ var bracketsElement = ({ open, close, separator, grow }, args) => ({
57
+ name: "m:d",
58
+ children: [new docx.BuilderElement({
59
+ name: "m:dPr",
60
+ children: [
61
+ createValueElement("m:begChr", open),
62
+ ...separator === void 0 ? [] : [createValueElement("m:sepChr", separator)],
63
+ createValueElement("m:endChr", close),
64
+ ...grow === false ? [createValueElement("m:grow", 0)] : []
65
+ ]
66
+ }), ...args]
67
+ });
68
+ //#endregion
69
+ //#region src/math/math-brackets.ts
70
+ /**
71
+ * Brackets of any characters around math, such as |x|, ‖v‖, ⟨a|b⟩ or a brace on one side.
72
+ *
73
+ * @module
74
+ */
75
+ /**
76
+ * Brackets of any characters around math (`m:d`), such as |x|, ‖v‖, ⟨a|b⟩ or a brace on one side only. They grow
77
+ * with what they hold.
78
+ *
79
+ * `MathRoundBrackets`, `MathSquareBrackets`, `MathCurlyBrackets` and `MathAngledBrackets` are fixed pairs of these.
80
+ *
81
+ * @example
82
+ * ```typescript
83
+ * new MathBrackets({ open: "|", close: "|", children: [new MathRun("x")] });
84
+ * new MathBrackets({ open: "⟨", close: "⟩", items: [[new MathRun("a")], [new MathRun("b")]] });
85
+ * new MathBrackets({ open: "{", close: "", children: [new MathRun("x")] });
86
+ * ```
87
+ */
88
+ var MathBrackets = class extends docx.BuilderElement {
89
+ constructor(options) {
90
+ var _options$items;
91
+ const { open = "(", close = ")", separator, grow } = options;
92
+ const items = (_options$items = options.items) !== null && _options$items !== void 0 ? _options$items : [options.children];
93
+ checkCharacter("MathBrackets", "open", open);
94
+ checkCharacter("MathBrackets", "close", close);
95
+ if (separator !== void 0) checkCharacter("MathBrackets", "separator", separator);
96
+ if (items.length === 0) throw new Error("MathBrackets: items is empty. Give at least one item, or use children");
97
+ items.forEach((item, index) => checkArgument("MathBrackets", options.items ? `item ${index + 1}` : "children", item));
98
+ super(bracketsElement({
99
+ open,
100
+ close,
101
+ separator,
102
+ grow
103
+ }, items.map(createArgument)));
104
+ }
105
+ };
106
+ //#endregion
107
+ //#region \0@oxc-project+runtime@0.150.0/helpers/esm/objectWithoutPropertiesLoose.js
108
+ function _objectWithoutPropertiesLoose(r, e) {
109
+ if (null == r) return {};
110
+ var t = {};
111
+ for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
112
+ if (e.includes(n)) continue;
113
+ t[n] = r[n];
114
+ }
115
+ return t;
116
+ }
117
+ //#endregion
118
+ //#region \0@oxc-project+runtime@0.150.0/helpers/esm/objectWithoutProperties.js
119
+ function _objectWithoutProperties(e, t) {
120
+ if (null == e) return {};
121
+ var o, r, i = _objectWithoutPropertiesLoose(e, t);
122
+ if (Object.getOwnPropertySymbols) {
123
+ var s = Object.getOwnPropertySymbols(e);
124
+ for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]);
125
+ }
126
+ return i;
127
+ }
128
+ //#endregion
129
+ //#region src/math/math-matrix.ts
130
+ /**
131
+ * Matrices, such as LaTeX's `pmatrix`, `bmatrix`, `vmatrix` and `Vmatrix`.
132
+ *
133
+ * @module
134
+ */
135
+ var _excluded = ["brackets"];
136
+ var BRACKETS = {
137
+ round: ["(", ")"],
138
+ square: ["[", "]"],
139
+ curly: ["{", "}"],
140
+ angled: ["⟨", "⟩"],
141
+ verticalBars: ["|", "|"],
142
+ doubleVerticalBars: ["‖", "‖"]
143
+ };
144
+ var COLUMN_ALIGNMENTS = [
145
+ "left",
146
+ "center",
147
+ "right"
148
+ ];
149
+ var VERTICAL_ALIGNMENTS = [
150
+ "top",
151
+ "center",
152
+ "bottom"
153
+ ];
154
+ /**
155
+ * A matrix (`m:m`), without brackets. Its errors name `owner`, its rows as `rowsName`, and each cell as `cellName` does.
156
+ */
157
+ var matrixElement = (owner, { rows, columnAlignment = "center", verticalAlignment = "center" }, { rowsName = "rows", cellName = (row, column) => `row ${row + 1}, cell ${column + 1}` } = {}) => {
158
+ if (rows.length === 0) throw new Error(`${owner}: there are no ${rowsName}. Give at least one`);
159
+ if (rows.length > 256) throw new Error(`${owner}: there are ${rows.length} ${rowsName}, but Word allows at most 256`);
160
+ const columns = Math.max(...rows.map((row) => row.length));
161
+ if (columns === 0) throw new Error(`${owner}: every row is empty. Give at least one cell`);
162
+ if (columns > 64) throw new Error(`${owner}: a row has ${columns} cells, but Word allows at most 64`);
163
+ const alignments = typeof columnAlignment === "string" ? Array.from({ length: columns }, () => columnAlignment) : columnAlignment;
164
+ if (alignments.length !== columns) throw new Error(`${owner}: columnAlignment has ${plural(alignments.length, "alignment")}, but the rows have ${plural(columns, "column")}. Give one for each`);
165
+ alignments.forEach((alignment) => checkOneOf(owner, "columnAlignment", alignment, COLUMN_ALIGNMENTS));
166
+ checkOneOf(owner, "verticalAlignment", verticalAlignment, VERTICAL_ALIGNMENTS);
167
+ rows.forEach((row, rowIndex) => row.forEach((cell, column) => checkArgument(owner, cellName(rowIndex, column), cell)));
168
+ const groups = alignments.reduce((all, alignment) => all.length > 0 && all[all.length - 1].alignment === alignment ? [...all.slice(0, -1), {
169
+ alignment,
170
+ count: all[all.length - 1].count + 1
171
+ }] : [...all, {
172
+ alignment,
173
+ count: 1
174
+ }], []);
175
+ return {
176
+ name: "m:m",
177
+ children: [new docx.BuilderElement({
178
+ name: "m:mPr",
179
+ children: [
180
+ createValueElement("m:baseJc", verticalAlignment),
181
+ createValueElement("m:plcHide", 1),
182
+ new docx.BuilderElement({
183
+ name: "m:mcs",
184
+ children: groups.map(({ alignment, count }) => new docx.BuilderElement({
185
+ name: "m:mc",
186
+ children: [new docx.BuilderElement({
187
+ name: "m:mcPr",
188
+ children: [createValueElement("m:count", count), createValueElement("m:mcJc", alignment)]
189
+ })]
190
+ }))
191
+ })
192
+ ]
193
+ }), ...rows.map((row) => new docx.BuilderElement({
194
+ name: "m:mr",
195
+ children: Array.from({ length: columns }, (_, column) => {
196
+ var _row$column;
197
+ return createArgument((_row$column = row[column]) !== null && _row$column !== void 0 ? _row$column : []);
198
+ })
199
+ }))]
200
+ };
201
+ };
202
+ /**
203
+ * A matrix (`m:m`), in brackets or none (`m:d`): LaTeX's `matrix`, `pmatrix`, `bmatrix`, `Bmatrix`, `vmatrix` and
204
+ * `Vmatrix`.
205
+ *
206
+ * Word lines up each column's cells as `columnAlignment` says, and hides the placeholders of empty cells. LibreOffice
207
+ * and Pages centre every column.
208
+ *
209
+ * @example
210
+ * ```typescript
211
+ * new MathMatrix({
212
+ * brackets: "round",
213
+ * rows: [
214
+ * [[new MathRun("1")], [new MathRun("2")]],
215
+ * [[new MathRun("3")], [new MathRun("4")]],
216
+ * ],
217
+ * });
218
+ * ```
219
+ */
220
+ var MathMatrix = class extends docx.BuilderElement {
221
+ constructor(_ref) {
222
+ let { brackets = "none" } = _ref, options = _objectWithoutProperties(_ref, _excluded);
223
+ checkOneOf("MathMatrix", "brackets", brackets, ["none", ...Object.keys(BRACKETS)]);
224
+ const matrix = matrixElement("MathMatrix", options);
225
+ super(brackets === "none" ? matrix : bracketsElement({
226
+ open: BRACKETS[brackets][0],
227
+ close: BRACKETS[brackets][1]
228
+ }, [(0, docx.createMathBase)({ children: [new docx.BuilderElement(matrix)] })]));
229
+ }
230
+ };
231
+ //#endregion
232
+ //#region src/math/math-cases.ts
233
+ /**
234
+ * Cases, such as LaTeX's `cases`: values, each with its condition, in a brace.
235
+ *
236
+ * @module
237
+ */
238
+ /**
239
+ * Cases, as LaTeX's `cases`: values, each with its condition, one to a line, in a brace on the left. The values and
240
+ * the conditions each line up on the left.
241
+ *
242
+ * They are written as pandoc writes them: a matrix of two columns in a brace with no closing bracket. Word's own
243
+ * equation editor lines the conditions up with an `&` instead, which LibreOffice draws.
244
+ *
245
+ * @example
246
+ * ```typescript
247
+ * new MathCases({
248
+ * cases: [
249
+ * { value: [new MathRun("x,")], condition: [new MathRun({ text: "if ", normalText: true }), new MathRun("x≥0")] },
250
+ * { value: [new MathRun("−x,")], condition: [new MathRun({ text: "otherwise", normalText: true })] },
251
+ * ],
252
+ * });
253
+ * ```
254
+ */
255
+ var MathCases = class extends docx.BuilderElement {
256
+ constructor({ cases }) {
257
+ const conditions = cases.some((one) => one.condition !== void 0);
258
+ const matrix = matrixElement("MathCases", {
259
+ rows: cases.map(({ value, condition }) => conditions ? [value, condition !== null && condition !== void 0 ? condition : []] : [value]),
260
+ columnAlignment: "left"
261
+ }, {
262
+ rowsName: "cases",
263
+ cellName: (row, column) => `case ${row + 1}'s ${column === 0 ? "value" : "condition"}`
264
+ });
265
+ super(bracketsElement({
266
+ open: "{",
267
+ close: ""
268
+ }, [(0, docx.createMathBase)({ children: [new docx.BuilderElement(matrix)] })]));
269
+ }
270
+ };
271
+ //#endregion
272
+ //#region src/math/math-equation-array.ts
273
+ /**
274
+ * Equation arrays: rows of math lined up at points, such as LaTeX's `align` and `aligned`, with equation numbers.
275
+ *
276
+ * @module
277
+ */
278
+ /**
279
+ * An equation array (`m:eqArr`): rows of math, one to a line, lined up at the points between their parts, as LaTeX's
280
+ * `align` and `aligned`, with an equation number at the end of any row.
281
+ *
282
+ * Word marks the points with an `&`, and the number with a `#`, in the text. So an `&` or a `#` in a `MathRun` in the
283
+ * array is taken as one too. LibreOffice shows the `&` and `#`, and centres the rows; Pages lines them up, but shows
284
+ * the `#`.
285
+ *
286
+ * @example
287
+ * ```typescript
288
+ * new MathEquationArray({
289
+ * rows: [
290
+ * { parts: [[new MathRun("y")], [new MathRun("=mx+b")]], equationNumber: "(1)" },
291
+ * { parts: [[new MathRun("y′")], [new MathRun("=m")]], equationNumber: "(2)" },
292
+ * ],
293
+ * });
294
+ * ```
295
+ */
296
+ var MathEquationArray = class extends docx.BuilderElement {
297
+ constructor({ rows, verticalAlignment }) {
298
+ if (rows.length === 0) throw new Error("MathEquationArray: there are no rows. Give at least one");
299
+ if (rows.length > 64) throw new Error(`MathEquationArray: there are ${rows.length} rows, but Word allows at most 64`);
300
+ if (verticalAlignment !== void 0) checkOneOf("MathEquationArray", "verticalAlignment", verticalAlignment, VERTICAL_ALIGNMENTS);
301
+ rows.forEach(({ parts }, row) => {
302
+ if (parts.length === 0) throw new Error(`MathEquationArray: row ${row + 1} has no parts. Give at least one, which can be empty`);
303
+ parts.forEach((part, index) => checkArgument("MathEquationArray", `row ${row + 1}, part ${index + 1}`, part));
304
+ });
305
+ super({
306
+ name: "m:eqArr",
307
+ children: [...verticalAlignment === void 0 ? [] : [new docx.BuilderElement({
308
+ name: "m:eqArrPr",
309
+ children: [createValueElement("m:baseJc", verticalAlignment)]
310
+ })], ...rows.map(({ parts, equationNumber }) => createArgument([...parts.flatMap((part, index) => index === 0 ? part : [new docx.MathRun("&"), ...part]), ...equationNumber ? [new docx.MathRun("#"), new docx.MathRun(equationNumber)] : []]))]
311
+ });
312
+ }
313
+ };
314
+ //#endregion
315
+ Object.defineProperty(exports, "Math", {
316
+ enumerable: true,
317
+ get: function() {
318
+ return docx.Math;
319
+ }
320
+ });
321
+ Object.defineProperty(exports, "MathAngledBrackets", {
322
+ enumerable: true,
323
+ get: function() {
324
+ return docx.MathAngledBrackets;
325
+ }
326
+ });
327
+ exports.MathBrackets = MathBrackets;
328
+ exports.MathCases = MathCases;
329
+ Object.defineProperty(exports, "MathCurlyBrackets", {
330
+ enumerable: true,
331
+ get: function() {
332
+ return docx.MathCurlyBrackets;
333
+ }
334
+ });
335
+ Object.defineProperty(exports, "MathDegree", {
336
+ enumerable: true,
337
+ get: function() {
338
+ return docx.MathDegree;
339
+ }
340
+ });
341
+ Object.defineProperty(exports, "MathDenominator", {
342
+ enumerable: true,
343
+ get: function() {
344
+ return docx.MathDenominator;
345
+ }
346
+ });
347
+ exports.MathEquationArray = MathEquationArray;
348
+ Object.defineProperty(exports, "MathFraction", {
349
+ enumerable: true,
350
+ get: function() {
351
+ return docx.MathFraction;
352
+ }
353
+ });
354
+ Object.defineProperty(exports, "MathFunction", {
355
+ enumerable: true,
356
+ get: function() {
357
+ return docx.MathFunction;
358
+ }
359
+ });
360
+ Object.defineProperty(exports, "MathFunctionName", {
361
+ enumerable: true,
362
+ get: function() {
363
+ return docx.MathFunctionName;
364
+ }
365
+ });
366
+ Object.defineProperty(exports, "MathFunctionProperties", {
367
+ enumerable: true,
368
+ get: function() {
369
+ return docx.MathFunctionProperties;
370
+ }
371
+ });
372
+ Object.defineProperty(exports, "MathIntegral", {
373
+ enumerable: true,
374
+ get: function() {
375
+ return docx.MathIntegral;
376
+ }
377
+ });
378
+ Object.defineProperty(exports, "MathLimit", {
379
+ enumerable: true,
380
+ get: function() {
381
+ return docx.MathLimit;
382
+ }
383
+ });
384
+ Object.defineProperty(exports, "MathLimitLower", {
385
+ enumerable: true,
386
+ get: function() {
387
+ return docx.MathLimitLower;
388
+ }
389
+ });
390
+ Object.defineProperty(exports, "MathLimitUpper", {
391
+ enumerable: true,
392
+ get: function() {
393
+ return docx.MathLimitUpper;
394
+ }
395
+ });
396
+ exports.MathMatrix = MathMatrix;
397
+ Object.defineProperty(exports, "MathNumerator", {
398
+ enumerable: true,
399
+ get: function() {
400
+ return docx.MathNumerator;
401
+ }
402
+ });
403
+ Object.defineProperty(exports, "MathPreSubSuperScript", {
404
+ enumerable: true,
405
+ get: function() {
406
+ return docx.MathPreSubSuperScript;
407
+ }
408
+ });
409
+ Object.defineProperty(exports, "MathRadical", {
410
+ enumerable: true,
411
+ get: function() {
412
+ return docx.MathRadical;
413
+ }
414
+ });
415
+ Object.defineProperty(exports, "MathRadicalProperties", {
416
+ enumerable: true,
417
+ get: function() {
418
+ return docx.MathRadicalProperties;
419
+ }
420
+ });
421
+ Object.defineProperty(exports, "MathRoundBrackets", {
422
+ enumerable: true,
423
+ get: function() {
424
+ return docx.MathRoundBrackets;
425
+ }
426
+ });
427
+ Object.defineProperty(exports, "MathRun", {
428
+ enumerable: true,
429
+ get: function() {
430
+ return docx.MathRun;
431
+ }
432
+ });
433
+ Object.defineProperty(exports, "MathSquareBrackets", {
434
+ enumerable: true,
435
+ get: function() {
436
+ return docx.MathSquareBrackets;
437
+ }
438
+ });
439
+ Object.defineProperty(exports, "MathSubScript", {
440
+ enumerable: true,
441
+ get: function() {
442
+ return docx.MathSubScript;
443
+ }
444
+ });
445
+ Object.defineProperty(exports, "MathSubSuperScript", {
446
+ enumerable: true,
447
+ get: function() {
448
+ return docx.MathSubSuperScript;
449
+ }
450
+ });
451
+ Object.defineProperty(exports, "MathSum", {
452
+ enumerable: true,
453
+ get: function() {
454
+ return docx.MathSum;
455
+ }
456
+ });
457
+ Object.defineProperty(exports, "MathSuperScript", {
458
+ enumerable: true,
459
+ get: function() {
460
+ return docx.MathSuperScript;
461
+ }
462
+ });
463
+ Object.defineProperty(exports, "createMathAccentCharacter", {
464
+ enumerable: true,
465
+ get: function() {
466
+ return docx.createMathAccentCharacter;
467
+ }
468
+ });
469
+ Object.defineProperty(exports, "createMathBase", {
470
+ enumerable: true,
471
+ get: function() {
472
+ return docx.createMathBase;
473
+ }
474
+ });
475
+ Object.defineProperty(exports, "createMathLimitLocation", {
476
+ enumerable: true,
477
+ get: function() {
478
+ return docx.createMathLimitLocation;
479
+ }
480
+ });
481
+ Object.defineProperty(exports, "createMathNAryProperties", {
482
+ enumerable: true,
483
+ get: function() {
484
+ return docx.createMathNAryProperties;
485
+ }
486
+ });
487
+ Object.defineProperty(exports, "createMathPreSubSuperScriptProperties", {
488
+ enumerable: true,
489
+ get: function() {
490
+ return docx.createMathPreSubSuperScriptProperties;
491
+ }
492
+ });
493
+ Object.defineProperty(exports, "createMathSubScriptElement", {
494
+ enumerable: true,
495
+ get: function() {
496
+ return docx.createMathSubScriptElement;
497
+ }
498
+ });
499
+ Object.defineProperty(exports, "createMathSubScriptProperties", {
500
+ enumerable: true,
501
+ get: function() {
502
+ return docx.createMathSubScriptProperties;
503
+ }
504
+ });
505
+ Object.defineProperty(exports, "createMathSubSuperScriptProperties", {
506
+ enumerable: true,
507
+ get: function() {
508
+ return docx.createMathSubSuperScriptProperties;
509
+ }
510
+ });
511
+ Object.defineProperty(exports, "createMathSuperScriptElement", {
512
+ enumerable: true,
513
+ get: function() {
514
+ return docx.createMathSuperScriptElement;
515
+ }
516
+ });
517
+ Object.defineProperty(exports, "createMathSuperScriptProperties", {
518
+ enumerable: true,
519
+ get: function() {
520
+ return docx.createMathSuperScriptProperties;
521
+ }
522
+ });
523
+ return exports;
524
+ })({}, docx);