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.
package/dist/math.mjs ADDED
@@ -0,0 +1,314 @@
1
+ import { BuilderElement, Math as Math$1, Math as Math$2, MathAngledBrackets, MathCurlyBrackets, MathDegree, MathDenominator, MathFraction, MathFunction, MathFunctionName, MathFunctionProperties, MathIntegral, MathLimit, MathLimitLower, MathLimitUpper, MathNumerator, MathPreSubSuperScript, MathRadical, MathRadicalProperties, MathRoundBrackets, MathRun, MathRun as MathRun$1, MathSquareBrackets, MathSubScript, MathSubSuperScript, MathSum, MathSuperScript, Paragraph, Run, createMathAccentCharacter, createMathBase, createMathBase as createMathBase$1, createMathLimitLocation, createMathNAryProperties, createMathPreSubSuperScriptProperties, createMathSubScriptElement, createMathSubScriptProperties, createMathSubSuperScriptProperties, createMathSuperScriptElement, createMathSuperScriptProperties } from "docx";
2
+ //#region src/math/math-elements.ts
3
+ /**
4
+ * What docx/math's components share: the checks on their arguments, and the elements they are made of. Nothing here is
5
+ * exported from docx/math.
6
+ *
7
+ * @module
8
+ */
9
+ /**
10
+ * A count of things, such as "1 column" or "3 columns".
11
+ */
12
+ var plural = (count, thing) => `${count} ${thing}${count === 1 ? "" : "s"}`;
13
+ /**
14
+ * An element whose value is its `m:val`, such as `<m:begChr m:val="("/>`.
15
+ */
16
+ var createValueElement = (name, value) => new BuilderElement({
17
+ name,
18
+ attributes: { value: {
19
+ key: "m:val",
20
+ value
21
+ } }
22
+ });
23
+ /**
24
+ * 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
25
+ * document text, which the schema doesn't allow there. Only an argument's own children can be checked.
26
+ */
27
+ var checkArgument = (owner, where, children) => {
28
+ for (const child of children) {
29
+ if (child instanceof Math$2) throw new Error(`${owner}: ${where} holds a Math. Word won't open math inside math, so give the Math's children instead`);
30
+ if (child instanceof Paragraph) throw new Error(`${owner}: ${where} holds a Paragraph. Math goes in a paragraph, not a paragraph in math`);
31
+ if (child instanceof Run) throw new Error(`${owner}: ${where} holds a TextRun or another run of document text, which can't go in math. Use a MathRun`);
32
+ }
33
+ };
34
+ /**
35
+ * Throws unless a bracket or separator is one character, or none. Characters are counted as code points, so `𝒜` is one.
36
+ */
37
+ var checkCharacter = (owner, option, value) => {
38
+ if ([...value].length > 1) throw new Error(`${owner}: ${option} is "${value}", which is more than one character. Give one character, or "" for none`);
39
+ };
40
+ /**
41
+ * Throws unless a value is one of those allowed, for code that isn't type checked.
42
+ */
43
+ var checkOneOf = (owner, option, value, allowed) => {
44
+ if (!allowed.includes(value)) throw new Error(`${owner}: ${option} is "${value}", which isn't one of ${allowed.map((one) => `"${one}"`).join(", ")}`);
45
+ };
46
+ /**
47
+ * An argument (`m:e`). An empty one gets a zero-width space, as LibreOffice can't read an equation with an empty
48
+ * argument, and draws "¿" in its place.
49
+ */
50
+ var createArgument = (children) => createMathBase$1({ children: children.length === 0 ? [new MathRun$1("​")] : children });
51
+ /**
52
+ * Brackets (`m:d`) around arguments, with a separator between them. They grow with their content unless `grow` is
53
+ * false, as Word's do when `m:grow` isn't written.
54
+ */
55
+ var bracketsElement = ({ open, close, separator, grow }, args) => ({
56
+ name: "m:d",
57
+ children: [new BuilderElement({
58
+ name: "m:dPr",
59
+ children: [
60
+ createValueElement("m:begChr", open),
61
+ ...separator === void 0 ? [] : [createValueElement("m:sepChr", separator)],
62
+ createValueElement("m:endChr", close),
63
+ ...grow === false ? [createValueElement("m:grow", 0)] : []
64
+ ]
65
+ }), ...args]
66
+ });
67
+ //#endregion
68
+ //#region src/math/math-brackets.ts
69
+ /**
70
+ * Brackets of any characters around math, such as |x|, ‖v‖, ⟨a|b⟩ or a brace on one side.
71
+ *
72
+ * @module
73
+ */
74
+ /**
75
+ * Brackets of any characters around math (`m:d`), such as |x|, ‖v‖, ⟨a|b⟩ or a brace on one side only. They grow
76
+ * with what they hold.
77
+ *
78
+ * `MathRoundBrackets`, `MathSquareBrackets`, `MathCurlyBrackets` and `MathAngledBrackets` are fixed pairs of these.
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * new MathBrackets({ open: "|", close: "|", children: [new MathRun("x")] });
83
+ * new MathBrackets({ open: "⟨", close: "⟩", items: [[new MathRun("a")], [new MathRun("b")]] });
84
+ * new MathBrackets({ open: "{", close: "", children: [new MathRun("x")] });
85
+ * ```
86
+ */
87
+ var MathBrackets = class extends BuilderElement {
88
+ constructor(options) {
89
+ var _options$items;
90
+ const { open = "(", close = ")", separator, grow } = options;
91
+ const items = (_options$items = options.items) !== null && _options$items !== void 0 ? _options$items : [options.children];
92
+ checkCharacter("MathBrackets", "open", open);
93
+ checkCharacter("MathBrackets", "close", close);
94
+ if (separator !== void 0) checkCharacter("MathBrackets", "separator", separator);
95
+ if (items.length === 0) throw new Error("MathBrackets: items is empty. Give at least one item, or use children");
96
+ items.forEach((item, index) => checkArgument("MathBrackets", options.items ? `item ${index + 1}` : "children", item));
97
+ super(bracketsElement({
98
+ open,
99
+ close,
100
+ separator,
101
+ grow
102
+ }, items.map(createArgument)));
103
+ }
104
+ };
105
+ //#endregion
106
+ //#region \0@oxc-project+runtime@0.150.0/helpers/esm/objectWithoutPropertiesLoose.js
107
+ function _objectWithoutPropertiesLoose(r, e) {
108
+ if (null == r) return {};
109
+ var t = {};
110
+ for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
111
+ if (e.includes(n)) continue;
112
+ t[n] = r[n];
113
+ }
114
+ return t;
115
+ }
116
+ //#endregion
117
+ //#region \0@oxc-project+runtime@0.150.0/helpers/esm/objectWithoutProperties.js
118
+ function _objectWithoutProperties(e, t) {
119
+ if (null == e) return {};
120
+ var o, r, i = _objectWithoutPropertiesLoose(e, t);
121
+ if (Object.getOwnPropertySymbols) {
122
+ var s = Object.getOwnPropertySymbols(e);
123
+ for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]);
124
+ }
125
+ return i;
126
+ }
127
+ //#endregion
128
+ //#region src/math/math-matrix.ts
129
+ /**
130
+ * Matrices, such as LaTeX's `pmatrix`, `bmatrix`, `vmatrix` and `Vmatrix`.
131
+ *
132
+ * @module
133
+ */
134
+ var _excluded = ["brackets"];
135
+ var BRACKETS = {
136
+ round: ["(", ")"],
137
+ square: ["[", "]"],
138
+ curly: ["{", "}"],
139
+ angled: ["⟨", "⟩"],
140
+ verticalBars: ["|", "|"],
141
+ doubleVerticalBars: ["‖", "‖"]
142
+ };
143
+ var COLUMN_ALIGNMENTS = [
144
+ "left",
145
+ "center",
146
+ "right"
147
+ ];
148
+ var VERTICAL_ALIGNMENTS = [
149
+ "top",
150
+ "center",
151
+ "bottom"
152
+ ];
153
+ /**
154
+ * A matrix (`m:m`), without brackets. Its errors name `owner`, its rows as `rowsName`, and each cell as `cellName` does.
155
+ */
156
+ var matrixElement = (owner, { rows, columnAlignment = "center", verticalAlignment = "center" }, { rowsName = "rows", cellName = (row, column) => `row ${row + 1}, cell ${column + 1}` } = {}) => {
157
+ if (rows.length === 0) throw new Error(`${owner}: there are no ${rowsName}. Give at least one`);
158
+ if (rows.length > 256) throw new Error(`${owner}: there are ${rows.length} ${rowsName}, but Word allows at most 256`);
159
+ const columns = Math.max(...rows.map((row) => row.length));
160
+ if (columns === 0) throw new Error(`${owner}: every row is empty. Give at least one cell`);
161
+ if (columns > 64) throw new Error(`${owner}: a row has ${columns} cells, but Word allows at most 64`);
162
+ const alignments = typeof columnAlignment === "string" ? Array.from({ length: columns }, () => columnAlignment) : columnAlignment;
163
+ 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`);
164
+ alignments.forEach((alignment) => checkOneOf(owner, "columnAlignment", alignment, COLUMN_ALIGNMENTS));
165
+ checkOneOf(owner, "verticalAlignment", verticalAlignment, VERTICAL_ALIGNMENTS);
166
+ rows.forEach((row, rowIndex) => row.forEach((cell, column) => checkArgument(owner, cellName(rowIndex, column), cell)));
167
+ const groups = alignments.reduce((all, alignment) => all.length > 0 && all[all.length - 1].alignment === alignment ? [...all.slice(0, -1), {
168
+ alignment,
169
+ count: all[all.length - 1].count + 1
170
+ }] : [...all, {
171
+ alignment,
172
+ count: 1
173
+ }], []);
174
+ return {
175
+ name: "m:m",
176
+ children: [new BuilderElement({
177
+ name: "m:mPr",
178
+ children: [
179
+ createValueElement("m:baseJc", verticalAlignment),
180
+ createValueElement("m:plcHide", 1),
181
+ new BuilderElement({
182
+ name: "m:mcs",
183
+ children: groups.map(({ alignment, count }) => new BuilderElement({
184
+ name: "m:mc",
185
+ children: [new BuilderElement({
186
+ name: "m:mcPr",
187
+ children: [createValueElement("m:count", count), createValueElement("m:mcJc", alignment)]
188
+ })]
189
+ }))
190
+ })
191
+ ]
192
+ }), ...rows.map((row) => new BuilderElement({
193
+ name: "m:mr",
194
+ children: Array.from({ length: columns }, (_, column) => {
195
+ var _row$column;
196
+ return createArgument((_row$column = row[column]) !== null && _row$column !== void 0 ? _row$column : []);
197
+ })
198
+ }))]
199
+ };
200
+ };
201
+ /**
202
+ * A matrix (`m:m`), in brackets or none (`m:d`): LaTeX's `matrix`, `pmatrix`, `bmatrix`, `Bmatrix`, `vmatrix` and
203
+ * `Vmatrix`.
204
+ *
205
+ * Word lines up each column's cells as `columnAlignment` says, and hides the placeholders of empty cells. LibreOffice
206
+ * and Pages centre every column.
207
+ *
208
+ * @example
209
+ * ```typescript
210
+ * new MathMatrix({
211
+ * brackets: "round",
212
+ * rows: [
213
+ * [[new MathRun("1")], [new MathRun("2")]],
214
+ * [[new MathRun("3")], [new MathRun("4")]],
215
+ * ],
216
+ * });
217
+ * ```
218
+ */
219
+ var MathMatrix = class extends BuilderElement {
220
+ constructor(_ref) {
221
+ let { brackets = "none" } = _ref, options = _objectWithoutProperties(_ref, _excluded);
222
+ checkOneOf("MathMatrix", "brackets", brackets, ["none", ...Object.keys(BRACKETS)]);
223
+ const matrix = matrixElement("MathMatrix", options);
224
+ super(brackets === "none" ? matrix : bracketsElement({
225
+ open: BRACKETS[brackets][0],
226
+ close: BRACKETS[brackets][1]
227
+ }, [createMathBase$1({ children: [new BuilderElement(matrix)] })]));
228
+ }
229
+ };
230
+ //#endregion
231
+ //#region src/math/math-cases.ts
232
+ /**
233
+ * Cases, such as LaTeX's `cases`: values, each with its condition, in a brace.
234
+ *
235
+ * @module
236
+ */
237
+ /**
238
+ * Cases, as LaTeX's `cases`: values, each with its condition, one to a line, in a brace on the left. The values and
239
+ * the conditions each line up on the left.
240
+ *
241
+ * They are written as pandoc writes them: a matrix of two columns in a brace with no closing bracket. Word's own
242
+ * equation editor lines the conditions up with an `&` instead, which LibreOffice draws.
243
+ *
244
+ * @example
245
+ * ```typescript
246
+ * new MathCases({
247
+ * cases: [
248
+ * { value: [new MathRun("x,")], condition: [new MathRun({ text: "if ", normalText: true }), new MathRun("x≥0")] },
249
+ * { value: [new MathRun("−x,")], condition: [new MathRun({ text: "otherwise", normalText: true })] },
250
+ * ],
251
+ * });
252
+ * ```
253
+ */
254
+ var MathCases = class extends BuilderElement {
255
+ constructor({ cases }) {
256
+ const conditions = cases.some((one) => one.condition !== void 0);
257
+ const matrix = matrixElement("MathCases", {
258
+ rows: cases.map(({ value, condition }) => conditions ? [value, condition !== null && condition !== void 0 ? condition : []] : [value]),
259
+ columnAlignment: "left"
260
+ }, {
261
+ rowsName: "cases",
262
+ cellName: (row, column) => `case ${row + 1}'s ${column === 0 ? "value" : "condition"}`
263
+ });
264
+ super(bracketsElement({
265
+ open: "{",
266
+ close: ""
267
+ }, [createMathBase$1({ children: [new BuilderElement(matrix)] })]));
268
+ }
269
+ };
270
+ //#endregion
271
+ //#region src/math/math-equation-array.ts
272
+ /**
273
+ * Equation arrays: rows of math lined up at points, such as LaTeX's `align` and `aligned`, with equation numbers.
274
+ *
275
+ * @module
276
+ */
277
+ /**
278
+ * An equation array (`m:eqArr`): rows of math, one to a line, lined up at the points between their parts, as LaTeX's
279
+ * `align` and `aligned`, with an equation number at the end of any row.
280
+ *
281
+ * Word marks the points with an `&`, and the number with a `#`, in the text. So an `&` or a `#` in a `MathRun` in the
282
+ * array is taken as one too. LibreOffice shows the `&` and `#`, and centres the rows; Pages lines them up, but shows
283
+ * the `#`.
284
+ *
285
+ * @example
286
+ * ```typescript
287
+ * new MathEquationArray({
288
+ * rows: [
289
+ * { parts: [[new MathRun("y")], [new MathRun("=mx+b")]], equationNumber: "(1)" },
290
+ * { parts: [[new MathRun("y′")], [new MathRun("=m")]], equationNumber: "(2)" },
291
+ * ],
292
+ * });
293
+ * ```
294
+ */
295
+ var MathEquationArray = class extends BuilderElement {
296
+ constructor({ rows, verticalAlignment }) {
297
+ if (rows.length === 0) throw new Error("MathEquationArray: there are no rows. Give at least one");
298
+ if (rows.length > 64) throw new Error(`MathEquationArray: there are ${rows.length} rows, but Word allows at most 64`);
299
+ if (verticalAlignment !== void 0) checkOneOf("MathEquationArray", "verticalAlignment", verticalAlignment, VERTICAL_ALIGNMENTS);
300
+ rows.forEach(({ parts }, row) => {
301
+ if (parts.length === 0) throw new Error(`MathEquationArray: row ${row + 1} has no parts. Give at least one, which can be empty`);
302
+ parts.forEach((part, index) => checkArgument("MathEquationArray", `row ${row + 1}, part ${index + 1}`, part));
303
+ });
304
+ super({
305
+ name: "m:eqArr",
306
+ children: [...verticalAlignment === void 0 ? [] : [new BuilderElement({
307
+ name: "m:eqArrPr",
308
+ children: [createValueElement("m:baseJc", verticalAlignment)]
309
+ })], ...rows.map(({ parts, equationNumber }) => createArgument([...parts.flatMap((part, index) => index === 0 ? part : [new MathRun$1("&"), ...part]), ...equationNumber ? [new MathRun$1("#"), new MathRun$1(equationNumber)] : []]))]
310
+ });
311
+ }
312
+ };
313
+ //#endregion
314
+ export { Math$1 as Math, MathAngledBrackets, MathBrackets, MathCases, MathCurlyBrackets, MathDegree, MathDenominator, MathEquationArray, MathFraction, MathFunction, MathFunctionName, MathFunctionProperties, MathIntegral, MathLimit, MathLimitLower, MathLimitUpper, MathMatrix, MathNumerator, MathPreSubSuperScript, MathRadical, MathRadicalProperties, MathRoundBrackets, MathRun, MathSquareBrackets, MathSubScript, MathSubSuperScript, MathSum, MathSuperScript, createMathAccentCharacter, createMathBase, createMathLimitLocation, createMathNAryProperties, createMathPreSubSuperScriptProperties, createMathSubScriptElement, createMathSubScriptProperties, createMathSubSuperScriptProperties, createMathSuperScriptElement, createMathSuperScriptProperties };