@viveeditor/docx 0.1.0 → 1.0.0-rc.1
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/README.md +2 -0
- package/dist/index.d.ts +32 -1
- package/dist/index.js +754 -69
- package/dist/index.js.map +1 -1
- package/package.json +18 -17
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { normalizeColor } from "@viveeditor/ext-format";
|
|
1
|
+
import { cleanUrl, repairContent } from "@viveeditor/core";
|
|
2
|
+
import { DEFAULT_FONT_FAMILIES, normalizeColor } from "@viveeditor/ext-format";
|
|
3
3
|
import { DEFAULT_SOURCE_POLICY, imageSize, normalizeImageSrc, sniffImageType } from "@viveeditor/ext-image";
|
|
4
4
|
import { Inflate, zipSync } from "fflate";
|
|
5
5
|
// src/errors.ts
|
|
@@ -186,6 +186,20 @@ const REL = "http://schemas.openxmlformats.org/officeDocument/2006/relationships
|
|
|
186
186
|
const REL_OFFICE_DOCUMENT = `${REL}/officeDocument`;
|
|
187
187
|
const REL_HYPERLINK = `${REL}/hyperlink`;
|
|
188
188
|
const REL_IMAGE = `${REL}/image`;
|
|
189
|
+
function cellStyleOf(tcPr) {
|
|
190
|
+
const fill = wAttr(child(tcPr, NS.w, "shd"), "fill") ?? "";
|
|
191
|
+
const vertical = wAttr(child(tcPr, NS.w, "vAlign"), "val");
|
|
192
|
+
return [/^[0-9a-f]{6}$/i.test(fill) && !/^f{6}$/i.test(fill) ? `background-color: #${fill}` : "", vertical === "center" || vertical === "bottom" ? `vertical-align: ${vertical === "center" ? "middle" : vertical}` : ""].filter(Boolean).join("; ");
|
|
193
|
+
}
|
|
194
|
+
const W15$1 = "http://schemas.microsoft.com/office/word/2012/wordml";
|
|
195
|
+
const W14$1 = "http://schemas.microsoft.com/office/word/2010/wordml";
|
|
196
|
+
const WORD_ID = /^\d{1,9}$/;
|
|
197
|
+
const WORD_DATE = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?Z?$/;
|
|
198
|
+
const MAX_COMMENT_BODY = 5e3;
|
|
199
|
+
const MAX_COMMENT_AUTHOR = 100;
|
|
200
|
+
const MAX_THREADS = 1e3;
|
|
201
|
+
const MAX_THREAD_COMMENTS = 200;
|
|
202
|
+
const MAX_OPEN_COMMENTS = 16;
|
|
189
203
|
const MAX_DEPTH = 64;
|
|
190
204
|
const MAX_TABLE_DEPTH = 3;
|
|
191
205
|
const MAX_COLUMNS = 100;
|
|
@@ -193,6 +207,8 @@ const MAX_DIMENSION$1 = 1e4;
|
|
|
193
207
|
const MAX_TEXT = 1e3;
|
|
194
208
|
const MAX_LINK = 2048;
|
|
195
209
|
const EMU_PER_PIXEL$1 = 9525;
|
|
210
|
+
const MIN_ROW_HEIGHT = 24;
|
|
211
|
+
const MAX_ROW_HEIGHT$1 = 2e3;
|
|
196
212
|
const isControl = (code) => code <= 31 && code !== 9 && code !== 10 && code !== 13 || code === 127;
|
|
197
213
|
const isInvisible = (code) => code <= 32 || code >= 127 && code <= 160 || code === 173 || code >= 8203 && code <= 8207 || code >= 8232 && code <= 8238 || code >= 8288 && code <= 8303 || code === 65279;
|
|
198
214
|
function without(value, drop) {
|
|
@@ -228,6 +244,10 @@ const CODE_CHARACTER_STYLES = /* @__PURE__ */ new Set([
|
|
|
228
244
|
"verbatim char",
|
|
229
245
|
"source text"
|
|
230
246
|
]);
|
|
247
|
+
const MIN_HALF_POINTS = 12;
|
|
248
|
+
const MAX_HALF_POINTS = 192;
|
|
249
|
+
const INDENT_TWIPS$1 = 720;
|
|
250
|
+
const MAX_INDENT$1 = 8;
|
|
231
251
|
const ALIGNMENTS$1 = {
|
|
232
252
|
center: "center",
|
|
233
253
|
right: "right",
|
|
@@ -235,6 +255,37 @@ const ALIGNMENTS$1 = {
|
|
|
235
255
|
both: "justify",
|
|
236
256
|
distribute: "justify"
|
|
237
257
|
};
|
|
258
|
+
const STRIPE_FILL$1 = "F2F2F2";
|
|
259
|
+
const LINE_HEIGHTS = /* @__PURE__ */ new Map([
|
|
260
|
+
["240", "1"],
|
|
261
|
+
["276", "1.15"],
|
|
262
|
+
["360", "1.5"],
|
|
263
|
+
["480", "2"]
|
|
264
|
+
]);
|
|
265
|
+
const GENERIC_FAMILIES = /* @__PURE__ */ new Map([
|
|
266
|
+
["times new roman", "serif"],
|
|
267
|
+
["arial", "sans-serif"],
|
|
268
|
+
["courier new", "monospace"]
|
|
269
|
+
]);
|
|
270
|
+
const NUMBER_FORMATS = /* @__PURE__ */ new Map([
|
|
271
|
+
["lowerLetter", "a"],
|
|
272
|
+
["upperLetter", "A"],
|
|
273
|
+
["lowerRoman", "i"],
|
|
274
|
+
["upperRoman", "I"]
|
|
275
|
+
]);
|
|
276
|
+
const BULLET_STYLES = /* @__PURE__ */ new Map([
|
|
277
|
+
["•", "disc"],
|
|
278
|
+
["◦", "circle"],
|
|
279
|
+
["▪", "square"]
|
|
280
|
+
]);
|
|
281
|
+
const BORDER_EDGES = [
|
|
282
|
+
["top"],
|
|
283
|
+
["left", "start"],
|
|
284
|
+
["bottom"],
|
|
285
|
+
["right", "end"],
|
|
286
|
+
["insideH"],
|
|
287
|
+
["insideV"]
|
|
288
|
+
];
|
|
238
289
|
function cleanLimits$1(input) {
|
|
239
290
|
const limits = { ...DEFAULT_IMPORT_LIMITS };
|
|
240
291
|
for (const key of Object.keys(limits)) {
|
|
@@ -243,6 +294,11 @@ function cleanLimits$1(input) {
|
|
|
243
294
|
}
|
|
244
295
|
return limits;
|
|
245
296
|
}
|
|
297
|
+
function rowHeightOf(trHeight) {
|
|
298
|
+
if (!trHeight || wAttr(trHeight, "hRule") === "auto") return null;
|
|
299
|
+
const twips = clampInt(wAttr(trHeight, "val"), 0, 1e5, 0);
|
|
300
|
+
return twips > 0 ? Math.min(MAX_ROW_HEIGHT$1, Math.max(MIN_ROW_HEIGHT, Math.round(twips / 15))) : null;
|
|
301
|
+
}
|
|
246
302
|
function clampInt(value, min, max, fallback) {
|
|
247
303
|
const number = Number.parseInt(value ?? "", 10);
|
|
248
304
|
return Number.isFinite(number) ? Math.min(max, Math.max(min, number)) : fallback;
|
|
@@ -260,7 +316,7 @@ function cleanLink(target) {
|
|
|
260
316
|
return null;
|
|
261
317
|
}
|
|
262
318
|
if (!LINK_PROTOCOLS.has(url.protocol)) return null;
|
|
263
|
-
return
|
|
319
|
+
return cleanUrl(url.href);
|
|
264
320
|
}
|
|
265
321
|
function resolvePart(base, target) {
|
|
266
322
|
const segments = (target.startsWith("/") ? target.slice(1) : base + target).split("/");
|
|
@@ -285,13 +341,29 @@ function readRelationships(doc) {
|
|
|
285
341
|
}
|
|
286
342
|
return relationships;
|
|
287
343
|
}
|
|
344
|
+
function readSize(rPr) {
|
|
345
|
+
const complex = onOff(child(rPr, NS.w, "rtl")) || onOff(child(rPr, NS.w, "cs"));
|
|
346
|
+
const value = wAttr(child(rPr, NS.w, "sz"), "val") ?? (complex ? wAttr(child(rPr, NS.w, "szCs"), "val") : null);
|
|
347
|
+
return value !== null && /^[0-9]{1,4}$/.test(value) ? Number(value) : null;
|
|
348
|
+
}
|
|
288
349
|
function readStyles(doc) {
|
|
289
350
|
const paragraphs = /* @__PURE__ */ new Map();
|
|
290
351
|
const runs = /* @__PURE__ */ new Map();
|
|
291
|
-
|
|
352
|
+
const sheet = {
|
|
292
353
|
paragraphs,
|
|
293
|
-
runs
|
|
354
|
+
runs,
|
|
355
|
+
defaultParagraph: null,
|
|
356
|
+
defaultSize: null,
|
|
357
|
+
defaultFont: null
|
|
294
358
|
};
|
|
359
|
+
if (!doc) return sheet;
|
|
360
|
+
const defaults = descendant(doc.documentElement, [
|
|
361
|
+
[NS.w, "docDefaults"],
|
|
362
|
+
[NS.w, "rPrDefault"],
|
|
363
|
+
[NS.w, "rPr"]
|
|
364
|
+
]);
|
|
365
|
+
sheet.defaultSize = readSize(defaults);
|
|
366
|
+
sheet.defaultFont = wAttr(child(defaults, NS.w, "rFonts"), "ascii");
|
|
295
367
|
for (const style of children(doc.documentElement, NS.w, "style")) {
|
|
296
368
|
const id = wAttr(style, "styleId");
|
|
297
369
|
if (!id) continue;
|
|
@@ -302,6 +374,11 @@ function readStyles(doc) {
|
|
|
302
374
|
continue;
|
|
303
375
|
}
|
|
304
376
|
if (type !== "paragraph") continue;
|
|
377
|
+
if (sheet.defaultParagraph === null && [
|
|
378
|
+
"1",
|
|
379
|
+
"true",
|
|
380
|
+
"on"
|
|
381
|
+
].includes(wAttr(style, "default") ?? "")) sheet.defaultParagraph = id;
|
|
305
382
|
const pPr = child(style, NS.w, "pPr");
|
|
306
383
|
const numPr = child(pPr, NS.w, "numPr");
|
|
307
384
|
const outline = wAttr(child(pPr, NS.w, "outlineLvl"), "val");
|
|
@@ -311,16 +388,15 @@ function readStyles(doc) {
|
|
|
311
388
|
heading,
|
|
312
389
|
quote: name === "quote" || name === "intense quote",
|
|
313
390
|
code: name === "html preformatted" || name === "code" || name.includes("source code"),
|
|
391
|
+
caption: name === "caption",
|
|
314
392
|
listParagraph: name === "list paragraph",
|
|
315
393
|
numId: wAttr(child(numPr, NS.w, "numId"), "val"),
|
|
316
394
|
ilvl: clampInt(wAttr(child(numPr, NS.w, "ilvl"), "val"), 0, 8, 0),
|
|
395
|
+
size: readSize(child(style, NS.w, "rPr")),
|
|
317
396
|
basedOn: wAttr(child(style, NS.w, "basedOn"), "val")
|
|
318
397
|
});
|
|
319
398
|
}
|
|
320
|
-
return
|
|
321
|
-
paragraphs,
|
|
322
|
-
runs
|
|
323
|
-
};
|
|
399
|
+
return sheet;
|
|
324
400
|
}
|
|
325
401
|
function resolveStyle(styles, id) {
|
|
326
402
|
let current = id ? styles.get(id) : void 0;
|
|
@@ -331,7 +407,9 @@ function resolveStyle(styles, id) {
|
|
|
331
407
|
result.heading ??= current.heading;
|
|
332
408
|
result.quote ||= current.quote;
|
|
333
409
|
result.code ||= current.code;
|
|
410
|
+
result.caption ||= current.caption;
|
|
334
411
|
result.listParagraph ||= current.listParagraph;
|
|
412
|
+
result.size ??= current.size;
|
|
335
413
|
if (result.numId === null) {
|
|
336
414
|
result.numId = current.numId;
|
|
337
415
|
result.ilvl = current.ilvl;
|
|
@@ -345,13 +423,20 @@ function readNumbering(doc) {
|
|
|
345
423
|
if (doc) {
|
|
346
424
|
for (const abstract of children(doc.documentElement, NS.w, "abstractNum")) {
|
|
347
425
|
const levels = /* @__PURE__ */ new Map();
|
|
426
|
+
const texts = /* @__PURE__ */ new Set();
|
|
348
427
|
for (const level of children(abstract, NS.w, "lvl")) {
|
|
349
428
|
const format = wAttr(child(level, NS.w, "numFmt"), "val") ?? "decimal";
|
|
429
|
+
const text = wAttr(child(level, NS.w, "lvlText"), "val") ?? "";
|
|
430
|
+
texts.add(text);
|
|
431
|
+
const ordered = format !== "bullet" && format !== "none";
|
|
350
432
|
levels.set(clampInt(wAttr(level, "ilvl"), 0, 8, 0), {
|
|
351
|
-
ordered
|
|
352
|
-
start: clampInt(wAttr(child(level, NS.w, "start"), "val"),
|
|
433
|
+
ordered,
|
|
434
|
+
start: clampInt(wAttr(child(level, NS.w, "start"), "val"), 0, 9999, 1),
|
|
435
|
+
type: ordered ? NUMBER_FORMATS.get(format) ?? null : null,
|
|
436
|
+
bullet: ordered ? null : BULLET_STYLES.get(text) ?? null
|
|
353
437
|
});
|
|
354
438
|
}
|
|
439
|
+
if (texts.size > 1) for (const level of levels.values()) level.bullet = null;
|
|
355
440
|
const id = wAttr(abstract, "abstractNumId");
|
|
356
441
|
if (id !== null) abstracts.set(id, levels);
|
|
357
442
|
}
|
|
@@ -362,7 +447,7 @@ function readNumbering(doc) {
|
|
|
362
447
|
const starts = /* @__PURE__ */ new Map();
|
|
363
448
|
for (const override of children(num, NS.w, "lvlOverride")) {
|
|
364
449
|
const start = wAttr(child(override, NS.w, "startOverride"), "val");
|
|
365
|
-
if (start !== null) starts.set(clampInt(wAttr(override, "ilvl"), 0, 8, 0), clampInt(start,
|
|
450
|
+
if (start !== null) starts.set(clampInt(wAttr(override, "ilvl"), 0, 8, 0), clampInt(start, 0, 9999, 1));
|
|
366
451
|
}
|
|
367
452
|
instances.set(id, {
|
|
368
453
|
abstractId,
|
|
@@ -375,10 +460,49 @@ function readNumbering(doc) {
|
|
|
375
460
|
const level = instance ? abstracts.get(instance.abstractId)?.get(ilvl) : void 0;
|
|
376
461
|
return {
|
|
377
462
|
ordered: level?.ordered ?? false,
|
|
378
|
-
start: instance?.starts.get(ilvl) ?? level?.start ?? 1
|
|
463
|
+
start: instance?.starts.get(ilvl) ?? level?.start ?? 1,
|
|
464
|
+
type: level?.type ?? null,
|
|
465
|
+
bullet: level?.bullet ?? null
|
|
379
466
|
};
|
|
380
467
|
};
|
|
381
468
|
}
|
|
469
|
+
const firstFamily = (entry) => (entry.split(",")[0] ?? "").trim().replace(/^(['"])(.*)\1$/, "$2").toLowerCase();
|
|
470
|
+
function fontLookup(list) {
|
|
471
|
+
const named = /* @__PURE__ */ new Map();
|
|
472
|
+
for (const entry of list) {
|
|
473
|
+
if (typeof entry !== "string") continue;
|
|
474
|
+
const key = firstFamily(entry);
|
|
475
|
+
if (key && !named.has(key)) named.set(key, entry);
|
|
476
|
+
}
|
|
477
|
+
return (font) => {
|
|
478
|
+
const key = (font ?? "").trim().toLowerCase();
|
|
479
|
+
if (!key) return null;
|
|
480
|
+
const generic = GENERIC_FAMILIES.get(key);
|
|
481
|
+
return named.get(key) ?? (generic ? named.get(generic) : void 0) ?? null;
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
function readBorders(borders) {
|
|
485
|
+
if (!borders) return {
|
|
486
|
+
border: null,
|
|
487
|
+
color: null
|
|
488
|
+
};
|
|
489
|
+
const edges = BORDER_EDGES.map((names) => names.reduce((found, name) => found ?? child(borders, NS.w, name), null));
|
|
490
|
+
const off = (edge) => !!edge && ["nil", "none"].includes(wAttr(edge, "val") ?? "");
|
|
491
|
+
const on = (edge) => !!edge && !off(edge);
|
|
492
|
+
const [top, left, bottom, right, insideH, insideV] = edges;
|
|
493
|
+
const border = edges.every(off) ? "none" : off(left) && off(right) && off(insideV) && on(top) && on(bottom) && on(insideH) ? "rows" : null;
|
|
494
|
+
const colors = new Set(edges.filter(on).map((edge) => (wAttr(edge, "color") ?? "auto").toLowerCase()));
|
|
495
|
+
const [only] = colors;
|
|
496
|
+
return {
|
|
497
|
+
border,
|
|
498
|
+
color: colors.size === 1 && only && /^[0-9a-f]{6}$/.test(only) ? normalizeColor(`#${only}`) : null
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
function pointsOf(size, base) {
|
|
502
|
+
if (size === null || size === void 0 || size === base) return null;
|
|
503
|
+
if (size < MIN_HALF_POINTS || size > MAX_HALF_POINTS) return null;
|
|
504
|
+
return `${Math.floor(size / 2)}${size % 2 === 1 ? ".5" : ""}pt`;
|
|
505
|
+
}
|
|
382
506
|
function readRunFormat(rPr) {
|
|
383
507
|
if (!rPr) return {};
|
|
384
508
|
const format = {};
|
|
@@ -405,17 +529,109 @@ function readRunFormat(rPr) {
|
|
|
405
529
|
"specVanish",
|
|
406
530
|
"webHidden"
|
|
407
531
|
].some((name) => onOff(child(rPr, NS.w, name)));
|
|
532
|
+
const vertical = wAttr(child(rPr, NS.w, "vertAlign"), "val");
|
|
533
|
+
if (vertical) format.script = vertical === "superscript" ? "sup" : vertical === "subscript" ? "sub" : null;
|
|
534
|
+
const size = readSize(rPr);
|
|
535
|
+
if (size !== null) format.size = size;
|
|
536
|
+
const font = wAttr(child(rPr, NS.w, "rFonts"), "ascii");
|
|
537
|
+
if (font) format.font = font;
|
|
408
538
|
const fill = wAttr(child(rPr, NS.w, "shd"), "fill");
|
|
409
539
|
if (!format.highlight && fill && /^[0-9A-Fa-f]{6}$/.test(fill) && fill.toLowerCase() !== "ffffff") format.highlight = normalizeColor(`#${fill}`);
|
|
410
540
|
return format;
|
|
411
541
|
}
|
|
542
|
+
const NO_COMMENTS = {
|
|
543
|
+
comments: /* @__PURE__ */ new Map(),
|
|
544
|
+
parentOf: /* @__PURE__ */ new Map(),
|
|
545
|
+
resolved: /* @__PURE__ */ new Set()
|
|
546
|
+
};
|
|
547
|
+
function codePointSlice(value, max) {
|
|
548
|
+
return [...value].slice(0, max).join("");
|
|
549
|
+
}
|
|
550
|
+
function wordAuthor(value) {
|
|
551
|
+
return codePointSlice(cleanText(value).replace(/[\t\n\r]+/g, " ").replace(/[\u202A-\u202E\u2066-\u2069]/g, ""), MAX_COMMENT_AUTHOR).trim() || "Word";
|
|
552
|
+
}
|
|
553
|
+
function randomPrefix() {
|
|
554
|
+
return [...crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(6))].map((byte) => (byte % 36).toString(36)).join("");
|
|
555
|
+
}
|
|
556
|
+
function wordTime(value, fallback) {
|
|
557
|
+
const match = value ? WORD_DATE.exec(value) : null;
|
|
558
|
+
if (!match) return fallback;
|
|
559
|
+
const [year, month, day, hour, minute, second] = match.slice(1).map(Number);
|
|
560
|
+
const date = new Date(Date.UTC(year ?? 0, (month ?? 1) - 1, day ?? 1, hour ?? 0, minute ?? 0, second ?? 0));
|
|
561
|
+
return date.getUTCFullYear() === year && date.getUTCMonth() === (month ?? 0) - 1 && date.getUTCDate() === day && date.getUTCHours() === hour && date.getUTCMinutes() === minute && date.getUTCSeconds() === second ? date.toISOString() : fallback;
|
|
562
|
+
}
|
|
563
|
+
function commentBody(comment) {
|
|
564
|
+
const lines = [];
|
|
565
|
+
for (const paragraph of children(comment, NS.w, "p")) {
|
|
566
|
+
let line = "";
|
|
567
|
+
for (const node of paragraph.getElementsByTagNameNS(NS.w, "*")) if (node.localName === "t") line += node.textContent ?? "";
|
|
568
|
+
else if (node.localName === "tab") line += " ";
|
|
569
|
+
else if (node.localName === "br" || node.localName === "cr") line += "\n";
|
|
570
|
+
lines.push(without(line, isControl).replace(/\r\n?/g, "\n"));
|
|
571
|
+
}
|
|
572
|
+
return codePointSlice(lines.join("\n").replace(/^\n+|\n+$/g, ""), MAX_COMMENT_BODY);
|
|
573
|
+
}
|
|
574
|
+
function readComments(comments, extended) {
|
|
575
|
+
if (!comments) return NO_COMMENTS;
|
|
576
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
577
|
+
const prefix = randomPrefix();
|
|
578
|
+
const result = /* @__PURE__ */ new Map();
|
|
579
|
+
const byParagraph = /* @__PURE__ */ new Map();
|
|
580
|
+
for (const comment of children(comments.documentElement, NS.w, "comment")) {
|
|
581
|
+
if (result.size >= MAX_THREADS * MAX_THREAD_COMMENTS) break;
|
|
582
|
+
const id = wAttr(comment, "id") ?? "";
|
|
583
|
+
if (!WORD_ID.test(id) || result.has(id)) continue;
|
|
584
|
+
const body = commentBody(comment);
|
|
585
|
+
if (!body.trim()) continue;
|
|
586
|
+
const name = wordAuthor(wAttr(comment, "author"));
|
|
587
|
+
result.set(id, {
|
|
588
|
+
id: `word-${prefix}-${id}`,
|
|
589
|
+
author: {
|
|
590
|
+
id: codePointSlice(`word:${name}`, 128),
|
|
591
|
+
name
|
|
592
|
+
},
|
|
593
|
+
body,
|
|
594
|
+
created: wordTime(wAttr(comment, "date"), now)
|
|
595
|
+
});
|
|
596
|
+
const last = children(comment, NS.w, "p").at(-1)?.getAttributeNS(W14$1, "paraId");
|
|
597
|
+
if (last) byParagraph.set(last.toUpperCase(), id);
|
|
598
|
+
}
|
|
599
|
+
const parentOf = /* @__PURE__ */ new Map();
|
|
600
|
+
const resolved = /* @__PURE__ */ new Set();
|
|
601
|
+
for (const entry of children(extended?.documentElement, W15$1, "commentEx")) {
|
|
602
|
+
const id = byParagraph.get((entry.getAttributeNS(W15$1, "paraId") ?? "").toUpperCase());
|
|
603
|
+
if (!id) continue;
|
|
604
|
+
const parent = byParagraph.get((entry.getAttributeNS(W15$1, "paraIdParent") ?? "").toUpperCase());
|
|
605
|
+
if (parent && parent !== id) parentOf.set(id, parent);
|
|
606
|
+
if (onOffValue(entry.getAttributeNS(W15$1, "done"))) resolved.add(id);
|
|
607
|
+
}
|
|
608
|
+
return {
|
|
609
|
+
comments: result,
|
|
610
|
+
parentOf,
|
|
611
|
+
resolved
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
const onOffValue = (value) => value === "1" || value === "true";
|
|
615
|
+
function revisionKind(node) {
|
|
616
|
+
if (is(node, NS.w, "ins") || is(node, NS.w, "moveTo")) return "insert";
|
|
617
|
+
if (is(node, NS.w, "del") || is(node, NS.w, "moveFrom")) return "delete";
|
|
618
|
+
return null;
|
|
619
|
+
}
|
|
620
|
+
function setChange(element, change) {
|
|
621
|
+
element.setAttribute("data-vive-change", change.id);
|
|
622
|
+
element.setAttribute("data-author", change.author);
|
|
623
|
+
element.setAttribute("data-name", change.name);
|
|
624
|
+
element.setAttribute("data-time", change.time);
|
|
625
|
+
}
|
|
412
626
|
var Reader = class {
|
|
413
627
|
limits;
|
|
414
628
|
relationships;
|
|
415
629
|
media;
|
|
416
|
-
|
|
417
|
-
runStyles;
|
|
630
|
+
styles;
|
|
418
631
|
listLevel;
|
|
632
|
+
fonts;
|
|
633
|
+
wordComments;
|
|
634
|
+
keepChanges;
|
|
419
635
|
doc = document.implementation.createHTMLDocument("");
|
|
420
636
|
images = [];
|
|
421
637
|
warnings = /* @__PURE__ */ new Map();
|
|
@@ -423,23 +639,124 @@ var Reader = class {
|
|
|
423
639
|
cells = 0;
|
|
424
640
|
fieldStack = [];
|
|
425
641
|
instructions = 0;
|
|
426
|
-
|
|
642
|
+
paragraphSize = null;
|
|
643
|
+
paragraphStyles;
|
|
644
|
+
runStyles;
|
|
645
|
+
openComments = /* @__PURE__ */ new Set();
|
|
646
|
+
anchored = /* @__PURE__ */ new Set();
|
|
647
|
+
changePrefix = randomPrefix();
|
|
648
|
+
now = (/* @__PURE__ */ new Date()).toISOString();
|
|
649
|
+
changeCount = 0;
|
|
650
|
+
insertion = null;
|
|
651
|
+
deletion = null;
|
|
652
|
+
pendingBreak = null;
|
|
653
|
+
constructor(limits, relationships, media, styles, listLevel, fonts, wordComments = NO_COMMENTS, keepChanges = false) {
|
|
427
654
|
this.limits = limits;
|
|
428
655
|
this.relationships = relationships;
|
|
429
656
|
this.media = media;
|
|
430
|
-
this.
|
|
431
|
-
this.runStyles = runStyles;
|
|
657
|
+
this.styles = styles;
|
|
432
658
|
this.listLevel = listLevel;
|
|
659
|
+
this.fonts = fonts;
|
|
660
|
+
this.wordComments = wordComments;
|
|
661
|
+
this.keepChanges = keepChanges;
|
|
662
|
+
this.paragraphStyles = styles.paragraphs;
|
|
663
|
+
this.runStyles = styles.runs;
|
|
433
664
|
}
|
|
434
665
|
warn(code) {
|
|
435
666
|
this.warnings.set(code, (this.warnings.get(code) ?? 0) + 1);
|
|
436
667
|
}
|
|
668
|
+
rootComment(id) {
|
|
669
|
+
if (!id || !this.wordComments.comments.has(id)) return null;
|
|
670
|
+
let root = id;
|
|
671
|
+
for (let step = 0; step < MAX_THREAD_COMMENTS; step++) {
|
|
672
|
+
const parent = this.wordComments.parentOf.get(root);
|
|
673
|
+
if (!parent || !this.wordComments.comments.has(parent)) break;
|
|
674
|
+
root = parent;
|
|
675
|
+
}
|
|
676
|
+
return root;
|
|
677
|
+
}
|
|
678
|
+
commentRange(node) {
|
|
679
|
+
const start = is(node, NS.w, "commentRangeStart");
|
|
680
|
+
if (!start && !is(node, NS.w, "commentRangeEnd")) return false;
|
|
681
|
+
const id = wAttr(node, "id");
|
|
682
|
+
const root = this.rootComment(id);
|
|
683
|
+
if (!root || root !== id) return true;
|
|
684
|
+
if (!start) this.openComments.delete(root);
|
|
685
|
+
else if (this.openComments.size < MAX_OPEN_COMMENTS) this.openComments.add(root);
|
|
686
|
+
else this.warn("NESTING_FLATTENED");
|
|
687
|
+
return true;
|
|
688
|
+
}
|
|
689
|
+
threads() {
|
|
690
|
+
const { comments, resolved } = this.wordComments;
|
|
691
|
+
const threads = /* @__PURE__ */ new Map();
|
|
692
|
+
for (const root of this.anchored) {
|
|
693
|
+
const comment = comments.get(root);
|
|
694
|
+
if (comment && threads.size < MAX_THREADS) threads.set(root, {
|
|
695
|
+
id: comment.id,
|
|
696
|
+
resolved: resolved.has(root),
|
|
697
|
+
comments: [comment]
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
for (const [id, comment] of comments) {
|
|
701
|
+
const root = this.rootComment(id);
|
|
702
|
+
const thread = root && root !== id ? threads.get(root) : void 0;
|
|
703
|
+
if (thread && thread.comments.length < MAX_THREAD_COMMENTS) thread.comments.push(comment);
|
|
704
|
+
}
|
|
705
|
+
return [...threads.values()];
|
|
706
|
+
}
|
|
437
707
|
warningList() {
|
|
438
708
|
return [...this.warnings].slice(0, 100).map(([code, count]) => ({
|
|
439
709
|
code,
|
|
440
710
|
count
|
|
441
711
|
}));
|
|
442
712
|
}
|
|
713
|
+
change(element) {
|
|
714
|
+
this.changeCount += 1;
|
|
715
|
+
const name = wordAuthor(wAttr(element, "author"));
|
|
716
|
+
return {
|
|
717
|
+
id: `word-${this.changePrefix}-${this.changeCount}`,
|
|
718
|
+
author: codePointSlice(`word:${name}`, 128),
|
|
719
|
+
name,
|
|
720
|
+
time: wordTime(wAttr(element, "date"), this.now)
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
revision(node, walk) {
|
|
724
|
+
const kind = revisionKind(node);
|
|
725
|
+
if (!kind) return false;
|
|
726
|
+
if (!this.keepChanges) {
|
|
727
|
+
if (kind === "insert") walk();
|
|
728
|
+
return true;
|
|
729
|
+
}
|
|
730
|
+
const key = kind === "insert" ? "insertion" : "deletion";
|
|
731
|
+
const saved = this[key];
|
|
732
|
+
this[key] = this.change(node);
|
|
733
|
+
walk();
|
|
734
|
+
this[key] = saved;
|
|
735
|
+
return true;
|
|
736
|
+
}
|
|
737
|
+
paragraphBreak(pPr) {
|
|
738
|
+
if (!this.keepChanges) return null;
|
|
739
|
+
for (const node of child(pPr, NS.w, "rPr")?.children ?? []) {
|
|
740
|
+
const kind = revisionKind(node);
|
|
741
|
+
if (kind) return {
|
|
742
|
+
kind,
|
|
743
|
+
...this.change(node)
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
return null;
|
|
747
|
+
}
|
|
748
|
+
tracked(node) {
|
|
749
|
+
let result = node;
|
|
750
|
+
for (const [tag, change] of [["del", this.deletion], ["ins", this.insertion]]) {
|
|
751
|
+
if (!change) continue;
|
|
752
|
+
this.visit(0);
|
|
753
|
+
const element = this.doc.createElement(tag);
|
|
754
|
+
setChange(element, change);
|
|
755
|
+
element.append(result);
|
|
756
|
+
result = element;
|
|
757
|
+
}
|
|
758
|
+
return result;
|
|
759
|
+
}
|
|
443
760
|
visit(depth) {
|
|
444
761
|
this.elements += 1;
|
|
445
762
|
if (this.elements > this.limits.elements) throw new DocxError("TOO_COMPLEX", "The Word document has too many elements.");
|
|
@@ -448,12 +765,13 @@ var Reader = class {
|
|
|
448
765
|
blocks(container, target, depth, tableDepth) {
|
|
449
766
|
for (const node of container.children) {
|
|
450
767
|
if (!this.visit(depth)) return;
|
|
768
|
+
if (this.commentRange(node)) continue;
|
|
451
769
|
if (is(node, NS.w, "p")) this.paragraph(node, target, depth + 1);
|
|
452
770
|
else if (is(node, NS.w, "tbl")) this.table(node, target, depth + 1, tableDepth + 1);
|
|
453
771
|
else if (is(node, NS.w, "sdt")) {
|
|
454
772
|
const content = child(node, NS.w, "sdtContent");
|
|
455
773
|
if (content) this.blocks(content, target, depth + 1, tableDepth);
|
|
456
|
-
} else if (
|
|
774
|
+
} else if (this.revision(node, () => this.blocks(node, target, depth + 1, tableDepth))) {} else if (is(node, NS.w, "customXml")) this.blocks(node, target, depth + 1, tableDepth);
|
|
457
775
|
else if (is(node, NS.mc, "AlternateContent")) {
|
|
458
776
|
const fallback = child(node, NS.mc, "Fallback");
|
|
459
777
|
if (fallback) this.blocks(fallback, target, depth + 1, tableDepth);
|
|
@@ -466,29 +784,79 @@ var Reader = class {
|
|
|
466
784
|
const numPr = child(pPr, NS.w, "numPr");
|
|
467
785
|
const numId = numPr ? wAttr(child(numPr, NS.w, "numId"), "val") : style?.numId ?? null;
|
|
468
786
|
const ilvl = numPr ? clampInt(wAttr(child(numPr, NS.w, "ilvl"), "val"), 0, 8, 0) : style?.ilvl ?? 0;
|
|
787
|
+
const incoming = this.pendingBreak;
|
|
788
|
+
this.pendingBreak = this.paragraphBreak(pPr);
|
|
469
789
|
const content = this.doc.createElement("span");
|
|
790
|
+
const sized = style ?? resolveStyle(this.paragraphStyles, this.styles.defaultParagraph);
|
|
791
|
+
this.paragraphSize = sized?.size ?? this.styles.defaultSize;
|
|
470
792
|
this.inline(paragraph, content, {}, depth);
|
|
471
793
|
if (style?.code) {
|
|
472
794
|
target.code(content.textContent ?? "");
|
|
473
795
|
return;
|
|
474
796
|
}
|
|
797
|
+
if (style?.caption && !incoming && !content.querySelector("img")) {
|
|
798
|
+
const caption = (content.textContent ?? "").trim();
|
|
799
|
+
if (caption && target.caption(caption)) return;
|
|
800
|
+
}
|
|
475
801
|
const heading = style?.heading ?? null;
|
|
476
|
-
const block = this.doc.createElement(heading ? `h${heading}` : "p");
|
|
477
|
-
block.append(...content.childNodes);
|
|
478
802
|
const jc = wAttr(child(pPr, NS.w, "jc"), "val") ?? "";
|
|
479
803
|
const align = Object.hasOwn(ALIGNMENTS$1, jc) ? ALIGNMENTS$1[jc] : void 0;
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
804
|
+
const spacing = child(pPr, NS.w, "spacing");
|
|
805
|
+
const rule = wAttr(spacing, "lineRule");
|
|
806
|
+
const lineHeight = rule === null || rule === "auto" ? LINE_HEIGHTS.get(wAttr(spacing, "line") ?? "") : void 0;
|
|
807
|
+
const ind = child(pPr, NS.w, "ind");
|
|
808
|
+
const twips = Number(wAttr(ind, "start") ?? wAttr(ind, "left"));
|
|
809
|
+
const listed = numId && numId !== "0" && !heading;
|
|
810
|
+
const indent = listed || style?.listParagraph || !(twips > 0) ? 0 : Math.min(MAX_INDENT$1, Math.round(twips / INDENT_TWIPS$1));
|
|
811
|
+
let change = incoming;
|
|
812
|
+
const text = () => {
|
|
813
|
+
const block = this.doc.createElement(heading ? `h${heading}` : "p");
|
|
814
|
+
if (align) block.style.setProperty("text-align", align);
|
|
815
|
+
if (lineHeight) block.style.setProperty("line-height", lineHeight);
|
|
816
|
+
if (indent) block.style.setProperty("margin-inline-start", `${indent * 2}em`);
|
|
817
|
+
if (change) {
|
|
818
|
+
block.setAttribute("data-vive-break", change.kind);
|
|
819
|
+
setChange(block, change);
|
|
820
|
+
change = null;
|
|
821
|
+
}
|
|
822
|
+
return block;
|
|
823
|
+
};
|
|
824
|
+
if (listed) {
|
|
825
|
+
const block = text();
|
|
826
|
+
block.append(...content.childNodes);
|
|
827
|
+
target.listItem(block, numId, ilvl, this.listLevel(numId, ilvl));
|
|
828
|
+
return;
|
|
829
|
+
}
|
|
830
|
+
let blocks = [];
|
|
831
|
+
let open = null;
|
|
832
|
+
for (const node of [...content.childNodes]) if (isImage(node)) {
|
|
833
|
+
const figure = this.doc.createElement("figure");
|
|
834
|
+
if (align === "center" || align === "right") figure.setAttribute("data-align", align);
|
|
835
|
+
figure.append(node);
|
|
836
|
+
blocks.push(figure);
|
|
837
|
+
open = null;
|
|
838
|
+
} else {
|
|
839
|
+
if (!open) {
|
|
840
|
+
open = text();
|
|
841
|
+
blocks.push(open);
|
|
842
|
+
}
|
|
843
|
+
open.append(node);
|
|
844
|
+
}
|
|
845
|
+
if (blocks.length === 0) blocks.push(text());
|
|
846
|
+
else if (blocks.some((block) => block.localName === "figure")) blocks = blocks.filter((block) => block.localName === "figure" || (block.textContent ?? "").trim());
|
|
847
|
+
for (const block of blocks) {
|
|
848
|
+
if (style?.listParagraph && !heading && target.continueItem(block)) continue;
|
|
849
|
+
if (style?.quote) target.quote(block);
|
|
850
|
+
else target.block(block);
|
|
851
|
+
}
|
|
485
852
|
}
|
|
486
853
|
inline(container, target, inherited, depth) {
|
|
487
854
|
for (const node of container.children) {
|
|
488
855
|
if (!this.visit(depth)) return;
|
|
856
|
+
if (this.commentRange(node)) continue;
|
|
489
857
|
if (is(node, NS.w, "r")) this.run(node, target, inherited, depth + 1);
|
|
490
858
|
else if (is(node, NS.w, "hyperlink")) this.hyperlink(node, target, inherited, depth + 1);
|
|
491
|
-
else if (
|
|
859
|
+
else if (this.revision(node, () => this.inline(node, target, inherited, depth + 1))) {} else if (is(node, NS.w, "smartTag") || is(node, NS.w, "customXml") || is(node, NS.w, "fldSimple")) this.inline(node, target, inherited, depth + 1);
|
|
492
860
|
else if (is(node, NS.w, "sdt")) {
|
|
493
861
|
const content = child(node, NS.w, "sdtContent");
|
|
494
862
|
if (content) this.inline(content, target, inherited, depth + 1);
|
|
@@ -509,6 +877,7 @@ var Reader = class {
|
|
|
509
877
|
}
|
|
510
878
|
const anchor = this.doc.createElement("a");
|
|
511
879
|
anchor.setAttribute("href", href);
|
|
880
|
+
if ((wAttr(link, "tgtFrame") ?? "").toLowerCase() === "_blank") anchor.setAttribute("target", "_blank");
|
|
512
881
|
this.inline(link, anchor, inherited, depth);
|
|
513
882
|
target.append(anchor);
|
|
514
883
|
}
|
|
@@ -538,12 +907,13 @@ var Reader = class {
|
|
|
538
907
|
continue;
|
|
539
908
|
}
|
|
540
909
|
if (this.instructions > 0) continue;
|
|
541
|
-
if (is(node, NS.w, "t")) this.text(node.textContent ?? "", format, target);
|
|
910
|
+
if (is(node, NS.w, "t") || this.deletion && is(node, NS.w, "delText")) this.text(node.textContent ?? "", format, target);
|
|
911
|
+
else if (is(node, NS.w, "drawing") && this.deletion) this.warn("OBJECT_DROPPED");
|
|
542
912
|
else if (is(node, NS.w, "tab")) this.text(" ", format, target);
|
|
543
913
|
else if (is(node, NS.w, "noBreakHyphen")) this.text("‑", format, target);
|
|
544
914
|
else if (is(node, NS.w, "br") || is(node, NS.w, "cr")) {
|
|
545
915
|
const type = wAttr(node, "type");
|
|
546
|
-
if (type !== "page" && type !== "column") target.append(this.doc.createElement("br"));
|
|
916
|
+
if (type !== "page" && type !== "column") target.append(this.tracked(this.doc.createElement("br")));
|
|
547
917
|
} else if (is(node, NS.w, "drawing")) this.drawing(node, target);
|
|
548
918
|
else if (is(node, NS.w, "pict") || is(node, NS.w, "object")) this.warn("OBJECT_DROPPED");
|
|
549
919
|
else if (is(node, NS.mc, "AlternateContent")) {
|
|
@@ -565,11 +935,27 @@ var Reader = class {
|
|
|
565
935
|
};
|
|
566
936
|
if (format.highlight) wrap("mark", ["background-color", format.highlight]);
|
|
567
937
|
if (format.color) wrap("span", ["color", format.color]);
|
|
938
|
+
const points = pointsOf(format.size, this.paragraphSize);
|
|
939
|
+
if (points) wrap("span", ["font-size", points]);
|
|
940
|
+
const family = !!format.font && format.font.toLowerCase() === this.styles.defaultFont?.toLowerCase() ? null : this.fonts(format.font);
|
|
941
|
+
if (family) wrap("span", ["font-family", family]);
|
|
568
942
|
if (format.strike) wrap("s");
|
|
569
943
|
if (format.underline) wrap("u");
|
|
570
944
|
if (format.italic) wrap("em");
|
|
945
|
+
if (format.script) wrap(format.script);
|
|
571
946
|
if (format.code) wrap("code");
|
|
572
947
|
if (format.bold) wrap("strong");
|
|
948
|
+
node = this.tracked(node);
|
|
949
|
+
for (const root of this.openComments) {
|
|
950
|
+
const comment = this.wordComments.comments.get(root);
|
|
951
|
+
if (!comment) continue;
|
|
952
|
+
this.visit(0);
|
|
953
|
+
const span = this.doc.createElement("span");
|
|
954
|
+
span.setAttribute("data-vive-comment", comment.id);
|
|
955
|
+
span.append(node);
|
|
956
|
+
node = span;
|
|
957
|
+
this.anchored.add(root);
|
|
958
|
+
}
|
|
573
959
|
target.append(node);
|
|
574
960
|
}
|
|
575
961
|
drawing(drawing, target) {
|
|
@@ -613,15 +999,26 @@ var Reader = class {
|
|
|
613
999
|
}
|
|
614
1000
|
const widths = children(child(table, NS.w, "tblGrid"), NS.w, "gridCol").map((column) => Math.max(0, Math.round(clampInt(wAttr(column, "w"), 0, 1e5, 0) / 15)));
|
|
615
1001
|
const element = this.doc.createElement("table");
|
|
1002
|
+
const tblPr = child(table, NS.w, "tblPr");
|
|
1003
|
+
const tableWidth = child(tblPr, NS.w, "tblW");
|
|
1004
|
+
if (wAttr(tableWidth, "type") === "pct" && ["5000", "100%"].includes(wAttr(tableWidth, "w") ?? "")) element.setAttribute("data-width", "full");
|
|
1005
|
+
if (wAttr(child(tblPr, NS.w, "jc"), "val") === "center") element.setAttribute("data-align", "center");
|
|
1006
|
+
const borders = readBorders(child(tblPr, NS.w, "tblBorders"));
|
|
1007
|
+
if (borders.border) element.setAttribute("data-border", borders.border);
|
|
1008
|
+
if (borders.color) element.setAttribute("style", `--vive-table-border-color: ${borders.color}`);
|
|
616
1009
|
const body = this.doc.createElement("tbody");
|
|
617
1010
|
element.append(body);
|
|
618
1011
|
const origins = /* @__PURE__ */ new Map();
|
|
619
1012
|
const rows = children(table, NS.w, "tr").filter((row) => child(row, NS.w, "tc"));
|
|
620
1013
|
let widest = 0;
|
|
1014
|
+
const stripes = [];
|
|
621
1015
|
rows.forEach((row, rowIndex) => {
|
|
622
1016
|
if (!this.visit(depth)) return;
|
|
623
|
-
const
|
|
1017
|
+
const trPr = child(row, NS.w, "trPr");
|
|
1018
|
+
const header = onOff(child(trPr, NS.w, "tblHeader"));
|
|
624
1019
|
const tableRow = this.doc.createElement("tr");
|
|
1020
|
+
const height = rowHeightOf(child(trPr, NS.w, "trHeight"));
|
|
1021
|
+
if (height) tableRow.setAttribute("style", `height: ${height}px`);
|
|
625
1022
|
let column = 0;
|
|
626
1023
|
for (const cell of children(row, NS.w, "tc")) {
|
|
627
1024
|
const tcPr = child(cell, NS.w, "tcPr");
|
|
@@ -642,8 +1039,16 @@ var Reader = class {
|
|
|
642
1039
|
if (span > 1) tableCell.setAttribute("colspan", String(span));
|
|
643
1040
|
const cellWidths = widths.slice(column, column + span);
|
|
644
1041
|
if (clampInt(wAttr(child(tcPr, NS.w, "tcW"), "w"), 0, 1e5, 0) > 0 && cellWidths.length === span && cellWidths.every((width) => width > 0)) tableCell.setAttribute("data-colwidth", cellWidths.join(","));
|
|
1042
|
+
const cellStyle = cellStyleOf(tcPr);
|
|
1043
|
+
stripes.push({
|
|
1044
|
+
cell: tableCell,
|
|
1045
|
+
even: rowIndex % 2 === 1 && !header,
|
|
1046
|
+
fill: (wAttr(child(tcPr, NS.w, "shd"), "fill") ?? "").toUpperCase()
|
|
1047
|
+
});
|
|
1048
|
+
if (cellStyle) tableCell.setAttribute("style", cellStyle);
|
|
645
1049
|
for (let offset = 0; offset < span; offset++) if (merge) origins.set(column + offset, tableCell);
|
|
646
1050
|
else origins.delete(column + offset);
|
|
1051
|
+
this.pendingBreak = null;
|
|
647
1052
|
this.blocks(cell, new BlockWriter(this.doc, tableCell), depth + 1, tableDepth);
|
|
648
1053
|
if (!tableCell.firstChild) tableCell.append(this.doc.createElement("p"));
|
|
649
1054
|
tableRow.append(tableCell);
|
|
@@ -653,8 +1058,20 @@ var Reader = class {
|
|
|
653
1058
|
if (widest * (rowIndex + 1) > this.limits.cells) throw new DocxError("TOO_COMPLEX", "The Word document has too many table cells.");
|
|
654
1059
|
body.append(tableRow);
|
|
655
1060
|
});
|
|
1061
|
+
this.pendingBreak = null;
|
|
1062
|
+
this.stripes(element, stripes);
|
|
656
1063
|
target.block(element);
|
|
657
1064
|
}
|
|
1065
|
+
stripes(table, cells) {
|
|
1066
|
+
const even = cells.filter((entry) => entry.even);
|
|
1067
|
+
const stripe = (entry) => entry.fill === STRIPE_FILL$1;
|
|
1068
|
+
if (!even.some(stripe) || !even.every((entry) => entry.fill !== "" && entry.fill !== "AUTO") || cells.some((entry) => !entry.even && stripe(entry))) return;
|
|
1069
|
+
table.setAttribute("data-striped", "true");
|
|
1070
|
+
for (const { cell } of even.filter(stripe)) {
|
|
1071
|
+
cell.style.removeProperty("background-color");
|
|
1072
|
+
if (!cell.getAttribute("style")) cell.removeAttribute("style");
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
658
1075
|
flatten(container, target, depth) {
|
|
659
1076
|
for (const node of container.children) {
|
|
660
1077
|
if (!this.visit(depth)) return;
|
|
@@ -703,6 +1120,15 @@ var BlockWriter = class {
|
|
|
703
1120
|
}
|
|
704
1121
|
this.quoteElement.append(element);
|
|
705
1122
|
}
|
|
1123
|
+
caption(text) {
|
|
1124
|
+
const figure = this.parent.lastElementChild;
|
|
1125
|
+
if (figure?.localName !== "figure" || figure.querySelector("figcaption")) return false;
|
|
1126
|
+
const caption = this.doc.createElement("figcaption");
|
|
1127
|
+
caption.append(text);
|
|
1128
|
+
figure.append(caption);
|
|
1129
|
+
this.reset(null);
|
|
1130
|
+
return true;
|
|
1131
|
+
}
|
|
706
1132
|
code(text) {
|
|
707
1133
|
this.reset("code");
|
|
708
1134
|
if (this.codeElement) {
|
|
@@ -732,6 +1158,8 @@ var BlockWriter = class {
|
|
|
732
1158
|
if (!top || top.ilvl < ilvl) {
|
|
733
1159
|
const list = this.doc.createElement(level.ordered ? "ol" : "ul");
|
|
734
1160
|
if (level.ordered && level.start !== 1) list.setAttribute("start", String(level.start));
|
|
1161
|
+
if (level.ordered && level.type) list.setAttribute("type", level.type);
|
|
1162
|
+
if (!level.ordered && level.bullet) list.style.setProperty("list-style-type", level.bullet);
|
|
735
1163
|
(top?.lastItem ?? this.parent).append(list);
|
|
736
1164
|
top = {
|
|
737
1165
|
ilvl,
|
|
@@ -748,6 +1176,11 @@ var BlockWriter = class {
|
|
|
748
1176
|
top.lastItem = item;
|
|
749
1177
|
}
|
|
750
1178
|
};
|
|
1179
|
+
function isImage(node) {
|
|
1180
|
+
if (!(node instanceof Element)) return false;
|
|
1181
|
+
if (node.localName === "img") return true;
|
|
1182
|
+
return node.localName === "a" && node.childElementCount === 1 && node.firstElementChild?.localName === "img" && !(node.textContent ?? "").trim();
|
|
1183
|
+
}
|
|
751
1184
|
function base64(bytes) {
|
|
752
1185
|
let binary = "";
|
|
753
1186
|
for (let offset = 0; offset < bytes.length; offset += 32768) binary += String.fromCharCode(...bytes.subarray(offset, offset + 32768));
|
|
@@ -794,11 +1227,12 @@ async function resolveImages(reader, options, limits) {
|
|
|
794
1227
|
else if (type && !tooBig && policy.allowBase64) src = normalizeImageSrc(`data:${type};base64,${base64(bytes)}`, policy);
|
|
795
1228
|
if (src) element.setAttribute("src", src);
|
|
796
1229
|
else {
|
|
797
|
-
element.remove();
|
|
1230
|
+
(element.closest("figure") ?? element).remove();
|
|
798
1231
|
reader.warn("IMAGE_DROPPED");
|
|
799
1232
|
}
|
|
800
1233
|
}
|
|
801
1234
|
}
|
|
1235
|
+
const COMMENT_PARTS = ["word/comments.xml", "word/commentsExtended.xml"];
|
|
802
1236
|
async function importDocx(file, options = {}) {
|
|
803
1237
|
const limits = cleanLimits$1(options.limits);
|
|
804
1238
|
if (file.size > limits.fileBytes) throw new DocxError("TOO_LARGE", "The Word document is too large.");
|
|
@@ -821,6 +1255,7 @@ async function importDocx(file, options = {}) {
|
|
|
821
1255
|
"word/styles.xml",
|
|
822
1256
|
"word/numbering.xml"
|
|
823
1257
|
].includes(name)) return true;
|
|
1258
|
+
if (options.comments !== false && COMMENT_PARTS.includes(name)) return true;
|
|
824
1259
|
if (!/^word\/media\/[^/]+$/.test(name) || mediaCount >= limits.images) return false;
|
|
825
1260
|
mediaCount += 1;
|
|
826
1261
|
return true;
|
|
@@ -836,13 +1271,14 @@ async function importDocx(file, options = {}) {
|
|
|
836
1271
|
if (!body) throw new DocxError("UNSUPPORTED", "This kind of Word document is not supported.");
|
|
837
1272
|
const styles = readStyles(part("word/styles.xml"));
|
|
838
1273
|
const media = new Map([...parts].filter(([name]) => name.startsWith("word/media/")));
|
|
839
|
-
const reader = new Reader(limits, readRelationships(part(relsPath)), media, styles.
|
|
1274
|
+
const reader = new Reader(limits, readRelationships(part(relsPath)), media, styles, readNumbering(part("word/numbering.xml")), fontLookup(options.fontFamilies ?? DEFAULT_FONT_FAMILIES), options.comments === false ? NO_COMMENTS : readComments(part("word/comments.xml"), part("word/commentsExtended.xml")), options.trackedChanges === "keep");
|
|
840
1275
|
reader.blocks(body, new BlockWriter(reader.doc, reader.doc.body), 0, 0);
|
|
841
1276
|
await resolveImages(reader, options, limits);
|
|
842
1277
|
throwIfAborted(options.signal);
|
|
843
1278
|
return {
|
|
844
1279
|
html: reader.doc.body.innerHTML,
|
|
845
|
-
warnings: reader.warningList()
|
|
1280
|
+
warnings: reader.warningList(),
|
|
1281
|
+
threads: reader.threads()
|
|
846
1282
|
};
|
|
847
1283
|
}
|
|
848
1284
|
// src/write.ts
|
|
@@ -860,7 +1296,25 @@ const PAGE_WIDTH_TWIPS = 9360;
|
|
|
860
1296
|
const MAX_IMAGE_PIXELS = 624;
|
|
861
1297
|
const EMU_PER_PIXEL = 9525;
|
|
862
1298
|
const TWIPS_PER_PIXEL = 15;
|
|
1299
|
+
const MAX_ROW_HEIGHT = 2e3;
|
|
863
1300
|
const MAX_LEVEL = 8;
|
|
1301
|
+
const STEP_HALF_POINTS = {
|
|
1302
|
+
tiny: 16,
|
|
1303
|
+
small: 19,
|
|
1304
|
+
big: 28,
|
|
1305
|
+
huge: 33
|
|
1306
|
+
};
|
|
1307
|
+
const POINT_SIZE = /^([0-9]{1,2})(?:\.([05]))?pt$/;
|
|
1308
|
+
const INDENT_TWIPS = 720;
|
|
1309
|
+
const MAX_INDENT = 8;
|
|
1310
|
+
function halfPoints(size) {
|
|
1311
|
+
if (typeof size !== "string") return null;
|
|
1312
|
+
if (Object.hasOwn(STEP_HALF_POINTS, size)) return STEP_HALF_POINTS[size] ?? null;
|
|
1313
|
+
const match = POINT_SIZE.exec(size);
|
|
1314
|
+
if (!match) return null;
|
|
1315
|
+
const halves = Number(match[1]) * 2 + (match[2] === "5" ? 1 : 0);
|
|
1316
|
+
return halves >= 12 && halves <= 192 ? halves : null;
|
|
1317
|
+
}
|
|
864
1318
|
const WATCH_URLS = {
|
|
865
1319
|
youtube: (id) => `https://www.youtube.com/watch?v=${id}`,
|
|
866
1320
|
vimeo: (id) => `https://vimeo.com/${id}`,
|
|
@@ -877,6 +1331,41 @@ const ALIGNMENTS = {
|
|
|
877
1331
|
right: "right",
|
|
878
1332
|
justify: "both"
|
|
879
1333
|
};
|
|
1334
|
+
const LINE_TWIPS = {
|
|
1335
|
+
"1": 240,
|
|
1336
|
+
"1.15": 276,
|
|
1337
|
+
"1.5": 360,
|
|
1338
|
+
"2": 480
|
|
1339
|
+
};
|
|
1340
|
+
const GENERIC_FONTS = {
|
|
1341
|
+
serif: "Times New Roman",
|
|
1342
|
+
"sans-serif": "Arial",
|
|
1343
|
+
monospace: "Courier New"
|
|
1344
|
+
};
|
|
1345
|
+
const FONT_NAME = /^[A-Za-z][A-Za-z0-9 -]{0,63}$/;
|
|
1346
|
+
const ORDERED_FORMATS = {
|
|
1347
|
+
a: "lowerLetter",
|
|
1348
|
+
A: "upperLetter",
|
|
1349
|
+
i: "lowerRoman",
|
|
1350
|
+
I: "upperRoman"
|
|
1351
|
+
};
|
|
1352
|
+
const BULLET_TEXTS = {
|
|
1353
|
+
disc: "•",
|
|
1354
|
+
circle: "◦",
|
|
1355
|
+
square: "▪"
|
|
1356
|
+
};
|
|
1357
|
+
const DEFAULT_BULLETS = [
|
|
1358
|
+
"•",
|
|
1359
|
+
"◦",
|
|
1360
|
+
"▪"
|
|
1361
|
+
];
|
|
1362
|
+
const STRIPE_FILL = "F2F2F2";
|
|
1363
|
+
const W14 = "http://schemas.microsoft.com/office/word/2010/wordml";
|
|
1364
|
+
const W15 = "http://schemas.microsoft.com/office/word/2012/wordml";
|
|
1365
|
+
const COMMENTS_EXTENDED = "http://schemas.microsoft.com/office/2011/relationships/commentsExtended";
|
|
1366
|
+
const COMMENT_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
1367
|
+
const WORD_TIME = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.\d{3})?Z$/;
|
|
1368
|
+
const MAX_WORD_COMMENTS = 1e4;
|
|
880
1369
|
const MAX_CONVERT_PIXELS = 5e7;
|
|
881
1370
|
const EMBEDDABLE = new Set(Object.keys(IMAGE_EXTENSIONS));
|
|
882
1371
|
const CONVERTIBLE = /* @__PURE__ */ new Set(["image/webp", "image/avif"]);
|
|
@@ -908,6 +1397,20 @@ const hexColor = (value) => {
|
|
|
908
1397
|
const match = typeof value === "string" ? /^#([0-9a-f]{6})(?:[0-9a-f]{2})?$/i.exec(value) : null;
|
|
909
1398
|
return match?.[1] ? match[1].toUpperCase() : null;
|
|
910
1399
|
};
|
|
1400
|
+
function wordFont(family) {
|
|
1401
|
+
if (typeof family !== "string") return null;
|
|
1402
|
+
const first = (family.split(",")[0] ?? "").trim().replace(/^(['"])(.*)\1$/, "$2");
|
|
1403
|
+
const generic = first.toLowerCase();
|
|
1404
|
+
if (Object.hasOwn(GENERIC_FONTS, generic)) return GENERIC_FONTS[generic] ?? null;
|
|
1405
|
+
return FONT_NAME.test(first) ? first : null;
|
|
1406
|
+
}
|
|
1407
|
+
function tableBorders(border, color) {
|
|
1408
|
+
if (border !== "rows" && border !== "none" && !color) return "";
|
|
1409
|
+
const edge = (name, shown) => shown ? `<w:${name} w:val="single" w:sz="4" w:space="0" w:color="${color ?? "auto"}"/>` : `<w:${name} w:val="nil"/>`;
|
|
1410
|
+
const lines = border !== "none";
|
|
1411
|
+
const grid = lines && border !== "rows";
|
|
1412
|
+
return `<w:tblBorders>${edge("top", lines)}${edge("left", grid)}${edge("bottom", lines)}${edge("right", grid)}${edge("insideH", lines)}${edge("insideV", grid)}</w:tblBorders>`;
|
|
1413
|
+
}
|
|
911
1414
|
const positiveInt = (value) => {
|
|
912
1415
|
const number = Number(value);
|
|
913
1416
|
return Number.isFinite(number) && number >= 1 ? Math.min(MAX_DIMENSION, Math.round(number)) : null;
|
|
@@ -988,9 +1491,106 @@ var Writer = class {
|
|
|
988
1491
|
imageAttempts = 0;
|
|
989
1492
|
drawingCount = 0;
|
|
990
1493
|
notes = [];
|
|
1494
|
+
abstracts = /* @__PURE__ */ new Map();
|
|
1495
|
+
cellAlign = null;
|
|
1496
|
+
threads = /* @__PURE__ */ new Map();
|
|
1497
|
+
wordIds = /* @__PURE__ */ new Map();
|
|
1498
|
+
wordComments = [];
|
|
1499
|
+
remaining = /* @__PURE__ */ new Map();
|
|
1500
|
+
openComments = /* @__PURE__ */ new Set();
|
|
1501
|
+
breaks = /* @__PURE__ */ new Map();
|
|
1502
|
+
revisionCount = MAX_WORD_COMMENTS;
|
|
991
1503
|
constructor(options, limits) {
|
|
992
1504
|
this.options = options;
|
|
993
1505
|
this.limits = limits;
|
|
1506
|
+
for (const thread of options.threads ?? []) if (COMMENT_ID.test(String(thread?.id)) && Array.isArray(thread.comments)) {
|
|
1507
|
+
if (thread.comments.length > 0) this.threads.set(thread.id, thread);
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
get hasComments() {
|
|
1511
|
+
return this.wordComments.length > 0;
|
|
1512
|
+
}
|
|
1513
|
+
commentIdsOf(node) {
|
|
1514
|
+
if (this.threads.size === 0) return [];
|
|
1515
|
+
return node.marks.filter((mark) => mark.type.name === "comment" && this.threads.has(String(mark.attrs.id))).map((mark) => String(mark.attrs.id));
|
|
1516
|
+
}
|
|
1517
|
+
countComments(doc) {
|
|
1518
|
+
if (this.threads.size === 0) return;
|
|
1519
|
+
doc.descendants((node) => {
|
|
1520
|
+
if (!node.isTextblock) return true;
|
|
1521
|
+
if (node.type.spec.code) return false;
|
|
1522
|
+
node.forEach((child) => {
|
|
1523
|
+
for (const id of this.commentIdsOf(child)) this.remaining.set(id, (this.remaining.get(id) ?? 0) + 1);
|
|
1524
|
+
});
|
|
1525
|
+
return false;
|
|
1526
|
+
});
|
|
1527
|
+
}
|
|
1528
|
+
countBreaks(doc) {
|
|
1529
|
+
let previous = null;
|
|
1530
|
+
doc.descendants((node) => {
|
|
1531
|
+
if (!node.isTextblock && !node.isLeaf) return true;
|
|
1532
|
+
const change = node.attrs.change;
|
|
1533
|
+
if (previous && (change?.kind === "insert" || change?.kind === "delete")) this.breaks.set(previous, change);
|
|
1534
|
+
previous = node;
|
|
1535
|
+
return false;
|
|
1536
|
+
});
|
|
1537
|
+
}
|
|
1538
|
+
revision(tag, change, content) {
|
|
1539
|
+
this.revisionCount += 1;
|
|
1540
|
+
const time = WORD_TIME.exec(String(change.time));
|
|
1541
|
+
const open = `<w:${tag} w:id="${this.revisionCount}" w:author="${escapeXml(cleanAuthor(change.name))}"${time ? ` w:date="${time[1]}Z"` : ""}`;
|
|
1542
|
+
return content === void 0 ? `${open}/>` : `${open}>${content}</w:${tag}>`;
|
|
1543
|
+
}
|
|
1544
|
+
startComment(threadId) {
|
|
1545
|
+
this.openComments.add(threadId);
|
|
1546
|
+
let ids = this.wordIds.get(threadId);
|
|
1547
|
+
if (!ids) {
|
|
1548
|
+
const thread = this.threads.get(threadId);
|
|
1549
|
+
ids = [];
|
|
1550
|
+
let parent = null;
|
|
1551
|
+
for (const comment of thread.comments) {
|
|
1552
|
+
if (this.wordComments.length >= MAX_WORD_COMMENTS) break;
|
|
1553
|
+
const id = this.wordComments.length;
|
|
1554
|
+
const paraId = (id + 1).toString(16).toUpperCase().padStart(8, "0");
|
|
1555
|
+
this.wordComments.push({
|
|
1556
|
+
id,
|
|
1557
|
+
paraId,
|
|
1558
|
+
parent,
|
|
1559
|
+
done: thread.resolved === true,
|
|
1560
|
+
comment
|
|
1561
|
+
});
|
|
1562
|
+
parent ??= paraId;
|
|
1563
|
+
ids.push(id);
|
|
1564
|
+
}
|
|
1565
|
+
this.wordIds.set(threadId, ids);
|
|
1566
|
+
}
|
|
1567
|
+
return ids.map((id) => `<w:commentRangeStart w:id="${id}"/>`).join("");
|
|
1568
|
+
}
|
|
1569
|
+
endComment(threadId) {
|
|
1570
|
+
this.openComments.delete(threadId);
|
|
1571
|
+
return (this.wordIds.get(threadId) ?? []).map((id) => `<w:commentRangeEnd w:id="${id}"/><w:r><w:commentReference w:id="${id}"/></w:r>`).join("");
|
|
1572
|
+
}
|
|
1573
|
+
closeComments() {
|
|
1574
|
+
if (this.openComments.size === 0) return "";
|
|
1575
|
+
return `<w:p>${[...this.openComments].map((id) => this.endComment(id)).join("")}</w:p>`;
|
|
1576
|
+
}
|
|
1577
|
+
commentsXml() {
|
|
1578
|
+
const comments = this.wordComments.map(({ id, paraId, comment }) => {
|
|
1579
|
+
const author = cleanAuthor(comment.author?.name);
|
|
1580
|
+
const time = WORD_TIME.exec(String(comment.created));
|
|
1581
|
+
const lines = String(comment.body ?? "").split("\n");
|
|
1582
|
+
const paragraphs = lines.map((line, index) => {
|
|
1583
|
+
const last = index === lines.length - 1;
|
|
1584
|
+
const reference = index === 0 ? "<w:r><w:annotationRef/></w:r>" : "";
|
|
1585
|
+
return `<w:p${last ? ` w14:paraId="${paraId}" w14:textId="77777777"` : ""}>${reference}${this.textRun(line, "")}</w:p>`;
|
|
1586
|
+
}).join("");
|
|
1587
|
+
return `<w:comment w:id="${id}" w:author="${escapeXml(author)}"${time ? ` w:date="${time[1]}Z"` : ""} w:initials="${escapeXml(initials(author))}">${paragraphs}</w:comment>`;
|
|
1588
|
+
});
|
|
1589
|
+
return `${DECLARATION}<w:comments xmlns:w="${W}" xmlns:w14="${W14}">${comments.join("")}</w:comments>`;
|
|
1590
|
+
}
|
|
1591
|
+
commentsExtendedXml() {
|
|
1592
|
+
const entries = this.wordComments.map(({ paraId, parent, done }) => `<w15:commentEx w15:paraId="${paraId}"${parent ? ` w15:paraIdParent="${parent}"` : ""} w15:done="${done ? 1 : 0}"/>`);
|
|
1593
|
+
return `${DECLARATION}<w15:commentsEx xmlns:w15="${W15}">${entries.join("")}</w15:commentsEx>`;
|
|
994
1594
|
}
|
|
995
1595
|
relationship(type, target, external = false) {
|
|
996
1596
|
this.relationshipCount += 1;
|
|
@@ -999,26 +1599,39 @@ var Writer = class {
|
|
|
999
1599
|
return id;
|
|
1000
1600
|
}
|
|
1001
1601
|
relationshipsXml() {
|
|
1002
|
-
|
|
1602
|
+
const comments = this.hasComments ? `<Relationship Id="rIdComments" Type="${R}/comments" Target="comments.xml"/><Relationship Id="rIdCommentsExtended" Type="${COMMENTS_EXTENDED}" Target="commentsExtended.xml"/>` : "";
|
|
1603
|
+
return `${DECLARATION}<Relationships xmlns="${PACKAGE_REL}"><Relationship Id="rId1" Type="${R}/styles" Target="styles.xml"/><Relationship Id="rId2" Type="${R}/numbering" Target="numbering.xml"/>${comments}${this.relationships.join("")}</Relationships>`;
|
|
1003
1604
|
}
|
|
1004
1605
|
notesXml() {
|
|
1005
1606
|
if (this.notes.length === 0) return "";
|
|
1006
1607
|
return "<w:p><w:pPr><w:pBdr><w:bottom w:val=\"single\" w:sz=\"6\" w:space=\"1\" w:color=\"auto\"/></w:pBdr></w:pPr></w:p>" + this.notes.map((note, index) => `<w:p>${this.textRun(`${index + 1}. `, "")}${this.inline(note)}</w:p>`).join("");
|
|
1007
1608
|
}
|
|
1008
1609
|
numberingXml() {
|
|
1009
|
-
const
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
}
|
|
1016
|
-
return `${DECLARATION}<w:numbering xmlns:w="${W}"
|
|
1610
|
+
const abstract = (id, format, text) => `<w:abstractNum w:abstractNumId="${id}"><w:multiLevelType w:val="hybridMultilevel"/>${Array.from({ length: 9 }, (_, level) => `<w:lvl w:ilvl="${level}"><w:start w:val="1"/><w:numFmt w:val="${format}"/><w:lvlText w:val="${text(level)}"/><w:lvlJc w:val="left"/><w:pPr><w:ind w:left="${720 * (level + 1)}" w:hanging="360"/></w:pPr></w:lvl>`).join("")}</w:abstractNum>`;
|
|
1611
|
+
const numbered = (level) => `%${level + 1}.`;
|
|
1612
|
+
let abstracts = abstract(0, "bullet", (level) => DEFAULT_BULLETS[level % 3] ?? "") + abstract(1, "decimal", numbered);
|
|
1613
|
+
for (const [key, id] of this.abstracts) {
|
|
1614
|
+
const value = key.slice(1);
|
|
1615
|
+
abstracts += key.startsWith("o") ? abstract(id, ORDERED_FORMATS[value] ?? "decimal", numbered) : abstract(id, "bullet", () => BULLET_TEXTS[value] ?? "");
|
|
1616
|
+
}
|
|
1617
|
+
return `${DECLARATION}<w:numbering xmlns:w="${W}">${abstracts}${this.numbering.join("")}</w:numbering>`;
|
|
1618
|
+
}
|
|
1619
|
+
abstractFor(node, ordered) {
|
|
1620
|
+
const value = String(ordered ? node.attrs.type : node.attrs.bulletStyle);
|
|
1621
|
+
if (!Object.hasOwn(ordered ? ORDERED_FORMATS : BULLET_TEXTS, value)) return ordered ? 1 : 0;
|
|
1622
|
+
const key = `${ordered ? "o" : "b"}${value}`;
|
|
1623
|
+
let id = this.abstracts.get(key);
|
|
1624
|
+
if (id === void 0) {
|
|
1625
|
+
id = this.abstracts.size + 2;
|
|
1626
|
+
this.abstracts.set(key, id);
|
|
1627
|
+
}
|
|
1628
|
+
return id;
|
|
1017
1629
|
}
|
|
1018
|
-
newList(ordered, level
|
|
1630
|
+
newList(node, ordered, level) {
|
|
1019
1631
|
this.numCount += 1;
|
|
1020
|
-
const
|
|
1021
|
-
|
|
1632
|
+
const start = Number(node.attrs.start);
|
|
1633
|
+
const override = ordered ? `<w:lvlOverride w:ilvl="${level}"><w:startOverride w:val="${Number.isInteger(start) ? Math.min(9999, Math.max(0, start)) : 1}"/></w:lvlOverride>` : "";
|
|
1634
|
+
this.numbering.push(`<w:num w:numId="${this.numCount}"><w:abstractNumId w:val="${this.abstractFor(node, ordered)}"/>${override}</w:num>`);
|
|
1022
1635
|
return this.numCount;
|
|
1023
1636
|
}
|
|
1024
1637
|
async blocks(parent, level, quote) {
|
|
@@ -1042,7 +1655,7 @@ var Writer = class {
|
|
|
1042
1655
|
case "codeBlock": return node.textContent.split("\n").map((line) => `<w:p><w:pPr><w:pStyle w:val="Code"/></w:pPr>${this.textRun(line, "")}</w:p>`).join("");
|
|
1043
1656
|
case "horizontalRule": return "<w:p><w:pPr><w:pBdr><w:bottom w:val=\"single\" w:sz=\"6\" w:space=\"1\" w:color=\"auto\"/></w:pBdr></w:pPr></w:p>";
|
|
1044
1657
|
case "table": return this.table(node);
|
|
1045
|
-
case "image": return
|
|
1658
|
+
case "image": return this.figure(node);
|
|
1046
1659
|
case "embed": return `<w:p>${this.embed(node)}</w:p>`;
|
|
1047
1660
|
case "mathBlock": return String(node.attrs.latex ?? "").split("\n").map((line) => `<w:p><w:pPr><w:pStyle w:val="Code"/></w:pPr>${this.textRun(line, "")}</w:p>`).join("");
|
|
1048
1661
|
default: return node.isTextblock ? this.paragraph(node, null) : node.childCount > 0 ? this.blocks(node, level, quote) : "";
|
|
@@ -1051,9 +1664,16 @@ var Writer = class {
|
|
|
1051
1664
|
paragraphProperties(node, style, list) {
|
|
1052
1665
|
let properties = style ? `<w:pStyle w:val="${style}"/>` : "";
|
|
1053
1666
|
if (list) properties += `<w:numPr><w:ilvl w:val="${list[1]}"/><w:numId w:val="${list[0]}"/></w:numPr>`;
|
|
1667
|
+
const lineHeight = String(node.attrs.lineHeight);
|
|
1668
|
+
const line = Object.hasOwn(LINE_TWIPS, lineHeight) ? LINE_TWIPS[lineHeight] : void 0;
|
|
1669
|
+
if (line) properties += `<w:spacing w:line="${line}" w:lineRule="auto"/>`;
|
|
1670
|
+
const indent = node.attrs.indent;
|
|
1671
|
+
if (!list && Number.isInteger(indent) && indent >= 1) properties += `<w:ind w:left="${Math.min(MAX_INDENT, indent) * INDENT_TWIPS}"/>`;
|
|
1054
1672
|
const textAlign = String(node.attrs.textAlign);
|
|
1055
|
-
const align = Object.hasOwn(ALIGNMENTS, textAlign) ? ALIGNMENTS[textAlign] :
|
|
1673
|
+
const align = Object.hasOwn(ALIGNMENTS, textAlign) ? ALIGNMENTS[textAlign] : this.cellAlign;
|
|
1056
1674
|
if (align) properties += `<w:jc w:val="${align}"/>`;
|
|
1675
|
+
const change = this.breaks.get(node);
|
|
1676
|
+
if (change) properties += `<w:rPr>${this.revision(change.kind === "insert" ? "ins" : "del", change)}</w:rPr>`;
|
|
1057
1677
|
return properties ? `<w:pPr>${properties}</w:pPr>` : "";
|
|
1058
1678
|
}
|
|
1059
1679
|
paragraph(node, style, list, prefix = "") {
|
|
@@ -1062,7 +1682,7 @@ var Writer = class {
|
|
|
1062
1682
|
async list(node, level) {
|
|
1063
1683
|
const ordered = node.type.name === "orderedList";
|
|
1064
1684
|
const depth = Math.min(MAX_LEVEL, level);
|
|
1065
|
-
const numId = this.newList(ordered, depth
|
|
1685
|
+
const numId = this.newList(node, ordered, depth);
|
|
1066
1686
|
let xml = "";
|
|
1067
1687
|
for (let index = 0; index < node.childCount; index++) {
|
|
1068
1688
|
const item = node.child(index);
|
|
@@ -1084,36 +1704,51 @@ var Writer = class {
|
|
|
1084
1704
|
let link = null;
|
|
1085
1705
|
let linkRuns = "";
|
|
1086
1706
|
const flush = () => {
|
|
1087
|
-
if (link) xml += `<w:hyperlink r:id="${link.id}">${linkRuns}</w:hyperlink>`;
|
|
1707
|
+
if (link) xml += `<w:hyperlink r:id="${link.id}"${link.blank ? " w:tgtFrame=\"_blank\"" : ""}>${linkRuns}</w:hyperlink>`;
|
|
1088
1708
|
link = null;
|
|
1089
1709
|
linkRuns = "";
|
|
1090
1710
|
};
|
|
1091
1711
|
node.forEach((child) => {
|
|
1092
1712
|
const linkMark = child.marks.find((mark) => mark.type.name === "link");
|
|
1093
1713
|
const href = linkMark ? cleanLink(String(linkMark.attrs.href ?? "")) : null;
|
|
1094
|
-
|
|
1714
|
+
const blank = linkMark?.attrs.target === "_blank";
|
|
1715
|
+
const key = href ? `${blank ? "blank" : "self"} ${href}` : null;
|
|
1716
|
+
if ((link?.key ?? null) !== key) {
|
|
1095
1717
|
flush();
|
|
1096
|
-
if (href) link = {
|
|
1097
|
-
|
|
1098
|
-
id: this.relationship("hyperlink", href, true)
|
|
1718
|
+
if (href && key) link = {
|
|
1719
|
+
key,
|
|
1720
|
+
id: this.relationship("hyperlink", href, true),
|
|
1721
|
+
blank
|
|
1099
1722
|
};
|
|
1100
1723
|
}
|
|
1101
1724
|
const properties = this.runProperties(child.marks, href !== null);
|
|
1102
|
-
const
|
|
1725
|
+
const comments = this.commentIdsOf(child);
|
|
1726
|
+
let run = comments.filter((id) => !this.openComments.has(id)).map((id) => this.startComment(id)).join("");
|
|
1727
|
+
const insertion = child.marks.find((mark) => mark.type.name === "insertion");
|
|
1728
|
+
const deletion = child.marks.find((mark) => mark.type.name === "deletion");
|
|
1729
|
+
let content = child.isText ? this.textRun(child.text ?? "", properties, !!deletion) : this.inlineNode(child, properties, !!deletion);
|
|
1730
|
+
if (deletion && content) content = this.revision("del", deletion.attrs, content);
|
|
1731
|
+
if (insertion && content) content = this.revision("ins", insertion.attrs, content);
|
|
1732
|
+
run += content;
|
|
1733
|
+
for (const id of comments) {
|
|
1734
|
+
const left = (this.remaining.get(id) ?? 1) - 1;
|
|
1735
|
+
this.remaining.set(id, left);
|
|
1736
|
+
if (left <= 0 && this.openComments.has(id)) run += this.endComment(id);
|
|
1737
|
+
}
|
|
1103
1738
|
if (link) linkRuns += run;
|
|
1104
1739
|
else xml += run;
|
|
1105
1740
|
});
|
|
1106
1741
|
flush();
|
|
1107
1742
|
return xml;
|
|
1108
1743
|
}
|
|
1109
|
-
inlineNode(node, properties) {
|
|
1744
|
+
inlineNode(node, properties, deleted) {
|
|
1110
1745
|
switch (node.type.name) {
|
|
1111
1746
|
case "hardBreak": return "<w:r><w:br/></w:r>";
|
|
1112
|
-
case "mention": return this.textRun(String(node.type.spec.leafText?.(node) ?? ""), properties);
|
|
1113
|
-
case "mathInline": return this.textRun(String(node.attrs.latex ?? ""), "<w:rStyle w:val=\"CodeChar\"/>");
|
|
1747
|
+
case "mention": return this.textRun(String(node.type.spec.leafText?.(node) ?? ""), properties, deleted);
|
|
1748
|
+
case "mathInline": return this.textRun(String(node.attrs.latex ?? ""), "<w:rStyle w:val=\"CodeChar\"/>", deleted);
|
|
1114
1749
|
case "footnote":
|
|
1115
1750
|
this.notes.push(node);
|
|
1116
|
-
return this.textRun(String(this.notes.length), "<w:vertAlign w:val=\"superscript\"/>");
|
|
1751
|
+
return this.textRun(String(this.notes.length), "<w:vertAlign w:val=\"superscript\"/>", deleted);
|
|
1117
1752
|
default: return "";
|
|
1118
1753
|
}
|
|
1119
1754
|
}
|
|
@@ -1121,18 +1756,25 @@ var Writer = class {
|
|
|
1121
1756
|
let properties = linked ? "<w:rStyle w:val=\"Hyperlink\"/>" : "";
|
|
1122
1757
|
const names = new Set(marks.map((mark) => mark.type.name));
|
|
1123
1758
|
if (names.has("code") && !linked) properties += "<w:rStyle w:val=\"CodeChar\"/>";
|
|
1759
|
+
const font = wordFont(marks.find((mark) => mark.type.name === "fontFamily")?.attrs.family);
|
|
1760
|
+
if (font) properties += `<w:rFonts w:ascii="${font}" w:hAnsi="${font}" w:cs="${font}"/>`;
|
|
1124
1761
|
if (names.has("bold")) properties += "<w:b/>";
|
|
1125
1762
|
if (names.has("italic")) properties += "<w:i/>";
|
|
1126
1763
|
if (names.has("strike")) properties += "<w:strike/>";
|
|
1127
1764
|
const color = hexColor(marks.find((mark) => mark.type.name === "textColor")?.attrs.color);
|
|
1128
1765
|
if (color) properties += `<w:color w:val="${color}"/>`;
|
|
1766
|
+
const size = halfPoints(marks.find((mark) => mark.type.name === "fontSize")?.attrs.size);
|
|
1767
|
+
if (size !== null) properties += `<w:sz w:val="${size}"/><w:szCs w:val="${size}"/>`;
|
|
1129
1768
|
if (names.has("underline")) properties += "<w:u w:val=\"single\"/>";
|
|
1130
1769
|
const fill = hexColor(marks.find((mark) => mark.type.name === "highlight")?.attrs.color);
|
|
1131
1770
|
if (names.has("highlight")) properties += `<w:shd w:val="clear" w:color="auto" w:fill="${fill ?? "FFFF00"}"/>`;
|
|
1771
|
+
if (names.has("superscript")) properties += "<w:vertAlign w:val=\"superscript\"/>";
|
|
1772
|
+
else if (names.has("subscript")) properties += "<w:vertAlign w:val=\"subscript\"/>";
|
|
1132
1773
|
return properties;
|
|
1133
1774
|
}
|
|
1134
|
-
textRun(text, properties) {
|
|
1135
|
-
const
|
|
1775
|
+
textRun(text, properties, deleted = false) {
|
|
1776
|
+
const tag = deleted ? "w:delText" : "w:t";
|
|
1777
|
+
const body = text.split(" ").map((part) => part ? `<${tag} xml:space="preserve">${escapeXml(part)}</${tag}>` : "").join("<w:tab/>");
|
|
1136
1778
|
return body ? `<w:r>${properties ? `<w:rPr>${properties}</w:rPr>` : ""}${body}</w:r>` : "";
|
|
1137
1779
|
}
|
|
1138
1780
|
linkParagraphContent(href, text) {
|
|
@@ -1207,7 +1849,18 @@ var Writer = class {
|
|
|
1207
1849
|
const cx = image.width * EMU_PER_PIXEL;
|
|
1208
1850
|
const cy = image.height * EMU_PER_PIXEL;
|
|
1209
1851
|
const number = this.drawingCount;
|
|
1210
|
-
|
|
1852
|
+
const href = typeof node.attrs.href === "string" ? cleanLink(node.attrs.href) : null;
|
|
1853
|
+
const run = `<w:r><w:drawing><wp:inline distT="0" distB="0" distL="0" distR="0"><wp:extent cx="${cx}" cy="${cy}"/><wp:docPr id="${number}" name="Picture ${number}" descr="${escapeXml(alt)}"/><a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:nvPicPr><pic:cNvPr id="${number}" name="${name}" descr="${escapeXml(alt)}"/><pic:cNvPicPr/></pic:nvPicPr><pic:blipFill><a:blip r:embed="${id}"/><a:stretch><a:fillRect/></a:stretch></pic:blipFill><pic:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="${cx}" cy="${cy}"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></pic:spPr></pic:pic></a:graphicData></a:graphic></wp:inline></w:drawing></w:r>`;
|
|
1854
|
+
if (!href) return run;
|
|
1855
|
+
return `<w:hyperlink r:id="${this.relationship("hyperlink", href, true)}">${run}</w:hyperlink>`;
|
|
1856
|
+
}
|
|
1857
|
+
async figure(node) {
|
|
1858
|
+
const align = node.attrs.align;
|
|
1859
|
+
const justify = align === "center" || align === "right" ? `<w:jc w:val="${align}"/>` : "";
|
|
1860
|
+
const image = `<w:p>${justify && `<w:pPr>${justify}</w:pPr>`}${await this.image(node)}</w:p>`;
|
|
1861
|
+
const caption = this.textRun(String(node.attrs.caption ?? ""), "");
|
|
1862
|
+
if (!caption) return image;
|
|
1863
|
+
return `${image}<w:p><w:pPr><w:pStyle w:val="Caption"/>${justify}</w:pPr>${caption}</w:p>`;
|
|
1211
1864
|
}
|
|
1212
1865
|
async table(node) {
|
|
1213
1866
|
const rows = [];
|
|
@@ -1223,7 +1876,9 @@ var Writer = class {
|
|
|
1223
1876
|
});
|
|
1224
1877
|
let rowXml = "";
|
|
1225
1878
|
let gridColumns = 0;
|
|
1226
|
-
|
|
1879
|
+
const outerAlign = this.cellAlign;
|
|
1880
|
+
const striped = node.attrs.striped === true;
|
|
1881
|
+
for (const [rowIndex, row] of rows.entries()) {
|
|
1227
1882
|
let column = 0;
|
|
1228
1883
|
let cellIndex = 0;
|
|
1229
1884
|
let cells = "";
|
|
@@ -1255,37 +1910,63 @@ var Writer = class {
|
|
|
1255
1910
|
span
|
|
1256
1911
|
});
|
|
1257
1912
|
}
|
|
1913
|
+
const shade = hexColor(cell.attrs.background);
|
|
1914
|
+
const stripe = striped && rowIndex % 2 === 1 && cell.type.name === "tableCell" ? STRIPE_FILL : null;
|
|
1915
|
+
const fill = shade ?? stripe;
|
|
1916
|
+
if (fill) properties += `<w:shd w:val="clear" w:color="auto" w:fill="${fill}"/>`;
|
|
1917
|
+
const vertical = cell.attrs.verticalAlign;
|
|
1918
|
+
if (vertical === "middle" || vertical === "bottom") properties += `<w:vAlign w:val="${vertical === "middle" ? "center" : vertical}"/>`;
|
|
1919
|
+
const cellAlign = String(cell.attrs.textAlign);
|
|
1920
|
+
this.cellAlign = Object.hasOwn(ALIGNMENTS, cellAlign) ? ALIGNMENTS[cellAlign] ?? null : null;
|
|
1258
1921
|
let content = await this.blocks(cell, 0, false);
|
|
1922
|
+
this.cellAlign = outerAlign;
|
|
1259
1923
|
if (!content.endsWith("</w:p>")) content += "<w:p/>";
|
|
1260
1924
|
cells += `<w:tc>${properties ? `<w:tcPr>${properties}</w:tcPr>` : ""}${content}</w:tc>`;
|
|
1261
1925
|
column += span;
|
|
1262
1926
|
}
|
|
1263
1927
|
while (pending.has(column)) continuation();
|
|
1264
1928
|
gridColumns = Math.max(gridColumns, column);
|
|
1265
|
-
|
|
1929
|
+
const rowHeight = positiveInt(row.attrs.height);
|
|
1930
|
+
const rowProperties = `${rowHeight ? `<w:trHeight w:val="${Math.min(MAX_ROW_HEIGHT, rowHeight) * TWIPS_PER_PIXEL}" w:hRule="atLeast"/>` : ""}${header ? "<w:tblHeader/>" : ""}`;
|
|
1931
|
+
rowXml += `<w:tr>${rowProperties ? `<w:trPr>${rowProperties}</w:trPr>` : ""}${cells}</w:tr>`;
|
|
1266
1932
|
}
|
|
1267
1933
|
gridColumns = Math.max(1, gridColumns);
|
|
1268
1934
|
const fallback = Math.round(PAGE_WIDTH_TWIPS / gridColumns);
|
|
1269
|
-
|
|
1935
|
+
const grid = Array.from({ length: gridColumns }, (_, index) => {
|
|
1270
1936
|
const width = firstWidths[index];
|
|
1271
1937
|
return `<w:gridCol w:w="${width ? width * TWIPS_PER_PIXEL : fallback}"/>`;
|
|
1272
|
-
}).join("")
|
|
1938
|
+
}).join("");
|
|
1939
|
+
return `<w:tbl><w:tblPr><w:tblStyle w:val="TableGrid"/>${node.attrs.width === "full" ? "<w:tblW w:w=\"5000\" w:type=\"pct\"/>" : "<w:tblW w:w=\"0\" w:type=\"auto\"/>"}${node.attrs.align === "center" ? "<w:jc w:val=\"center\"/>" : ""}${tableBorders(node.attrs.border, hexColor(node.attrs.borderColor))}<w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/></w:tblPr><w:tblGrid>${grid}</w:tblGrid>${rowXml}</w:tbl>`;
|
|
1273
1940
|
}
|
|
1274
1941
|
};
|
|
1942
|
+
function cleanAuthor(name) {
|
|
1943
|
+
let author = "";
|
|
1944
|
+
for (const character of String(name ?? "")) {
|
|
1945
|
+
if (author.length >= 100) break;
|
|
1946
|
+
const code = character.codePointAt(0) ?? 0;
|
|
1947
|
+
author += code < 32 || code === 127 ? " " : character;
|
|
1948
|
+
}
|
|
1949
|
+
return author.trim() || "viveEditor";
|
|
1950
|
+
}
|
|
1951
|
+
function initials(author) {
|
|
1952
|
+
return author.split(/\s+/).filter(Boolean).slice(0, 2).map((word) => [...word][0]?.toLocaleUpperCase() ?? "").join("");
|
|
1953
|
+
}
|
|
1275
1954
|
function stylesXml() {
|
|
1276
1955
|
const heading = (level, size) => `<w:style w:type="paragraph" w:styleId="Heading${level}"><w:name w:val="heading ${level}"/><w:basedOn w:val="Normal"/><w:next w:val="Normal"/><w:qFormat/><w:pPr><w:keepNext/><w:spacing w:before="240" w:after="80"/><w:outlineLvl w:val="${level - 1}"/></w:pPr><w:rPr><w:b/><w:sz w:val="${size}"/></w:rPr></w:style>`;
|
|
1277
|
-
return `${DECLARATION}<w:styles xmlns:w="${W}"><w:docDefaults><w:rPrDefault><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri" w:eastAsia="Calibri" w:cs="Calibri"/><w:sz w:val="22"/><w:lang w:val="en-US"/></w:rPr></w:rPrDefault><w:pPrDefault><w:pPr><w:spacing w:after="120" w:line="276" w:lineRule="auto"/></w:pPr></w:pPrDefault></w:docDefaults><w:style w:type="paragraph" w:default="1" w:styleId="Normal"><w:name w:val="Normal"/><w:qFormat/></w:style><w:style w:type="paragraph" w:styleId="Title"><w:name w:val="Title"/><w:basedOn w:val="Normal"/><w:qFormat/><w:rPr><w:sz w:val="56"/></w:rPr></w:style>${heading(1, 40)}${heading(2, 32)}${heading(3, 28)}${heading(4, 24)}${heading(5, 22)}${heading(6, 22)}<w:style w:type="paragraph" w:styleId="Quote"><w:name w:val="Quote"/><w:basedOn w:val="Normal"/><w:qFormat/><w:pPr><w:pBdr><w:left w:val="single" w:sz="18" w:space="8" w:color="BFBFBF"/></w:pBdr><w:ind w:left="567"/></w:pPr><w:rPr><w:i/><w:color w:val="404040"/></w:rPr></w:style><w:style w:type="paragraph" w:styleId="Code"><w:name w:val="Code"/><w:basedOn w:val="Normal"/><w:pPr><w:shd w:val="clear" w:color="auto" w:fill="F2F2F2"/><w:spacing w:after="0" w:line="240" w:lineRule="auto"/></w:pPr><w:rPr><w:rFonts w:ascii="Consolas" w:hAnsi="Consolas" w:cs="Consolas"/><w:sz w:val="20"/></w:rPr></w:style><w:style w:type="paragraph" w:styleId="ListParagraph"><w:name w:val="List Paragraph"/><w:basedOn w:val="Normal"/><w:qFormat/><w:pPr><w:ind w:left="720"/></w:pPr></w:style><w:style w:type="character" w:default="1" w:styleId="DefaultParagraphFont"><w:name w:val="Default Paragraph Font"/><w:uiPriority w:val="1"/><w:semiHidden/></w:style><w:style w:type="character" w:styleId="Hyperlink"><w:name w:val="Hyperlink"/><w:basedOn w:val="DefaultParagraphFont"/><w:rPr><w:color w:val="0563C1"/><w:u w:val="single"/></w:rPr></w:style><w:style w:type="character" w:styleId="CodeChar"><w:name w:val="Code Char"/><w:basedOn w:val="DefaultParagraphFont"/><w:rPr><w:rFonts w:ascii="Consolas" w:hAnsi="Consolas" w:cs="Consolas"/><w:shd w:val="clear" w:color="auto" w:fill="F2F2F2"/></w:rPr></w:style><w:style w:type="table" w:default="1" w:styleId="TableNormal"><w:name w:val="Normal Table"/><w:tblPr><w:tblInd w:w="0" w:type="dxa"/><w:tblCellMar><w:left w:w="108" w:type="dxa"/><w:right w:w="108" w:type="dxa"/></w:tblCellMar></w:tblPr></w:style><w:style w:type="table" w:styleId="TableGrid"><w:name w:val="Table Grid"/><w:basedOn w:val="TableNormal"/><w:pPr><w:spacing w:after="0"/></w:pPr><w:tblPr><w:tblBorders><w:top w:val="single" w:sz="4" w:space="0" w:color="auto"/><w:left w:val="single" w:sz="4" w:space="0" w:color="auto"/><w:bottom w:val="single" w:sz="4" w:space="0" w:color="auto"/><w:right w:val="single" w:sz="4" w:space="0" w:color="auto"/><w:insideH w:val="single" w:sz="4" w:space="0" w:color="auto"/><w:insideV w:val="single" w:sz="4" w:space="0" w:color="auto"/></w:tblBorders></w:tblPr></w:style></w:styles>`;
|
|
1956
|
+
return `${DECLARATION}<w:styles xmlns:w="${W}"><w:docDefaults><w:rPrDefault><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri" w:eastAsia="Calibri" w:cs="Calibri"/><w:sz w:val="22"/><w:lang w:val="en-US"/></w:rPr></w:rPrDefault><w:pPrDefault><w:pPr><w:spacing w:after="120" w:line="276" w:lineRule="auto"/></w:pPr></w:pPrDefault></w:docDefaults><w:style w:type="paragraph" w:default="1" w:styleId="Normal"><w:name w:val="Normal"/><w:qFormat/></w:style><w:style w:type="paragraph" w:styleId="Title"><w:name w:val="Title"/><w:basedOn w:val="Normal"/><w:qFormat/><w:rPr><w:sz w:val="56"/></w:rPr></w:style>${heading(1, 40)}${heading(2, 32)}${heading(3, 28)}${heading(4, 24)}${heading(5, 22)}${heading(6, 22)}<w:style w:type="paragraph" w:styleId="Quote"><w:name w:val="Quote"/><w:basedOn w:val="Normal"/><w:qFormat/><w:pPr><w:pBdr><w:left w:val="single" w:sz="18" w:space="8" w:color="BFBFBF"/></w:pBdr><w:ind w:left="567"/></w:pPr><w:rPr><w:i/><w:color w:val="404040"/></w:rPr></w:style><w:style w:type="paragraph" w:styleId="Code"><w:name w:val="Code"/><w:basedOn w:val="Normal"/><w:pPr><w:shd w:val="clear" w:color="auto" w:fill="F2F2F2"/><w:spacing w:after="0" w:line="240" w:lineRule="auto"/></w:pPr><w:rPr><w:rFonts w:ascii="Consolas" w:hAnsi="Consolas" w:cs="Consolas"/><w:sz w:val="20"/></w:rPr></w:style><w:style w:type="paragraph" w:styleId="Caption"><w:name w:val="caption"/><w:basedOn w:val="Normal"/><w:rPr><w:i/><w:sz w:val="18"/></w:rPr></w:style><w:style w:type="paragraph" w:styleId="ListParagraph"><w:name w:val="List Paragraph"/><w:basedOn w:val="Normal"/><w:qFormat/><w:pPr><w:ind w:left="720"/></w:pPr></w:style><w:style w:type="character" w:default="1" w:styleId="DefaultParagraphFont"><w:name w:val="Default Paragraph Font"/><w:uiPriority w:val="1"/><w:semiHidden/></w:style><w:style w:type="character" w:styleId="Hyperlink"><w:name w:val="Hyperlink"/><w:basedOn w:val="DefaultParagraphFont"/><w:rPr><w:color w:val="0563C1"/><w:u w:val="single"/></w:rPr></w:style><w:style w:type="character" w:styleId="CodeChar"><w:name w:val="Code Char"/><w:basedOn w:val="DefaultParagraphFont"/><w:rPr><w:rFonts w:ascii="Consolas" w:hAnsi="Consolas" w:cs="Consolas"/><w:shd w:val="clear" w:color="auto" w:fill="F2F2F2"/></w:rPr></w:style><w:style w:type="table" w:default="1" w:styleId="TableNormal"><w:name w:val="Normal Table"/><w:tblPr><w:tblInd w:w="0" w:type="dxa"/><w:tblCellMar><w:left w:w="108" w:type="dxa"/><w:right w:w="108" w:type="dxa"/></w:tblCellMar></w:tblPr></w:style><w:style w:type="table" w:styleId="TableGrid"><w:name w:val="Table Grid"/><w:basedOn w:val="TableNormal"/><w:pPr><w:spacing w:after="0"/></w:pPr><w:tblPr><w:tblBorders><w:top w:val="single" w:sz="4" w:space="0" w:color="auto"/><w:left w:val="single" w:sz="4" w:space="0" w:color="auto"/><w:bottom w:val="single" w:sz="4" w:space="0" w:color="auto"/><w:right w:val="single" w:sz="4" w:space="0" w:color="auto"/><w:insideH w:val="single" w:sz="4" w:space="0" w:color="auto"/><w:insideV w:val="single" w:sz="4" w:space="0" w:color="auto"/></w:tblBorders></w:tblPr></w:style></w:styles>`;
|
|
1278
1957
|
}
|
|
1279
1958
|
const encoder = new TextEncoder();
|
|
1280
1959
|
async function exportDocx(source, options = {}) {
|
|
1281
|
-
const doc = source.schema.nodeFromJSON(source.getJSON());
|
|
1960
|
+
const doc = source.schema.nodeFromJSON(repairContent(source.schema, source.getJSON()));
|
|
1282
1961
|
doc.check();
|
|
1283
1962
|
const writer = new Writer(options, cleanLimits(options.limits));
|
|
1284
|
-
|
|
1963
|
+
writer.countComments(doc);
|
|
1964
|
+
writer.countBreaks(doc);
|
|
1965
|
+
const body = await writer.blocks(doc, 0, false) + writer.closeComments() + writer.notesXml();
|
|
1285
1966
|
options.signal?.throwIfAborted();
|
|
1286
1967
|
const title = escapeXml((options.title ?? "").trim().slice(0, 255));
|
|
1287
1968
|
const files = {
|
|
1288
|
-
"[Content_Types].xml": encoder.encode(`${DECLARATION}<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Default Extension="png" ContentType="image/png"/><Default Extension="jpeg" ContentType="image/jpeg"/><Default Extension="gif" ContentType="image/gif"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/><Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/><Override PartName="/word/numbering.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml"/><Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"
|
|
1969
|
+
"[Content_Types].xml": encoder.encode(`${DECLARATION}<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Default Extension="png" ContentType="image/png"/><Default Extension="jpeg" ContentType="image/jpeg"/><Default Extension="gif" ContentType="image/gif"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/><Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/><Override PartName="/word/numbering.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml"/><Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>${writer.hasComments ? "<Override PartName=\"/word/comments.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml\"/><Override PartName=\"/word/commentsExtended.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.wordprocessingml.commentsExtended+xml\"/>" : ""}</Types>`),
|
|
1289
1970
|
"_rels/.rels": encoder.encode(`${DECLARATION}<Relationships xmlns="${PACKAGE_REL}"><Relationship Id="rId1" Type="${R}/officeDocument" Target="word/document.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/></Relationships>`),
|
|
1290
1971
|
"docProps/core.xml": encoder.encode(`${DECLARATION}<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/">${title ? `<dc:title>${title}</dc:title>` : ""}</cp:coreProperties>`),
|
|
1291
1972
|
"word/document.xml": encoder.encode(`${DECLARATION}<w:document xmlns:w="${W}" xmlns:r="${R}" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"><w:body>${body}<w:sectPr><w:pgSz w:w="11906" w:h="16838"/><w:pgMar w:top="1440" w:right="1273" w:bottom="1440" w:left="1273" w:header="708" w:footer="708" w:gutter="0"/></w:sectPr></w:body></w:document>`),
|
|
@@ -1294,6 +1975,10 @@ async function exportDocx(source, options = {}) {
|
|
|
1294
1975
|
"word/_rels/document.xml.rels": encoder.encode(writer.relationshipsXml()),
|
|
1295
1976
|
...writer.media
|
|
1296
1977
|
};
|
|
1978
|
+
if (writer.hasComments) {
|
|
1979
|
+
files["word/comments.xml"] = encoder.encode(writer.commentsXml());
|
|
1980
|
+
files["word/commentsExtended.xml"] = encoder.encode(writer.commentsExtendedXml());
|
|
1981
|
+
}
|
|
1297
1982
|
return new Blob([zipSync(files)], { type: DOCX_MIME });
|
|
1298
1983
|
}
|
|
1299
1984
|
export { DEFAULT_IMPORT_LIMITS, DOCX_MIME, DocxError, exportDocx, importDocx };
|