@stream-mdx/react 0.5.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.
- package/CHANGELOG.md +371 -0
- package/README.md +79 -4
- package/dist/blocks-renderer-DAOBFBfi.d.ts +29 -0
- package/dist/blocks-renderer-DQEL1kJW.d.cts +29 -0
- package/dist/components/bottom-stick-scroll-area.cjs +117 -193
- package/dist/components/bottom-stick-scroll-area.mjs +112 -183
- package/dist/components/diff-renderer.cjs +86 -0
- package/dist/components/diff-renderer.d.cts +11 -0
- package/dist/components/diff-renderer.d.ts +11 -0
- package/dist/components/diff-renderer.mjs +66 -0
- package/dist/components/index.cjs +2761 -1508
- package/dist/components/index.d.cts +7 -2
- package/dist/components/index.d.ts +7 -2
- package/dist/components/index.mjs +2730 -1493
- package/dist/components/katex-math.cjs +220 -0
- package/dist/components/katex-math.d.cts +10 -0
- package/dist/components/katex-math.d.ts +10 -0
- package/dist/components/katex-math.mjs +188 -0
- package/dist/contexts/math-tracker.cjs +240 -521
- package/dist/contexts/math-tracker.mjs +235 -513
- package/dist/index-Cmne1yUK.d.cts +723 -0
- package/dist/index-D7dyyu3q.d.ts +723 -0
- package/dist/index.cjs +13029 -6475
- package/dist/index.d.cts +69 -39
- package/dist/index.d.ts +69 -39
- package/dist/index.mjs +13007 -6442
- package/dist/math/display-wrapper.cjs +7 -15
- package/dist/math/display-wrapper.d.cts +2 -2
- package/dist/math/display-wrapper.d.ts +2 -2
- package/dist/math/display-wrapper.mjs +2 -6
- package/dist/node-views-Bpc6Gkgl.d.cts +13 -0
- package/dist/node-views-DpnYdaAS.d.ts +13 -0
- package/dist/renderer/hooks.cjs +35 -20
- package/dist/renderer/hooks.d.cts +22 -1
- package/dist/renderer/hooks.d.ts +22 -1
- package/dist/renderer/hooks.mjs +27 -12
- package/dist/renderer/patch-batching.cjs +5 -8
- package/dist/renderer/patch-coalescing.cjs +5 -8
- package/dist/renderer/patch-commit-scheduler.cjs +138 -356
- package/dist/renderer/patch-commit-scheduler.d.cts +13 -1
- package/dist/renderer/patch-commit-scheduler.d.ts +13 -1
- package/dist/renderer/patch-commit-scheduler.mjs +133 -348
- package/dist/renderer/store.cjs +3775 -1373
- package/dist/renderer/store.d.cts +108 -3
- package/dist/renderer/store.d.ts +108 -3
- package/dist/renderer/store.mjs +3778 -1362
- package/dist/renderer/virtualized-code.cjs +415 -71
- package/dist/renderer/virtualized-code.d.cts +24 -23
- package/dist/renderer/virtualized-code.d.ts +24 -23
- package/dist/renderer/virtualized-code.mjs +409 -62
- package/dist/renderer.cjs +10655 -4334
- package/dist/renderer.d.cts +98 -29
- package/dist/renderer.d.ts +98 -29
- package/dist/renderer.mjs +10582 -4212
- package/dist/server-regions-CPLHGli0.d.cts +32 -0
- package/dist/server-regions-mUhyoKi7.d.ts +32 -0
- package/dist/server.cjs +10195 -3872
- package/dist/server.d.cts +17 -252
- package/dist/server.d.ts +17 -252
- package/dist/server.mjs +9997 -3649
- package/dist/{streaming-markdown-CEI9eDZm.d.ts → streaming-markdown-yELbJxyH.d.ts} +49 -55
- package/dist/{streaming-markdown-DRisClXb.d.cts → streaming-markdown-zVwFuaJG.d.cts} +49 -55
- package/dist/streaming-markdown.cjs +11660 -5378
- package/dist/streaming-markdown.d.cts +7 -2
- package/dist/streaming-markdown.d.ts +7 -2
- package/dist/streaming-markdown.mjs +11679 -5347
- package/dist/trusted-mdx/index.cjs +833 -0
- package/dist/trusted-mdx/index.d.cts +11 -0
- package/dist/trusted-mdx/index.d.ts +11 -0
- package/dist/trusted-mdx/index.mjs +807 -0
- package/dist/trusted-mdx/native-children-slot.cjs +45 -0
- package/dist/trusted-mdx/native-children-slot.d.cts +11 -0
- package/dist/trusted-mdx/native-children-slot.d.ts +11 -0
- package/dist/trusted-mdx/native-children-slot.mjs +24 -0
- package/dist/trusted-mdx/server.cjs +752 -0
- package/dist/trusted-mdx/server.d.cts +24 -0
- package/dist/trusted-mdx/server.d.ts +24 -0
- package/dist/trusted-mdx/server.mjs +724 -0
- package/dist/trusted-mdx-contract-ElKpkDI5.d.cts +113 -0
- package/dist/trusted-mdx-contract-ElKpkDI5.d.ts +113 -0
- package/dist/utils/inline-html.cjs +246 -147
- package/dist/utils/inline-html.d.cts +37 -6
- package/dist/utils/inline-html.d.ts +37 -6
- package/dist/utils/inline-html.mjs +237 -134
- package/package.json +41 -18
- package/dist/index-BVhp_pcF.d.ts +0 -251
- package/dist/index-EaeouYp2.d.cts +0 -251
- package/dist/mdx-client.cjs +0 -408
- package/dist/mdx-client.d.cts +0 -113
- package/dist/mdx-client.d.ts +0 -113
- package/dist/mdx-client.mjs +0 -369
- package/dist/mdx-coordinator.cjs +0 -584
- package/dist/mdx-coordinator.d.cts +0 -21
- package/dist/mdx-coordinator.d.ts +0 -21
- package/dist/mdx-coordinator.mjs +0 -550
- package/dist/renderer/node-views.cjs +0 -1887
- package/dist/renderer/node-views.d.cts +0 -14
- package/dist/renderer/node-views.d.ts +0 -14
- package/dist/renderer/node-views.mjs +0 -1861
package/dist/mdx-coordinator.cjs
DELETED
|
@@ -1,584 +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}\0${block.payload.raw ?? ""}`;
|
|
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 failedSignatureRef = (0, import_react2.useRef)(/* @__PURE__ */ new Map());
|
|
403
|
-
const applyCompiledRef = (0, import_react2.useCallback)(
|
|
404
|
-
(block, compiledId) => {
|
|
405
|
-
if (mode === "worker") {
|
|
406
|
-
return;
|
|
407
|
-
}
|
|
408
|
-
if (options?.workerClient) {
|
|
409
|
-
options.workerClient.setMdxCompiled(block.id, compiledId, block.payload.raw ?? "");
|
|
410
|
-
return;
|
|
411
|
-
}
|
|
412
|
-
const store = options?.store;
|
|
413
|
-
if (!store) {
|
|
414
|
-
return;
|
|
415
|
-
}
|
|
416
|
-
const currentBlocks = store.getBlocks();
|
|
417
|
-
const index = currentBlocks.findIndex((candidate) => candidate.id === block.id);
|
|
418
|
-
if (index === -1) return;
|
|
419
|
-
const current = currentBlocks[index];
|
|
420
|
-
if ((current.payload.raw ?? "") !== (block.payload.raw ?? "")) {
|
|
421
|
-
return;
|
|
422
|
-
}
|
|
423
|
-
if (current.payload.compiledMdxRef?.id === compiledId) {
|
|
424
|
-
return;
|
|
425
|
-
}
|
|
426
|
-
store.applyPatches([
|
|
427
|
-
{
|
|
428
|
-
op: "setProps",
|
|
429
|
-
at: { blockId: block.id, nodeId: block.id },
|
|
430
|
-
props: {
|
|
431
|
-
block: {
|
|
432
|
-
...current,
|
|
433
|
-
payload: {
|
|
434
|
-
...current.payload,
|
|
435
|
-
compiledMdxRef: { id: compiledId },
|
|
436
|
-
meta: {
|
|
437
|
-
...current.payload.meta ?? {},
|
|
438
|
-
mdxStatus: "compiled"
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
]);
|
|
445
|
-
},
|
|
446
|
-
[options?.workerClient, options?.store, mode]
|
|
447
|
-
);
|
|
448
|
-
(0, import_react2.useEffect)(() => {
|
|
449
|
-
if (mode === "worker") {
|
|
450
|
-
return;
|
|
451
|
-
}
|
|
452
|
-
const targets = blocks.filter((b) => {
|
|
453
|
-
if (b.type !== "mdx" || !b.isFinalized || b.payload.compiledMdxRef || compiledMap.has(b.id)) {
|
|
454
|
-
return false;
|
|
455
|
-
}
|
|
456
|
-
const raw = b.payload.raw ?? "";
|
|
457
|
-
return failedSignatureRef.current.get(b.id) !== raw;
|
|
458
|
-
});
|
|
459
|
-
if (targets.length === 0) return;
|
|
460
|
-
let cancelled = false;
|
|
461
|
-
(async () => {
|
|
462
|
-
for (const block of targets) {
|
|
463
|
-
try {
|
|
464
|
-
const compiled = await mdxClientRef.current.compile(block);
|
|
465
|
-
if (cancelled) return;
|
|
466
|
-
registerInlineMdxModule({
|
|
467
|
-
id: compiled.id,
|
|
468
|
-
code: compiled.code,
|
|
469
|
-
dependencies: compiled.dependencies
|
|
470
|
-
});
|
|
471
|
-
setCompiledMap((prev) => {
|
|
472
|
-
if (prev.has(block.id)) return prev;
|
|
473
|
-
const next = new Map(prev);
|
|
474
|
-
next.set(block.id, compiled.id);
|
|
475
|
-
return next;
|
|
476
|
-
});
|
|
477
|
-
failedSignatureRef.current.delete(block.id);
|
|
478
|
-
applyCompiledRef(block, compiled.id);
|
|
479
|
-
} catch (e) {
|
|
480
|
-
console.warn("MDX compile failed for block", block.id, e);
|
|
481
|
-
failedSignatureRef.current.set(block.id, block.payload.raw ?? "");
|
|
482
|
-
if (options?.workerClient) {
|
|
483
|
-
options.workerClient.setMdxError(block.id, e instanceof Error ? e.message : String(e), block.payload.raw ?? "");
|
|
484
|
-
} else if (options?.store) {
|
|
485
|
-
const currentBlocks = options.store.getBlocks();
|
|
486
|
-
const current = currentBlocks.find((candidate) => candidate.id === block.id);
|
|
487
|
-
if (!current || (current.payload.raw ?? "") !== (block.payload.raw ?? "")) {
|
|
488
|
-
continue;
|
|
489
|
-
}
|
|
490
|
-
options.store.applyPatches([
|
|
491
|
-
{
|
|
492
|
-
op: "setProps",
|
|
493
|
-
at: { blockId: block.id, nodeId: block.id },
|
|
494
|
-
props: {
|
|
495
|
-
block: {
|
|
496
|
-
...current,
|
|
497
|
-
payload: {
|
|
498
|
-
...current.payload,
|
|
499
|
-
meta: { ...current.payload.meta ?? {}, mdxStatus: "error", mdxError: e instanceof Error ? e.message : String(e) }
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
]);
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
})();
|
|
509
|
-
return () => {
|
|
510
|
-
cancelled = true;
|
|
511
|
-
};
|
|
512
|
-
}, [blocks, compiledMap, applyCompiledRef, options?.workerClient, options?.store, mode]);
|
|
513
|
-
(0, import_react2.useEffect)(() => {
|
|
514
|
-
if (mode === "worker") {
|
|
515
|
-
return;
|
|
516
|
-
}
|
|
517
|
-
const blockMap = new Map(blocks.map((block) => [block.id, block]));
|
|
518
|
-
setCompiledMap((prev) => {
|
|
519
|
-
let mutated = false;
|
|
520
|
-
const next = new Map(prev);
|
|
521
|
-
for (const [blockId] of prev) {
|
|
522
|
-
const candidate = blockMap.get(blockId);
|
|
523
|
-
if (!candidate || candidate.type !== "mdx") {
|
|
524
|
-
next.delete(blockId);
|
|
525
|
-
mutated = true;
|
|
526
|
-
continue;
|
|
527
|
-
}
|
|
528
|
-
const previousSignature = blockSignatureRef.current.get(blockId);
|
|
529
|
-
const currentSignature = candidate.payload.raw ?? "";
|
|
530
|
-
if (previousSignature !== void 0 && previousSignature !== currentSignature) {
|
|
531
|
-
next.delete(blockId);
|
|
532
|
-
failedSignatureRef.current.delete(blockId);
|
|
533
|
-
mutated = true;
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
return mutated ? next : prev;
|
|
537
|
-
});
|
|
538
|
-
const signatureMap = /* @__PURE__ */ new Map();
|
|
539
|
-
for (const block of blocks) {
|
|
540
|
-
if (block.type === "mdx") {
|
|
541
|
-
signatureMap.set(block.id, block.payload.raw ?? "");
|
|
542
|
-
if (!block.isFinalized || block.payload.compiledMdxRef) {
|
|
543
|
-
failedSignatureRef.current.delete(block.id);
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
blockSignatureRef.current = signatureMap;
|
|
548
|
-
}, [blocks, mode]);
|
|
549
|
-
const derivedBlocks = (0, import_react2.useMemo)(() => {
|
|
550
|
-
if (mode === "worker") {
|
|
551
|
-
return blocks;
|
|
552
|
-
}
|
|
553
|
-
if (options?.workerClient) {
|
|
554
|
-
return blocks;
|
|
555
|
-
}
|
|
556
|
-
if (compiledMap.size === 0) return blocks;
|
|
557
|
-
let changed = false;
|
|
558
|
-
const out = blocks.map((b) => {
|
|
559
|
-
if (b.type === "mdx" && !b.payload.compiledMdxRef) {
|
|
560
|
-
const id = compiledMap.get(b.id);
|
|
561
|
-
if (id) {
|
|
562
|
-
changed = true;
|
|
563
|
-
return {
|
|
564
|
-
...b,
|
|
565
|
-
payload: {
|
|
566
|
-
...b.payload,
|
|
567
|
-
compiledMdxRef: { id }
|
|
568
|
-
}
|
|
569
|
-
};
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
return b;
|
|
573
|
-
});
|
|
574
|
-
return changed ? out : blocks;
|
|
575
|
-
}, [blocks, compiledMap, mode, options?.workerClient]);
|
|
576
|
-
if (options?.workerClient) {
|
|
577
|
-
return blocks;
|
|
578
|
-
}
|
|
579
|
-
return derivedBlocks;
|
|
580
|
-
}
|
|
581
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
582
|
-
0 && (module.exports = {
|
|
583
|
-
useMdxCoordinator
|
|
584
|
-
});
|
|
@@ -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, rawSignature: string): void;
|
|
12
|
-
setMdxError(blockId: string, message: string, rawSignature: 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, rawSignature: string): void;
|
|
12
|
-
setMdxError(blockId: string, message: string, rawSignature: 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 };
|