@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.
Files changed (99) hide show
  1. package/CHANGELOG.md +371 -0
  2. package/README.md +79 -4
  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 +117 -193
  6. package/dist/components/bottom-stick-scroll-area.mjs +112 -183
  7. package/dist/components/diff-renderer.cjs +86 -0
  8. package/dist/components/diff-renderer.d.cts +11 -0
  9. package/dist/components/diff-renderer.d.ts +11 -0
  10. package/dist/components/diff-renderer.mjs +66 -0
  11. package/dist/components/index.cjs +2761 -1508
  12. package/dist/components/index.d.cts +7 -2
  13. package/dist/components/index.d.ts +7 -2
  14. package/dist/components/index.mjs +2730 -1493
  15. package/dist/components/katex-math.cjs +220 -0
  16. package/dist/components/katex-math.d.cts +10 -0
  17. package/dist/components/katex-math.d.ts +10 -0
  18. package/dist/components/katex-math.mjs +188 -0
  19. package/dist/contexts/math-tracker.cjs +240 -521
  20. package/dist/contexts/math-tracker.mjs +235 -513
  21. package/dist/index-Cmne1yUK.d.cts +723 -0
  22. package/dist/index-D7dyyu3q.d.ts +723 -0
  23. package/dist/index.cjs +13029 -6475
  24. package/dist/index.d.cts +69 -39
  25. package/dist/index.d.ts +69 -39
  26. package/dist/index.mjs +13007 -6442
  27. package/dist/math/display-wrapper.cjs +7 -15
  28. package/dist/math/display-wrapper.d.cts +2 -2
  29. package/dist/math/display-wrapper.d.ts +2 -2
  30. package/dist/math/display-wrapper.mjs +2 -6
  31. package/dist/node-views-Bpc6Gkgl.d.cts +13 -0
  32. package/dist/node-views-DpnYdaAS.d.ts +13 -0
  33. package/dist/renderer/hooks.cjs +35 -20
  34. package/dist/renderer/hooks.d.cts +22 -1
  35. package/dist/renderer/hooks.d.ts +22 -1
  36. package/dist/renderer/hooks.mjs +27 -12
  37. package/dist/renderer/patch-batching.cjs +5 -8
  38. package/dist/renderer/patch-coalescing.cjs +5 -8
  39. package/dist/renderer/patch-commit-scheduler.cjs +138 -356
  40. package/dist/renderer/patch-commit-scheduler.d.cts +13 -1
  41. package/dist/renderer/patch-commit-scheduler.d.ts +13 -1
  42. package/dist/renderer/patch-commit-scheduler.mjs +133 -348
  43. package/dist/renderer/store.cjs +3775 -1373
  44. package/dist/renderer/store.d.cts +108 -3
  45. package/dist/renderer/store.d.ts +108 -3
  46. package/dist/renderer/store.mjs +3778 -1362
  47. package/dist/renderer/virtualized-code.cjs +415 -71
  48. package/dist/renderer/virtualized-code.d.cts +24 -23
  49. package/dist/renderer/virtualized-code.d.ts +24 -23
  50. package/dist/renderer/virtualized-code.mjs +409 -62
  51. package/dist/renderer.cjs +10655 -4334
  52. package/dist/renderer.d.cts +98 -29
  53. package/dist/renderer.d.ts +98 -29
  54. package/dist/renderer.mjs +10582 -4212
  55. package/dist/server-regions-CPLHGli0.d.cts +32 -0
  56. package/dist/server-regions-mUhyoKi7.d.ts +32 -0
  57. package/dist/server.cjs +10195 -3872
  58. package/dist/server.d.cts +17 -252
  59. package/dist/server.d.ts +17 -252
  60. package/dist/server.mjs +9997 -3649
  61. package/dist/{streaming-markdown-CEI9eDZm.d.ts → streaming-markdown-yELbJxyH.d.ts} +49 -55
  62. package/dist/{streaming-markdown-DRisClXb.d.cts → streaming-markdown-zVwFuaJG.d.cts} +49 -55
  63. package/dist/streaming-markdown.cjs +11660 -5378
  64. package/dist/streaming-markdown.d.cts +7 -2
  65. package/dist/streaming-markdown.d.ts +7 -2
  66. package/dist/streaming-markdown.mjs +11679 -5347
  67. package/dist/trusted-mdx/index.cjs +833 -0
  68. package/dist/trusted-mdx/index.d.cts +11 -0
  69. package/dist/trusted-mdx/index.d.ts +11 -0
  70. package/dist/trusted-mdx/index.mjs +807 -0
  71. package/dist/trusted-mdx/native-children-slot.cjs +45 -0
  72. package/dist/trusted-mdx/native-children-slot.d.cts +11 -0
  73. package/dist/trusted-mdx/native-children-slot.d.ts +11 -0
  74. package/dist/trusted-mdx/native-children-slot.mjs +24 -0
  75. package/dist/trusted-mdx/server.cjs +752 -0
  76. package/dist/trusted-mdx/server.d.cts +24 -0
  77. package/dist/trusted-mdx/server.d.ts +24 -0
  78. package/dist/trusted-mdx/server.mjs +724 -0
  79. package/dist/trusted-mdx-contract-ElKpkDI5.d.cts +113 -0
  80. package/dist/trusted-mdx-contract-ElKpkDI5.d.ts +113 -0
  81. package/dist/utils/inline-html.cjs +246 -147
  82. package/dist/utils/inline-html.d.cts +37 -6
  83. package/dist/utils/inline-html.d.ts +37 -6
  84. package/dist/utils/inline-html.mjs +237 -134
  85. package/package.json +41 -18
  86. package/dist/index-BVhp_pcF.d.ts +0 -251
  87. package/dist/index-EaeouYp2.d.cts +0 -251
  88. package/dist/mdx-client.cjs +0 -408
  89. package/dist/mdx-client.d.cts +0 -113
  90. package/dist/mdx-client.d.ts +0 -113
  91. package/dist/mdx-client.mjs +0 -369
  92. package/dist/mdx-coordinator.cjs +0 -584
  93. package/dist/mdx-coordinator.d.cts +0 -21
  94. package/dist/mdx-coordinator.d.ts +0 -21
  95. package/dist/mdx-coordinator.mjs +0 -550
  96. package/dist/renderer/node-views.cjs +0 -1887
  97. package/dist/renderer/node-views.d.cts +0 -14
  98. package/dist/renderer/node-views.d.ts +0 -14
  99. package/dist/renderer/node-views.mjs +0 -1861
