@publisle/adapter-core 0.2.0 → 0.2.2
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/LICENSE +1 -1
- package/README.md +4 -8
- package/dist/async.d.ts +5 -0
- package/dist/async.d.ts.map +1 -0
- package/dist/async.js +28 -0
- package/dist/async.js.map +1 -0
- package/dist/publication-core.d.ts +76 -0
- package/dist/publication-core.d.ts.map +1 -0
- package/dist/publication-core.js +202 -0
- package/dist/publication-core.js.map +1 -0
- package/dist/publication.d.ts +4 -75
- package/dist/publication.d.ts.map +1 -1
- package/dist/publication.js +8 -200
- package/dist/publication.js.map +1 -1
- package/dist/render-plan-core.d.ts +17 -0
- package/dist/render-plan-core.d.ts.map +1 -0
- package/dist/render-plan-core.js +981 -0
- package/dist/render-plan-core.js.map +1 -0
- package/dist/render-plan.d.ts +4 -14
- package/dist/render-plan.d.ts.map +1 -1
- package/dist/render-plan.js +6 -974
- package/dist/render-plan.js.map +1 -1
- package/package.json +13 -8
- package/src/async.ts +29 -0
- package/src/publication-core.ts +341 -0
- package/src/publication.ts +9 -339
- package/src/render-plan-core.ts +1426 -0
- package/src/render-plan.ts +10 -1416
package/src/publication.ts
CHANGED
|
@@ -1,341 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
JsonValue,
|
|
7
|
-
PreparedDocument,
|
|
8
|
-
PublicationMetadata,
|
|
9
|
-
BlockId,
|
|
10
|
-
} from "@publisle/schema";
|
|
11
|
-
import { serializeNodes } from "./html.ts";
|
|
12
|
-
export {
|
|
13
|
-
instantiatePublication,
|
|
14
|
-
collectPublicationAssets,
|
|
15
|
-
publicationAssetUrl,
|
|
16
|
-
attachPublication,
|
|
17
|
-
} from "./publication-runtime.ts";
|
|
18
|
-
import { createRenderPlan } from "./render-plan.ts";
|
|
19
|
-
import type { AdapterCompilerOptions, RenderPlan } from "./types.ts";
|
|
20
|
-
|
|
21
|
-
function escapeAttribute(value: string): string {
|
|
22
|
-
return value
|
|
23
|
-
.replaceAll("&", "&")
|
|
24
|
-
.replaceAll('"', """)
|
|
25
|
-
.replaceAll("<", "<");
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const PUBLICATION_FORMAT = "publisle:publication" as const;
|
|
29
|
-
export const PUBLICATION_FORMAT_VERSION = 1 as const;
|
|
30
|
-
export const RENDERER_BUILD = "1";
|
|
31
|
-
export const DOCUMENT_STYLESHEET_ID = "publisle/document.css";
|
|
32
|
-
export const KATEX_STYLESHEET_ID = "publisle/katex.css";
|
|
33
|
-
|
|
34
|
-
export interface AssetReference {
|
|
35
|
-
readonly id: string;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface ModuleReference {
|
|
39
|
-
readonly id: string;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface PublishedIsland {
|
|
43
|
-
readonly key: string;
|
|
44
|
-
readonly implementation: string;
|
|
45
|
-
readonly activation: Activation;
|
|
46
|
-
readonly mode: "mount" | "hydrate";
|
|
47
|
-
readonly props: JsonValue;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface PublicationArtifact {
|
|
51
|
-
readonly provenance?: {
|
|
52
|
-
readonly sourceIdentity: string | null;
|
|
53
|
-
readonly semanticIdentity: string;
|
|
54
|
-
readonly contracts: readonly import("@publisle/schema").ContractDependency[];
|
|
55
|
-
readonly resources: import("@publisle/schema").ResourcePlan;
|
|
56
|
-
readonly configuration: JsonValue;
|
|
57
|
-
readonly reproducible: boolean;
|
|
58
|
-
readonly seed?: JsonValue;
|
|
59
|
-
readonly limitation?: string;
|
|
60
|
-
};
|
|
61
|
-
readonly diagnosticIdentity?: string;
|
|
62
|
-
readonly compatibility?: {
|
|
63
|
-
readonly islandInputVersion: 1;
|
|
64
|
-
readonly staticFidelity: "supported" | "fallback" | "unsupported";
|
|
65
|
-
};
|
|
66
|
-
readonly format: typeof PUBLICATION_FORMAT;
|
|
67
|
-
readonly formatVersion: typeof PUBLICATION_FORMAT_VERSION;
|
|
68
|
-
readonly identity: string;
|
|
69
|
-
readonly rendererBuild: string;
|
|
70
|
-
readonly html: string;
|
|
71
|
-
readonly styles: readonly AssetReference[];
|
|
72
|
-
readonly modules: readonly ModuleReference[];
|
|
73
|
-
readonly islands: readonly PublishedIsland[];
|
|
74
|
-
readonly metadata?: PublicationMetadata;
|
|
75
|
-
readonly diagnostics: readonly Diagnostic[];
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export interface PublicationCompilerOptions extends AdapterCompilerOptions {
|
|
79
|
-
/** Host pins for otherwise opaque renderer/compiler configuration. Never infer function source. */
|
|
80
|
-
readonly build?: {
|
|
81
|
-
readonly configuration: JsonValue;
|
|
82
|
-
readonly reproducible: boolean;
|
|
83
|
-
readonly seed?: JsonValue;
|
|
84
|
-
};
|
|
85
|
-
readonly styles?: "minimal" | "none";
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export interface PublicationPlacement {
|
|
89
|
-
readonly instanceId: string;
|
|
90
|
-
readonly html: string;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/** Minimal browser attachment data; excludes HTML, source AST, resources and diagnostics. */
|
|
94
|
-
export type PublicationReaderManifest = Pick<
|
|
95
|
-
PublicationArtifact,
|
|
96
|
-
"format" | "formatVersion" | "identity" | "compatibility" | "islands"
|
|
97
|
-
>;
|
|
98
|
-
export function publicationReaderManifest(
|
|
99
|
-
artifact: PublicationArtifact,
|
|
100
|
-
): PublicationReaderManifest {
|
|
101
|
-
return {
|
|
102
|
-
format: artifact.format,
|
|
103
|
-
formatVersion: artifact.formatVersion,
|
|
104
|
-
identity: artifact.identity,
|
|
105
|
-
...(artifact.compatibility === undefined
|
|
106
|
-
? {}
|
|
107
|
-
: { compatibility: artifact.compatibility }),
|
|
108
|
-
islands: artifact.islands,
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export interface PublicationEnvironment {
|
|
113
|
-
readonly implementations?: Readonly<Record<string, () => Promise<unknown>>>;
|
|
114
|
-
readonly mount?: (
|
|
115
|
-
module: unknown,
|
|
116
|
-
target: HTMLElement,
|
|
117
|
-
props: JsonValue,
|
|
118
|
-
services: Readonly<Record<string, unknown>> | undefined,
|
|
119
|
-
) => unknown;
|
|
120
|
-
readonly unmount?: (instance: unknown) => void;
|
|
121
|
-
readonly services?: Readonly<Record<string, unknown>>;
|
|
122
|
-
readonly assetBase?: string;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export interface PublicationHandle {
|
|
126
|
-
dispose(): void;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function isRenderPlan(
|
|
130
|
-
source: PreparedDocument | RenderPlan,
|
|
131
|
-
): source is RenderPlan {
|
|
132
|
-
return "nodes" in source && "document" in source;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function withIslandRenderers(
|
|
136
|
-
document: PreparedDocument,
|
|
137
|
-
options: AdapterCompilerOptions,
|
|
138
|
-
): AdapterCompilerOptions {
|
|
139
|
-
const renderers = { ...(options.renderers ?? {}) };
|
|
140
|
-
for (const island of document.islands) {
|
|
141
|
-
renderers[island.blockType] ??= { module: island.blockType };
|
|
142
|
-
}
|
|
143
|
-
return { ...options, renderers };
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function compilePlan(
|
|
147
|
-
plan: RenderPlan,
|
|
148
|
-
styles: "minimal" | "none",
|
|
149
|
-
build?: PublicationCompilerOptions["build"],
|
|
150
|
-
rawHtmlPlacement: "reject" | "preserve" = "reject",
|
|
151
|
-
): PublicationArtifact {
|
|
152
|
-
const islands: PublishedIsland[] = [];
|
|
153
|
-
const diagnostics = [...plan.diagnostics];
|
|
154
|
-
let fidelity: "supported" | "fallback" | "unsupported" = "supported";
|
|
155
|
-
const substantive = (nodes: RenderPlan["nodes"]): boolean =>
|
|
156
|
-
nodes.some((node) => {
|
|
157
|
-
if (node.kind === "text") return node.value.trim().length > 0;
|
|
158
|
-
if (node.kind === "raw")
|
|
159
|
-
return (
|
|
160
|
-
node.value.replace(/<[^>]*>/gu, "").trim().length > 0 ||
|
|
161
|
-
/<(?:img|svg|video|audio)\b/iu.test(node.value)
|
|
162
|
-
);
|
|
163
|
-
if (node.kind === "element")
|
|
164
|
-
return (
|
|
165
|
-
["img", "svg", "video", "audio", "iframe", "canvas"].includes(
|
|
166
|
-
node.tag,
|
|
167
|
-
) || substantive(node.children)
|
|
168
|
-
);
|
|
169
|
-
return node.kind === "component"
|
|
170
|
-
? substantive(node.artifact ?? [])
|
|
171
|
-
: substantive(node.fallback);
|
|
172
|
-
});
|
|
173
|
-
const lower = (nodes: RenderPlan["nodes"]): RenderPlan["nodes"] =>
|
|
174
|
-
nodes.flatMap((node) => {
|
|
175
|
-
if (
|
|
176
|
-
node.kind === "raw" &&
|
|
177
|
-
/\b(?:id|for|headers|list|form|href|aria-labelledby|aria-describedby|aria-controls|aria-owns|aria-flowto|aria-activedescendant|aria-details|aria-errormessage)\s*=/iu.test(
|
|
178
|
-
node.value,
|
|
179
|
-
)
|
|
180
|
-
) {
|
|
181
|
-
diagnostics.push({
|
|
182
|
-
level: rawHtmlPlacement === "reject" ? "error" : "warning",
|
|
183
|
-
code: "raw-html-placement-unscoped",
|
|
184
|
-
message:
|
|
185
|
-
"Authored trusted HTML IDs/references cannot be generically namespaced. Host must reject it or explicitly preserve it with placement restrictions.",
|
|
186
|
-
});
|
|
187
|
-
if (rawHtmlPlacement === "reject") {
|
|
188
|
-
fidelity = "unsupported";
|
|
189
|
-
return [];
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
if (node.kind === "component") {
|
|
193
|
-
const replacement = node.artifact ?? [];
|
|
194
|
-
const supported = substantive(replacement);
|
|
195
|
-
if (!supported) fidelity = "unsupported";
|
|
196
|
-
else if (fidelity !== "unsupported") fidelity = "fallback";
|
|
197
|
-
diagnostics.push({
|
|
198
|
-
level: supported ? "warning" : "error",
|
|
199
|
-
code: supported
|
|
200
|
-
? "artifact-component-lowered"
|
|
201
|
-
: "unsupported-artifact-component",
|
|
202
|
-
message: supported
|
|
203
|
-
? "Artifact uses approved static lowering/authored fallback; native component fidelity is not implied."
|
|
204
|
-
: "Framework component has no static lowering or substantive authored fallback.",
|
|
205
|
-
...(node.blockId === undefined
|
|
206
|
-
? {}
|
|
207
|
-
: { blockId: node.blockId as BlockId }),
|
|
208
|
-
});
|
|
209
|
-
return lower(replacement);
|
|
210
|
-
}
|
|
211
|
-
if (node.kind === "element")
|
|
212
|
-
return [{ ...node, children: lower(node.children) }];
|
|
213
|
-
if (node.kind === "island")
|
|
214
|
-
return [{ ...node, fallback: lower(node.fallback) }];
|
|
215
|
-
return [node];
|
|
216
|
-
});
|
|
217
|
-
const nodes = lower(plan.nodes);
|
|
218
|
-
const modules: ModuleReference[] = [];
|
|
219
|
-
const seen = new Set<string>();
|
|
220
|
-
const visit = (nodes: RenderPlan["nodes"]): void => {
|
|
221
|
-
for (const node of nodes) {
|
|
222
|
-
if (node.kind === "island") {
|
|
223
|
-
const block = plan.document.blocks.find(
|
|
224
|
-
(entry) => entry.id === node.blockId,
|
|
225
|
-
);
|
|
226
|
-
const implementation =
|
|
227
|
-
node.implementation ?? block?.type ?? node.module;
|
|
228
|
-
islands.push({
|
|
229
|
-
key: node.blockId,
|
|
230
|
-
implementation,
|
|
231
|
-
activation: node.activation,
|
|
232
|
-
mode: "mount",
|
|
233
|
-
props: node.props,
|
|
234
|
-
});
|
|
235
|
-
if (!seen.has(implementation)) {
|
|
236
|
-
seen.add(implementation);
|
|
237
|
-
modules.push({ id: implementation });
|
|
238
|
-
}
|
|
239
|
-
continue;
|
|
240
|
-
}
|
|
241
|
-
if (node.kind === "element") visit(node.children);
|
|
242
|
-
}
|
|
243
|
-
};
|
|
244
|
-
visit(nodes);
|
|
245
|
-
const metadata = plan.metadata;
|
|
246
|
-
const language = metadata?.language;
|
|
247
|
-
const direction = metadata?.direction;
|
|
248
|
-
const root = [
|
|
249
|
-
'class="publisle-document"',
|
|
250
|
-
"data-publisle-root",
|
|
251
|
-
...(typeof language === "string" && language
|
|
252
|
-
? [`lang="${escapeAttribute(language)}"`]
|
|
253
|
-
: []),
|
|
254
|
-
...(direction === "ltr" || direction === "rtl" || direction === "auto"
|
|
255
|
-
? [`dir="${direction}"`]
|
|
256
|
-
: []),
|
|
257
|
-
].join(" ");
|
|
258
|
-
const html = `<div ${root}>${serializeNodes(nodes)}</div>`;
|
|
259
|
-
const hasMath = plan.document.blocks.some(
|
|
260
|
-
(block) =>
|
|
261
|
-
block.type === "publisle:math" ||
|
|
262
|
-
JSON.stringify(block.data).includes('"inlineMath"'),
|
|
263
|
-
);
|
|
264
|
-
const styleEntries =
|
|
265
|
-
styles === "none"
|
|
266
|
-
? []
|
|
267
|
-
: [
|
|
268
|
-
{ id: DOCUMENT_STYLESHEET_ID },
|
|
269
|
-
...(hasMath ? [{ id: KATEX_STYLESHEET_ID }] : []),
|
|
270
|
-
];
|
|
271
|
-
const unsigned = {
|
|
272
|
-
format: PUBLICATION_FORMAT,
|
|
273
|
-
formatVersion: PUBLICATION_FORMAT_VERSION,
|
|
274
|
-
rendererBuild: RENDERER_BUILD,
|
|
275
|
-
html,
|
|
276
|
-
styles: styleEntries,
|
|
277
|
-
modules,
|
|
278
|
-
islands,
|
|
279
|
-
...(metadata === undefined ? {} : { metadata }),
|
|
280
|
-
compatibility: { islandInputVersion: 1 as const, staticFidelity: fidelity },
|
|
281
|
-
provenance: {
|
|
282
|
-
sourceIdentity: plan.document.sourceIdentity ?? null,
|
|
283
|
-
semanticIdentity:
|
|
284
|
-
plan.document.semanticIdentity ?? plan.document.cacheIdentity,
|
|
285
|
-
contracts: plan.document.dependencies ?? [],
|
|
286
|
-
resources: plan.document.resources,
|
|
287
|
-
configuration: build?.configuration ?? {
|
|
288
|
-
styles,
|
|
289
|
-
rendererBuild: RENDERER_BUILD,
|
|
290
|
-
},
|
|
291
|
-
reproducible: build?.reproducible ?? false,
|
|
292
|
-
...(build?.seed === undefined ? {} : { seed: build.seed }),
|
|
293
|
-
...(build === undefined
|
|
294
|
-
? {
|
|
295
|
-
limitation:
|
|
296
|
-
"Host compiler/renderer configuration and nondeterministic inputs have not been pinned. No byte-reproduction claim.",
|
|
297
|
-
}
|
|
298
|
-
: {}),
|
|
299
|
-
},
|
|
300
|
-
};
|
|
301
|
-
const artifact: PublicationArtifact = {
|
|
302
|
-
...unsigned,
|
|
303
|
-
diagnostics,
|
|
304
|
-
diagnosticIdentity: sha256Hex(canonicalizeJson(diagnostics)),
|
|
305
|
-
identity: sha256Hex(canonicalizeJson(unsigned)),
|
|
306
|
-
};
|
|
307
|
-
return artifact;
|
|
308
|
-
}
|
|
309
|
-
|
|
1
|
+
import katex from "katex";
|
|
2
|
+
import { installMathRenderer } from "./render-plan-core.ts";
|
|
3
|
+
import { compilePublication as compile } from "./publication-core.ts";
|
|
4
|
+
export * from "./publication-core.ts";
|
|
5
|
+
/** Synchronous compiler with bundled math support. */
|
|
310
6
|
export function compilePublication(
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
)
|
|
314
|
-
|
|
315
|
-
if (isRenderPlan(source))
|
|
316
|
-
return compilePlan(source, styles, options.build, options.rawHtmlPlacement);
|
|
317
|
-
const compilerOptions: AdapterCompilerOptions = {
|
|
318
|
-
...(options.citations === undefined
|
|
319
|
-
? {}
|
|
320
|
-
: { citations: options.citations }),
|
|
321
|
-
...(options.bibliography === undefined
|
|
322
|
-
? {}
|
|
323
|
-
: { bibliography: options.bibliography }),
|
|
324
|
-
...(options.renderers === undefined
|
|
325
|
-
? {}
|
|
326
|
-
: { renderers: options.renderers }),
|
|
327
|
-
...(options.rawHtml === undefined ? {} : { rawHtml: options.rawHtml }),
|
|
328
|
-
...(options.embedProviders === undefined
|
|
329
|
-
? {}
|
|
330
|
-
: { embedProviders: options.embedProviders }),
|
|
331
|
-
...(options.diagramRenderers === undefined
|
|
332
|
-
? {}
|
|
333
|
-
: { diagramRenderers: options.diagramRenderers }),
|
|
334
|
-
};
|
|
335
|
-
return compilePlan(
|
|
336
|
-
createRenderPlan(source, withIslandRenderers(source, compilerOptions)),
|
|
337
|
-
styles,
|
|
338
|
-
options.build,
|
|
339
|
-
options.rawHtmlPlacement,
|
|
340
|
-
);
|
|
7
|
+
...args: Parameters<typeof compile>
|
|
8
|
+
): ReturnType<typeof compile> {
|
|
9
|
+
installMathRenderer(katex);
|
|
10
|
+
return compile(...args);
|
|
341
11
|
}
|