@json-to-office/shared 1.2.0 → 1.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/dist/capabilities-DrHJ6_4G.d.ts +210 -0
- package/dist/chunk-BJNBJSQG.js +691 -0
- package/dist/chunk-BJNBJSQG.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/rendering/index.d.ts +177 -181
- package/dist/rendering/index.js +29 -3
- package/package.json +1 -1
- package/dist/chunk-JM5KTMNL.js +0 -240
- package/dist/chunk-JM5KTMNL.js.map +0 -1
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format-independent renderer contracts.
|
|
3
|
+
*
|
|
4
|
+
* This module deliberately knows nothing about DOCX or PPTX semantics. Each
|
|
5
|
+
* format owns its own intermediate representation (`DocxIR`, `PptxIR`) and its
|
|
6
|
+
* own feature union; the only thing shared between them is the shape of the
|
|
7
|
+
* contract a backend adapter must satisfy.
|
|
8
|
+
*
|
|
9
|
+
* Do not add format-specific feature names, IR nodes or units here.
|
|
10
|
+
*/
|
|
11
|
+
/** The Office formats this repository can produce. */
|
|
12
|
+
type OfficeFormat = 'docx' | 'pptx';
|
|
13
|
+
/**
|
|
14
|
+
* Options every renderer accepts.
|
|
15
|
+
*
|
|
16
|
+
* `deterministic` asks the adapter (and the packaging step after it) to make
|
|
17
|
+
* output byte-stable across runs: fixed zip entry timestamps, fixed core
|
|
18
|
+
* metadata timestamps, no random identifiers.
|
|
19
|
+
*
|
|
20
|
+
* `generatedAt` pins the timestamp written into package metadata. Callers that
|
|
21
|
+
* want reproducible bytes pass both.
|
|
22
|
+
*/
|
|
23
|
+
interface RenderOptions {
|
|
24
|
+
deterministic?: boolean;
|
|
25
|
+
generatedAt?: Date;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A backend that turns a format-specific IR into package bytes.
|
|
29
|
+
*
|
|
30
|
+
* @typeParam TIR - the format's intermediate representation (plain data)
|
|
31
|
+
* @typeParam TFeature - the format's feature union (see `capabilities.ts`)
|
|
32
|
+
* @typeParam TId - the string-literal union of renderer ids for the format
|
|
33
|
+
*/
|
|
34
|
+
interface OfficeRenderer<TIR, TFeature extends string, TId extends string> {
|
|
35
|
+
readonly id: TId;
|
|
36
|
+
readonly format: OfficeFormat;
|
|
37
|
+
readonly capabilities: ReadonlySet<TFeature>;
|
|
38
|
+
render(document: TIR, options?: RenderOptions): Promise<Uint8Array>;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Exhaustiveness guard for discriminated-union switches.
|
|
42
|
+
*
|
|
43
|
+
* Reaching this at runtime means an IR node kind was added without a matching
|
|
44
|
+
* `case`, so it throws rather than silently dropping content.
|
|
45
|
+
*/
|
|
46
|
+
declare function assertNever(value: never, context?: string): never;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Diagnostics raised when an IR asks a renderer for something it cannot do.
|
|
50
|
+
*
|
|
51
|
+
* These are distinct from `GenerationWarning` (see `../types/warnings`), which
|
|
52
|
+
* describes authoring problems found while building the document. A renderer
|
|
53
|
+
* diagnostic describes a *backend* limitation: the document is fine, this
|
|
54
|
+
* particular adapter just cannot express part of it.
|
|
55
|
+
*/
|
|
56
|
+
type RendererDiagnosticSeverity = 'error' | 'warning';
|
|
57
|
+
/**
|
|
58
|
+
* One unsupported (or degraded) feature at one place in the IR.
|
|
59
|
+
*
|
|
60
|
+
* `path` is an IR path such as `slides[2].elements[0].fill` — not an author-JSON
|
|
61
|
+
* path — because the check runs against compiled IR. Compilers record the
|
|
62
|
+
* authoring path alongside where it is useful for the message text.
|
|
63
|
+
*/
|
|
64
|
+
interface RendererDiagnostic<TFeature extends string = string> {
|
|
65
|
+
feature: TFeature;
|
|
66
|
+
path: string;
|
|
67
|
+
severity: RendererDiagnosticSeverity;
|
|
68
|
+
message: string;
|
|
69
|
+
}
|
|
70
|
+
interface UnsupportedRendererFeatureErrorInit<TFeature extends string = string> {
|
|
71
|
+
format: OfficeFormat;
|
|
72
|
+
rendererId: string;
|
|
73
|
+
diagnostics: readonly RendererDiagnostic<TFeature>[];
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Aggregated failure thrown *before* rendering starts.
|
|
77
|
+
*
|
|
78
|
+
* One error carries every unsupported feature found in the IR so a caller sees
|
|
79
|
+
* the whole gap at once instead of fixing them one render at a time.
|
|
80
|
+
*/
|
|
81
|
+
declare class UnsupportedRendererFeatureError<TFeature extends string = string> extends Error {
|
|
82
|
+
readonly code = "UNSUPPORTED_RENDERER_FEATURE";
|
|
83
|
+
readonly format: OfficeFormat;
|
|
84
|
+
readonly rendererId: string;
|
|
85
|
+
/** Distinct unsupported features, in first-seen order. */
|
|
86
|
+
readonly features: readonly TFeature[];
|
|
87
|
+
/** Distinct IR paths that required them, in first-seen order. */
|
|
88
|
+
readonly paths: readonly string[];
|
|
89
|
+
/** Every error-severity diagnostic that produced this failure. */
|
|
90
|
+
readonly diagnostics: readonly RendererDiagnostic<TFeature>[];
|
|
91
|
+
constructor(init: UnsupportedRendererFeatureErrorInit<TFeature>);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* A renderer id that is not registered for the format asked for.
|
|
95
|
+
*
|
|
96
|
+
* Caller input, not an infrastructure failure — which is the whole reason it is
|
|
97
|
+
* a class with a `code` rather than a bare `Error`. A server matching on the
|
|
98
|
+
* message text could only answer `500`, so an unknown id looked like the
|
|
99
|
+
* service falling over, and a retry looked worth attempting (#263).
|
|
100
|
+
*/
|
|
101
|
+
declare class UnknownRendererError extends Error {
|
|
102
|
+
readonly code = "UNKNOWN_RENDERER";
|
|
103
|
+
readonly format: OfficeFormat;
|
|
104
|
+
/** What the caller asked for. */
|
|
105
|
+
readonly rendererId: string;
|
|
106
|
+
/** Every id registered for this format, in registration order. */
|
|
107
|
+
readonly availableIds: readonly string[];
|
|
108
|
+
constructor(format: OfficeFormat, rendererId: string, availableIds: readonly string[]);
|
|
109
|
+
}
|
|
110
|
+
/** Build a `RendererDiagnostic` with `severity: 'error'`. */
|
|
111
|
+
declare function rendererError<TFeature extends string>(feature: TFeature, path: string, message: string): RendererDiagnostic<TFeature>;
|
|
112
|
+
/** Build a `RendererDiagnostic` with `severity: 'warning'`. */
|
|
113
|
+
declare function rendererWarning<TFeature extends string>(feature: TFeature, path: string, message: string): RendererDiagnostic<TFeature>;
|
|
114
|
+
/** Split diagnostics into blocking errors and non-blocking warnings. */
|
|
115
|
+
declare function partitionDiagnostics<TFeature extends string>(diagnostics: readonly RendererDiagnostic<TFeature>[]): {
|
|
116
|
+
errors: RendererDiagnostic<TFeature>[];
|
|
117
|
+
warnings: RendererDiagnostic<TFeature>[];
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Capability checking: what an IR *requires* versus what an adapter *provides*.
|
|
122
|
+
*
|
|
123
|
+
* A compiler records one `FeatureRequirement` each time it emits an IR node that
|
|
124
|
+
* needs a backend capability. Before rendering, `assertRendererSupports` diffs
|
|
125
|
+
* those requirements against the adapter's `capabilities` set and throws a
|
|
126
|
+
* single aggregated `UnsupportedRendererFeatureError` if anything is missing.
|
|
127
|
+
*
|
|
128
|
+
* The point is that nothing is dropped silently: a feature either appears in the
|
|
129
|
+
* adapter's capability set and is rendered, or it fails loudly before bytes are
|
|
130
|
+
* produced.
|
|
131
|
+
*/
|
|
132
|
+
/** One capability an IR node needs, and where in the IR it was needed. */
|
|
133
|
+
interface FeatureRequirement<TFeature extends string = string> {
|
|
134
|
+
feature: TFeature;
|
|
135
|
+
/** IR path, e.g. `sections[0].children[3].image`. */
|
|
136
|
+
path: string;
|
|
137
|
+
/** Optional detail folded into the failure message. */
|
|
138
|
+
detail?: string;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Accumulates feature requirements during compilation.
|
|
142
|
+
*
|
|
143
|
+
* Deliberately per-compilation (never module-global) so concurrent generations
|
|
144
|
+
* never share state.
|
|
145
|
+
*/
|
|
146
|
+
declare class FeatureRequirementCollector<TFeature extends string> {
|
|
147
|
+
private readonly requirements;
|
|
148
|
+
private readonly seen;
|
|
149
|
+
/**
|
|
150
|
+
* Record that `feature` is needed at `path`.
|
|
151
|
+
*
|
|
152
|
+
* Duplicate (feature, path) pairs collapse, so a compiler can call this
|
|
153
|
+
* unconditionally inside a loop without inflating the diagnostics.
|
|
154
|
+
*/
|
|
155
|
+
require(feature: TFeature, path: string, detail?: string): void;
|
|
156
|
+
/** Every recorded requirement, in first-seen order. */
|
|
157
|
+
list(): readonly FeatureRequirement<TFeature>[];
|
|
158
|
+
/** Distinct required features, in first-seen order. */
|
|
159
|
+
features(): readonly TFeature[];
|
|
160
|
+
/** True when nothing has been required yet. */
|
|
161
|
+
isEmpty(): boolean;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Diff required features against a capability set.
|
|
165
|
+
*
|
|
166
|
+
* Returns one error-severity diagnostic per unsupported requirement. An empty
|
|
167
|
+
* array means the renderer can render the IR.
|
|
168
|
+
*/
|
|
169
|
+
declare function diagnoseUnsupportedFeatures<TFeature extends string>(required: readonly FeatureRequirement<TFeature>[], capabilities: ReadonlySet<TFeature>, rendererId: string): RendererDiagnostic<TFeature>[];
|
|
170
|
+
/**
|
|
171
|
+
* Throw one aggregated error if the renderer is missing any required feature.
|
|
172
|
+
*
|
|
173
|
+
* Call this after compiling to IR and before handing the IR to an adapter.
|
|
174
|
+
*/
|
|
175
|
+
declare function assertRendererSupports<TFeature extends string>(required: readonly FeatureRequirement<TFeature>[], renderer: Pick<OfficeRenderer<unknown, TFeature, string>, 'id' | 'format' | 'capabilities'>): void;
|
|
176
|
+
/**
|
|
177
|
+
* A registry of renderers for a single format.
|
|
178
|
+
*
|
|
179
|
+
* Instances are created per format module, not per generation, and hold only
|
|
180
|
+
* immutable adapter descriptors — never per-document state.
|
|
181
|
+
*/
|
|
182
|
+
declare class RendererRegistry<TIR, TFeature extends string, TId extends string> {
|
|
183
|
+
private readonly format;
|
|
184
|
+
private readonly defaultId;
|
|
185
|
+
private readonly renderers;
|
|
186
|
+
constructor(format: OfficeFormat, defaultId: TId);
|
|
187
|
+
/**
|
|
188
|
+
* Register a lazily-constructed renderer.
|
|
189
|
+
*
|
|
190
|
+
* The factory is async and only invoked on selection, so an adapter whose
|
|
191
|
+
* backend is an optional dependency is never imported unless it is chosen.
|
|
192
|
+
*/
|
|
193
|
+
register(id: TId, factory: () => Promise<OfficeRenderer<TIR, TFeature, TId>>): void;
|
|
194
|
+
/** Renderer ids registered for this format, in registration order. */
|
|
195
|
+
ids(): readonly TId[];
|
|
196
|
+
/** The id used when a caller does not pass one. */
|
|
197
|
+
getDefaultId(): TId;
|
|
198
|
+
has(id: string): id is TId;
|
|
199
|
+
/**
|
|
200
|
+
* Resolve a renderer, defaulting when `id` is omitted.
|
|
201
|
+
*
|
|
202
|
+
* An unknown id is `UnknownRendererError`, which carries the id asked for and
|
|
203
|
+
* the ones that exist, so a caller boundary can answer "bad request" rather
|
|
204
|
+
* than "the server broke". A missing optional dependency is re-thrown with an
|
|
205
|
+
* actionable install hint.
|
|
206
|
+
*/
|
|
207
|
+
resolve(id?: TId): Promise<OfficeRenderer<TIR, TFeature, TId>>;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export { type FeatureRequirement as F, type OfficeFormat as O, type RenderOptions as R, UnknownRendererError as U, FeatureRequirementCollector as a, type OfficeRenderer as b, type RendererDiagnostic as c, type RendererDiagnosticSeverity as d, RendererRegistry as e, UnsupportedRendererFeatureError as f, type UnsupportedRendererFeatureErrorInit as g, assertNever as h, assertRendererSupports as i, diagnoseUnsupportedFeatures as j, rendererWarning as k, partitionDiagnostics as p, rendererError as r };
|