@open-document/core 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{check-pEQh4jJE.js → check-CL0-0eB8.js} +1 -1
- package/dist/cli/bin.js +5 -9
- package/dist/{export-BQ-GvMsb.js → export-CmVJdl9a.js} +3 -6
- package/dist/{import-Daat3L2J.js → import-E6vRo7xm.js} +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +5 -1
- package/dist/ops/index.d.ts +2 -3
- package/dist/ops/index.js +3 -3
- package/dist/{ops-DQfckiyp.js → ops-OZZVBgsM.js} +4 -3
- package/dist/{sdk-C9nl30dW.d.ts → sdk-BqU6mwD_.d.ts} +8 -1
- package/dist/{sdk-DhcqZdAw.js → sdk-CqNZzbkz.js} +12 -1
- package/package.json +1 -1
- package/skills/doc-authoring/SKILL.md +2 -1
- package/src/app/components/footnote.tsx +6 -1
- package/src/app/components/numbering.tsx +1 -0
- package/src/app/lib/agent-bridge.ts +13 -0
- package/src/app/lib/docx/from-dom.ts +889 -0
- package/src/app/lib/docx/ooxml.test.ts +299 -0
- package/src/app/lib/docx/ooxml.ts +755 -0
- package/src/app/lib/export-docx.ts +225 -0
- package/src/app/lib/export-dom.ts +19 -3
- package/src/app/lib/export-image.ts +45 -2
- package/src/app/lib/sdk.ts +8 -0
- package/src/app/routes/doc.tsx +12 -1
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { buildDocxParts, type DocxModel, escapeXml, fontsFor, hexColor } from './ooxml.ts';
|
|
3
|
+
|
|
4
|
+
describe('fontsFor', () => {
|
|
5
|
+
it('skips system aliases for the first real family', () => {
|
|
6
|
+
expect(
|
|
7
|
+
fontsFor('-apple-system, BlinkMacSystemFont, "Inter", system-ui, sans-serif', 'sans'),
|
|
8
|
+
).toEqual({ ascii: 'Inter', eastAsia: 'Inter', kind: 'sans' });
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('keeps a CJK family in the stack as the East Asian font', () => {
|
|
12
|
+
expect(fontsFor('"Noto Sans TC", "Helvetica Neue", sans-serif', 'sans')).toEqual({
|
|
13
|
+
ascii: 'Helvetica Neue',
|
|
14
|
+
eastAsia: 'Noto Sans TC',
|
|
15
|
+
kind: 'sans',
|
|
16
|
+
});
|
|
17
|
+
expect(fontsFor('Georgia, "PingFang TC", serif', 'serif')).toEqual({
|
|
18
|
+
ascii: 'Georgia',
|
|
19
|
+
eastAsia: 'PingFang TC',
|
|
20
|
+
kind: 'serif',
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('falls back by generic family when nothing is named', () => {
|
|
25
|
+
expect(fontsFor('system-ui, sans-serif', 'sans').ascii).toBe('Arial');
|
|
26
|
+
expect(fontsFor('serif', 'sans').ascii).toBe('Times New Roman');
|
|
27
|
+
expect(fontsFor('ui-monospace, monospace', 'sans').ascii).toBe('Courier New');
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('hexColor', () => {
|
|
32
|
+
it('reads computed colours and hex alike', () => {
|
|
33
|
+
expect(hexColor('rgb(22, 24, 29)')).toBe('16181d');
|
|
34
|
+
expect(hexColor('rgba(37, 99, 235, 1)')).toBe('2563eb');
|
|
35
|
+
expect(hexColor('#2563EB')).toBe('2563eb');
|
|
36
|
+
expect(hexColor('#abc')).toBe('aabbcc');
|
|
37
|
+
expect(hexColor('rgba(0, 0, 0, 0)')).toBeNull();
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe('escapeXml', () => {
|
|
42
|
+
it('escapes markup and drops characters XML cannot hold', () => {
|
|
43
|
+
expect(escapeXml('a < b & "c"\u0001')).toBe('a < b & "c"');
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const MODEL: DocxModel = {
|
|
48
|
+
title: 'Report & review',
|
|
49
|
+
page: { width: 794 * 15, height: 1123 * 15, landscape: false, margin: 76 * 15 },
|
|
50
|
+
fonts: {
|
|
51
|
+
body: { ascii: 'Inter', eastAsia: 'Noto Sans TC', kind: 'sans' },
|
|
52
|
+
heading: { ascii: 'Inter', eastAsia: 'Noto Sans TC', kind: 'sans' },
|
|
53
|
+
mono: { ascii: 'JetBrains Mono', eastAsia: 'JetBrains Mono', kind: 'mono' },
|
|
54
|
+
},
|
|
55
|
+
sizes: { title: 66, h1: 42, h2: 30, h3: 24, body: 21, caption: 15 },
|
|
56
|
+
colors: { text: '16181d', muted: '6b7280', accent: '2563eb' },
|
|
57
|
+
line: 325,
|
|
58
|
+
blocks: [
|
|
59
|
+
{ type: 'paragraph', style: 'Heading1', inlines: [{ type: 'text', text: '1. Method' }] },
|
|
60
|
+
{ type: 'toc', entries: [{ text: '1. Method', level: 1 }] },
|
|
61
|
+
{
|
|
62
|
+
type: 'paragraph',
|
|
63
|
+
inlines: [
|
|
64
|
+
{ type: 'text', text: 'See ' },
|
|
65
|
+
{ type: 'link', href: 'https://example.com', runs: [{ type: 'text', text: 'the guide' }] },
|
|
66
|
+
{ type: 'text', text: ' and ' },
|
|
67
|
+
{ type: 'text', text: 'code', mono: true },
|
|
68
|
+
{ type: 'footnote', id: 1 },
|
|
69
|
+
{ type: 'break' },
|
|
70
|
+
{ type: 'image', image: 'figure-1' },
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
type: 'table',
|
|
75
|
+
columns: [3000, 2000],
|
|
76
|
+
rows: [
|
|
77
|
+
{
|
|
78
|
+
header: true,
|
|
79
|
+
cells: [
|
|
80
|
+
{ blocks: [{ type: 'paragraph', inlines: [{ type: 'text', text: 'Service' }] }] },
|
|
81
|
+
{ blocks: [] },
|
|
82
|
+
],
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
},
|
|
86
|
+
{ type: 'paragraph', inlines: [], pageBreakBefore: true, spacingBefore: 0, lineExact: 6000 },
|
|
87
|
+
],
|
|
88
|
+
footnotes: [
|
|
89
|
+
{ id: 1, paragraphs: [{ type: 'paragraph', inlines: [{ type: 'text', text: 'A source.' }] }] },
|
|
90
|
+
],
|
|
91
|
+
footer: {
|
|
92
|
+
type: 'paragraph',
|
|
93
|
+
rightTab: 9000,
|
|
94
|
+
inlines: [
|
|
95
|
+
{ type: 'text', text: 'Report' },
|
|
96
|
+
{ type: 'tab' },
|
|
97
|
+
{ type: 'field', instr: 'PAGE', cached: '1' },
|
|
98
|
+
],
|
|
99
|
+
},
|
|
100
|
+
images: [{ name: 'figure-1', ext: 'png', bytes: new Uint8Array([1, 2]), width: 100, height: 50 }],
|
|
101
|
+
lists: ['bullet', 'decimal'],
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
describe('buildDocxParts', () => {
|
|
105
|
+
const parts = buildDocxParts(MODEL);
|
|
106
|
+
const text = (name: string) => parts[name] as string;
|
|
107
|
+
|
|
108
|
+
it('writes every part a package needs, and the media', () => {
|
|
109
|
+
expect(Object.keys(parts).sort()).toEqual([
|
|
110
|
+
'[Content_Types].xml',
|
|
111
|
+
'_rels/.rels',
|
|
112
|
+
'docProps/core.xml',
|
|
113
|
+
'word/_rels/document.xml.rels',
|
|
114
|
+
'word/document.xml',
|
|
115
|
+
'word/fontTable.xml',
|
|
116
|
+
'word/footer1.xml',
|
|
117
|
+
'word/footnotes.xml',
|
|
118
|
+
'word/media/figure-1.png',
|
|
119
|
+
'word/numbering.xml',
|
|
120
|
+
'word/settings.xml',
|
|
121
|
+
'word/styles.xml',
|
|
122
|
+
]);
|
|
123
|
+
expect(text('[Content_Types].xml')).toContain(
|
|
124
|
+
'<Default Extension="png" ContentType="image/png"/>',
|
|
125
|
+
);
|
|
126
|
+
expect(text('docProps/core.xml')).toContain('<dc:title>Report & review</dc:title>');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('sets the page from the geometry and hangs the footer on the section', () => {
|
|
130
|
+
const doc = text('word/document.xml');
|
|
131
|
+
expect(doc).toContain('<w:pgSz w:w="11910" w:h="16845"/>');
|
|
132
|
+
expect(doc).toContain('<w:pgMar w:top="1140"');
|
|
133
|
+
expect(doc).toMatch(/<w:footerReference w:type="default" r:id="rId\d+"\/>/);
|
|
134
|
+
expect(text('word/footer1.xml')).toContain('<w:fldSimple w:instr=" PAGE ">');
|
|
135
|
+
expect(text('word/footer1.xml')).toContain('<w:tab w:val="right" w:pos="9000"/>');
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('names a stand-in of the same kind for every font', () => {
|
|
139
|
+
const fonts = text('word/fontTable.xml');
|
|
140
|
+
expect(fonts).toContain(
|
|
141
|
+
'<w:font w:name="JetBrains Mono"><w:altName w:val="Courier New"/><w:family w:val="modern"/><w:pitch w:val="fixed"/></w:font>',
|
|
142
|
+
);
|
|
143
|
+
expect(fonts).toContain('<w:font w:name="Noto Sans TC"><w:altName w:val="Arial"/>');
|
|
144
|
+
expect(text('word/_rels/document.xml.rels')).toContain('Target="fontTable.xml"');
|
|
145
|
+
expect(text('[Content_Types].xml')).toContain('/word/fontTable.xml');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('keeps the footer off sections that end without one', () => {
|
|
149
|
+
const sectioned = buildDocxParts({
|
|
150
|
+
...MODEL,
|
|
151
|
+
blocks: [
|
|
152
|
+
{
|
|
153
|
+
type: 'paragraph',
|
|
154
|
+
inlines: [{ type: 'text', text: 'Cover' }],
|
|
155
|
+
sectionEnd: { footer: false },
|
|
156
|
+
},
|
|
157
|
+
{ type: 'paragraph', inlines: [{ type: 'text', text: 'Body' }] },
|
|
158
|
+
],
|
|
159
|
+
});
|
|
160
|
+
const doc = sectioned['word/document.xml'] as string;
|
|
161
|
+
const rels = sectioned['word/_rels/document.xml.rels'] as string;
|
|
162
|
+
const bare = rels.match(/Id="(rId\d+)"[^>]*Target="footer2\.xml"/)?.[1];
|
|
163
|
+
const full = rels.match(/Id="(rId\d+)"[^>]*Target="footer1\.xml"/)?.[1];
|
|
164
|
+
// The paragraph that ends a section holds its properties, ahead of its text.
|
|
165
|
+
expect(doc).toMatch(
|
|
166
|
+
new RegExp(`<w:sectPr><w:footerReference w:type="default" r:id="${bare}"/>.*?Cover`),
|
|
167
|
+
);
|
|
168
|
+
expect(doc).toMatch(
|
|
169
|
+
new RegExp(`Body.*<w:sectPr><w:footerReference w:type="default" r:id="${full}"/>`),
|
|
170
|
+
);
|
|
171
|
+
expect(sectioned['word/footer2.xml']).toContain('<w:ftr');
|
|
172
|
+
expect(parts['word/footer2.xml']).toBeUndefined();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('shades a cell and keeps code out of the spelling check', () => {
|
|
176
|
+
const boxed = buildDocxParts({
|
|
177
|
+
...MODEL,
|
|
178
|
+
blocks: [
|
|
179
|
+
{
|
|
180
|
+
type: 'table',
|
|
181
|
+
columns: [9000],
|
|
182
|
+
rows: [
|
|
183
|
+
{
|
|
184
|
+
header: false,
|
|
185
|
+
cells: [
|
|
186
|
+
{
|
|
187
|
+
blocks: [
|
|
188
|
+
{ type: 'paragraph', inlines: [{ type: 'text', text: 'docs/', mono: true }] },
|
|
189
|
+
],
|
|
190
|
+
fill: 'f5f5f7',
|
|
191
|
+
padding: { top: 240, left: 300, bottom: 240, right: 300 },
|
|
192
|
+
},
|
|
193
|
+
],
|
|
194
|
+
},
|
|
195
|
+
],
|
|
196
|
+
},
|
|
197
|
+
],
|
|
198
|
+
});
|
|
199
|
+
const doc = boxed['word/document.xml'] as string;
|
|
200
|
+
expect(doc).toContain(
|
|
201
|
+
'</w:tcBorders><w:shd w:val="clear" w:color="auto" w:fill="f5f5f7"/><w:tcMar>',
|
|
202
|
+
);
|
|
203
|
+
expect(doc).toMatch(/<w:noProof\/>(?:(?!<\/w:r>).)*?docs\//);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('bookmarks a caption and points a dotted page reference at it', () => {
|
|
207
|
+
const listed = buildDocxParts({
|
|
208
|
+
...MODEL,
|
|
209
|
+
blocks: [
|
|
210
|
+
{
|
|
211
|
+
type: 'paragraph',
|
|
212
|
+
rightTab: 9630,
|
|
213
|
+
leader: true,
|
|
214
|
+
inlines: [
|
|
215
|
+
{ type: 'text', text: 'Table 1 Usage' },
|
|
216
|
+
{ type: 'tab' },
|
|
217
|
+
{ type: 'field', instr: 'PAGEREF od_t_usage \\h', cached: '4' },
|
|
218
|
+
],
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
type: 'paragraph',
|
|
222
|
+
style: 'Caption',
|
|
223
|
+
bookmark: 'od_t_usage',
|
|
224
|
+
inlines: [{ type: 'text', text: 'Usage' }],
|
|
225
|
+
},
|
|
226
|
+
],
|
|
227
|
+
});
|
|
228
|
+
const doc = listed['word/document.xml'] as string;
|
|
229
|
+
expect(doc).toContain('<w:tab w:val="right" w:leader="dot" w:pos="9630"/>');
|
|
230
|
+
expect(doc).toContain('<w:fldSimple w:instr=" PAGEREF od_t_usage \\h ">');
|
|
231
|
+
expect(doc).toMatch(
|
|
232
|
+
/<w:bookmarkStart w:id="0" w:name="od_t_usage"\/>.*?Usage.*?<w:bookmarkEnd w:id="0"\/>/,
|
|
233
|
+
);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('holds a gap open with an exact line height where space before would be dropped', () => {
|
|
237
|
+
expect(text('word/document.xml')).toContain(
|
|
238
|
+
'<w:pageBreakBefore/><w:spacing w:before="0" w:line="6000" w:lineRule="exact"/>',
|
|
239
|
+
);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('keeps the page’s line height and leads contents entries to a right tab', () => {
|
|
243
|
+
const styles = text('word/styles.xml');
|
|
244
|
+
expect(styles).toContain('<w:spacing w:after="160" w:line="325" w:lineRule="atLeast"/>');
|
|
245
|
+
// Text block: the sheet less both margins.
|
|
246
|
+
expect(styles).toContain('<w:tab w:val="right" w:leader="dot" w:pos="9630"/>');
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it('writes headings as styles Word navigates by, with East Asian fonts', () => {
|
|
250
|
+
const styles = text('word/styles.xml');
|
|
251
|
+
expect(styles).toContain('w:styleId="Heading1"');
|
|
252
|
+
expect(styles).toContain('<w:outlineLvl w:val="0"/>');
|
|
253
|
+
expect(styles).toContain('w:eastAsia="Noto Sans TC"');
|
|
254
|
+
expect(text('word/document.xml')).toContain('<w:pStyle w:val="Heading1"/>');
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it('links externally, marks code in the mono font, and references the note', () => {
|
|
258
|
+
const doc = text('word/document.xml');
|
|
259
|
+
const rels = text('word/_rels/document.xml.rels');
|
|
260
|
+
expect(rels).toContain('Target="https://example.com" TargetMode="External"');
|
|
261
|
+
expect(doc).toMatch(
|
|
262
|
+
/<w:hyperlink r:id="rId\d+"><w:r><w:rPr><w:rStyle w:val="Hyperlink"\/><\/w:rPr>/,
|
|
263
|
+
);
|
|
264
|
+
expect(doc).toContain('w:ascii="JetBrains Mono"');
|
|
265
|
+
expect(doc).toContain('<w:footnoteReference w:id="1"/>');
|
|
266
|
+
expect(text('word/footnotes.xml')).toContain('<w:footnoteRef/>');
|
|
267
|
+
expect(text('word/footnotes.xml')).toContain('A source.');
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it('embeds the image through a relationship', () => {
|
|
271
|
+
const doc = text('word/document.xml');
|
|
272
|
+
const rels = text('word/_rels/document.xml.rels');
|
|
273
|
+
expect(rels).toContain('Target="media/figure-1.png"');
|
|
274
|
+
expect(doc).toContain('<wp:extent cx="100" cy="50"/>');
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('writes a contents field Word updates, and marks table headers to repeat', () => {
|
|
278
|
+
const doc = text('word/document.xml');
|
|
279
|
+
expect(doc).toContain(
|
|
280
|
+
'<w:instrText xml:space="preserve"> TOC \\o "1-3" \\h \\z \\u </w:instrText>',
|
|
281
|
+
);
|
|
282
|
+
const settings = text('word/settings.xml');
|
|
283
|
+
expect(settings).toContain('<w:updateFields w:val="true"/>');
|
|
284
|
+
// Schema order: Word rejects the part when these are swapped.
|
|
285
|
+
expect(settings.indexOf('characterSpacingControl')).toBeLessThan(
|
|
286
|
+
settings.indexOf('updateFields'),
|
|
287
|
+
);
|
|
288
|
+
expect(settings.indexOf('updateFields')).toBeLessThan(settings.indexOf('footnotePr'));
|
|
289
|
+
expect(doc).toContain('<w:tblHeader/>');
|
|
290
|
+
// An empty cell still ends in a paragraph, or Word calls the file corrupt.
|
|
291
|
+
expect(doc).toContain('</w:tcPr><w:p/></w:tc>');
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('gives each ordered list its own count', () => {
|
|
295
|
+
const numbering = text('word/numbering.xml');
|
|
296
|
+
expect(numbering).toContain('<w:num w:numId="1"><w:abstractNumId w:val="0"/></w:num>');
|
|
297
|
+
expect(numbering).toContain('<w:num w:numId="2"><w:abstractNumId w:val="1"/><w:lvlOverride');
|
|
298
|
+
});
|
|
299
|
+
});
|