Chapter numbering rides the heading styles' w:numPr, so re-import read every
chapter as a list item — which swallowed the page break in front of it, since
the break is read for body paragraphs only. Headings now also write their own
w:outlineLvl, so a document setting its chapters in a style of its own
("Appendix 1") keeps them headings. SVG goes out rasterized: Word reads none,
and the importer makes one of every EMF too.
The corpus cross-format leg now carries the file's own stylesheet, where both
the numbering and a heading's style live; measured on one fixture, the leg
ODT → .docx → back loses 0 of 34 headings and 0 of 7 page breaks, was 7 and 7.
101 lines
5.0 KiB
TypeScript
101 lines
5.0 KiB
TypeScript
// The committed corpus (`tests/corpus/`: documents the `docx` lib writes, plus the ODT
|
|
// twins LibreOffice converts them into) read as documents rather than as renderer
|
|
// fixtures — a file this editor did not write has to import, survive our own export,
|
|
// and read back the same.
|
|
import { describe, it, expect } from 'vitest';
|
|
import { readFileSync, readdirSync, existsSync } from 'node:fs';
|
|
import { join, dirname } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { buildOdt } from '../src/lib/export/odt';
|
|
import { importOdt } from '../src/lib/import/odt';
|
|
import { buildDocx } from '../src/lib/export/docx';
|
|
import { importDocx } from '../src/lib/import/docx';
|
|
|
|
type N = any;
|
|
|
|
const FIX = join(dirname(fileURLToPath(import.meta.url)), 'corpus');
|
|
// `make-fixtures.mjs` writes them; a real-world document stays out of the repo, in the
|
|
// gitignored parity fixture directory, and out of this run.
|
|
const files = existsSync(FIX)
|
|
? readdirSync(FIX).filter((f) => /^\d\d-.+\.(docx|odt)$/.test(f)).sort()
|
|
: [];
|
|
|
|
const load = (f: string) => new Uint8Array(readFileSync(join(FIX, f)));
|
|
const readAny = (f: string, bytes: Uint8Array) =>
|
|
f.endsWith('.odt') ? importOdt(bytes) : importDocx(bytes);
|
|
const importAny = (f: string, bytes: Uint8Array): N => readAny(f, bytes).content;
|
|
|
|
// A block's type and its text, flattened depth-first: the shape a round trip must keep.
|
|
// The heading attrs ride along: a chapter that loses its level, its own style or the
|
|
// break in front of it still says the same words, and reflows the document anyway.
|
|
function outline(node: N, out: string[] = []): string[] {
|
|
for (const child of node.content ?? []) {
|
|
if (child.type === 'text') continue;
|
|
const a = child.type === 'heading' ? child.attrs ?? {} : null;
|
|
const head = a ? `heading${a.level}${a.styleName ? `[${a.styleName}]` : ''}${a.breakBefore ? '+brk' : ''}` : child.type;
|
|
out.push(`${head}:${textOf(child).replace(/\s+/g, ' ').trim()}`);
|
|
outline(child, out);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function textOf(node: N): string {
|
|
if (node.type === 'text') return node.text ?? '';
|
|
return (node.content ?? []).map(textOf).join('');
|
|
}
|
|
|
|
describe.skipIf(!files.length)('the authored corpus', () => {
|
|
it('has both formats of every document', () => {
|
|
const docx = files.filter((f) => f.endsWith('.docx')).map((f) => f.slice(0, -5));
|
|
const odt = files.filter((f) => f.endsWith('.odt')).map((f) => f.slice(0, -4));
|
|
expect(odt).toEqual(docx);
|
|
});
|
|
|
|
for (const f of files) {
|
|
it(`${f} imports and survives its own round trip`, async () => {
|
|
const doc = importAny(f, load(f));
|
|
expect(outline(doc).length).toBeGreaterThan(0);
|
|
const margins = { top: 2, bottom: 2, left: 2, right: 2 };
|
|
const again = f.endsWith('.odt')
|
|
? importOdt(await buildOdt(doc, margins, 'portrait'))
|
|
: importDocx(await buildDocx(doc, margins, 'portrait'));
|
|
expect(outline(again.content)).toEqual(outline(doc));
|
|
});
|
|
}
|
|
|
|
// Word re-saves of the same documents (tests/corpus/word/): Word rewrites the file
|
|
// in its own dialect — rsids, proofErr, separator notes, theme docDefaults — which
|
|
// neither the docx lib nor LibreOffice produce, so this is the third author here.
|
|
const WORD = join(FIX, 'word');
|
|
const wordFiles = existsSync(WORD)
|
|
? readdirSync(WORD).filter((f) => /^\d\d-.+\.docx$/.test(f)).sort()
|
|
: [];
|
|
for (const f of wordFiles) {
|
|
it(`word/${f} reads like the original and survives the round trip`, async () => {
|
|
const doc = importDocx(new Uint8Array(readFileSync(join(WORD, f)))).content as N;
|
|
expect(outline(doc), 'matches the docx-lib original').toEqual(outline(importAny(f, load(f))));
|
|
const margins = { top: 2, bottom: 2, left: 2, right: 2 };
|
|
const again = importDocx(await buildDocx(doc, margins, 'portrait'));
|
|
expect(outline(again.content)).toEqual(outline(doc));
|
|
});
|
|
}
|
|
|
|
// The same document in both formats, and each one exported as the other: the four
|
|
// legs a document takes through this editor have to agree on what it says. The file's
|
|
// **own stylesheet** rides the cross legs, as it does when the app saves — chapter
|
|
// numbering and a heading's own style live there and nowhere in the document tree, so
|
|
// an export handed builtinStyleSheet() never carries either.
|
|
for (const name of files.filter((f) => f.endsWith('.docx')).map((f) => f.slice(0, -5))) {
|
|
it(`${name} reads the same out of either format`, async () => {
|
|
const docx = readAny(`${name}.docx`, load(`${name}.docx`));
|
|
const odt = readAny(`${name}.odt`, load(`${name}.odt`));
|
|
expect(outline(odt.content)).toEqual(outline(docx.content));
|
|
const margins = { top: 2, bottom: 2, left: 2, right: 2 };
|
|
const asOdt = await buildOdt(docx.content, margins, 'portrait', undefined, undefined, undefined, docx.styles);
|
|
expect(outline(importOdt(asOdt).content)).toEqual(outline(docx.content));
|
|
const asDocx = await buildDocx(odt.content, margins, 'portrait', undefined, undefined, undefined, odt.styles);
|
|
expect(outline(importDocx(asDocx).content)).toEqual(outline(odt.content));
|
|
});
|
|
}
|
|
});
|