@@ -1,369 +0,0 @@
1
- "use client";
2
-
3
- // src/mdx-client.ts
4
- import React from "react";
5
- import * as ReactJsxRuntime from "react/jsx-runtime";
6
- var withClassName = (tag, className) => {
7
- return (props) => React.createElement(tag, { className, ...props });
8
- };
9
- function assignMdxChildKey(child, index) {
10
- if (child == null) return child;
11
- if (Array.isArray(child)) {
12
- return child.map((entry, subIndex) => assignMdxChildKey(entry, `${index}-${subIndex}`));
13
- }
14
- if (React.isValidElement(child)) {
15
- if (child.key !== null && child.key !== void 0) {
16
- return child;
17
- }
18
- return React.cloneElement(child, { key: index });
19
- }
20
- return child;
21
- }
22
- var MDXClient = class {
23
- constructor(compileEndpoint = "/api/mdx-compile-v2") {
24
- this.cache = /* @__PURE__ */ new Map();
25
- this.inlineModules = /* @__PURE__ */ new Map();
26
- this.compiledCache = /* @__PURE__ */ new Map();
27
- this.compiledInflight = /* @__PURE__ */ new Map();
28
- this.compileEndpoint = compileEndpoint;
29
- }
30
- /**
31
- * Compile MDX block on server
32
- */
33
- async compile(block) {
34
- if (block.type !== "mdx") {
35
- throw new Error("Block is not MDX type");
36
- }
37
- const cacheKey = `${block.id}\0${block.payload.raw ?? ""}`;
38
- const cachedPromise = this.cache.get(cacheKey);
39
- if (cachedPromise) {
40
- return cachedPromise;
41
- }
42
- const compilationPromise = this.doCompile(block);
43
- this.cache.set(cacheKey, compilationPromise);
44
- try {
45
- const result = await compilationPromise;
46
- return result;
47
- } catch (error) {
48
- this.cache.delete(cacheKey);
49
- throw error;
50
- }
51
- }
52
- async getCompiledInternal(ref) {
53
- const inline = this.inlineModules.get(ref.id);
54
- if (inline) {
55
- return this.cloneCompiled(inline);
56
- }
57
- const cached = this.compiledCache.get(ref.id);
58
- if (cached) {
59
- return this.cloneCompiled(cached);
60
- }
61
- const inflight = this.compiledInflight.get(ref.id);
62
- if (inflight) {
63
- const resolved = await inflight;
64
- return this.cloneCompiled(resolved);
65
- }
66
- const fetchPromise = this.fetchCompiled(ref);
67
- this.compiledInflight.set(ref.id, fetchPromise);
68
- try {
69
- const data = await fetchPromise;
70
- this.compiledCache.set(ref.id, data);
71
- return this.cloneCompiled(data);
72
- } finally {
73
- this.compiledInflight.delete(ref.id);
74
- }
75
- }
76
- async getCompiled(ref, options) {
77
- if (options?.signal) {
78
- const inline = this.inlineModules.get(ref.id);
79
- if (inline) {
80
- return this.cloneCompiled(inline);
81
- }
82
- const cached = this.compiledCache.get(ref.id);
83
- if (cached) {
84
- return this.cloneCompiled(cached);
85
- }
86
- const data = await this.fetchCompiled(ref, options);
87
- this.compiledCache.set(ref.id, data);
88
- return this.cloneCompiled(data);
89
- }
90
- return this.getCompiledInternal(ref);
91
- }
92
- /**
93
- * Clear compilation cache
94
- */
95
- clearCache() {
96
- this.cache.clear();
97
- this.compiledCache.clear();
98
- this.compiledInflight.clear();
99
- }
100
- registerInlineModule(compiled) {
101
- this.inlineModules.set(compiled.id, {
102
- ...compiled,
103
- dependencies: Array.isArray(compiled.dependencies) ? [...compiled.dependencies] : []
104
- });
105
- }
106
- clearInlineModules() {
107
- this.inlineModules.clear();
108
- }
109
- /**
110
- * Actual compilation implementation
111
- */
112
- async doCompile(block) {
113
- const response = await fetch(this.compileEndpoint, {
114
- method: "POST",
115
- headers: {
116
- "Content-Type": "application/json"
117
- },
118
- body: JSON.stringify({
119
- content: block.payload.raw,
120
- blockId: block.id
121
- })
122
- });
123
- if (!response.ok) {
124
- const error = await response.json().catch(() => ({ error: "Unknown error" }));
125
- throw new Error(`MDX compilation failed: ${error.error || response.statusText}`);
126
- }
127
- const data = await response.json();
128
- return {
129
- id: data.id,
130
- code: data.code,
131
- dependencies: data.dependencies || [],
132
- cached: data.cached || false
133
- };
134
- }
135
- async fetchCompiled(ref, options) {
136
- const response = await fetch(`${this.compileEndpoint}?id=${ref.id}`, {
137
- signal: options?.signal
138
- });
139
- if (!response.ok) {
140
- throw new Error(`Failed to get compiled MDX: ${response.statusText}`);
141
- }
142
- const data = await response.json();
143
- return {
144
- id: data.id,
145
- code: data.code,
146
- dependencies: data.dependencies || [],
147
- timestamp: data.timestamp
148
- };
149
- }
150
- cloneCompiled(compiled) {
151
- return {
152
- ...compiled,
153
- dependencies: Array.isArray(compiled.dependencies) ? [...compiled.dependencies] : []
154
- };
155
- }
156
- };
157
- var MDXComponentFactory = class {
158
- constructor(mdxClient) {
159
- this.components = /* @__PURE__ */ new Map();
160
- this.customComponents = /* @__PURE__ */ new Map();
161
- this.mdxClient = mdxClient;
162
- this.baseComponents = this.createBaseComponents();
163
- }
164
- registerInlineModule(module) {
165
- this.mdxClient.registerInlineModule(module);
166
- this.components.delete(module.id);
167
- }
168
- /**
169
- * Create React component from compiled MDX
170
- */
171
- async createComponent(ref) {
172
- const cached = this.components.get(ref.id);
173
- if (cached) {
174
- return cached;
175
- }
176
- const compiled = await this.mdxClient.getCompiled(ref);
177
- const component = this.evaluateCompiledMDX(compiled);
178
- this.components.set(ref.id, component);
179
- return component;
180
- }
181
- /**
182
- * Safely evaluate compiled MDX code
183
- */
184
- evaluateCompiledMDX(compiled) {
185
- try {
186
- const context = this.createEvaluationContext();
187
- const argNames = Object.keys(context);
188
- const argValues = Object.values(context);
189
- const moduleExports = new Function(...argNames, compiled.code)(...argValues);
190
- const MDXComponent = this.resolveEvaluatedModule(moduleExports);
191
- return (props) => {
192
- const registryComponents = this.getComponents();
193
- const incomingComponents = props?.components;
194
- const mergedComponents = incomingComponents ? { ...registryComponents, ...incomingComponents } : registryComponents;
195
- return React.createElement(MDXComponent, {
196
- ...props,
197
- components: mergedComponents
198
- });
199
- };
200
- } catch (error) {
201
- console.error("Failed to evaluate MDX:", error);
202
- return () => React.createElement(
203
- "div",
204
- {
205
- className: "mdx-error"
206
- },
207
- `MDX Error: ${error instanceof Error ? error.message : "Unknown error"}`
208
- );
209
- }
210
- }
211
- /**
212
- * Create safe evaluation context for MDX
213
- */
214
- createEvaluationContext() {
215
- const runtime = ReactJsxRuntime;
216
- const createElementWithChildren = (type, props, key) => {
217
- const baseProps = props ?? {};
218
- const finalProps = key === void 0 || key === null ? baseProps : { ...baseProps, key };
219
- if (Array.isArray(finalProps.children)) {
220
- finalProps.children = finalProps.children.map((child, index) => assignMdxChildKey(child, index));
221
- }
222
- return React.createElement(type, finalProps);
223
- };
224
- const runtimeJsx = typeof runtime.jsx === "function" ? runtime.jsx.bind(runtime) : createElementWithChildren;
225
- const runtimeJsxs = typeof runtime.jsxs === "function" ? runtime.jsxs.bind(runtime) : createElementWithChildren;
226
- const runtimeJsxDEV = typeof runtime.jsxDEV === "function" ? runtime.jsxDEV.bind(runtime) : (type, props, key, ..._rest) => createElementWithChildren(type, props, key);
227
- const jsxRuntime = {
228
- jsx: runtimeJsx,
229
- jsxs: runtimeJsxs,
230
- jsxDEV: runtimeJsxDEV,
231
- Fragment: runtime.Fragment ?? React.Fragment
232
- };
233
- return {
234
- __mdx_runtime: jsxRuntime,
235
- React,
236
- Fragment: React.Fragment,
237
- useState: React.useState,
238
- useEffect: React.useEffect,
239
- useMemo: React.useMemo,
240
- useCallback: React.useCallback,
241
- useRef: React.useRef,
242
- useContext: React.useContext,
243
- useReducer: React.useReducer,
244
- useLayoutEffect: React.useLayoutEffect,
245
- console: {
246
- log: console.log,
247
- warn: console.warn,
248
- error: console.error
249
- }
250
- };
251
- }
252
- resolveEvaluatedModule(output) {
253
- if (!output) {
254
- throw new Error("Compiled MDX module returned no exports");
255
- }
256
- if (typeof output === "function") {
257
- return output;
258
- }
259
- if (typeof output.default === "function") {
260
- return output.default;
261
- }
262
- if (typeof output.MDXContent === "function") {
263
- return output.MDXContent;
264
- }
265
- throw new Error("Compiled MDX module did not export a component");
266
- }
267
- getComponents() {
268
- if (this.customComponents.size === 0) {
269
- return { ...this.baseComponents };
270
- }
271
- const merged = { ...this.baseComponents };
272
- for (const [key, component] of this.customComponents) {
273
- merged[key] = component;
274
- }
275
- return merged;
276
- }
277
- /**
278
- * Register or replace custom MDX components
279
- */
280
- registerComponents(components, options) {
281
- if (options?.replace) {
282
- this.customComponents.clear();
283
- }
284
- for (const [key, component] of Object.entries(components)) {
285
- if (!component) continue;
286
- this.customComponents.set(key, component);
287
- }
288
- }
289
- /**
290
- * Get base MDX components
291
- */
292
- createBaseComponents() {
293
- return {
294
- // Standard HTML elements
295
- h1: withClassName("h1", "mdx-h1"),
296
- h2: withClassName("h2", "mdx-h2"),
297
- h3: withClassName("h3", "mdx-h3"),
298
- h4: withClassName("h4", "mdx-h4"),
299
- h5: withClassName("h5", "mdx-h5"),
300
- h6: withClassName("h6", "mdx-h6"),
301
- p: withClassName("p", "mdx-p"),
302
- strong: withClassName("strong", "mdx-strong"),
303
- em: withClassName("em", "mdx-em"),
304
- code: withClassName("code", "mdx-code"),
305
- pre: withClassName("pre", "mdx-pre"),
306
- ul: withClassName("ul", "mdx-ul"),
307
- ol: withClassName("ol", "mdx-ol"),
308
- li: withClassName("li", "mdx-li"),
309
- blockquote: withClassName("blockquote", "mdx-blockquote"),
310
- a: (props) => React.createElement("a", {
311
- className: "mdx-link",
312
- target: props.href?.startsWith("http") ? "_blank" : void 0,
313
- rel: props.href?.startsWith("http") ? "noopener noreferrer" : void 0,
314
- ...props
315
- }),
316
- img: (props) => React.createElement("img", { className: "mdx-img", ...props }),
317
- table: withClassName("table", "mdx-table"),
318
- thead: withClassName("thead", "mdx-thead"),
319
- tbody: withClassName("tbody", "mdx-tbody"),
320
- tr: withClassName("tr", "mdx-tr"),
321
- th: withClassName("th", "mdx-th"),
322
- td: withClassName("td", "mdx-td")
323
- };
324
- }
325
- /**
326
- * Clear component cache
327
- */
328
- clearCache() {
329
- this.components.clear();
330
- }
331
- clearCustomComponents() {
332
- this.customComponents.clear();
333
- }
334
- };
335
- var globalMDXClient = null;
336
- var globalComponentFactory = null;
337
- function getMDXClient(compileEndpoint) {
338
- if (!globalMDXClient) {
339
- globalMDXClient = new MDXClient(compileEndpoint);
340
- }
341
- return globalMDXClient;
342
- }
343
- function getMDXComponentFactory(compileEndpoint) {
344
- if (!globalComponentFactory) {
345
- const client = getMDXClient(compileEndpoint);
346
- globalComponentFactory = new MDXComponentFactory(client);
347
- }
348
- return globalComponentFactory;
349
- }
350
- function registerMDXComponents(components, options) {
351
- const factory = getMDXComponentFactory();
352
- factory.registerComponents(components, options);
353
- }
354
- function registerInlineMdxModule(compiled) {
355
- const factory = getMDXComponentFactory();
356
- factory.registerInlineModule({
357
- id: compiled.id,
358
- code: compiled.code,
359
- dependencies: compiled.dependencies ?? []
360
- });
361
- }
362
- export {
363
- MDXClient,
364
- MDXComponentFactory,
365
- getMDXClient,
366
- getMDXComponentFactory,
367
- registerInlineMdxModule,
368
- registerMDXComponents
369
- };