@folijs/docx 0.0.0-stage → 0.4.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.
Files changed (84) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/LICENSE +21 -0
  3. package/README.md +151 -2
  4. package/dist/export/content.d.ts +17 -0
  5. package/dist/export/content.d.ts.map +1 -0
  6. package/dist/export/content.js +165 -0
  7. package/dist/export/content.js.map +1 -0
  8. package/dist/export/index.d.ts +21 -0
  9. package/dist/export/index.d.ts.map +1 -0
  10. package/dist/export/index.js +360 -0
  11. package/dist/export/index.js.map +1 -0
  12. package/dist/export/parts.d.ts +24 -0
  13. package/dist/export/parts.d.ts.map +1 -0
  14. package/dist/export/parts.js +413 -0
  15. package/dist/export/parts.js.map +1 -0
  16. package/dist/export/properties.d.ts +39 -0
  17. package/dist/export/properties.d.ts.map +1 -0
  18. package/dist/export/properties.js +506 -0
  19. package/dist/export/properties.js.map +1 -0
  20. package/dist/export/xmlout.d.ts +63 -0
  21. package/dist/export/xmlout.d.ts.map +1 -0
  22. package/dist/export/xmlout.js +194 -0
  23. package/dist/export/xmlout.js.map +1 -0
  24. package/dist/import/content.d.ts +35 -0
  25. package/dist/import/content.d.ts.map +1 -0
  26. package/dist/import/content.js +370 -0
  27. package/dist/import/content.js.map +1 -0
  28. package/dist/import/index.d.ts +31 -0
  29. package/dist/import/index.d.ts.map +1 -0
  30. package/dist/import/index.js +106 -0
  31. package/dist/import/index.js.map +1 -0
  32. package/dist/import/ooxml.d.ts +39 -0
  33. package/dist/import/ooxml.d.ts.map +1 -0
  34. package/dist/import/ooxml.js +71 -0
  35. package/dist/import/ooxml.js.map +1 -0
  36. package/dist/import/parts.d.ts +35 -0
  37. package/dist/import/parts.d.ts.map +1 -0
  38. package/dist/import/parts.js +365 -0
  39. package/dist/import/parts.js.map +1 -0
  40. package/dist/import/properties.d.ts +50 -0
  41. package/dist/import/properties.d.ts.map +1 -0
  42. package/dist/import/properties.js +404 -0
  43. package/dist/import/properties.js.map +1 -0
  44. package/dist/import/report.d.ts +28 -0
  45. package/dist/import/report.d.ts.map +1 -0
  46. package/dist/import/report.js +42 -0
  47. package/dist/import/report.js.map +1 -0
  48. package/dist/index.d.ts +6 -0
  49. package/dist/index.d.ts.map +1 -0
  50. package/dist/index.js +6 -0
  51. package/dist/index.js.map +1 -0
  52. package/dist/package.d.ts +70 -0
  53. package/dist/package.d.ts.map +1 -0
  54. package/dist/package.js +259 -0
  55. package/dist/package.js.map +1 -0
  56. package/dist/settings.d.ts +8 -0
  57. package/dist/settings.d.ts.map +1 -0
  58. package/dist/settings.js +72 -0
  59. package/dist/settings.js.map +1 -0
  60. package/dist/xml.d.ts +79 -0
  61. package/dist/xml.d.ts.map +1 -0
  62. package/dist/xml.js +384 -0
  63. package/dist/xml.js.map +1 -0
  64. package/dist/zip.d.ts +66 -0
  65. package/dist/zip.d.ts.map +1 -0
  66. package/dist/zip.js +356 -0
  67. package/dist/zip.js.map +1 -0
  68. package/package.json +36 -3
  69. package/src/export/content.ts +229 -0
  70. package/src/export/index.ts +473 -0
  71. package/src/export/parts.ts +545 -0
  72. package/src/export/properties.ts +608 -0
  73. package/src/export/xmlout.ts +225 -0
  74. package/src/import/content.ts +413 -0
  75. package/src/import/index.ts +139 -0
  76. package/src/import/ooxml.ts +78 -0
  77. package/src/import/parts.ts +423 -0
  78. package/src/import/properties.ts +507 -0
  79. package/src/import/report.ts +64 -0
  80. package/src/index.ts +24 -0
  81. package/src/package.ts +328 -0
  82. package/src/settings.ts +75 -0
  83. package/src/xml.ts +443 -0
  84. package/src/zip.ts +401 -0
