@stream-mdx/react 0.4.0 → 1.0.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.
Files changed (106) hide show
  1. package/CHANGELOG.md +385 -4
  2. package/README.md +141 -16
  3. package/dist/blocks-renderer-DAOBFBfi.d.ts +29 -0
  4. package/dist/blocks-renderer-DQEL1kJW.d.cts +29 -0
  5. package/dist/components/bottom-stick-scroll-area.cjs +268 -0
  6. package/dist/components/bottom-stick-scroll-area.d.cts +36 -0
  7. package/dist/components/bottom-stick-scroll-area.d.ts +36 -0
  8. package/dist/components/bottom-stick-scroll-area.mjs +249 -0
  9. package/dist/components/diff-renderer.cjs +86 -0
  10. package/dist/components/diff-renderer.d.cts +11 -0
  11. package/dist/components/diff-renderer.d.ts +11 -0
  12. package/dist/components/diff-renderer.mjs +66 -0
  13. package/dist/components/index.cjs +2879 -1295
  14. package/dist/components/index.d.cts +9 -2
  15. package/dist/components/index.d.ts +9 -2
  16. package/dist/components/index.mjs +2842 -1277
  17. package/dist/components/katex-math.cjs +220 -0
  18. package/dist/components/katex-math.d.cts +10 -0
  19. package/dist/components/katex-math.d.ts +10 -0
  20. package/dist/components/katex-math.mjs +188 -0
  21. package/dist/contexts/math-tracker.cjs +240 -521
  22. package/dist/contexts/math-tracker.mjs +235 -513
  23. package/dist/index-Cmne1yUK.d.cts +723 -0
  24. package/dist/index-D7dyyu3q.d.ts +723 -0
  25. package/dist/index.cjs +13180 -5640
  26. package/dist/index.d.cts +72 -40
  27. package/dist/index.d.ts +72 -40
  28. package/dist/index.mjs +12587 -5038
  29. package/dist/math/display-wrapper.cjs +7 -15
  30. package/dist/math/display-wrapper.d.cts +2 -2
  31. package/dist/math/display-wrapper.d.ts +2 -2
  32. package/dist/math/display-wrapper.mjs +2 -6
  33. package/dist/node-views-Bpc6Gkgl.d.cts +13 -0
  34. package/dist/node-views-DpnYdaAS.d.ts +13 -0
  35. package/dist/renderer/hooks.cjs +35 -20
  36. package/dist/renderer/hooks.d.cts +22 -1
  37. package/dist/renderer/hooks.d.ts +22 -1
  38. package/dist/renderer/hooks.mjs +27 -12
  39. package/dist/renderer/patch-batching.cjs +7 -8
  40. package/dist/renderer/patch-batching.d.cts +1 -1
  41. package/dist/renderer/patch-batching.d.ts +1 -1
  42. package/dist/renderer/patch-batching.mjs +2 -1
  43. package/dist/renderer/patch-coalescing.cjs +5 -8
  44. package/dist/renderer/patch-commit-scheduler.cjs +160 -326
  45. package/dist/renderer/patch-commit-scheduler.d.cts +28 -4
  46. package/dist/renderer/patch-commit-scheduler.d.ts +28 -4
  47. package/dist/renderer/patch-commit-scheduler.mjs +157 -318
  48. package/dist/renderer/store.cjs +3847 -1099
  49. package/dist/renderer/store.d.cts +111 -3
  50. package/dist/renderer/store.d.ts +111 -3
  51. package/dist/renderer/store.mjs +3847 -1085
  52. package/dist/renderer/virtualized-code.cjs +415 -71
  53. package/dist/renderer/virtualized-code.d.cts +24 -23
  54. package/dist/renderer/virtualized-code.d.ts +24 -23
  55. package/dist/renderer/virtualized-code.mjs +409 -62
  56. package/dist/renderer.cjs +10939 -3974
  57. package/dist/renderer.d.cts +100 -29
  58. package/dist/renderer.d.ts +100 -29
  59. package/dist/renderer.mjs +10711 -3697
  60. package/dist/server-regions-CPLHGli0.d.cts +32 -0
  61. package/dist/server-regions-mUhyoKi7.d.ts +32 -0
  62. package/dist/server.cjs +10286 -3365
  63. package/dist/server.d.cts +17 -247
  64. package/dist/server.d.ts +17 -247
  65. package/dist/server.mjs +10565 -3619
  66. package/dist/{streaming-markdown-DOc2uLiP.d.ts → streaming-markdown-yELbJxyH.d.ts} +51 -55
  67. package/dist/{streaming-markdown-D-JcByd9.d.cts → streaming-markdown-zVwFuaJG.d.cts} +51 -55
  68. package/dist/streaming-markdown.cjs +11828 -4879
  69. package/dist/streaming-markdown.d.cts +9 -2
  70. package/dist/streaming-markdown.d.ts +9 -2
  71. package/dist/streaming-markdown.mjs +11820 -4821
  72. package/dist/trusted-mdx/index.cjs +833 -0
  73. package/dist/trusted-mdx/index.d.cts +11 -0
  74. package/dist/trusted-mdx/index.d.ts +11 -0
  75. package/dist/trusted-mdx/index.mjs +807 -0
  76. package/dist/trusted-mdx/native-children-slot.cjs +45 -0
  77. package/dist/trusted-mdx/native-children-slot.d.cts +11 -0
  78. package/dist/trusted-mdx/native-children-slot.d.ts +11 -0
  79. package/dist/trusted-mdx/native-children-slot.mjs +24 -0
  80. package/dist/trusted-mdx/server.cjs +752 -0
  81. package/dist/trusted-mdx/server.d.cts +24 -0
  82. package/dist/trusted-mdx/server.d.ts +24 -0
  83. package/dist/trusted-mdx/server.mjs +724 -0
  84. package/dist/trusted-mdx-contract-ElKpkDI5.d.cts +113 -0
  85. package/dist/trusted-mdx-contract-ElKpkDI5.d.ts +113 -0
  86. package/dist/utils/inline-html.cjs +246 -147
  87. package/dist/utils/inline-html.d.cts +39 -6
  88. package/dist/utils/inline-html.d.ts +39 -6
  89. package/dist/utils/inline-html.mjs +237 -134
  90. package/package.json +52 -18
  91. package/shadcn/README.md +28 -0
  92. package/shadcn/bottom-stick-scroll-area.tsx +358 -0
  93. package/dist/index-D2MYusAM.d.cts +0 -248
  94. package/dist/index-D2MYusAM.d.ts +0 -248
  95. package/dist/mdx-client.cjs +0 -408
  96. package/dist/mdx-client.d.cts +0 -113
  97. package/dist/mdx-client.d.ts +0 -113
  98. package/dist/mdx-client.mjs +0 -369
  99. package/dist/mdx-coordinator.cjs +0 -563
  100. package/dist/mdx-coordinator.d.cts +0 -21
  101. package/dist/mdx-coordinator.d.ts +0 -21
  102. package/dist/mdx-coordinator.mjs +0 -529
  103. package/dist/renderer/node-views.cjs +0 -1642
  104. package/dist/renderer/node-views.d.cts +0 -12
  105. package/dist/renderer/node-views.d.ts +0 -12
  106. package/dist/renderer/node-views.mjs +0 -1616
