Skip to content
This repository was archived by the owner on Aug 20, 2026. It is now read-only.

Commit 4dee32a

Browse files
committed
refactor(from-package): build target bytes via documents.js's buildDocumentBytes
The DocumentPackage-to-any-format dispatch (pdf writes the layout directly, every other format rebuilds through the matching buildXPackage, xlsx/odf rejected outright) now lives in documents.js's own buildDocumentBytes rather than a local buildBytesForTarget duplicating the identical switch.
1 parent ed5a97e commit 4dee32a

1 file changed

Lines changed: 2 additions & 48 deletions

File tree

src/commands/from-package.ts

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
import { type Command } from 'commander';
2-
import {
3-
type DocumentFormat,
4-
type DocumentPackage,
5-
UnrecognizedDocumentSchemaError,
6-
buildDocxPackage,
7-
buildMarkdownText,
8-
buildOdgPackage,
9-
buildOdpPackage,
10-
buildOdsPackage,
11-
buildOdtPackage,
12-
buildPptxPackage,
13-
documentFromJson,
14-
encodeMarkdownText,
15-
encodePackage,
16-
writePdf,
17-
} from 'documents.js';
18-
import { encodePackage as encodeOdfPackage } from 'odf.js';
2+
import { UnrecognizedDocumentSchemaError, buildDocumentBytes, documentFromJson } from 'documents.js';
193
import { createRuntimeSignal } from '../runtime/abort';
204
import { createDiagnosticReporter } from '../runtime/diagnostics';
215
import { EXIT_INPUT_ERROR, EXIT_SUCCESS, EXIT_USAGE_ERROR, mapErrorToExit } from '../runtime/exit-codes';
@@ -27,36 +11,6 @@ interface FromPackageCliOptions extends ConversionCliFlags {
2711
readonly to?: string;
2812
}
2913

30-
// Every target this command can build a DocumentPackage into, and how: 'pdf' writes the package's own LayoutDocument half directly (no font registry, no positioned formulas -- neither survives the JSON round trip, since both are side channels a DocumentPackage never carries, so a formula renders as nothing and an embedded font falls back to the standard 14 or a vendored substitute; see the documents.js README's own DocumentPackage gotcha), everything else builds a fresh package from the ContentDocument half through the identical buildXPackage function the matching pdf-to-X/bridge conversion already uses, then encodes it with that format's own codec (ooxml.js's for docx/pptx, odf.js's for odt/odp/ods/odg). 'xlsx' and 'odf' have no builder at all -- documents.js deliberately never re-exports ooxml.js's buildXlsxPackage (see the README's own Architecture note), and a formula document has no write path from ContentDocument to begin with -- so both are rejected outright rather than attempted.
31-
function buildBytesForTarget(pkg: DocumentPackage, target: DocumentFormat): Uint8Array {
32-
if (target === 'pdf') {
33-
if (pkg.layout === undefined) {
34-
throw new Error("this DocumentPackage has no layout -- only a package dumped from a <format>-to-pdf or pdf-to-<format> conversion carries one; a bridge conversion's own dump (e.g. odt-to-docx) never does, so 'pdf' is not a reachable target from it");
35-
}
36-
return writePdf(pkg.layout);
37-
}
38-
switch (target) {
39-
case 'docx':
40-
return encodePackage(buildDocxPackage(pkg.content));
41-
case 'pptx':
42-
return encodePackage(buildPptxPackage(pkg.content));
43-
case 'odt':
44-
return encodeOdfPackage(buildOdtPackage(pkg.content));
45-
case 'odp':
46-
return encodeOdfPackage(buildOdpPackage(pkg.content));
47-
case 'ods':
48-
return encodeOdfPackage(buildOdsPackage(pkg.content));
49-
case 'odg':
50-
return encodeOdfPackage(buildOdgPackage(pkg.content));
51-
case 'markdown':
52-
return encodeMarkdownText(buildMarkdownText(pkg.content));
53-
case 'xlsx':
54-
throw new Error("'xlsx' cannot be built from a DocumentPackage directly -- documents.js does not re-export a ContentDocument-to-xlsx builder; convert to 'ods' here, then run 'ods-to-xlsx' on the result instead");
55-
case 'odf':
56-
throw new Error("'odf' (a standalone formula document) cannot be built from a DocumentPackage -- there is no ContentDocument-to-odf builder");
57-
}
58-
}
59-
6014
async function runFromPackage(input: string, output: string | undefined, options: FromPackageCliOptions): Promise<number> {
6115
const command = 'from-package';
6216

@@ -92,7 +46,7 @@ async function runFromPackage(input: string, output: string | undefined, options
9246
return EXIT_USAGE_ERROR;
9347
}
9448

95-
const bytes = buildBytesForTarget(result.value, target.format);
49+
const bytes = buildDocumentBytes(result.value, target.format);
9650
await writeOutput(resolvedOutput, bytes);
9751

9852
const reporter = createDiagnosticReporter({ json: options.json, quiet: options.quiet, command });

0 commit comments

Comments
 (0)