ooxml.js 2.4.0 → 2.5.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/README.md +40 -2
- package/dist/codec.cjs +28 -0
- package/dist/codec.d.cts +72 -0
- package/dist/codec.d.ts +72 -0
- package/dist/codec.js +24 -0
- package/dist/compact.cjs +162 -0
- package/dist/compact.d.cts +70 -0
- package/dist/compact.d.ts +70 -0
- package/dist/compact.js +152 -0
- package/dist/image/sniff.cjs +28 -0
- package/dist/image/sniff.d.cts +5 -0
- package/dist/image/sniff.d.ts +5 -0
- package/dist/image/sniff.js +27 -0
- package/dist/index.cjs +83 -3025
- package/dist/index.d.cts +22 -527
- package/dist/index.d.ts +22 -527
- package/dist/index.js +22 -2964
- package/dist/model/node.cjs +67 -0
- package/dist/model/node.d.cts +2 -0
- package/dist/model/node.d.ts +2 -0
- package/dist/model/node.js +58 -0
- package/dist/model/package.cjs +19 -0
- package/dist/model/package.d.cts +2 -0
- package/dist/model/package.d.ts +2 -0
- package/dist/model/package.js +15 -0
- package/dist/node-CkBWRjNR.d.cts +83 -0
- package/dist/node-CkBWRjNR.d.ts +83 -0
- package/dist/package-BUojjTXf.d.ts +114 -0
- package/dist/package-L24lkba-.d.cts +114 -0
- package/dist/package-io/read.cjs +33 -0
- package/dist/package-io/read.d.cts +5 -0
- package/dist/package-io/read.d.ts +5 -0
- package/dist/package-io/read.js +32 -0
- package/dist/package-io/write.cjs +18 -0
- package/dist/package-io/write.d.cts +5 -0
- package/dist/package-io/write.d.ts +5 -0
- package/dist/package-io/write.js +17 -0
- package/dist/typed/docx/read.cjs +332 -0
- package/dist/typed/docx/read.d.cts +52 -0
- package/dist/typed/docx/read.d.ts +52 -0
- package/dist/typed/docx/read.js +328 -0
- package/dist/typed/docx/styles.cjs +148 -0
- package/dist/typed/docx/styles.d.cts +29 -0
- package/dist/typed/docx/styles.d.ts +29 -0
- package/dist/typed/docx/styles.js +146 -0
- package/dist/typed/pptx/inherit.cjs +96 -0
- package/dist/typed/pptx/inherit.d.cts +30 -0
- package/dist/typed/pptx/inherit.d.ts +30 -0
- package/dist/typed/pptx/inherit.js +90 -0
- package/dist/typed/pptx/read.cjs +364 -0
- package/dist/typed/pptx/read.d.cts +44 -0
- package/dist/typed/pptx/read.d.ts +44 -0
- package/dist/typed/pptx/read.js +362 -0
- package/dist/typed/shared/color.cjs +85 -0
- package/dist/typed/shared/color.d.cts +9 -0
- package/dist/typed/shared/color.d.ts +9 -0
- package/dist/typed/shared/color.js +84 -0
- package/dist/typed/shared/drawingml.cjs +191 -0
- package/dist/typed/shared/drawingml.d.cts +40 -0
- package/dist/typed/shared/drawingml.d.ts +40 -0
- package/dist/typed/shared/drawingml.js +180 -0
- package/dist/typed/shared/metadata.cjs +44 -0
- package/dist/typed/shared/metadata.d.cts +16 -0
- package/dist/typed/shared/metadata.d.ts +16 -0
- package/dist/typed/shared/metadata.js +42 -0
- package/dist/typed/shared/source-path.cjs +18 -0
- package/dist/typed/shared/source-path.d.cts +5 -0
- package/dist/typed/shared/source-path.d.ts +5 -0
- package/dist/typed/shared/source-path.js +17 -0
- package/dist/typed/shared/units.cjs +55 -0
- package/dist/typed/shared/units.d.cts +20 -0
- package/dist/typed/shared/units.d.ts +20 -0
- package/dist/typed/shared/units.js +38 -0
- package/dist/typed/util.cjs +85 -0
- package/dist/typed/util.d.cts +18 -0
- package/dist/typed/util.d.ts +18 -0
- package/dist/typed/util.js +77 -0
- package/dist/typed/xlsx/a1.cjs +64 -0
- package/dist/typed/xlsx/a1.d.cts +19 -0
- package/dist/typed/xlsx/a1.d.ts +19 -0
- package/dist/typed/xlsx/a1.js +58 -0
- package/dist/typed/xlsx/build.cjs +456 -0
- package/dist/typed/xlsx/build.d.cts +6 -0
- package/dist/typed/xlsx/build.d.ts +6 -0
- package/dist/typed/xlsx/build.js +455 -0
- package/dist/typed/xlsx/content.cjs +257 -0
- package/dist/typed/xlsx/content.d.cts +6 -0
- package/dist/typed/xlsx/content.d.ts +6 -0
- package/dist/typed/xlsx/content.js +256 -0
- package/dist/typed/xlsx/defined-names.cjs +90 -0
- package/dist/typed/xlsx/defined-names.d.cts +20 -0
- package/dist/typed/xlsx/defined-names.d.ts +20 -0
- package/dist/typed/xlsx/defined-names.js +83 -0
- package/dist/typed/xlsx/print-settings.cjs +116 -0
- package/dist/typed/xlsx/print-settings.d.cts +8 -0
- package/dist/typed/xlsx/print-settings.d.ts +8 -0
- package/dist/typed/xlsx/print-settings.js +114 -0
- package/dist/typed/xlsx/shared-strings.cjs +35 -0
- package/dist/typed/xlsx/shared-strings.d.cts +12 -0
- package/dist/typed/xlsx/shared-strings.d.ts +12 -0
- package/dist/typed/xlsx/shared-strings.js +33 -0
- package/dist/typed/xlsx/units.cjs +21 -0
- package/dist/typed/xlsx/units.d.cts +8 -0
- package/dist/typed/xlsx/units.d.ts +8 -0
- package/dist/typed/xlsx/units.js +16 -0
- package/dist/typed/xlsx/util.cjs +53 -0
- package/dist/typed/xlsx/util.d.cts +10 -0
- package/dist/typed/xlsx/util.d.ts +10 -0
- package/dist/typed/xlsx/util.js +47 -0
- package/dist/typed/xlsx.cjs +140 -0
- package/dist/typed/xlsx.d.cts +43 -0
- package/dist/typed/xlsx.d.ts +43 -0
- package/dist/typed/xlsx.js +135 -0
- package/dist/util/base64.cjs +48 -0
- package/dist/util/base64.d.cts +5 -0
- package/dist/util/base64.d.ts +5 -0
- package/dist/util/base64.js +46 -0
- package/dist/xml/build.cjs +46 -0
- package/dist/xml/build.d.cts +5 -0
- package/dist/xml/build.d.ts +5 -0
- package/dist/xml/build.js +46 -0
- package/dist/xml/entities.cjs +7 -0
- package/dist/xml/entities.d.cts +4 -0
- package/dist/xml/entities.d.ts +4 -0
- package/dist/xml/entities.js +6 -0
- package/dist/xml/fragment.cjs +22 -0
- package/dist/xml/fragment.d.cts +6 -0
- package/dist/xml/fragment.d.ts +6 -0
- package/dist/xml/fragment.js +20 -0
- package/dist/xml/parse.cjs +88 -0
- package/dist/xml/parse.d.cts +5 -0
- package/dist/xml/parse.d.ts +5 -0
- package/dist/xml/parse.js +88 -0
- package/dist/zip.cjs +12 -0
- package/dist/zip.d.cts +5 -0
- package/dist/zip.d.ts +5 -0
- package/dist/zip.js +10 -0
- package/package.json +9 -1
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { attr, childrenWithTag, elementsWithTag, resolveRelationships, rootElement, textContent } from "../util.js";
|
|
2
|
+
import { DocumentMetadataSchema, readCoreProperties } from "../shared/metadata.js";
|
|
3
|
+
import { twipsToPt } from "../shared/units.js";
|
|
4
|
+
import { EMPTY_THEME, readTheme } from "../shared/drawingml.js";
|
|
5
|
+
import { assignSourcePaths } from "../shared/source-path.js";
|
|
6
|
+
import { resolveParagraphProperties, resolveRunProperties } from "./styles.js";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import { ContentSectionSchema, PAGE_SIZE_LETTER, rgbHexToColor } from "document-schema.js";
|
|
9
|
+
//#region src/typed/docx/read.ts
|
|
10
|
+
const CommentSchema = z.object({
|
|
11
|
+
author: z.string().optional(),
|
|
12
|
+
text: z.string()
|
|
13
|
+
});
|
|
14
|
+
const FootnoteSchema = z.object({
|
|
15
|
+
type: z.string().optional(),
|
|
16
|
+
text: z.string()
|
|
17
|
+
});
|
|
18
|
+
const DocxDocumentSchema = z.object({
|
|
19
|
+
metadata: DocumentMetadataSchema,
|
|
20
|
+
sections: z.array(ContentSectionSchema),
|
|
21
|
+
comments: z.array(CommentSchema),
|
|
22
|
+
footnotes: z.array(FootnoteSchema),
|
|
23
|
+
headers: z.array(z.string()),
|
|
24
|
+
footers: z.array(z.string())
|
|
25
|
+
});
|
|
26
|
+
const DOCUMENT_PART_PATH = "word/document.xml";
|
|
27
|
+
const STYLES_PART_PATH = "word/styles.xml";
|
|
28
|
+
const THEME_REL_SUFFIX = "/theme";
|
|
29
|
+
const DEFAULT_MARGIN_PT = 72;
|
|
30
|
+
const DEFAULT_MARGINS = {
|
|
31
|
+
topPt: DEFAULT_MARGIN_PT,
|
|
32
|
+
rightPt: DEFAULT_MARGIN_PT,
|
|
33
|
+
bottomPt: DEFAULT_MARGIN_PT,
|
|
34
|
+
leftPt: DEFAULT_MARGIN_PT
|
|
35
|
+
};
|
|
36
|
+
function readPageSize(sectPr) {
|
|
37
|
+
const pgSz = childrenWithTag(sectPr, "w:pgSz")[0];
|
|
38
|
+
const w = pgSz === void 0 ? void 0 : attr(pgSz, "w:w");
|
|
39
|
+
const h = pgSz === void 0 ? void 0 : attr(pgSz, "w:h");
|
|
40
|
+
return w === void 0 || h === void 0 ? PAGE_SIZE_LETTER : {
|
|
41
|
+
widthPt: twipsToPt(Number(w)),
|
|
42
|
+
heightPt: twipsToPt(Number(h))
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function readMargins(sectPr) {
|
|
46
|
+
const pgMar = childrenWithTag(sectPr, "w:pgMar")[0];
|
|
47
|
+
if (pgMar === void 0) return DEFAULT_MARGINS;
|
|
48
|
+
const top = attr(pgMar, "w:top");
|
|
49
|
+
const right = attr(pgMar, "w:right");
|
|
50
|
+
const bottom = attr(pgMar, "w:bottom");
|
|
51
|
+
const left = attr(pgMar, "w:left");
|
|
52
|
+
return {
|
|
53
|
+
topPt: top === void 0 ? DEFAULT_MARGIN_PT : twipsToPt(Number(top)),
|
|
54
|
+
rightPt: right === void 0 ? DEFAULT_MARGIN_PT : twipsToPt(Number(right)),
|
|
55
|
+
bottomPt: bottom === void 0 ? DEFAULT_MARGIN_PT : twipsToPt(Number(bottom)),
|
|
56
|
+
leftPt: left === void 0 ? DEFAULT_MARGIN_PT : twipsToPt(Number(left))
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function readListMembership(pPr) {
|
|
60
|
+
const numPr = pPr === void 0 ? void 0 : childrenWithTag(pPr, "w:numPr")[0];
|
|
61
|
+
if (numPr === void 0) return;
|
|
62
|
+
const numIdEl = childrenWithTag(numPr, "w:numId")[0];
|
|
63
|
+
const numId = numIdEl === void 0 ? void 0 : attr(numIdEl, "w:val");
|
|
64
|
+
if (numId === void 0) return;
|
|
65
|
+
const ilvlEl = childrenWithTag(numPr, "w:ilvl")[0];
|
|
66
|
+
const ilvlVal = ilvlEl === void 0 ? void 0 : attr(ilvlEl, "w:val");
|
|
67
|
+
return {
|
|
68
|
+
numId,
|
|
69
|
+
level: ilvlVal === void 0 ? 0 : Number(ilvlVal)
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function readToggle(el) {
|
|
73
|
+
if (el === void 0) return false;
|
|
74
|
+
const val = attr(el, "w:val");
|
|
75
|
+
return val === void 0 || val !== "0" && val !== "false" && val !== "off";
|
|
76
|
+
}
|
|
77
|
+
function hasPageBreakBefore(paragraph) {
|
|
78
|
+
const pPr = childrenWithTag(paragraph, "w:pPr")[0];
|
|
79
|
+
return readToggle(pPr === void 0 ? void 0 : childrenWithTag(pPr, "w:pageBreakBefore")[0]);
|
|
80
|
+
}
|
|
81
|
+
function readRunText(run) {
|
|
82
|
+
let text = "";
|
|
83
|
+
for (const child of run.children) {
|
|
84
|
+
if (child.type !== "element") continue;
|
|
85
|
+
if (child.tag === "w:t") text += textContent(child);
|
|
86
|
+
else if (child.tag === "w:tab") text += " ";
|
|
87
|
+
else if (child.tag === "w:br" || child.tag === "w:cr") text += "\n";
|
|
88
|
+
}
|
|
89
|
+
return text;
|
|
90
|
+
}
|
|
91
|
+
function readRun(run, paragraph, context) {
|
|
92
|
+
const props = resolveRunProperties(run, paragraph, context);
|
|
93
|
+
return {
|
|
94
|
+
text: readRunText(run),
|
|
95
|
+
bold: props.bold,
|
|
96
|
+
italic: props.italic,
|
|
97
|
+
underline: props.underline,
|
|
98
|
+
strike: props.strike,
|
|
99
|
+
fontFamily: props.fontFamily,
|
|
100
|
+
sizePt: props.sizePt,
|
|
101
|
+
color: props.color
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function readParagraphRuns(paragraph, context, rels) {
|
|
105
|
+
const runs = [];
|
|
106
|
+
let fieldState = "none";
|
|
107
|
+
function walk(nodes, hyperlinkTarget) {
|
|
108
|
+
for (const node of nodes) {
|
|
109
|
+
if (node.type !== "element") continue;
|
|
110
|
+
if (node.tag === "w:r") {
|
|
111
|
+
const fldChar = childrenWithTag(node, "w:fldChar")[0];
|
|
112
|
+
if (fldChar !== void 0) {
|
|
113
|
+
const type = attr(fldChar, "w:fldCharType");
|
|
114
|
+
if (type === "begin") fieldState = "code";
|
|
115
|
+
else if (type === "separate") fieldState = "result";
|
|
116
|
+
else if (type === "end") fieldState = "none";
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (fieldState === "code") continue;
|
|
120
|
+
const run = readRun(node, paragraph, context);
|
|
121
|
+
runs.push(hyperlinkTarget === void 0 ? run : {
|
|
122
|
+
...run,
|
|
123
|
+
hyperlink: hyperlinkTarget
|
|
124
|
+
});
|
|
125
|
+
} else if (node.tag === "w:fldSimple") walk(node.children, hyperlinkTarget);
|
|
126
|
+
else if (node.tag === "w:hyperlink") {
|
|
127
|
+
const rId = attr(node, "r:id");
|
|
128
|
+
const target = rId === void 0 ? void 0 : rels.get(rId)?.target;
|
|
129
|
+
walk(node.children, target ?? hyperlinkTarget);
|
|
130
|
+
} else if (node.tag === "w:ins") walk(node.children, hyperlinkTarget);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
walk(paragraph.children, void 0);
|
|
134
|
+
return runs;
|
|
135
|
+
}
|
|
136
|
+
function readParagraph(paragraph, context, rels) {
|
|
137
|
+
const pPr = childrenWithTag(paragraph, "w:pPr")[0];
|
|
138
|
+
const pStyleEl = pPr === void 0 ? void 0 : childrenWithTag(pPr, "w:pStyle")[0];
|
|
139
|
+
const props = resolveParagraphProperties(paragraph, context);
|
|
140
|
+
return {
|
|
141
|
+
kind: "paragraph",
|
|
142
|
+
runs: readParagraphRuns(paragraph, context, rels),
|
|
143
|
+
styleId: pStyleEl === void 0 ? void 0 : attr(pStyleEl, "w:val"),
|
|
144
|
+
alignment: props.alignment,
|
|
145
|
+
list: readListMembership(pPr),
|
|
146
|
+
spacingBeforePt: props.spacingBeforePt,
|
|
147
|
+
spacingAfterPt: props.spacingAfterPt,
|
|
148
|
+
lineSpacing: props.lineSpacing,
|
|
149
|
+
indentLeftPt: props.indentLeftPt,
|
|
150
|
+
indentFirstLinePt: props.indentFirstLinePt
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function readCellShading(tcPr) {
|
|
154
|
+
const shd = tcPr === void 0 ? void 0 : childrenWithTag(tcPr, "w:shd")[0];
|
|
155
|
+
const fill = shd === void 0 ? void 0 : attr(shd, "w:fill");
|
|
156
|
+
return fill === void 0 || fill === "auto" || fill === "none" ? void 0 : rgbHexToColor(fill);
|
|
157
|
+
}
|
|
158
|
+
function readRawCell(tc, context, rels) {
|
|
159
|
+
const tcPr = childrenWithTag(tc, "w:tcPr")[0];
|
|
160
|
+
const gridSpanEl = tcPr === void 0 ? void 0 : childrenWithTag(tcPr, "w:gridSpan")[0];
|
|
161
|
+
const gridSpanVal = gridSpanEl === void 0 ? void 0 : attr(gridSpanEl, "w:val");
|
|
162
|
+
const vMerge = tcPr === void 0 ? void 0 : childrenWithTag(tcPr, "w:vMerge")[0];
|
|
163
|
+
const vMergeVal = vMerge === void 0 ? void 0 : attr(vMerge, "w:val") ?? "continue";
|
|
164
|
+
return {
|
|
165
|
+
gridSpan: gridSpanVal === void 0 ? 1 : Number(gridSpanVal),
|
|
166
|
+
isVMergeContinuation: vMergeVal === "continue",
|
|
167
|
+
background: readCellShading(tcPr),
|
|
168
|
+
blocks: readBodyBlocks(tc.children, context, rels)
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function readTable(tbl, context, rels) {
|
|
172
|
+
const tblGrid = childrenWithTag(tbl, "w:tblGrid")[0];
|
|
173
|
+
const columnWidthsPt = tblGrid === void 0 ? [] : childrenWithTag(tblGrid, "w:gridCol").map((col) => twipsToPt(Number(attr(col, "w:w") ?? "0")));
|
|
174
|
+
const rawRows = childrenWithTag(tbl, "w:tr").map((tr) => childrenWithTag(tr, "w:tc").map((tc) => readRawCell(tc, context, rels)));
|
|
175
|
+
const rowColumnIndices = rawRows.map((row) => {
|
|
176
|
+
const indices = [];
|
|
177
|
+
let col = 0;
|
|
178
|
+
for (const cell of row) {
|
|
179
|
+
indices.push(col);
|
|
180
|
+
col += cell.gridSpan;
|
|
181
|
+
}
|
|
182
|
+
return indices;
|
|
183
|
+
});
|
|
184
|
+
return {
|
|
185
|
+
kind: "table",
|
|
186
|
+
columnWidthsPt,
|
|
187
|
+
rows: rawRows.map((row, rowIndex) => ({ cells: row.map((cell, cellIndex) => {
|
|
188
|
+
if (cell.isVMergeContinuation) return { blocks: [] };
|
|
189
|
+
const colIndex = rowColumnIndices[rowIndex][cellIndex];
|
|
190
|
+
let rowSpan = 1;
|
|
191
|
+
for (let r = rowIndex + 1; r < rawRows.length; r++) {
|
|
192
|
+
const matchIndex = rowColumnIndices[r].indexOf(colIndex);
|
|
193
|
+
if (!(matchIndex === -1 ? void 0 : rawRows[r][matchIndex])?.isVMergeContinuation) break;
|
|
194
|
+
rowSpan++;
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
blocks: cell.blocks,
|
|
198
|
+
colSpan: cell.gridSpan > 1 ? cell.gridSpan : void 0,
|
|
199
|
+
rowSpan: rowSpan > 1 ? rowSpan : void 0,
|
|
200
|
+
background: cell.background
|
|
201
|
+
};
|
|
202
|
+
}) }))
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
function readBodyBlocks(nodes, context, rels) {
|
|
206
|
+
const blocks = [];
|
|
207
|
+
for (const node of nodes) {
|
|
208
|
+
if (node.type !== "element") continue;
|
|
209
|
+
if (node.tag === "w:p") {
|
|
210
|
+
if (hasPageBreakBefore(node)) blocks.push({ kind: "pageBreak" });
|
|
211
|
+
blocks.push(readParagraph(node, context, rels));
|
|
212
|
+
} else if (node.tag === "w:tbl") blocks.push(readTable(node, context, rels));
|
|
213
|
+
else if (node.tag === "w:sdt") {
|
|
214
|
+
const sdtContent = childrenWithTag(node, "w:sdtContent")[0];
|
|
215
|
+
if (sdtContent !== void 0) blocks.push(...readBodyBlocks(sdtContent.children, context, rels));
|
|
216
|
+
} else if (node.tag === "w:ins") blocks.push(...readBodyBlocks(node.children, context, rels));
|
|
217
|
+
else if (node.tag === "mc:AlternateContent") {
|
|
218
|
+
const target = childrenWithTag(node, "mc:Fallback")[0] ?? childrenWithTag(node, "mc:Choice")[0];
|
|
219
|
+
if (target !== void 0) blocks.push(...readBodyBlocks(target.children, context, rels));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return blocks;
|
|
223
|
+
}
|
|
224
|
+
function readSections(body, context, rels) {
|
|
225
|
+
const sections = [];
|
|
226
|
+
let currentBlocks = [];
|
|
227
|
+
for (const node of body.children) {
|
|
228
|
+
if (node.type !== "element") continue;
|
|
229
|
+
if (node.tag === "w:sectPr") {
|
|
230
|
+
sections.push({
|
|
231
|
+
pageSize: readPageSize(node),
|
|
232
|
+
margins: readMargins(node),
|
|
233
|
+
blocks: currentBlocks
|
|
234
|
+
});
|
|
235
|
+
currentBlocks = [];
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
if (node.tag === "w:p") {
|
|
239
|
+
const pPr = childrenWithTag(node, "w:pPr")[0];
|
|
240
|
+
const sectPr = pPr === void 0 ? void 0 : childrenWithTag(pPr, "w:sectPr")[0];
|
|
241
|
+
if (hasPageBreakBefore(node)) currentBlocks.push({ kind: "pageBreak" });
|
|
242
|
+
currentBlocks.push(readParagraph(node, context, rels));
|
|
243
|
+
if (sectPr !== void 0) {
|
|
244
|
+
sections.push({
|
|
245
|
+
pageSize: readPageSize(sectPr),
|
|
246
|
+
margins: readMargins(sectPr),
|
|
247
|
+
blocks: currentBlocks
|
|
248
|
+
});
|
|
249
|
+
currentBlocks = [];
|
|
250
|
+
}
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
currentBlocks.push(...readBodyBlocks([node], context, rels));
|
|
254
|
+
}
|
|
255
|
+
if (currentBlocks.length > 0 || sections.length === 0) sections.push({
|
|
256
|
+
pageSize: PAGE_SIZE_LETTER,
|
|
257
|
+
margins: DEFAULT_MARGINS,
|
|
258
|
+
blocks: currentBlocks
|
|
259
|
+
});
|
|
260
|
+
sections.forEach((section, sectionIndex) => assignSourcePaths(section.blocks, `sections[${sectionIndex}]`));
|
|
261
|
+
return sections;
|
|
262
|
+
}
|
|
263
|
+
function readDocumentTheme(pkg, docRels) {
|
|
264
|
+
for (const rel of docRels.values()) if (rel.type.endsWith(THEME_REL_SUFFIX)) {
|
|
265
|
+
const themeRoot = rootElement(pkg.parts[rel.target]);
|
|
266
|
+
if (themeRoot !== void 0) return readTheme(themeRoot);
|
|
267
|
+
}
|
|
268
|
+
return EMPTY_THEME;
|
|
269
|
+
}
|
|
270
|
+
function readComment(comment) {
|
|
271
|
+
const author = attr(comment, "w:author");
|
|
272
|
+
const result = { text: elementsWithTag(comment.children, "w:t").map(textContent).join("") };
|
|
273
|
+
if (author !== void 0) result.author = author;
|
|
274
|
+
return result;
|
|
275
|
+
}
|
|
276
|
+
function readFootnote(footnote) {
|
|
277
|
+
const type = attr(footnote, "w:type");
|
|
278
|
+
const result = { text: elementsWithTag(footnote.children, "w:t").map(textContent).join("") };
|
|
279
|
+
if (type !== void 0) result.type = type;
|
|
280
|
+
return result;
|
|
281
|
+
}
|
|
282
|
+
function readComments(pkg) {
|
|
283
|
+
const root = rootElement(pkg.parts["word/comments.xml"]);
|
|
284
|
+
if (root === void 0) return [];
|
|
285
|
+
return childrenWithTag(root, "w:comment").map(readComment);
|
|
286
|
+
}
|
|
287
|
+
function readFootnotes(pkg) {
|
|
288
|
+
const root = rootElement(pkg.parts["word/footnotes.xml"]);
|
|
289
|
+
if (root === void 0) return [];
|
|
290
|
+
const out = [];
|
|
291
|
+
for (const fn of childrenWithTag(root, "w:footnote")) {
|
|
292
|
+
const type = attr(fn, "w:type");
|
|
293
|
+
if (type === "separator" || type === "continuationSeparator") continue;
|
|
294
|
+
out.push(readFootnote(fn));
|
|
295
|
+
}
|
|
296
|
+
return out;
|
|
297
|
+
}
|
|
298
|
+
function readHeaderFooterText(pkg, prefix) {
|
|
299
|
+
const out = [];
|
|
300
|
+
for (const path of Object.keys(pkg.parts)) {
|
|
301
|
+
if (!path.startsWith(prefix) || !path.endsWith(".xml")) continue;
|
|
302
|
+
const part = pkg.parts[path];
|
|
303
|
+
if (part?.kind !== "xml") continue;
|
|
304
|
+
out.push(elementsWithTag(part.nodes, "w:t").map(textContent).join(""));
|
|
305
|
+
}
|
|
306
|
+
return out;
|
|
307
|
+
}
|
|
308
|
+
function readDocx(pkg) {
|
|
309
|
+
const documentRoot = rootElement(pkg.parts[DOCUMENT_PART_PATH]);
|
|
310
|
+
if (documentRoot === void 0) throw new Error(`readDocx: package has no ${DOCUMENT_PART_PATH} part`);
|
|
311
|
+
const body = childrenWithTag(documentRoot, "w:body")[0];
|
|
312
|
+
if (body === void 0) throw new Error(`readDocx: ${DOCUMENT_PART_PATH} has no w:body element`);
|
|
313
|
+
const docRels = resolveRelationships(pkg, DOCUMENT_PART_PATH);
|
|
314
|
+
const context = {
|
|
315
|
+
stylesRoot: rootElement(pkg.parts[STYLES_PART_PATH]),
|
|
316
|
+
theme: readDocumentTheme(pkg, docRels)
|
|
317
|
+
};
|
|
318
|
+
return {
|
|
319
|
+
metadata: readCoreProperties(pkg),
|
|
320
|
+
sections: readSections(body, context, docRels),
|
|
321
|
+
comments: readComments(pkg),
|
|
322
|
+
footnotes: readFootnotes(pkg),
|
|
323
|
+
headers: readHeaderFooterText(pkg, "word/header"),
|
|
324
|
+
footers: readHeaderFooterText(pkg, "word/footer")
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
//#endregion
|
|
328
|
+
export { CommentSchema, DocxDocumentSchema, FootnoteSchema, readDocx };
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_typed_util = require("../util.cjs");
|
|
3
|
+
const require_typed_shared_units = require("../shared/units.cjs");
|
|
4
|
+
let document_schema_js = require("document-schema.js");
|
|
5
|
+
//#region src/typed/docx/styles.ts
|
|
6
|
+
function mergeParagraphLayer(base, layer) {
|
|
7
|
+
return {
|
|
8
|
+
alignment: layer.alignment ?? base.alignment,
|
|
9
|
+
spacingBeforePt: layer.spacingBeforePt ?? base.spacingBeforePt,
|
|
10
|
+
spacingAfterPt: layer.spacingAfterPt ?? base.spacingAfterPt,
|
|
11
|
+
lineSpacing: layer.lineSpacing ?? base.lineSpacing,
|
|
12
|
+
indentLeftPt: layer.indentLeftPt ?? base.indentLeftPt,
|
|
13
|
+
indentFirstLinePt: layer.indentFirstLinePt ?? base.indentFirstLinePt
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function mergeRunLayer(base, layer) {
|
|
17
|
+
return {
|
|
18
|
+
bold: layer.bold ?? base.bold,
|
|
19
|
+
italic: layer.italic ?? base.italic,
|
|
20
|
+
underline: layer.underline ?? base.underline,
|
|
21
|
+
strike: layer.strike ?? base.strike,
|
|
22
|
+
fontFamily: layer.fontFamily ?? base.fontFamily,
|
|
23
|
+
sizePt: layer.sizePt ?? base.sizePt,
|
|
24
|
+
color: layer.color ?? base.color
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function readToggle(el) {
|
|
28
|
+
if (el === void 0) return;
|
|
29
|
+
const val = require_typed_util.attr(el, "w:val");
|
|
30
|
+
return val === void 0 || val !== "0" && val !== "false" && val !== "off";
|
|
31
|
+
}
|
|
32
|
+
function readUnderline(u) {
|
|
33
|
+
if (u === void 0) return;
|
|
34
|
+
const val = require_typed_util.attr(u, "w:val");
|
|
35
|
+
return val !== void 0 && val !== "none";
|
|
36
|
+
}
|
|
37
|
+
function readRunColor(colorEl) {
|
|
38
|
+
if (colorEl === void 0) return;
|
|
39
|
+
const val = require_typed_util.attr(colorEl, "w:val");
|
|
40
|
+
return val === void 0 || val === "auto" ? void 0 : (0, document_schema_js.rgbHexToColor)(val);
|
|
41
|
+
}
|
|
42
|
+
function readRunFontFamily(rFonts, theme) {
|
|
43
|
+
if (rFonts === void 0) return;
|
|
44
|
+
const ascii = require_typed_util.attr(rFonts, "w:ascii");
|
|
45
|
+
if (ascii !== void 0) return ascii;
|
|
46
|
+
const asciiTheme = require_typed_util.attr(rFonts, "w:asciiTheme");
|
|
47
|
+
if (asciiTheme === "majorHAnsi" || asciiTheme === "majorAscii") return theme.majorFont;
|
|
48
|
+
if (asciiTheme === "minorHAnsi" || asciiTheme === "minorAscii") return theme.minorFont;
|
|
49
|
+
}
|
|
50
|
+
function readRunPropertiesLayer(rPr, theme) {
|
|
51
|
+
if (rPr === void 0) return {};
|
|
52
|
+
const sz = require_typed_util.childrenWithTag(rPr, "w:sz")[0];
|
|
53
|
+
const szVal = sz === void 0 ? void 0 : require_typed_util.attr(sz, "w:val");
|
|
54
|
+
return {
|
|
55
|
+
bold: readToggle(require_typed_util.childrenWithTag(rPr, "w:b")[0]),
|
|
56
|
+
italic: readToggle(require_typed_util.childrenWithTag(rPr, "w:i")[0]),
|
|
57
|
+
underline: readUnderline(require_typed_util.childrenWithTag(rPr, "w:u")[0]),
|
|
58
|
+
strike: readToggle(require_typed_util.childrenWithTag(rPr, "w:strike")[0]),
|
|
59
|
+
fontFamily: readRunFontFamily(require_typed_util.childrenWithTag(rPr, "w:rFonts")[0], theme),
|
|
60
|
+
sizePt: szVal === void 0 ? void 0 : require_typed_shared_units.halfPointsToPt(Number(szVal)),
|
|
61
|
+
color: readRunColor(require_typed_util.childrenWithTag(rPr, "w:color")[0])
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function readAlignment(jc) {
|
|
65
|
+
const val = jc === void 0 ? void 0 : require_typed_util.attr(jc, "w:val");
|
|
66
|
+
if (val === "left" || val === "start") return "left";
|
|
67
|
+
if (val === "center") return "center";
|
|
68
|
+
if (val === "right" || val === "end") return "right";
|
|
69
|
+
if (val === "both" || val === "distribute") return "justify";
|
|
70
|
+
}
|
|
71
|
+
function readParagraphPropertiesLayer(pPr) {
|
|
72
|
+
if (pPr === void 0) return {};
|
|
73
|
+
const spacing = require_typed_util.childrenWithTag(pPr, "w:spacing")[0];
|
|
74
|
+
const before = spacing === void 0 ? void 0 : require_typed_util.attr(spacing, "w:before");
|
|
75
|
+
const after = spacing === void 0 ? void 0 : require_typed_util.attr(spacing, "w:after");
|
|
76
|
+
const line = spacing === void 0 ? void 0 : require_typed_util.attr(spacing, "w:line");
|
|
77
|
+
const lineRule = spacing === void 0 ? void 0 : require_typed_util.attr(spacing, "w:lineRule");
|
|
78
|
+
const ind = require_typed_util.childrenWithTag(pPr, "w:ind")[0];
|
|
79
|
+
const left = ind === void 0 ? void 0 : require_typed_util.attr(ind, "w:left") ?? require_typed_util.attr(ind, "w:start");
|
|
80
|
+
const firstLine = ind === void 0 ? void 0 : require_typed_util.attr(ind, "w:firstLine");
|
|
81
|
+
const hanging = ind === void 0 ? void 0 : require_typed_util.attr(ind, "w:hanging");
|
|
82
|
+
return {
|
|
83
|
+
alignment: readAlignment(require_typed_util.childrenWithTag(pPr, "w:jc")[0]),
|
|
84
|
+
spacingBeforePt: before === void 0 ? void 0 : require_typed_shared_units.twipsToPt(Number(before)),
|
|
85
|
+
spacingAfterPt: after === void 0 ? void 0 : require_typed_shared_units.twipsToPt(Number(after)),
|
|
86
|
+
lineSpacing: line === void 0 || lineRule === "exact" || lineRule === "atLeast" ? void 0 : require_typed_shared_units.lineUnitsToMultiplier(Number(line)),
|
|
87
|
+
indentLeftPt: left === void 0 ? void 0 : require_typed_shared_units.twipsToPt(Number(left)),
|
|
88
|
+
indentFirstLinePt: firstLine !== void 0 ? require_typed_shared_units.twipsToPt(Number(firstLine)) : hanging !== void 0 ? -require_typed_shared_units.twipsToPt(Number(hanging)) : void 0
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function findStyle(stylesRoot, styleId, type) {
|
|
92
|
+
return require_typed_util.elementsWithTag([stylesRoot], "w:style").find((s) => require_typed_util.attr(s, "w:type") === type && require_typed_util.attr(s, "w:styleId") === styleId);
|
|
93
|
+
}
|
|
94
|
+
function findDefaultStyle(stylesRoot, type) {
|
|
95
|
+
return require_typed_util.elementsWithTag([stylesRoot], "w:style").find((s) => require_typed_util.attr(s, "w:type") === type && require_typed_util.attr(s, "w:default") === "1");
|
|
96
|
+
}
|
|
97
|
+
function resolveBasedOnChain(stylesRoot, styleId, type) {
|
|
98
|
+
const chain = [];
|
|
99
|
+
const visited = /* @__PURE__ */ new Set();
|
|
100
|
+
let currentId = styleId;
|
|
101
|
+
while (currentId !== void 0 && !visited.has(currentId)) {
|
|
102
|
+
visited.add(currentId);
|
|
103
|
+
const style = findStyle(stylesRoot, currentId, type);
|
|
104
|
+
if (style === void 0) break;
|
|
105
|
+
chain.unshift(style);
|
|
106
|
+
const basedOn = require_typed_util.childrenWithTag(style, "w:basedOn")[0];
|
|
107
|
+
currentId = basedOn === void 0 ? void 0 : require_typed_util.attr(basedOn, "w:val");
|
|
108
|
+
}
|
|
109
|
+
return chain;
|
|
110
|
+
}
|
|
111
|
+
function docDefaultsElement(stylesRoot, wrapperTag, innerTag) {
|
|
112
|
+
const docDefaults = stylesRoot === void 0 ? void 0 : require_typed_util.childrenWithTag(stylesRoot, "w:docDefaults")[0];
|
|
113
|
+
const wrapper = docDefaults === void 0 ? void 0 : require_typed_util.childrenWithTag(docDefaults, wrapperTag)[0];
|
|
114
|
+
return wrapper === void 0 ? void 0 : require_typed_util.childrenWithTag(wrapper, innerTag)[0];
|
|
115
|
+
}
|
|
116
|
+
function resolveParagraphProperties(paragraph, context) {
|
|
117
|
+
const pPr = require_typed_util.childrenWithTag(paragraph, "w:pPr")[0];
|
|
118
|
+
const pStyleEl = pPr === void 0 ? void 0 : require_typed_util.childrenWithTag(pPr, "w:pStyle")[0];
|
|
119
|
+
const styleId = pStyleEl === void 0 ? void 0 : require_typed_util.attr(pStyleEl, "w:val");
|
|
120
|
+
let resolved = readParagraphPropertiesLayer(docDefaultsElement(context.stylesRoot, "w:pPrDefault", "w:pPr"));
|
|
121
|
+
if (context.stylesRoot !== void 0) {
|
|
122
|
+
const defaultStyle = findDefaultStyle(context.stylesRoot, "paragraph");
|
|
123
|
+
if (defaultStyle !== void 0) resolved = mergeParagraphLayer(resolved, readParagraphPropertiesLayer(require_typed_util.childrenWithTag(defaultStyle, "w:pPr")[0]));
|
|
124
|
+
if (styleId !== void 0) for (const style of resolveBasedOnChain(context.stylesRoot, styleId, "paragraph")) resolved = mergeParagraphLayer(resolved, readParagraphPropertiesLayer(require_typed_util.childrenWithTag(style, "w:pPr")[0]));
|
|
125
|
+
}
|
|
126
|
+
return mergeParagraphLayer(resolved, readParagraphPropertiesLayer(pPr));
|
|
127
|
+
}
|
|
128
|
+
function resolveRunProperties(run, paragraph, context) {
|
|
129
|
+
const pPr = require_typed_util.childrenWithTag(paragraph, "w:pPr")[0];
|
|
130
|
+
const pStyleEl = pPr === void 0 ? void 0 : require_typed_util.childrenWithTag(pPr, "w:pStyle")[0];
|
|
131
|
+
const pStyleId = pStyleEl === void 0 ? void 0 : require_typed_util.attr(pStyleEl, "w:val");
|
|
132
|
+
let resolved = readRunPropertiesLayer(docDefaultsElement(context.stylesRoot, "w:rPrDefault", "w:rPr"), context.theme);
|
|
133
|
+
if (context.stylesRoot !== void 0) {
|
|
134
|
+
const defaultStyle = findDefaultStyle(context.stylesRoot, "paragraph");
|
|
135
|
+
if (defaultStyle !== void 0) resolved = mergeRunLayer(resolved, readRunPropertiesLayer(require_typed_util.childrenWithTag(defaultStyle, "w:rPr")[0], context.theme));
|
|
136
|
+
if (pStyleId !== void 0) for (const style of resolveBasedOnChain(context.stylesRoot, pStyleId, "paragraph")) resolved = mergeRunLayer(resolved, readRunPropertiesLayer(require_typed_util.childrenWithTag(style, "w:rPr")[0], context.theme));
|
|
137
|
+
}
|
|
138
|
+
const paragraphMarkRPr = pPr === void 0 ? void 0 : require_typed_util.childrenWithTag(pPr, "w:rPr")[0];
|
|
139
|
+
resolved = mergeRunLayer(resolved, readRunPropertiesLayer(paragraphMarkRPr, context.theme));
|
|
140
|
+
const rPr = require_typed_util.childrenWithTag(run, "w:rPr")[0];
|
|
141
|
+
const rStyleEl = rPr === void 0 ? void 0 : require_typed_util.childrenWithTag(rPr, "w:rStyle")[0];
|
|
142
|
+
const rStyleId = rStyleEl === void 0 ? void 0 : require_typed_util.attr(rStyleEl, "w:val");
|
|
143
|
+
if (context.stylesRoot !== void 0 && rStyleId !== void 0) for (const style of resolveBasedOnChain(context.stylesRoot, rStyleId, "character")) resolved = mergeRunLayer(resolved, readRunPropertiesLayer(require_typed_util.childrenWithTag(style, "w:rPr")[0], context.theme));
|
|
144
|
+
return mergeRunLayer(resolved, readRunPropertiesLayer(rPr, context.theme));
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
exports.resolveParagraphProperties = resolveParagraphProperties;
|
|
148
|
+
exports.resolveRunProperties = resolveRunProperties;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { l as XmlElement } from "../../node-CkBWRjNR.cjs";
|
|
2
|
+
import { DrawingTheme } from "../shared/drawingml.cjs";
|
|
3
|
+
import { Alignment, Color } from "document-schema.js";
|
|
4
|
+
//#region src/typed/docx/styles.d.ts
|
|
5
|
+
interface DocxStyleContext {
|
|
6
|
+
readonly stylesRoot: XmlElement | undefined;
|
|
7
|
+
readonly theme: DrawingTheme;
|
|
8
|
+
}
|
|
9
|
+
interface ResolvedParagraphProperties {
|
|
10
|
+
readonly alignment?: Alignment;
|
|
11
|
+
readonly spacingBeforePt?: number;
|
|
12
|
+
readonly spacingAfterPt?: number;
|
|
13
|
+
readonly lineSpacing?: number;
|
|
14
|
+
readonly indentLeftPt?: number;
|
|
15
|
+
readonly indentFirstLinePt?: number;
|
|
16
|
+
}
|
|
17
|
+
interface ResolvedRunProperties {
|
|
18
|
+
readonly bold?: boolean;
|
|
19
|
+
readonly italic?: boolean;
|
|
20
|
+
readonly underline?: boolean;
|
|
21
|
+
readonly strike?: boolean;
|
|
22
|
+
readonly fontFamily?: string;
|
|
23
|
+
readonly sizePt?: number;
|
|
24
|
+
readonly color?: Color;
|
|
25
|
+
}
|
|
26
|
+
declare function resolveParagraphProperties(paragraph: XmlElement, context: DocxStyleContext): ResolvedParagraphProperties;
|
|
27
|
+
declare function resolveRunProperties(run: XmlElement, paragraph: XmlElement, context: DocxStyleContext): ResolvedRunProperties;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { DocxStyleContext, ResolvedParagraphProperties, ResolvedRunProperties, resolveParagraphProperties, resolveRunProperties };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { l as XmlElement } from "../../node-CkBWRjNR.js";
|
|
2
|
+
import { DrawingTheme } from "../shared/drawingml.js";
|
|
3
|
+
import { Alignment, Color } from "document-schema.js";
|
|
4
|
+
//#region src/typed/docx/styles.d.ts
|
|
5
|
+
interface DocxStyleContext {
|
|
6
|
+
readonly stylesRoot: XmlElement | undefined;
|
|
7
|
+
readonly theme: DrawingTheme;
|
|
8
|
+
}
|
|
9
|
+
interface ResolvedParagraphProperties {
|
|
10
|
+
readonly alignment?: Alignment;
|
|
11
|
+
readonly spacingBeforePt?: number;
|
|
12
|
+
readonly spacingAfterPt?: number;
|
|
13
|
+
readonly lineSpacing?: number;
|
|
14
|
+
readonly indentLeftPt?: number;
|
|
15
|
+
readonly indentFirstLinePt?: number;
|
|
16
|
+
}
|
|
17
|
+
interface ResolvedRunProperties {
|
|
18
|
+
readonly bold?: boolean;
|
|
19
|
+
readonly italic?: boolean;
|
|
20
|
+
readonly underline?: boolean;
|
|
21
|
+
readonly strike?: boolean;
|
|
22
|
+
readonly fontFamily?: string;
|
|
23
|
+
readonly sizePt?: number;
|
|
24
|
+
readonly color?: Color;
|
|
25
|
+
}
|
|
26
|
+
declare function resolveParagraphProperties(paragraph: XmlElement, context: DocxStyleContext): ResolvedParagraphProperties;
|
|
27
|
+
declare function resolveRunProperties(run: XmlElement, paragraph: XmlElement, context: DocxStyleContext): ResolvedRunProperties;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { DocxStyleContext, ResolvedParagraphProperties, ResolvedRunProperties, resolveParagraphProperties, resolveRunProperties };
|