@@ -1,563 +0,0 @@
1
- "use client";
2
- "use strict";
3
- "use client";
4
- var __create = Object.create;
5
- var __defProp = Object.defineProperty;
6
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
- var __getOwnPropNames = Object.getOwnPropertyNames;
8
- var __getProtoOf = Object.getPrototypeOf;
9
- var __hasOwnProp = Object.prototype.hasOwnProperty;
10
- var __export = (target, all) => {
11
- for (var name in all)
12
- __defProp(target, name, { get: all[name], enumerable: true });
13
- };
14
- var __copyProps = (to, from, except, desc) => {
15
- if (from && typeof from === "object" || typeof from === "function") {
16
- for (let key of __getOwnPropNames(from))
17
- if (!__hasOwnProp.call(to, key) && key !== except)
18
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
19
- }
20
- return to;
21
- };
22
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
23
- // If the importer is in node compatibility mode or this is not an ESM
24
- // file that has been converted to a CommonJS file using a Babel-
25
- // compatible transform (i.e. "__esModule" has not been set), then set
26
- // "default" to the CommonJS "module.exports" for node compatibility.
27
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
28
- mod
29
- ));
30
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
31
-
32
- // src/mdx-coordinator.ts
33
- var mdx_coordinator_exports = {};
34
- __export(mdx_coordinator_exports, {
35
- useMdxCoordinator: () => useMdxCoordinator
36
- });
37
- module.exports = __toCommonJS(mdx_coordinator_exports);
38
- var import_react2 = require("react");
39
-
40
- // src/mdx-client.ts
41
- var import_react = __toESM(require("react"), 1);
42
- var ReactJsxRuntime = __toESM(require("react/jsx-runtime"), 1);
43
- var withClassName = (tag, className) => {
44
- return (props) => import_react.default.createElement(tag, { className, ...props });
45
- };
46
- function assignMdxChildKey(child, index) {
47
- if (child == null) return child;
48
- if (Array.isArray(child)) {
49
- return child.map((entry, subIndex) => assignMdxChildKey(entry, `${index}-${subIndex}`));
50
- }
51
- if (import_react.default.isValidElement(child)) {
52
- if (child.key !== null && child.key !== void 0) {
53
- return child;
54
- }
55
- return import_react.default.cloneElement(child, { key: index });
56
- }
57
- return child;
58
- }
59
- var MDXClient = class {
60
- constructor(compileEndpoint = "/api/mdx-compile-v2") {
61
- this.cache = /* @__PURE__ */ new Map();
62
- this.inlineModules = /* @__PURE__ */ new Map();
63
- this.compiledCache = /* @__PURE__ */ new Map();
64
- this.compiledInflight = /* @__PURE__ */ new Map();
65
- this.compileEndpoint = compileEndpoint;
66
- }
67
- /**
68
- * Compile MDX block on server
69
- */
70
- async compile(block) {
71
- if (block.type !== "mdx") {
72
- throw new Error("Block is not MDX type");
73
- }
74
- const cacheKey = block.id;
75
- const cachedPromise = this.cache.get(cacheKey);
76
- if (cachedPromise) {
77
- return cachedPromise;
78
- }
79
- const compilationPromise = this.doCompile(block);
80
- this.cache.set(cacheKey, compilationPromise);
81
- try {
82
- const result = await compilationPromise;
83
- return result;
84
- } catch (error) {
85
- this.cache.delete(cacheKey);
86
- throw error;
87
- }
88
- }
89
- async getCompiledInternal(ref) {
90
- const inline = this.inlineModules.get(ref.id);
91
- if (inline) {
92
- return this.cloneCompiled(inline);
93
- }
94
- const cached = this.compiledCache.get(ref.id);
95
- if (cached) {
96
- return this.cloneCompiled(cached);
97
- }
98
- const inflight = this.compiledInflight.get(ref.id);
99
- if (inflight) {
100
- const resolved = await inflight;
101
- return this.cloneCompiled(resolved);
102
- }
103
- const fetchPromise = this.fetchCompiled(ref);
104
- this.compiledInflight.set(ref.id, fetchPromise);
105
- try {
106
- const data = await fetchPromise;
107
- this.compiledCache.set(ref.id, data);
108
- return this.cloneCompiled(data);
109
- } finally {
110
- this.compiledInflight.delete(ref.id);
111
- }
112
- }
113
- async getCompiled(ref, options) {
114
- if (options?.signal) {
115
- const inline = this.inlineModules.get(ref.id);
116
- if (inline) {
117
- return this.cloneCompiled(inline);
118
- }
119
- const cached = this.compiledCache.get(ref.id);
120
- if (cached) {
121
- return this.cloneCompiled(cached);
122
- }
123
- const data = await this.fetchCompiled(ref, options);
124
- this.compiledCache.set(ref.id, data);
125
- return this.cloneCompiled(data);
126
- }
127
- return this.getCompiledInternal(ref);
128
- }
129
- /**
130
- * Clear compilation cache
131
- */
132
- clearCache() {
133
- this.cache.clear();
134
- this.compiledCache.clear();
135
- this.compiledInflight.clear();
136
- }
137
- registerInlineModule(compiled) {
138
- this.inlineModules.set(compiled.id, {
139
- ...compiled,
140
- dependencies: Array.isArray(compiled.dependencies) ? [...compiled.dependencies] : []
141
- });
142
- }
143
- clearInlineModules() {
144
- this.inlineModules.clear();
145
- }
146
- /**
147
- * Actual compilation implementation
148
- */
149
- async doCompile(block) {
150
- const response = await fetch(this.compileEndpoint, {
151
- method: "POST",
152
- headers: {
153
- "Content-Type": "application/json"
154
- },
155
- body: JSON.stringify({
156
- content: block.payload.raw,
157
- blockId: block.id
158
- })
159
- });
160
- if (!response.ok) {
161
- const error = await response.json().catch(() => ({ error: "Unknown error" }));
162
- throw new Error(`MDX compilation failed: ${error.error || response.statusText}`);
163
- }
164
- const data = await response.json();
165
- return {
166
- id: data.id,
167
- code: data.code,
168
- dependencies: data.dependencies || [],
169
- cached: data.cached || false
170
- };
171
- }
172
- async fetchCompiled(ref, options) {
173
- const response = await fetch(`${this.compileEndpoint}?id=${ref.id}`, {
174
- signal: options?.signal
175
- });
176
- if (!response.ok) {
177
- throw new Error(`Failed to get compiled MDX: ${response.statusText}`);
178
- }
179
- const data = await response.json();
180
- return {
181
- id: data.id,
182
- code: data.code,
183
- dependencies: data.dependencies || [],
184
- timestamp: data.timestamp
185
- };
186
- }
187
- cloneCompiled(compiled) {
188
- return {
189
- ...compiled,
190
- dependencies: Array.isArray(compiled.dependencies) ? [...compiled.dependencies] : []
191
- };
192
- }
193
- };
194
- var MDXComponentFactory = class {
195
- constructor(mdxClient) {
196
- this.components = /* @__PURE__ */ new Map();
197
- this.customComponents = /* @__PURE__ */ new Map();
198
- this.mdxClient = mdxClient;
199
- this.baseComponents = this.createBaseComponents();
200
- }
201
- registerInlineModule(module2) {
202
- this.mdxClient.registerInlineModule(module2);
203
- this.components.delete(module2.id);
204
- }
205
- /**
206
- * Create React component from compiled MDX
207
- */
208
- async createComponent(ref) {
209
- const cached = this.components.get(ref.id);
210
- if (cached) {
211
- return cached;
212
- }
213
- const compiled = await this.mdxClient.getCompiled(ref);
214
- const component = this.evaluateCompiledMDX(compiled);
215
- this.components.set(ref.id, component);
216
- return component;
217
- }
218
- /**
219
- * Safely evaluate compiled MDX code
220
- */
221
- evaluateCompiledMDX(compiled) {
222
- try {
223
- const context = this.createEvaluationContext();
224
- const argNames = Object.keys(context);
225
- const argValues = Object.values(context);
226
- const moduleExports = new Function(...argNames, compiled.code)(...argValues);
227
- const MDXComponent = this.resolveEvaluatedModule(moduleExports);
228
- return (props) => {
229
- const registryComponents = this.getComponents();
230
- const incomingComponents = props?.components;
231
- const mergedComponents = incomingComponents ? { ...registryComponents, ...incomingComponents } : registryComponents;
232
- return import_react.default.createElement(MDXComponent, {
233
- ...props,
234
- components: mergedComponents
235
- });
236
- };
237
- } catch (error) {
238
- console.error("Failed to evaluate MDX:", error);
239
- return () => import_react.default.createElement(
240
- "div",
241
- {
242
- className: "mdx-error"
243
- },
244
- `MDX Error: ${error instanceof Error ? error.message : "Unknown error"}`
245
- );
246
- }
247
- }
248
- /**
249
- * Create safe evaluation context for MDX
250
- */
251
- createEvaluationContext() {
252
- const runtime = ReactJsxRuntime;
253
- const createElementWithChildren = (type, props, key) => {
254
- const baseProps = props ?? {};
255
- const finalProps = key === void 0 || key === null ? baseProps : { ...baseProps, key };
256
- if (Array.isArray(finalProps.children)) {
257
- finalProps.children = finalProps.children.map((child, index) => assignMdxChildKey(child, index));
258
- }
259
- return import_react.default.createElement(type, finalProps);
260
- };
261
- const runtimeJsx = typeof runtime.jsx === "function" ? runtime.jsx.bind(runtime) : createElementWithChildren;
262
- const runtimeJsxs = typeof runtime.jsxs === "function" ? runtime.jsxs.bind(runtime) : createElementWithChildren;
263
- const runtimeJsxDEV = typeof runtime.jsxDEV === "function" ? runtime.jsxDEV.bind(runtime) : (type, props, key, ..._rest) => createElementWithChildren(type, props, key);
264
- const jsxRuntime = {
265
- jsx: runtimeJsx,
266
- jsxs: runtimeJsxs,
267
- jsxDEV: runtimeJsxDEV,
268
- Fragment: runtime.Fragment ?? import_react.default.Fragment
269
- };
270
- return {
271
- __mdx_runtime: jsxRuntime,
272
- React: import_react.default,
273
- Fragment: import_react.default.Fragment,
274
- useState: import_react.default.useState,
275
- useEffect: import_react.default.useEffect,
276
- useMemo: import_react.default.useMemo,
277
- useCallback: import_react.default.useCallback,
278
- useRef: import_react.default.useRef,
279
- useContext: import_react.default.useContext,
280
- useReducer: import_react.default.useReducer,
281
- useLayoutEffect: import_react.default.useLayoutEffect,
282
- console: {
283
- log: console.log,
284
- warn: console.warn,
285
- error: console.error
286
- }
287
- };
288
- }
289
- resolveEvaluatedModule(output) {
290
- if (!output) {
291
- throw new Error("Compiled MDX module returned no exports");
292
- }
293
- if (typeof output === "function") {
294
- return output;
295
- }
296
- if (typeof output.default === "function") {
297
- return output.default;
298
- }
299
- if (typeof output.MDXContent === "function") {
300
- return output.MDXContent;
301
- }
302
- throw new Error("Compiled MDX module did not export a component");
303
- }
304
- getComponents() {
305
- if (this.customComponents.size === 0) {
306
- return { ...this.baseComponents };
307
- }
308
- const merged = { ...this.baseComponents };
309
- for (const [key, component] of this.customComponents) {
310
- merged[key] = component;
311
- }
312
- return merged;
313
- }
314
- /**
315
- * Register or replace custom MDX components
316
- */
317
- registerComponents(components, options) {
318
- if (options?.replace) {
319
- this.customComponents.clear();
320
- }
321
- for (const [key, component] of Object.entries(components)) {
322
- if (!component) continue;
323
- this.customComponents.set(key, component);
324
- }
325
- }
326
- /**
327
- * Get base MDX components
328
- */
329
- createBaseComponents() {
330
- return {
331
- // Standard HTML elements
332
- h1: withClassName("h1", "mdx-h1"),
333
- h2: withClassName("h2", "mdx-h2"),
334
- h3: withClassName("h3", "mdx-h3"),
335
- h4: withClassName("h4", "mdx-h4"),
336
- h5: withClassName("h5", "mdx-h5"),
337
- h6: withClassName("h6", "mdx-h6"),
338
- p: withClassName("p", "mdx-p"),
339
- strong: withClassName("strong", "mdx-strong"),
340
- em: withClassName("em", "mdx-em"),
341
- code: withClassName("code", "mdx-code"),
342
- pre: withClassName("pre", "mdx-pre"),
343
- ul: withClassName("ul", "mdx-ul"),
344
- ol: withClassName("ol", "mdx-ol"),
345
- li: withClassName("li", "mdx-li"),
346
- blockquote: withClassName("blockquote", "mdx-blockquote"),
347
- a: (props) => import_react.default.createElement("a", {
348
- className: "mdx-link",
349
- target: props.href?.startsWith("http") ? "_blank" : void 0,
350
- rel: props.href?.startsWith("http") ? "noopener noreferrer" : void 0,
351
- ...props
352
- }),
353
- img: (props) => import_react.default.createElement("img", { className: "mdx-img", ...props }),
354
- table: withClassName("table", "mdx-table"),
355
- thead: withClassName("thead", "mdx-thead"),
356
- tbody: withClassName("tbody", "mdx-tbody"),
357
- tr: withClassName("tr", "mdx-tr"),
358
- th: withClassName("th", "mdx-th"),
359
- td: withClassName("td", "mdx-td")
360
- };
361
- }
362
- /**
363
- * Clear component cache
364
- */
365
- clearCache() {
366
- this.components.clear();
367
- }
368
- clearCustomComponents() {
369
- this.customComponents.clear();
370
- }
371
- };
372
- var globalMDXClient = null;
373
- var globalComponentFactory = null;
374
- function getMDXClient(compileEndpoint) {
375
- if (!globalMDXClient) {
376
- globalMDXClient = new MDXClient(compileEndpoint);
377
- }
378
- return globalMDXClient;
379
- }
380
- function getMDXComponentFactory(compileEndpoint) {
381
- if (!globalComponentFactory) {
382
- const client = getMDXClient(compileEndpoint);
383
- globalComponentFactory = new MDXComponentFactory(client);
384
- }
385
- return globalComponentFactory;
386
- }
387
- function registerInlineMdxModule(compiled) {
388
- const factory = getMDXComponentFactory();
389
- factory.registerInlineModule({
390
- id: compiled.id,
391
- code: compiled.code,
392
- dependencies: compiled.dependencies ?? []
393
- });
394
- }
395
-
396
- // src/mdx-coordinator.ts
397
- function useMdxCoordinator(blocks, compileEndpoint, options) {
398
- const mdxClientRef = (0, import_react2.useRef)(getMDXClient(compileEndpoint));
399
- const mode = options?.mode ?? "server";
400
- const [compiledMap, setCompiledMap] = (0, import_react2.useState)(/* @__PURE__ */ new Map());
401
- const blockSignatureRef = (0, import_react2.useRef)(/* @__PURE__ */ new Map());
402
- const applyCompiledRef = (0, import_react2.useCallback)(
403
- (block, compiledId) => {
404
- if (mode === "worker") {
405
- return;
406
- }
407
- if (options?.workerClient) {
408
- options.workerClient.setMdxCompiled(block.id, compiledId);
409
- return;
410
- }
411
- const store = options?.store;
412
- if (!store) {
413
- return;
414
- }
415
- const currentBlocks = store.getBlocks();
416
- const index = currentBlocks.findIndex((candidate) => candidate.id === block.id);
417
- if (index === -1) return;
418
- const current = currentBlocks[index];
419
- if (current.payload.compiledMdxRef?.id === compiledId) {
420
- return;
421
- }
422
- store.applyPatches([
423
- {
424
- op: "setProps",
425
- at: { blockId: block.id, nodeId: block.id },
426
- props: {
427
- block: {
428
- ...current,
429
- payload: {
430
- ...current.payload,
431
- compiledMdxRef: { id: compiledId },
432
- meta: {
433
- ...current.payload.meta ?? {},
434
- mdxStatus: "compiled"
435
- }
436
- }
437
- }
438
- }
439
- }
440
- ]);
441
- },
442
- [options?.workerClient, options?.store, mode]
443
- );
444
- (0, import_react2.useEffect)(() => {
445
- if (mode === "worker") {
446
- return;
447
- }
448
- const targets = blocks.filter((b) => b.type === "mdx" && b.isFinalized && !b.payload.compiledMdxRef && !compiledMap.has(b.id));
449
- if (targets.length === 0) return;
450
- let cancelled = false;
451
- (async () => {
452
- for (const block of targets) {
453
- try {
454
- const compiled = await mdxClientRef.current.compile(block);
455
- if (cancelled) return;
456
- registerInlineMdxModule({
457
- id: compiled.id,
458
- code: compiled.code,
459
- dependencies: compiled.dependencies
460
- });
461
- setCompiledMap((prev) => {
462
- if (prev.has(block.id)) return prev;
463
- const next = new Map(prev);
464
- next.set(block.id, compiled.id);
465
- return next;
466
- });
467
- applyCompiledRef(block, compiled.id);
468
- } catch (e) {
469
- console.warn("MDX compile failed for block", block.id, e);
470
- if (options?.workerClient) {
471
- options.workerClient.setMdxError(block.id, e instanceof Error ? e.message : String(e));
472
- } else if (options?.store) {
473
- options.store.applyPatches([
474
- {
475
- op: "setProps",
476
- at: { blockId: block.id, nodeId: block.id },
477
- props: {
478
- block: {
479
- ...block,
480
- payload: {
481
- ...block.payload,
482
- meta: { ...block.payload.meta ?? {}, mdxStatus: "error", mdxError: e instanceof Error ? e.message : String(e) }
483
- }
484
- }
485
- }
486
- }
487
- ]);
488
- }
489
- }
490
- }
491
- })();
492
- return () => {
493
- cancelled = true;
494
- };
495
- }, [blocks, compiledMap, applyCompiledRef, options?.workerClient, options?.store, mode]);
496
- (0, import_react2.useEffect)(() => {
497
- if (mode === "worker") {
498
- return;
499
- }
500
- const blockMap = new Map(blocks.map((block) => [block.id, block]));
501
- setCompiledMap((prev) => {
502
- let mutated = false;
503
- const next = new Map(prev);
504
- for (const [blockId] of prev) {
505
- const candidate = blockMap.get(blockId);
506
- if (!candidate || candidate.type !== "mdx") {
507
- next.delete(blockId);
508
- mutated = true;
509
- continue;
510
- }
511
- const previousSignature = blockSignatureRef.current.get(blockId);
512
- const currentSignature = candidate.payload.raw ?? "";
513
- if (previousSignature !== void 0 && previousSignature !== currentSignature) {
514
- next.delete(blockId);
515
- mutated = true;
516
- }
517
- }
518
- return mutated ? next : prev;
519
- });
520
- const signatureMap = /* @__PURE__ */ new Map();
521
- for (const block of blocks) {
522
- if (block.type === "mdx") {
523
- signatureMap.set(block.id, block.payload.raw ?? "");
524
- }
525
- }
526
- blockSignatureRef.current = signatureMap;
527
- }, [blocks, mode]);
528
- const derivedBlocks = (0, import_react2.useMemo)(() => {
529
- if (mode === "worker") {
530
- return blocks;
531
- }
532
- if (options?.workerClient) {
533
- return blocks;
534
- }
535
- if (compiledMap.size === 0) return blocks;
536
- let changed = false;
537
- const out = blocks.map((b) => {
538
- if (b.type === "mdx" && !b.payload.compiledMdxRef) {
539
- const id = compiledMap.get(b.id);
540
- if (id) {
541
- changed = true;
542
- return {
543
- ...b,
544
- payload: {
545
- ...b.payload,
546
- compiledMdxRef: { id }
547
- }
548
- };
549
- }
550
- }
551
- return b;
552
- });
553
- return changed ? out : blocks;
554
- }, [blocks, compiledMap, mode, options?.workerClient]);
555
- if (options?.workerClient) {
556
- return blocks;
557
- }
558
- return derivedBlocks;
559
- }
560
- // Annotate the CommonJS export names for ESM import in node:
561
- 0 && (module.exports = {
562
- useMdxCoordinator
563
- });
@@ -1,21 +0,0 @@
1
- import { Block } from '@stream-mdx/core';
2
- import { RendererStore } from './renderer/store.cjs';
3
-
4
- /**
5
- * Lightweight client coordinator for MDX blocks.
6
- * - Watches finalized MDX blocks
7
- * - Compiles them via server endpoint
8
- * - Returns a derived blocks array with `compiledMdxRef` populated
9
- */
10
- interface MdxWorkerClientLike {
11
- setMdxCompiled(blockId: string, compiledId: string): void;
12
- setMdxError(blockId: string, message: string): void;
13
- }
14
- interface MdxCoordinatorOptions {
15
- workerClient?: MdxWorkerClientLike;
16
- store?: RendererStore;
17
- mode?: "server" | "worker";
18
- }
19
- declare function useMdxCoordinator(blocks: ReadonlyArray<Block>, compileEndpoint?: string, options?: MdxCoordinatorOptions): ReadonlyArray<Block>;
20
-
21
- export { useMdxCoordinator };
@@ -1,21 +0,0 @@
1
- import { Block } from '@stream-mdx/core';
2
- import { RendererStore } from './renderer/store.js';
3
-
4
- /**
5
- * Lightweight client coordinator for MDX blocks.
6
- * - Watches finalized MDX blocks
7
- * - Compiles them via server endpoint
8
- * - Returns a derived blocks array with `compiledMdxRef` populated
9
- */
10
- interface MdxWorkerClientLike {
11
- setMdxCompiled(blockId: string, compiledId: string): void;
12
- setMdxError(blockId: string, message: string): void;
13
- }
14
- interface MdxCoordinatorOptions {
15
- workerClient?: MdxWorkerClientLike;
16
- store?: RendererStore;
17
- mode?: "server" | "worker";
18
- }
19
- declare function useMdxCoordinator(blocks: ReadonlyArray<Block>, compileEndpoint?: string, options?: MdxCoordinatorOptions): ReadonlyArray<Block>;
20
-
21
- export { useMdxCoordinator };