@@ -0,0 +1,507 @@
1
+ /**
2
+ * Word's property elements (`w:pPr`, `w:rPr`, `w:sectPr`) to schema v3 attributes and marks.
3
+ * Each value is checked on its own against the schema: one Word cannot store in the model
4
+ * (out of range, a unit Foli does not read) is left out and reported, and stays in the
5
+ * element's verbatim `ext`, so nothing is lost.
6
+ */
7
+
8
+ import { checkSection, type MarkJSON, RUN_PROPERTY_MARKS, schema } from "@folijs/core";
9
+ import type { XmlElement } from "../xml.js";
10
+ import { attr, child, compact, elements, integer, isW, R, toggle, W } from "./ooxml.js";
11
+ import type { Recorder } from "./report.js";
12
+
13
+ export interface Context {
14
+ readonly source: string;
15
+ readonly report: Recorder;
16
+ }
17
+
18
+ export function sourceOf(context: Context, element: XmlElement): string {
19
+ return context.source.slice(element.start, element.end);
20
+ }
21
+
22
+ const ATTRIBUTE_NAME = /^[A-Za-z_][\w.-]*(:[A-Za-z_][\w.-]*)?$/;
23
+
24
+ /**
25
+ * An element's attributes not in `modeled`, as an `attributes` bag (undefined when none). One
26
+ * the bag cannot hold (a name outside its pattern, a value over 64 KiB, past 64) is reported.
27
+ */
28
+ export function attributeBag(
29
+ element: XmlElement,
30
+ modeled: readonly string[],
31
+ context: Context,
32
+ ): Record<string, string> | undefined {
33
+ const bag: Record<string, string> = {};
34
+ let count = 0;
35
+ for (const a of element.attributes) {
36
+ if (modeled.includes(a.name)) continue;
37
+ if (!ATTRIBUTE_NAME.test(a.name) || a.value.length > 65_535 || count === 64) {
38
+ context.report.unmodeled(`${element.name}@${a.name}`, "not kept as an attribute");
39
+ continue;
40
+ }
41
+ Object.defineProperty(bag, a.name, {
42
+ value: a.value,
43
+ enumerable: true,
44
+ writable: true,
45
+ configurable: true,
46
+ });
47
+ count++;
48
+ }
49
+ return count ? bag : undefined;
50
+ }
51
+
52
+ const PARAGRAPH_ATTRS = schema.nodes.paragraph?.spec.attrs ?? {};
53
+
54
+ /** Keeps `value` as paragraph attribute `name` if the schema accepts it; reports it otherwise. */
55
+ export function setParagraph(
56
+ out: Record<string, unknown>,
57
+ name: string,
58
+ value: unknown,
59
+ element: string,
60
+ context: Context,
61
+ ): void {
62
+ if (value === undefined) return;
63
+ const error = PARAGRAPH_ATTRS[name]?.validate?.(value);
64
+ if (error) context.report.unmodeled(element, `${name} ${error}`);
65
+ else out[name] = value;
66
+ }
67
+
68
+ /** The mark if the schema accepts it; reported and left out otherwise. */
69
+ export function mark(json: MarkJSON, element: string, context: Context): MarkJSON | undefined {
70
+ try {
71
+ schema.markFromJSON(json);
72
+ return json;
73
+ } catch (error) {
74
+ context.report.unmodeled(element, (error as Error).message);
75
+ return undefined;
76
+ }
77
+ }
78
+
79
+ /** Who and when, for tracked changes (`w:ins`, `w:del`, `w:rPrChange`…). */
80
+ export function revisionOf(element: XmlElement): {
81
+ id: number | undefined;
82
+ author: string;
83
+ date: string | null;
84
+ } {
85
+ return {
86
+ id: integer(attr(element, "id")),
87
+ author: attr(element, "author") ?? "",
88
+ date: attr(element, "date") ?? null,
89
+ };
90
+ }
91
+
92
+ /**
93
+ * A tracked change's mark (`insertion`, `deletion`, with `move` for `w:moveFrom`/`w:moveTo`).
94
+ * An invalid date is left out rather than losing the change.
95
+ */
96
+ export function revisionMark(
97
+ type: "insertion" | "deletion",
98
+ element: XmlElement,
99
+ move: string | null,
100
+ context: Context,
101
+ ): MarkJSON | undefined {
102
+ const { id, author, date } = revisionOf(element);
103
+ if (id === undefined) {
104
+ context.report.unmodeled(element.name, "no w:id");
105
+ return undefined;
106
+ }
107
+ const attributes = attributeBag(element, ["w:id", "w:author", "w:date"], context) ?? null;
108
+ const attrs = { id, author, date, move, attributes };
109
+ return (
110
+ mark({ type, attrs }, element.name, context) ??
111
+ mark({ type, attrs: { ...attrs, date: null } }, element.name, context)
112
+ );
113
+ }
114
+
115
+ const TOGGLES: Record<string, string> = {
116
+ strike: "strike",
117
+ dstrike: "doubleStrike",
118
+ caps: "caps",
119
+ smallCaps: "smallCaps",
120
+ vanish: "hidden",
121
+ };
122
+
123
+ /**
124
+ * `w:rPr`'s run properties as marks (without `runExt`, the caller's). Tracked changes of the
125
+ * paragraph mark (`w:ins`/`w:del` in a paragraph's `w:rPr`) are not run properties: see
126
+ * `paragraphRevision`.
127
+ */
128
+ export function runProperties(rPr: XmlElement | undefined, context: Context): MarkJSON[] {
129
+ if (!rPr) return [];
130
+ const marks: (MarkJSON | undefined)[] = [];
131
+ const name = (local: string) => `${rPr.prefix ? `${rPr.prefix}:` : ""}${local}`;
132
+ const twin = (main: string, cs: string, type: string) => {
133
+ const read = (el: XmlElement | undefined) => {
134
+ if (!el) return null;
135
+ const value = toggle(el);
136
+ if (value === undefined) context.report.unmodeled(el.name, `w:val ${attr(el, "val")}`);
137
+ return value ?? null;
138
+ };
139
+ const value = read(child(rPr, main));
140
+ const complexScript = read(child(rPr, cs));
141
+ if (value === null && complexScript === null) return;
142
+ marks.push(mark({ type, attrs: { value, complexScript } }, name(main), context));
143
+ };
144
+ twin("b", "bCs", "bold");
145
+ twin("i", "iCs", "italic");
146
+ for (const el of elements(rPr)) {
147
+ if (!isW(el)) continue;
148
+ const val = attr(el, "val");
149
+ switch (el.local) {
150
+ case "rStyle":
151
+ if (val !== undefined)
152
+ marks.push(mark({ type: "characterStyle", attrs: { id: val } }, el.name, context));
153
+ break;
154
+ case "u":
155
+ marks.push(
156
+ mark(
157
+ {
158
+ type: "underline",
159
+ attrs: { style: val ?? "single", color: attr(el, "color") ?? null },
160
+ },
161
+ el.name,
162
+ context,
163
+ ),
164
+ );
165
+ break;
166
+ case "rFonts":
167
+ marks.push(
168
+ mark(
169
+ {
170
+ type: "font",
171
+ attrs: {
172
+ ascii: attr(el, "ascii") ?? null,
173
+ hAnsi: attr(el, "hAnsi") ?? null,
174
+ eastAsia: attr(el, "eastAsia") ?? null,
175
+ cs: attr(el, "cs") ?? null,
176
+ asciiTheme: attr(el, "asciiTheme") ?? null,
177
+ hAnsiTheme: attr(el, "hAnsiTheme") ?? null,
178
+ eastAsiaTheme: attr(el, "eastAsiaTheme") ?? null,
179
+ // ECMA-376 spells it `w:cstheme`; some writers use `w:csTheme`.
180
+ csTheme: attr(el, "cstheme") ?? attr(el, "csTheme") ?? null,
181
+ hint: attr(el, "hint") ?? null,
182
+ },
183
+ },
184
+ el.name,
185
+ context,
186
+ ),
187
+ );
188
+ break;
189
+ case "sz":
190
+ case "szCs": {
191
+ if (el.local === "szCs" && child(rPr, "sz")) break; // read with sz
192
+ const sz = child(rPr, "sz");
193
+ const szCs = child(rPr, "szCs");
194
+ marks.push(
195
+ mark(
196
+ {
197
+ type: "fontSize",
198
+ attrs: {
199
+ value: whole(sz, "val", context) ?? null,
200
+ complexScript: whole(szCs, "val", context) ?? null,
201
+ },
202
+ },
203
+ el.name,
204
+ context,
205
+ ),
206
+ );
207
+ break;
208
+ }
209
+ case "color":
210
+ marks.push(
211
+ mark(
212
+ {
213
+ type: "color",
214
+ attrs: {
215
+ value: val ?? "auto",
216
+ themeColor: attr(el, "themeColor") ?? null,
217
+ themeTint: attr(el, "themeTint") ?? null,
218
+ themeShade: attr(el, "themeShade") ?? null,
219
+ },
220
+ },
221
+ el.name,
222
+ context,
223
+ ),
224
+ );
225
+ break;
226
+ case "highlight":
227
+ marks.push(mark({ type: "highlight", attrs: { value: val } }, el.name, context));
228
+ break;
229
+ case "shd":
230
+ marks.push(
231
+ mark(
232
+ {
233
+ type: "shading",
234
+ attrs: {
235
+ pattern: val,
236
+ color: attr(el, "color") ?? "auto",
237
+ fill: attr(el, "fill") ?? "auto",
238
+ },
239
+ },
240
+ el.name,
241
+ context,
242
+ ),
243
+ );
244
+ break;
245
+ case "vertAlign":
246
+ marks.push(mark({ type: "verticalAlign", attrs: { value: val } }, el.name, context));
247
+ break;
248
+ case "spacing":
249
+ marks.push(
250
+ mark({ type: "characterSpacing", attrs: { value: integer(val) } }, el.name, context),
251
+ );
252
+ break;
253
+ case "rPrChange": {
254
+ const { id, author, date } = revisionOf(el);
255
+ // In canonical form, as the editor records it (attributes left at their defaults out).
256
+ const previous = runProperties(child(el, "rPr"), context).map((m) =>
257
+ schema.markFromJSON(m).toJSON(),
258
+ );
259
+ const change = { type: "formatChange", attrs: { id, author, date, previous } };
260
+ marks.push(
261
+ mark(change, el.name, context) ??
262
+ mark({ ...change, attrs: { ...change.attrs, date: null } }, el.name, context),
263
+ );
264
+ break;
265
+ }
266
+ default: {
267
+ const type = Object.hasOwn(TOGGLES, el.local) ? TOGGLES[el.local] : undefined;
268
+ if (type) {
269
+ const value = toggle(el);
270
+ if (value === undefined) context.report.unmodeled(el.name, `w:val ${val}`);
271
+ else marks.push(mark({ type, attrs: { value } }, el.name, context));
272
+ }
273
+ }
274
+ }
275
+ }
276
+ return marks.filter((m): m is MarkJSON => m !== undefined);
277
+ }
278
+
279
+ /** A paragraph mark's tracked insertion and deletion (`w:pPr/w:rPr/w:ins`, `w:del`, moves). */
280
+ function paragraphRevision(rPr: XmlElement | undefined): Record<string, unknown> | undefined {
281
+ if (!rPr) return undefined;
282
+ const pick = (...locals: string[]) => {
283
+ const el = elements(rPr).find((e) => locals.some((local) => isW(e, local)));
284
+ if (!el) return undefined;
285
+ const { id, author, date } = revisionOf(el);
286
+ return id === undefined ? undefined : compact({ id, author, date: date ?? undefined });
287
+ };
288
+ return compact({ insertion: pick("ins", "moveTo"), deletion: pick("del", "moveFrom") });
289
+ }
290
+
291
+ /**
292
+ * An integer attribute; a value that is there but not an integer (a universal measure such as
293
+ * `1.5cm`, a decimal) is reported and read as missing.
294
+ */
295
+ function whole(
296
+ element: XmlElement | undefined,
297
+ local: string,
298
+ context: Context,
299
+ ): number | undefined {
300
+ const value = attr(element, local);
301
+ const n = integer(value);
302
+ if (element && value !== undefined && n === undefined) {
303
+ context.report.unmodeled(`${element.name}@${local}`, `${value} is not a whole number`);
304
+ }
305
+ return n;
306
+ }
307
+
308
+ /** An ST_OnOff attribute (`w:beforeAutospacing="1"`); another value is reported, read as missing. */
309
+ export function onOff(
310
+ element: XmlElement | undefined,
311
+ local: string,
312
+ context: Context,
313
+ ns = W,
314
+ ): boolean | undefined {
315
+ const value = attr(element, local, ns);
316
+ if (value === undefined) return undefined;
317
+ if (value === "1" || value === "true" || value === "on") return true;
318
+ if (value === "0" || value === "false" || value === "off") return false;
319
+ context.report.unmodeled(`${element?.name}@${local}`, `${value} is not on or off`);
320
+ return undefined;
321
+ }
322
+
323
+ const toggleProperty = (el: XmlElement | undefined) => (el ? toggle(el) : undefined);
324
+
325
+ /**
326
+ * `w:pPr` as paragraph attributes. With `paragraph`, also what only a paragraph has (its
327
+ * section, the paragraph mark's revisions, a formatting change, the verbatim `ext`).
328
+ */
329
+ export function paragraphProperties(
330
+ pPr: XmlElement | undefined,
331
+ context: Context,
332
+ paragraph: boolean,
333
+ ): Record<string, unknown> {
334
+ const out: Record<string, unknown> = {};
335
+ if (!pPr) return out;
336
+ const at = (local: string) => child(pPr, local);
337
+ const set = (name: string, value: unknown, local: string) =>
338
+ setParagraph(out, name, value, `${pPr.prefix ? `${pPr.prefix}:` : ""}${local}`, context);
339
+ set("style", attr(at("pStyle"), "val"), "pStyle");
340
+ set("justification", attr(at("jc"), "val"), "jc");
341
+ const ind = at("ind");
342
+ if (ind) {
343
+ set(
344
+ "indent",
345
+ compact({
346
+ left: whole(ind, attr(ind, "left") === undefined ? "start" : "left", context),
347
+ right: whole(ind, attr(ind, "right") === undefined ? "end" : "right", context),
348
+ firstLine: whole(ind, "firstLine", context),
349
+ hanging: whole(ind, "hanging", context),
350
+ }),
351
+ "ind",
352
+ );
353
+ }
354
+ const spacing = at("spacing");
355
+ if (spacing) {
356
+ const auto = (local: string) => onOff(spacing, local, context);
357
+ set(
358
+ "spacing",
359
+ compact({
360
+ before: whole(spacing, "before", context),
361
+ after: whole(spacing, "after", context),
362
+ line: whole(spacing, "line", context),
363
+ lineRule: attr(spacing, "lineRule"),
364
+ beforeAutospacing: auto("beforeAutospacing"),
365
+ afterAutospacing: auto("afterAutospacing"),
366
+ }),
367
+ "spacing",
368
+ );
369
+ }
370
+ for (const [name, local] of [
371
+ ["keepNext", "keepNext"],
372
+ ["keepLines", "keepLines"],
373
+ ["widowControl", "widowControl"],
374
+ ["pageBreakBefore", "pageBreakBefore"],
375
+ ["contextualSpacing", "contextualSpacing"],
376
+ ] as const) {
377
+ set(name, toggleProperty(at(local)), local);
378
+ }
379
+ set("outlineLevel", whole(at("outlineLvl"), "val", context), "outlineLvl");
380
+ const numPr = at("numPr");
381
+ if (numPr)
382
+ set(
383
+ "numbering",
384
+ compact({
385
+ id: whole(child(numPr, "numId"), "val", context),
386
+ level: whole(child(numPr, "ilvl"), "val", context),
387
+ }),
388
+ "numPr",
389
+ );
390
+ const tabs = at("tabs");
391
+ if (tabs) {
392
+ set(
393
+ "tabs",
394
+ elements(tabs)
395
+ .filter((t) => isW(t, "tab"))
396
+ .map((t) =>
397
+ compact({
398
+ position: whole(t, "pos", context),
399
+ kind: attr(t, "val"),
400
+ leader: attr(t, "leader"),
401
+ }),
402
+ ),
403
+ "tabs",
404
+ );
405
+ }
406
+ const pBdr = at("pBdr");
407
+ if (pBdr) {
408
+ const borders: Record<string, unknown> = {};
409
+ for (const side of elements(pBdr)) {
410
+ borders[side.local] = compact({
411
+ style: attr(side, "val"),
412
+ size: whole(side, "sz", context),
413
+ space: whole(side, "space", context),
414
+ color: attr(side, "color"),
415
+ themeColor: attr(side, "themeColor"),
416
+ });
417
+ }
418
+ set("borders", compact(borders), "pBdr");
419
+ }
420
+ const shd = at("shd");
421
+ if (shd)
422
+ set(
423
+ "shading",
424
+ compact({
425
+ pattern: attr(shd, "val"),
426
+ color: attr(shd, "color"),
427
+ fill: attr(shd, "fill"),
428
+ themeFill: attr(shd, "themeFill"),
429
+ }),
430
+ "shd",
431
+ );
432
+ const rPr = at("rPr");
433
+ // The mark's recorded formatting change stays in `ext`: a list of run properties holds none.
434
+ const markFormat = runProperties(rPr, context)
435
+ .filter((m) => RUN_PROPERTY_MARKS.includes(m.type))
436
+ .map((m) => schema.markFromJSON(m).toJSON());
437
+ if (markFormat.length) set("markFormat", markFormat, "rPr");
438
+ if (!paragraph) return out;
439
+
440
+ set("revision", paragraphRevision(rPr), "rPr");
441
+ const sectPr = at("sectPr");
442
+ if (sectPr) set("section", section(sectPr, context), "sectPr");
443
+ const change = at("pPrChange");
444
+ if (change) {
445
+ const { id, author, date } = revisionOf(change);
446
+ set(
447
+ "formatChange",
448
+ compact({
449
+ id,
450
+ author,
451
+ date: date ?? undefined,
452
+ previous: paragraphProperties(child(change, "pPr"), context, false),
453
+ }),
454
+ "pPrChange",
455
+ );
456
+ }
457
+ out.ext = sourceOf(context, pPr);
458
+ return out;
459
+ }
460
+
461
+ /** `w:sectPr` as a section; every part checked on its own. */
462
+ export function section(sectPr: XmlElement, context: Context): Record<string, unknown> {
463
+ const pgSz = child(sectPr, "pgSz");
464
+ const pgMar = child(sectPr, "pgMar");
465
+ const references = (local: string) =>
466
+ compact(
467
+ Object.fromEntries(
468
+ elements(sectPr)
469
+ .filter((el) => isW(el, local))
470
+ .map((el) => [attr(el, "type") ?? "default", attr(el, "id", R)]),
471
+ ),
472
+ );
473
+ const titlePg = child(sectPr, "titlePg");
474
+ const parts: Record<string, unknown> = {
475
+ pageSize:
476
+ pgSz &&
477
+ compact({
478
+ width: whole(pgSz, "w", context),
479
+ height: whole(pgSz, "h", context),
480
+ orientation: attr(pgSz, "orient"),
481
+ }),
482
+ margins:
483
+ pgMar &&
484
+ compact({
485
+ top: whole(pgMar, "top", context),
486
+ right: whole(pgMar, "right", context),
487
+ bottom: whole(pgMar, "bottom", context),
488
+ left: whole(pgMar, "left", context),
489
+ header: whole(pgMar, "header", context),
490
+ footer: whole(pgMar, "footer", context),
491
+ gutter: whole(pgMar, "gutter", context),
492
+ }),
493
+ type: attr(child(sectPr, "type"), "val"),
494
+ titlePage: titlePg ? toggle(titlePg) : undefined,
495
+ headers: references("headerReference"),
496
+ footers: references("footerReference"),
497
+ };
498
+ const out: Record<string, unknown> = {};
499
+ for (const [key, value] of Object.entries(parts)) {
500
+ if (value === undefined) continue;
501
+ const error = checkSection({ [key]: value });
502
+ if (error) context.report.unmodeled(`${sectPr.name}`, error);
503
+ else out[key] = value;
504
+ }
505
+ out.ext = sourceOf(context, sectPr);
506
+ return out;
507
+ }
@@ -0,0 +1,64 @@
1
+ /** What an import could not turn into editable content: the coverage report. */
2
+ export interface ImportReport {
3
+ /** Opaque nodes (kept verbatim, not editable yet) by element name: `{ "w:tbl": 1 }`. */
4
+ readonly opaque: Readonly<Record<string, number>>;
5
+ /**
6
+ * Things not imported because Word recomputes them or they show nothing: spelling and grammar
7
+ * marks (`w:proofErr`), Word's last page breaks (`w:lastRenderedPageBreak`), editing session
8
+ * ids on runs (`w:r@w:rsidR`…), and runs with properties but no content (`w:r (empty)`).
9
+ */
10
+ readonly dropped: Readonly<Record<string, number>>;
11
+ /**
12
+ * Values the model could not hold (out of range, a unit it does not read), as
13
+ * `part: element: reason`. Each stays in its element's verbatim `ext`, so export keeps it;
14
+ * it is only not editable. At most 100 are listed; `unmodeledCount` counts all.
15
+ */
16
+ readonly unmodeled: readonly string[];
17
+ readonly unmodeledCount: number;
18
+ }
19
+
20
+ export class Recorder {
21
+ // Maps, not objects: element names such as `constructor` are only names.
22
+ readonly #opaque = new Map<string, number>();
23
+ readonly #dropped = new Map<string, number>();
24
+ readonly #unmodeled: string[] = [];
25
+ #unmodeledCount = 0;
26
+ /** The part being read, for messages. */
27
+ part = "";
28
+
29
+ opaque(name: string): void {
30
+ this.#opaque.set(name, (this.#opaque.get(name) ?? 0) + 1);
31
+ }
32
+
33
+ dropped(name: string): void {
34
+ this.#dropped.set(name, (this.#dropped.get(name) ?? 0) + 1);
35
+ }
36
+
37
+ unmodeled(element: string, reason: string): void {
38
+ this.#unmodeledCount++;
39
+ if (this.#unmodeled.length < 100) this.#unmodeled.push(`${this.part}: ${element}: ${reason}`);
40
+ }
41
+
42
+ report(): ImportReport {
43
+ return {
44
+ opaque: sorted(this.#opaque),
45
+ dropped: sorted(this.#dropped),
46
+ unmodeled: [...this.#unmodeled],
47
+ unmodeledCount: this.#unmodeledCount,
48
+ };
49
+ }
50
+ }
51
+
52
+ /** Counts as a plain object sorted by name, each name an own property. */
53
+ function sorted(counts: ReadonlyMap<string, number>): Record<string, number> {
54
+ const out: Record<string, number> = {};
55
+ for (const [name, n] of [...counts].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
56
+ Object.defineProperty(out, name, {
57
+ value: n,
58
+ enumerable: true,
59
+ writable: true,
60
+ configurable: true,
61
+ });
62
+ }
63
+ return out;
64
+ }
package/src/index.ts ADDED
@@ -0,0 +1,24 @@
1
+ export { type ExportOptions, exportDocx } from "./export/index.js";
2
+ export { type DocxImport, type ImportReport, importDocx, importPackage } from "./import/index.js";
3
+ export {
4
+ DocxError,
5
+ DocxPackage,
6
+ type DocxPart,
7
+ type NewPart,
8
+ type ReadPackageOptions,
9
+ type Relationship,
10
+ readPackage,
11
+ } from "./package.js";
12
+ export {
13
+ type ParseXmlOptions,
14
+ parseXml,
15
+ type XmlAttribute,
16
+ type XmlDocument,
17
+ type XmlElement,
18
+ XmlError,
19
+ type XmlName,
20
+ type XmlNode,
21
+ type XmlOther,
22
+ type XmlText,
23
+ } from "./xml.js";
24
+ export { ZipError, type ZipLimits } from "./zip.js";