@folijs/docx 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/LICENSE +21 -0
- package/README.md +141 -2
- package/dist/export/content.d.ts +17 -0
- package/dist/export/content.d.ts.map +1 -0
- package/dist/export/content.js +165 -0
- package/dist/export/content.js.map +1 -0
- package/dist/export/index.d.ts +14 -0
- package/dist/export/index.d.ts.map +1 -0
- package/dist/export/index.js +352 -0
- package/dist/export/index.js.map +1 -0
- package/dist/export/parts.d.ts +24 -0
- package/dist/export/parts.d.ts.map +1 -0
- package/dist/export/parts.js +413 -0
- package/dist/export/parts.js.map +1 -0
- package/dist/export/properties.d.ts +39 -0
- package/dist/export/properties.d.ts.map +1 -0
- package/dist/export/properties.js +506 -0
- package/dist/export/properties.js.map +1 -0
- package/dist/export/xmlout.d.ts +63 -0
- package/dist/export/xmlout.d.ts.map +1 -0
- package/dist/export/xmlout.js +194 -0
- package/dist/export/xmlout.js.map +1 -0
- package/dist/import/content.d.ts +35 -0
- package/dist/import/content.d.ts.map +1 -0
- package/dist/import/content.js +370 -0
- package/dist/import/content.js.map +1 -0
- package/dist/import/index.d.ts +26 -0
- package/dist/import/index.d.ts.map +1 -0
- package/dist/import/index.js +96 -0
- package/dist/import/index.js.map +1 -0
- package/dist/import/ooxml.d.ts +38 -0
- package/dist/import/ooxml.d.ts.map +1 -0
- package/dist/import/ooxml.js +70 -0
- package/dist/import/ooxml.js.map +1 -0
- package/dist/import/parts.d.ts +35 -0
- package/dist/import/parts.d.ts.map +1 -0
- package/dist/import/parts.js +365 -0
- package/dist/import/parts.js.map +1 -0
- package/dist/import/properties.d.ts +50 -0
- package/dist/import/properties.d.ts.map +1 -0
- package/dist/import/properties.js +403 -0
- package/dist/import/properties.js.map +1 -0
- package/dist/import/report.d.ts +28 -0
- package/dist/import/report.d.ts.map +1 -0
- package/dist/import/report.js +42 -0
- package/dist/import/report.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/package.d.ts +70 -0
- package/dist/package.d.ts.map +1 -0
- package/dist/package.js +259 -0
- package/dist/package.js.map +1 -0
- package/dist/xml.d.ts +79 -0
- package/dist/xml.d.ts.map +1 -0
- package/dist/xml.js +384 -0
- package/dist/xml.js.map +1 -0
- package/dist/zip.d.ts +66 -0
- package/dist/zip.d.ts.map +1 -0
- package/dist/zip.js +356 -0
- package/dist/zip.js.map +1 -0
- package/package.json +36 -3
- package/src/export/content.ts +229 -0
- package/src/export/index.ts +445 -0
- package/src/export/parts.ts +545 -0
- package/src/export/properties.ts +608 -0
- package/src/export/xmlout.ts +225 -0
- package/src/import/content.ts +413 -0
- package/src/import/index.ts +126 -0
- package/src/import/ooxml.ts +77 -0
- package/src/import/parts.ts +423 -0
- package/src/import/properties.ts +504 -0
- package/src/import/report.ts +64 -0
- package/src/index.ts +24 -0
- package/src/package.ts +328 -0
- package/src/xml.ts +443 -0
- package/src/zip.ts +401 -0
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema v3 to `.docx` (ADR-0005): the parts the editor owns are written from the document
|
|
3
|
+
* (`document.xml`, `styles.xml`, `numbering.xml`, `comments.xml`, `commentsExtended.xml` and
|
|
4
|
+
* the relationships new hyperlinks need); every other part is copied from the original package
|
|
5
|
+
* byte for byte. Without a package, a new one is made.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { type FoliDocument, parseDocument } from "@folijs/core";
|
|
9
|
+
import { child, REL } from "../import/ooxml.js";
|
|
10
|
+
import { DocxError, type DocxPackage, type NewPart } from "../package.js";
|
|
11
|
+
import { deflateEntry, writeZip } from "../zip.js";
|
|
12
|
+
import { blocksXml, type WriteContext } from "./content.js";
|
|
13
|
+
import {
|
|
14
|
+
commentsExtendedXml,
|
|
15
|
+
commentsXml,
|
|
16
|
+
numberingXml,
|
|
17
|
+
type OriginalPart,
|
|
18
|
+
stylesXml,
|
|
19
|
+
} from "./parts.js";
|
|
20
|
+
import { sectionXml } from "./properties.js";
|
|
21
|
+
import {
|
|
22
|
+
DEFAULT_NAMESPACES,
|
|
23
|
+
escapeAttribute,
|
|
24
|
+
type Namespaces,
|
|
25
|
+
NEW_ROOT_ATTRIBUTES,
|
|
26
|
+
namespacesOf,
|
|
27
|
+
} from "./xmlout.js";
|
|
28
|
+
|
|
29
|
+
const CT = {
|
|
30
|
+
main: "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
|
|
31
|
+
styles: "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml",
|
|
32
|
+
numbering: "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml",
|
|
33
|
+
comments: "application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml",
|
|
34
|
+
commentsExtended:
|
|
35
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.commentsExtended+xml",
|
|
36
|
+
rels: "application/vnd.openxmlformats-package.relationships+xml",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const encoder = new TextEncoder();
|
|
40
|
+
|
|
41
|
+
/** New relationships of a source part (hyperlink targets), allocated as content is written. */
|
|
42
|
+
class Relationships {
|
|
43
|
+
readonly added: { id: string; type: string; target: string; external: boolean }[] = [];
|
|
44
|
+
readonly #ids: Set<string>;
|
|
45
|
+
readonly #byTarget = new Map<string, string>();
|
|
46
|
+
readonly #existing: ReadonlyMap<string, { type: string; target: string; external: boolean }>;
|
|
47
|
+
|
|
48
|
+
constructor(
|
|
49
|
+
existing: readonly { id: string; type: string; target: string; external: boolean }[],
|
|
50
|
+
) {
|
|
51
|
+
this.#existing = new Map(existing.map((r) => [r.id, r]));
|
|
52
|
+
this.#ids = new Set(existing.map((r) => r.id));
|
|
53
|
+
for (const r of existing)
|
|
54
|
+
if (r.type === REL.hyperlink && r.external) this.#byTarget.set(r.target, r.id);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
hyperlink(href: string, id: string | undefined): string {
|
|
58
|
+
const own = id === undefined ? undefined : this.#existing.get(id);
|
|
59
|
+
if (own && own.type === REL.hyperlink && own.target === href) return id as string;
|
|
60
|
+
const known = this.#byTarget.get(href);
|
|
61
|
+
if (known) return known;
|
|
62
|
+
return this.add(REL.hyperlink, href, true);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
add(type: string, target: string, external: boolean): string {
|
|
66
|
+
let n = this.#ids.size + 1;
|
|
67
|
+
while (this.#ids.has(`rId${n}`)) n++;
|
|
68
|
+
const id = `rId${n}`;
|
|
69
|
+
this.#ids.add(id);
|
|
70
|
+
this.added.push({ id, type, target, external });
|
|
71
|
+
if (type === REL.hyperlink && external) this.#byTarget.set(target, id);
|
|
72
|
+
return id;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The relationships part with the added ones (`original` its text, if it exists). */
|
|
76
|
+
xml(original: string | undefined): string {
|
|
77
|
+
const entries = this.added
|
|
78
|
+
.map(
|
|
79
|
+
(r) =>
|
|
80
|
+
`<Relationship Id="${r.id}" Type="${escapeAttribute(r.type)}" Target="${escapeAttribute(r.target)}"${r.external ? ' TargetMode="External"' : ""}/>`,
|
|
81
|
+
)
|
|
82
|
+
.join("");
|
|
83
|
+
if (original === undefined) {
|
|
84
|
+
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">${entries}</Relationships>`;
|
|
85
|
+
}
|
|
86
|
+
const close = original.lastIndexOf("</Relationships>");
|
|
87
|
+
if (close === -1) return original.replace(/\/>\s*$/, `>${entries}</Relationships>`);
|
|
88
|
+
return original.slice(0, close) + entries + original.slice(close);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function contextFor(namespaces: Namespaces, relationships: Relationships): WriteContext {
|
|
93
|
+
return { namespaces, hyperlink: (href, id) => relationships.hyperlink(href, id) };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** `/word/_rels/document.xml.rels` for `/word/document.xml`. */
|
|
97
|
+
function relationshipsPartName(source: string): string {
|
|
98
|
+
const slash = source.lastIndexOf("/");
|
|
99
|
+
return `${source.slice(0, slash)}/_rels/${source.slice(slash + 1)}.rels`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** A part path relative to the main document's folder (`/word/styles.xml` → `styles.xml`). */
|
|
103
|
+
function relative(from: string, to: string): string {
|
|
104
|
+
const folder = from.slice(0, from.lastIndexOf("/") + 1);
|
|
105
|
+
return to.startsWith(folder) ? to.slice(folder.length) : to;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The document as a `.docx`. With `original` (the package it was imported from), the parts
|
|
110
|
+
* Foli does not edit come back byte for byte and unchanged elements verbatim.
|
|
111
|
+
*/
|
|
112
|
+
export async function exportDocx(
|
|
113
|
+
document: FoliDocument,
|
|
114
|
+
original?: DocxPackage,
|
|
115
|
+
): Promise<Uint8Array> {
|
|
116
|
+
const doc = withThreadIds(parseDocument(document), original);
|
|
117
|
+
return original ? intoPackage(doc, original) : newPackage(doc);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const PARA_ID = "w14:paraId";
|
|
121
|
+
type Json = Record<string, unknown>;
|
|
122
|
+
|
|
123
|
+
/** A comment's last paragraph's `w14:paraId`: what `commentsExtended` keys its thread state by. */
|
|
124
|
+
function paraIdOf(comment: FoliDocument["comments"][number]): string | undefined {
|
|
125
|
+
const last = comment.content.filter((b) => b.type === "paragraph").at(-1);
|
|
126
|
+
return (last?.attrs?.attributes as Record<string, string> | undefined)?.[PARA_ID];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The document with a `w14:paraId` on the last paragraph of each comment that is in a thread
|
|
131
|
+
* (a reply, replied to, or resolved) and has none (a comment made in the editor), so its thread
|
|
132
|
+
* state can be written. Unique, below 0x80000000 as Word requires. Not when the comments part
|
|
133
|
+
* cannot declare it (an existing part without the `w14` namespace).
|
|
134
|
+
*/
|
|
135
|
+
function withThreadIds(doc: FoliDocument, pkg: DocxPackage | undefined): FoliDocument {
|
|
136
|
+
const parents = new Set(doc.comments.map((c) => c.parent).filter((p) => p !== undefined));
|
|
137
|
+
const needs = doc.comments.filter(
|
|
138
|
+
(c) =>
|
|
139
|
+
(c.parent !== undefined || c.done !== undefined || parents.has(c.id)) &&
|
|
140
|
+
!paraIdOf(c) &&
|
|
141
|
+
c.content.some((b) => b.type === "paragraph"),
|
|
142
|
+
);
|
|
143
|
+
if (!needs.length) return doc;
|
|
144
|
+
if (pkg) {
|
|
145
|
+
const name = pkg
|
|
146
|
+
.relationships(pkg.mainDocument)
|
|
147
|
+
.find((r) => r.type === REL.comments && !r.external)?.partName;
|
|
148
|
+
const part = name ? pkg.part(name) : undefined;
|
|
149
|
+
if (
|
|
150
|
+
part &&
|
|
151
|
+
!namespacesOf(pkg.xml(part.name).root, pkg.xml(part.name).source).prefixes.has("w14")
|
|
152
|
+
)
|
|
153
|
+
return doc;
|
|
154
|
+
}
|
|
155
|
+
const used = new Set<string>();
|
|
156
|
+
JSON.stringify(doc, (key, value) => {
|
|
157
|
+
if (key === PARA_ID && typeof value === "string") used.add(value.toUpperCase());
|
|
158
|
+
return value;
|
|
159
|
+
});
|
|
160
|
+
let next = 0x10000000;
|
|
161
|
+
const fresh = () => {
|
|
162
|
+
let id: string;
|
|
163
|
+
do id = (next++).toString(16).toUpperCase().padStart(8, "0");
|
|
164
|
+
while (used.has(id));
|
|
165
|
+
used.add(id);
|
|
166
|
+
return id;
|
|
167
|
+
};
|
|
168
|
+
const copy = structuredClone(doc);
|
|
169
|
+
for (const comment of copy.comments) {
|
|
170
|
+
if (!needs.some((c) => c.id === comment.id) || paraIdOf(comment)) continue;
|
|
171
|
+
const last = comment.content.filter((b) => b.type === "paragraph").at(-1) as { attrs?: Json };
|
|
172
|
+
last.attrs = {
|
|
173
|
+
...(last.attrs ?? {}),
|
|
174
|
+
attributes: { ...((last.attrs?.attributes as Json) ?? {}), [PARA_ID]: fresh() },
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
return copy;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async function intoPackage(doc: FoliDocument, pkg: DocxPackage): Promise<Uint8Array> {
|
|
181
|
+
const mainName = pkg.mainDocument;
|
|
182
|
+
const main = pkg.xml(mainName);
|
|
183
|
+
const body = child(main.root, "body");
|
|
184
|
+
if (!body) throw new DocxError(`${mainName}: no w:body`);
|
|
185
|
+
const namespaces = namespacesOf(main.root, main.source);
|
|
186
|
+
const rels = new Relationships(pkg.relationships(mainName));
|
|
187
|
+
const context = contextFor(namespaces, rels);
|
|
188
|
+
|
|
189
|
+
const content = blocksXml(doc.content.content ?? [], context) + bodySection(doc, namespaces);
|
|
190
|
+
const documentXml =
|
|
191
|
+
body.openEnd === body.end
|
|
192
|
+
? `${main.source.slice(0, body.start)}<${body.name}>${content}</${body.name}>${main.source.slice(body.end)}`
|
|
193
|
+
: main.source.slice(0, body.openEnd) + content + main.source.slice(body.closeStart);
|
|
194
|
+
|
|
195
|
+
const replace = new Map<string, Uint8Array>();
|
|
196
|
+
const add: NewPart[] = [];
|
|
197
|
+
const set = (name: string, text: string) => {
|
|
198
|
+
if (pkg.text(name) !== text) replace.set(name, encoder.encode(utf8Declaration(text)));
|
|
199
|
+
};
|
|
200
|
+
set(mainName, documentXml);
|
|
201
|
+
|
|
202
|
+
/** The main document's related part of `type`, or a new one at `defaultName`. */
|
|
203
|
+
const part = (
|
|
204
|
+
type: string,
|
|
205
|
+
defaultName: string,
|
|
206
|
+
contentType: string,
|
|
207
|
+
write: (o: OriginalPart | undefined, ns: Namespaces) => string,
|
|
208
|
+
needed: boolean,
|
|
209
|
+
) => {
|
|
210
|
+
const relationship = pkg.relationships(mainName).find((r) => r.type === type && !r.external);
|
|
211
|
+
const existing = relationship?.partName ? pkg.part(relationship.partName) : undefined;
|
|
212
|
+
if (existing) {
|
|
213
|
+
const xml = pkg.xml(existing.name);
|
|
214
|
+
set(
|
|
215
|
+
existing.name,
|
|
216
|
+
write({ root: xml.root, source: xml.source }, namespacesOf(xml.root, xml.source)),
|
|
217
|
+
);
|
|
218
|
+
return existing.name;
|
|
219
|
+
}
|
|
220
|
+
if (!needed) return undefined;
|
|
221
|
+
if (pkg.part(defaultName)) {
|
|
222
|
+
// There, but not related to the document: written in place, then related.
|
|
223
|
+
const xml = pkg.xml(defaultName);
|
|
224
|
+
set(
|
|
225
|
+
defaultName,
|
|
226
|
+
write({ root: xml.root, source: xml.source }, namespacesOf(xml.root, xml.source)),
|
|
227
|
+
);
|
|
228
|
+
rels.add(type, relative(mainName, defaultName), false);
|
|
229
|
+
return defaultName;
|
|
230
|
+
}
|
|
231
|
+
add.push({
|
|
232
|
+
name: defaultName,
|
|
233
|
+
data: encoder.encode(write(undefined, namespaces)),
|
|
234
|
+
contentType,
|
|
235
|
+
});
|
|
236
|
+
rels.add(type, relative(mainName, defaultName), false);
|
|
237
|
+
return defaultName;
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
part(
|
|
241
|
+
REL.styles,
|
|
242
|
+
"/word/styles.xml",
|
|
243
|
+
CT.styles,
|
|
244
|
+
(o, ns) => stylesXml(doc, o, ns),
|
|
245
|
+
doc.styles.length > 0 || Object.keys(doc.defaults).length > 0,
|
|
246
|
+
);
|
|
247
|
+
const numbered = doc.numbering.abstracts.length + doc.numbering.instances.length > 0;
|
|
248
|
+
part(
|
|
249
|
+
REL.numbering,
|
|
250
|
+
"/word/numbering.xml",
|
|
251
|
+
CT.numbering,
|
|
252
|
+
(o, ns) => numberingXml(doc, o, ns),
|
|
253
|
+
numbered,
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
// Comments: their hyperlinks resolve against the comments part's relationships.
|
|
257
|
+
const commentsName = pkg
|
|
258
|
+
.relationships(mainName)
|
|
259
|
+
.find((r) => r.type === REL.comments && !r.external)?.partName;
|
|
260
|
+
const commentRels = new Relationships(commentsName ? pkg.relationships(commentsName) : []);
|
|
261
|
+
const comments = part(
|
|
262
|
+
REL.comments,
|
|
263
|
+
"/word/comments.xml",
|
|
264
|
+
CT.comments,
|
|
265
|
+
(o, ns) => commentsXml(doc, o, contextFor(ns, commentRels)),
|
|
266
|
+
doc.comments.length > 0,
|
|
267
|
+
);
|
|
268
|
+
if (comments && commentRels.added.length) writeRelationships(comments, commentRels);
|
|
269
|
+
// Replies and resolved state: rewritten only when they changed.
|
|
270
|
+
if (comments && changedThreads(doc, pkg)) {
|
|
271
|
+
part(
|
|
272
|
+
REL.commentsExtended,
|
|
273
|
+
"/word/commentsExtended.xml",
|
|
274
|
+
CT.commentsExtended,
|
|
275
|
+
(o) => commentsExtendedXml(doc, o),
|
|
276
|
+
true,
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (rels.added.length) writeRelationships(mainName, rels);
|
|
281
|
+
return pkg.write(replace, add);
|
|
282
|
+
|
|
283
|
+
function writeRelationships(source: string, relationships: Relationships): void {
|
|
284
|
+
const relsName = relationshipsPartName(source);
|
|
285
|
+
if (pkg.part(relsName)) set(relsName, relationships.xml(pkg.text(relsName)));
|
|
286
|
+
else
|
|
287
|
+
add.push({
|
|
288
|
+
name: relsName,
|
|
289
|
+
data: encoder.encode(relationships.xml(undefined)),
|
|
290
|
+
contentType: CT.rels,
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Whether the comments' replies or resolved state differ from what the package says. */
|
|
296
|
+
function changedThreads(doc: FoliDocument, pkg: DocxPackage): boolean {
|
|
297
|
+
const extended = pkg
|
|
298
|
+
.relationships(pkg.mainDocument)
|
|
299
|
+
.find((r) => r.type === REL.commentsExtended && !r.external)?.partName;
|
|
300
|
+
const W15 = "http://schemas.microsoft.com/office/word/2012/wordml";
|
|
301
|
+
const states = new Map<string, { parent: string | undefined; done: string | undefined }>();
|
|
302
|
+
if (extended && pkg.part(extended)) {
|
|
303
|
+
for (const c of pkg.xml(extended).root.children) {
|
|
304
|
+
if (c.kind !== "element" || c.ns !== W15 || c.local !== "commentEx") continue;
|
|
305
|
+
const get = (local: string) =>
|
|
306
|
+
c.attributes.find((a) => a.ns === W15 && a.local === local)?.value;
|
|
307
|
+
states.set(get("paraId") ?? "", { parent: get("paraIdParent"), done: get("done") });
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
const byId = new Map<number, string | undefined>();
|
|
311
|
+
for (const c of doc.comments) if (!byId.has(c.id)) byId.set(c.id, paraIdOf(c));
|
|
312
|
+
const paraId = (id: number | undefined) => (id === undefined ? undefined : byId.get(id));
|
|
313
|
+
for (const comment of doc.comments) {
|
|
314
|
+
const id = paraId(comment.id);
|
|
315
|
+
if (!id) continue;
|
|
316
|
+
const state = states.get(id);
|
|
317
|
+
const done = state?.done === "1" || state?.done === "true" || state?.done === "on";
|
|
318
|
+
if (!state && (comment.done || comment.parent !== undefined)) return true;
|
|
319
|
+
if (
|
|
320
|
+
state &&
|
|
321
|
+
(done !== !!comment.done || (state.parent ?? undefined) !== paraId(comment.parent))
|
|
322
|
+
)
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** A new package: content types, relationships, the document and the parts it needs. */
|
|
329
|
+
async function newPackage(doc: FoliDocument): Promise<Uint8Array> {
|
|
330
|
+
const reference = packageReference(doc);
|
|
331
|
+
if (reference) {
|
|
332
|
+
throw new DocxError(
|
|
333
|
+
`The document refers to a part of its original package (${reference}): export it with that package`,
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
const rels = new Relationships([]);
|
|
337
|
+
const context = contextFor(DEFAULT_NAMESPACES, rels);
|
|
338
|
+
const main = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n<w:document ${NEW_ROOT_ATTRIBUTES}><w:body>${blocksXml(doc.content.content ?? [], context)}${bodySection(doc, DEFAULT_NAMESPACES)}</w:body></w:document>`;
|
|
339
|
+
const parts: { name: string; text: string; contentType: string }[] = [
|
|
340
|
+
{ name: "/word/document.xml", text: main, contentType: CT.main },
|
|
341
|
+
];
|
|
342
|
+
const add = (name: string, type: string, contentType: string, text: string) => {
|
|
343
|
+
parts.push({ name, text, contentType });
|
|
344
|
+
rels.add(type, relative("/word/document.xml", name), false);
|
|
345
|
+
};
|
|
346
|
+
add("/word/styles.xml", REL.styles, CT.styles, stylesXml(doc, undefined, DEFAULT_NAMESPACES));
|
|
347
|
+
if (doc.numbering.abstracts.length + doc.numbering.instances.length) {
|
|
348
|
+
add(
|
|
349
|
+
"/word/numbering.xml",
|
|
350
|
+
REL.numbering,
|
|
351
|
+
CT.numbering,
|
|
352
|
+
numberingXml(doc, undefined, DEFAULT_NAMESPACES),
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
if (doc.comments.length) {
|
|
356
|
+
const commentRels = new Relationships([]);
|
|
357
|
+
add(
|
|
358
|
+
"/word/comments.xml",
|
|
359
|
+
REL.comments,
|
|
360
|
+
CT.comments,
|
|
361
|
+
commentsXml(doc, undefined, contextFor(DEFAULT_NAMESPACES, commentRels)),
|
|
362
|
+
);
|
|
363
|
+
if (commentRels.added.length)
|
|
364
|
+
parts.push({
|
|
365
|
+
name: "/word/_rels/comments.xml.rels",
|
|
366
|
+
text: commentRels.xml(undefined),
|
|
367
|
+
contentType: CT.rels,
|
|
368
|
+
});
|
|
369
|
+
if (doc.comments.some((c) => paraIdOf(c) && (c.parent !== undefined || c.done !== undefined))) {
|
|
370
|
+
add(
|
|
371
|
+
"/word/commentsExtended.xml",
|
|
372
|
+
REL.commentsExtended,
|
|
373
|
+
CT.commentsExtended,
|
|
374
|
+
commentsExtendedXml(doc, undefined),
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
parts.push({
|
|
379
|
+
name: "/word/_rels/document.xml.rels",
|
|
380
|
+
text: rels.xml(undefined),
|
|
381
|
+
contentType: CT.rels,
|
|
382
|
+
});
|
|
383
|
+
const overrides = parts
|
|
384
|
+
.filter((p) => p.contentType !== CT.rels)
|
|
385
|
+
.map((p) => `<Override PartName="${p.name}" ContentType="${p.contentType}"/>`)
|
|
386
|
+
.join("");
|
|
387
|
+
const files: [string, string][] = [
|
|
388
|
+
[
|
|
389
|
+
"[Content_Types].xml",
|
|
390
|
+
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="${CT.rels}"/><Default Extension="xml" ContentType="application/xml"/>${overrides}</Types>`,
|
|
391
|
+
],
|
|
392
|
+
[
|
|
393
|
+
"_rels/.rels",
|
|
394
|
+
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships>`,
|
|
395
|
+
],
|
|
396
|
+
...parts.map((p): [string, string] => [p.name.slice(1), p.text]),
|
|
397
|
+
];
|
|
398
|
+
const entries = [];
|
|
399
|
+
for (const [name, text] of files) entries.push(await deflateEntry(name, encoder.encode(text)));
|
|
400
|
+
return writeZip(entries);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** The body's last section: written when the document has one or read one (an empty `{}` read
|
|
404
|
+
* from nothing is Word's default page, written as no `w:sectPr`). */
|
|
405
|
+
function bodySection(doc: FoliDocument, namespaces: Namespaces): string {
|
|
406
|
+
const section = doc.section as Record<string, unknown>;
|
|
407
|
+
return Object.keys(section).length ? sectionXml(section, namespaces) : "";
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Where the document refers to a part of a package by relationship id (headers, footers,
|
|
412
|
+
* pictures and other content kept verbatim): such a document needs that package to export.
|
|
413
|
+
*/
|
|
414
|
+
function packageReference(doc: FoliDocument): string | undefined {
|
|
415
|
+
const sections = [doc.section, ...(doc.content.content ?? []).map((b) => b.attrs?.section)];
|
|
416
|
+
for (const s of sections) {
|
|
417
|
+
const section = s as { headers?: object; footers?: object } | undefined;
|
|
418
|
+
if (section?.headers || section?.footers) return "a header or footer";
|
|
419
|
+
}
|
|
420
|
+
let found: string | undefined;
|
|
421
|
+
JSON.stringify([doc.content, doc.comments], (key, value) => {
|
|
422
|
+
if (!found && key === "xml" && typeof value === "string" && /\sr:[A-Za-z]+="/.test(value))
|
|
423
|
+
found = "content kept verbatim";
|
|
424
|
+
const link = value as {
|
|
425
|
+
type?: string;
|
|
426
|
+
attrs?: { href?: unknown; attributes?: Record<string, string> };
|
|
427
|
+
};
|
|
428
|
+
// A link with a target gets a new relationship; one without keeps its reference.
|
|
429
|
+
if (
|
|
430
|
+
!found &&
|
|
431
|
+
link?.type === "link" &&
|
|
432
|
+
link.attrs?.href == null &&
|
|
433
|
+
link.attrs?.attributes?.["r:id"]
|
|
434
|
+
) {
|
|
435
|
+
found = "a hyperlink";
|
|
436
|
+
}
|
|
437
|
+
return value;
|
|
438
|
+
});
|
|
439
|
+
return found;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/** A part's text with an encoding declaration of UTF-8, the encoding it is written in. */
|
|
443
|
+
function utf8Declaration(text: string): string {
|
|
444
|
+
return text.replace(/^(<\?xml[^?]*?encoding=)(["'])[^"']*\2/, "$1$2UTF-8$2");
|
|
445
|
+
}
|