@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
@@ -0,0 +1,220 @@
1
+ "use client";
2
+ "use strict";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: !0 });
11
+ }, __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from == "object" || typeof from == "function")
13
+ for (let key of __getOwnPropNames(from))
14
+ !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ return to;
16
+ };
17
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
18
+ // If the importer is in node compatibility mode or this is not an ESM
19
+ // file that has been converted to a CommonJS file using a Babel-
20
+ // compatible transform (i.e. "__esModule" has not been set), then set
21
+ // "default" to the CommonJS "module.exports" for node compatibility.
22
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
23
+ mod
24
+ )), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
25
+
26
+ // src/components/katex-math.tsx
27
+ var katex_math_exports = {};
28
+ __export(katex_math_exports, {
29
+ DocumentMathMacroScope: () => DocumentMathMacroScope,
30
+ DocumentMathMacroScopeContext: () => DocumentMathMacroScopeContext,
31
+ KaTeXBlockMath: () => KaTeXBlockMath,
32
+ KaTeXInlineMath: () => KaTeXInlineMath,
33
+ MathExpressionCompletenessContext: () => MathExpressionCompletenessContext,
34
+ loadKaTeX: () => loadKaTeX
35
+ });
36
+ module.exports = __toCommonJS(katex_math_exports);
37
+ var import_react = require("react"), import_jsx_runtime = require("react/jsx-runtime"), katexPromise = null;
38
+ async function loadKaTeX() {
39
+ return katexPromise || (katexPromise = import("katex").then((mod) => mod && typeof mod == "object" && "default" in mod && mod.default ? mod.default : mod)), katexPromise;
40
+ }
41
+ var DOCUMENT_POSITION_PRECEDING = 2, DOCUMENT_POSITION_FOLLOWING = 4, DocumentMathMacroScope = class {
42
+ constructor() {
43
+ this.entries = /* @__PURE__ */ new Set();
44
+ this.orderedEntries = [];
45
+ this.macroSnapshots = [];
46
+ this.katex = null;
47
+ this.loadError = null;
48
+ this.loading = !1;
49
+ this.disposed = !1;
50
+ }
51
+ register(element, tex, displayMode, complete, publish) {
52
+ let entry = { element, tex, displayMode, complete, publish };
53
+ return this.entries.add(entry), this.refresh(), () => {
54
+ this.entries.delete(entry) && this.refresh();
55
+ };
56
+ }
57
+ dispose() {
58
+ this.disposed = !0, this.entries.clear(), this.orderedEntries = [], this.macroSnapshots = [];
59
+ }
60
+ refresh() {
61
+ if (!this.disposed) {
62
+ if (this.loadError) {
63
+ for (let entry of this.entries)
64
+ entry.publish({ tex: entry.tex, displayMode: entry.displayMode, scope: this, error: this.loadError });
65
+ return;
66
+ }
67
+ if (this.katex) {
68
+ this.recompute(this.katex);
69
+ return;
70
+ }
71
+ this.loading || (this.loading = !0, loadKaTeX().then((katex) => {
72
+ this.disposed || (this.katex = katex, this.refresh());
73
+ }).catch((reason) => {
74
+ if (!this.disposed) {
75
+ this.loadError = reason instanceof Error ? reason : new Error(String(reason));
76
+ for (let entry of this.entries)
77
+ entry.publish({ tex: entry.tex, displayMode: entry.displayMode, scope: this, error: this.loadError });
78
+ }
79
+ }).finally(() => {
80
+ this.loading = !1;
81
+ }));
82
+ }
83
+ }
84
+ recompute(katex) {
85
+ let nextEntries = [...this.entries].sort((left, right) => {
86
+ let position = left.element.compareDocumentPosition(right.element);
87
+ return position & DOCUMENT_POSITION_FOLLOWING ? -1 : position & DOCUMENT_POSITION_PRECEDING ? 1 : 0;
88
+ }), unchangedPrefix = 0;
89
+ for (; unchangedPrefix < this.orderedEntries.length && unchangedPrefix < nextEntries.length && this.orderedEntries[unchangedPrefix] === nextEntries[unchangedPrefix]; )
90
+ unchangedPrefix += 1;
91
+ let macros = unchangedPrefix > 0 ? { ...this.macroSnapshots[unchangedPrefix - 1] } : {};
92
+ this.orderedEntries = nextEntries, this.macroSnapshots.length = unchangedPrefix;
93
+ for (let index = unchangedPrefix; index < nextEntries.length; index += 1) {
94
+ let entry = nextEntries[index], preparation = null;
95
+ if (entry.complete) {
96
+ let nextMacros = { ...macros };
97
+ try {
98
+ let html = katex.renderToString(entry.tex, {
99
+ displayMode: entry.displayMode,
100
+ throwOnError: !1,
101
+ trust: !1,
102
+ maxExpand: 1e3,
103
+ macros: nextMacros
104
+ });
105
+ macros = nextMacros, preparation = { tex: entry.tex, displayMode: entry.displayMode, scope: this, html };
106
+ } catch (reason) {
107
+ preparation = {
108
+ tex: entry.tex,
109
+ displayMode: entry.displayMode,
110
+ scope: this,
111
+ error: reason instanceof Error ? reason : new Error(String(reason))
112
+ };
113
+ }
114
+ }
115
+ this.macroSnapshots[index] = { ...macros }, entry.publish(preparation);
116
+ }
117
+ }
118
+ }, DocumentMathMacroScopeContext = (0, import_react.createContext)(null), MathExpressionCompletenessContext = (0, import_react.createContext)(!0), MathEngineOutputView = ({ engine, tex, displayMode }) => {
119
+ let output = (0, import_react.useMemo)(
120
+ () => engine.render(tex, { displayMode }),
121
+ [engine, engine.id, tex, displayMode]
122
+ );
123
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: output });
124
+ };
125
+ function useMathPreparation(tex, displayMode, elementRef, enabled) {
126
+ let scope = (0, import_react.useContext)(DocumentMathMacroScopeContext), complete = (0, import_react.useContext)(MathExpressionCompletenessContext), [preparation, setPreparation] = (0, import_react.useState)(null);
127
+ return (0, import_react.useEffect)(() => {
128
+ if (!enabled) return;
129
+ let active = !0;
130
+ if (scope) {
131
+ let element = elementRef.current;
132
+ return element ? scope.register(element, tex, displayMode, complete, (result) => {
133
+ active && setPreparation(result);
134
+ }) : () => {
135
+ active = !1;
136
+ };
137
+ }
138
+ return (async () => {
139
+ try {
140
+ let html = (await loadKaTeX()).renderToString(tex, {
141
+ displayMode,
142
+ throwOnError: !1,
143
+ trust: !1,
144
+ maxExpand: 1e3
145
+ });
146
+ active && setPreparation({ tex, displayMode, scope: null, html });
147
+ } catch (reason) {
148
+ active && setPreparation({
149
+ tex,
150
+ displayMode,
151
+ scope: null,
152
+ error: reason instanceof Error ? reason : new Error(String(reason))
153
+ });
154
+ }
155
+ })(), () => {
156
+ active = !1;
157
+ };
158
+ }, [tex, displayMode, scope, complete, elementRef, enabled]), scope && !complete ? null : preparation?.tex === tex && preparation.displayMode === displayMode && preparation.scope === scope ? preparation : null;
159
+ }
160
+ var KaTeXInlineMath = ({ tex, className = "", fallback, engine }) => {
161
+ let elementRef = (0, import_react.useRef)(null), preparation = useMathPreparation(tex, !1, elementRef, !engine);
162
+ return engine ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: `math-inline ${className}`.trim(), "data-tex": tex, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(MathEngineOutputView, { engine, tex, displayMode: !1 }) }) : preparation && "error" in preparation && fallback ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: fallback(tex, preparation.error) }) : preparation && "html" in preparation ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
163
+ "span",
164
+ {
165
+ ref: elementRef,
166
+ className: `math-inline katex-inline ${className}`.trim(),
167
+ "data-tex": tex,
168
+ dangerouslySetInnerHTML: { __html: preparation.html }
169
+ }
170
+ ) : /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { ref: elementRef, className: `math-inline ${className}`.trim(), "data-tex": tex, children: [
171
+ "$",
172
+ tex,
173
+ "$"
174
+ ] });
175
+ };
176
+ KaTeXInlineMath.displayName = "KaTeXInlineMath";
177
+ var KaTeXBlockMath = ({ tex, className = "", fallback, engine }) => {
178
+ let elementRef = (0, import_react.useRef)(null), preparation = useMathPreparation(tex, !0, elementRef, !engine);
179
+ return engine ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
180
+ "div",
181
+ {
182
+ className: `math-display markdown-math-display ${className}`.trim(),
183
+ "data-tex": tex,
184
+ style: { overflowX: "auto" },
185
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(MathEngineOutputView, { engine, tex, displayMode: !0 })
186
+ }
187
+ ) : preparation && "error" in preparation && fallback ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: fallback(tex, preparation.error) }) : preparation && "html" in preparation ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
188
+ "div",
189
+ {
190
+ ref: elementRef,
191
+ className: `math-display katex-block markdown-math-display ${className}`.trim(),
192
+ "data-tex": tex,
193
+ style: { overflowX: "auto" },
194
+ dangerouslySetInnerHTML: { __html: preparation.html }
195
+ }
196
+ ) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
197
+ "div",
198
+ {
199
+ ref: elementRef,
200
+ className: `math-display markdown-math-display ${className}`.trim(),
201
+ "data-tex": tex,
202
+ style: { overflowX: "auto" },
203
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { className: "math-display-content", children: [
204
+ "$$",
205
+ tex,
206
+ "$$"
207
+ ] })
208
+ }
209
+ );
210
+ };
211
+ KaTeXBlockMath.displayName = "KaTeXBlockMath";
212
+ // Annotate the CommonJS export names for ESM import in node:
213
+ 0 && (module.exports = {
214
+ DocumentMathMacroScope,
215
+ DocumentMathMacroScopeContext,
216
+ KaTeXBlockMath,
217
+ KaTeXInlineMath,
218
+ MathExpressionCompletenessContext,
219
+ loadKaTeX
220
+ });
@@ -0,0 +1,10 @@
1
+ import 'react';
2
+ export { au as DocumentMathMacroScope, av as DocumentMathMacroScopeContext, az as KaTeXBlockMath, ay as KaTeXInlineMath, ax as KaTeXMathProps, aw as MathExpressionCompletenessContext, at as loadKaTeX } from '../index-Cmne1yUK.cjs';
3
+ import './bottom-stick-scroll-area.cjs';
4
+ import 'react/jsx-runtime';
5
+ import './diff-renderer.cjs';
6
+ import '@stream-mdx/core';
7
+ import '@stream-mdx/protocol';
8
+ import '../trusted-mdx-contract-ElKpkDI5.cjs';
9
+ import '@stream-mdx/worker/engine';
10
+ import '../renderer/virtualized-code.cjs';
@@ -0,0 +1,10 @@
1
+ import 'react';
2
+ export { au as DocumentMathMacroScope, av as DocumentMathMacroScopeContext, az as KaTeXBlockMath, ay as KaTeXInlineMath, ax as KaTeXMathProps, aw as MathExpressionCompletenessContext, at as loadKaTeX } from '../index-D7dyyu3q.js';
3
+ import './bottom-stick-scroll-area.js';
4
+ import 'react/jsx-runtime';
5
+ import './diff-renderer.js';
6
+ import '@stream-mdx/core';
7
+ import '@stream-mdx/protocol';
8
+ import '../trusted-mdx-contract-ElKpkDI5.js';
9
+ import '@stream-mdx/worker/engine';
10
+ import '../renderer/virtualized-code.js';
@@ -0,0 +1,188 @@
1
+ "use client";
2
+
3
+ // src/components/katex-math.tsx
4
+ import { createContext, useContext, useEffect, useMemo, useRef, useState } from "react";
5
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
6
+ var katexPromise = null;
7
+ async function loadKaTeX() {
8
+ return katexPromise || (katexPromise = import("katex").then((mod) => mod && typeof mod == "object" && "default" in mod && mod.default ? mod.default : mod)), katexPromise;
9
+ }
10
+ var DOCUMENT_POSITION_PRECEDING = 2, DOCUMENT_POSITION_FOLLOWING = 4, DocumentMathMacroScope = class {
11
+ constructor() {
12
+ this.entries = /* @__PURE__ */ new Set();
13
+ this.orderedEntries = [];
14
+ this.macroSnapshots = [];
15
+ this.katex = null;
16
+ this.loadError = null;
17
+ this.loading = !1;
18
+ this.disposed = !1;
19
+ }
20
+ register(element, tex, displayMode, complete, publish) {
21
+ let entry = { element, tex, displayMode, complete, publish };
22
+ return this.entries.add(entry), this.refresh(), () => {
23
+ this.entries.delete(entry) && this.refresh();
24
+ };
25
+ }
26
+ dispose() {
27
+ this.disposed = !0, this.entries.clear(), this.orderedEntries = [], this.macroSnapshots = [];
28
+ }
29
+ refresh() {
30
+ if (!this.disposed) {
31
+ if (this.loadError) {
32
+ for (let entry of this.entries)
33
+ entry.publish({ tex: entry.tex, displayMode: entry.displayMode, scope: this, error: this.loadError });
34
+ return;
35
+ }
36
+ if (this.katex) {
37
+ this.recompute(this.katex);
38
+ return;
39
+ }
40
+ this.loading || (this.loading = !0, loadKaTeX().then((katex) => {
41
+ this.disposed || (this.katex = katex, this.refresh());
42
+ }).catch((reason) => {
43
+ if (!this.disposed) {
44
+ this.loadError = reason instanceof Error ? reason : new Error(String(reason));
45
+ for (let entry of this.entries)
46
+ entry.publish({ tex: entry.tex, displayMode: entry.displayMode, scope: this, error: this.loadError });
47
+ }
48
+ }).finally(() => {
49
+ this.loading = !1;
50
+ }));
51
+ }
52
+ }
53
+ recompute(katex) {
54
+ let nextEntries = [...this.entries].sort((left, right) => {
55
+ let position = left.element.compareDocumentPosition(right.element);
56
+ return position & DOCUMENT_POSITION_FOLLOWING ? -1 : position & DOCUMENT_POSITION_PRECEDING ? 1 : 0;
57
+ }), unchangedPrefix = 0;
58
+ for (; unchangedPrefix < this.orderedEntries.length && unchangedPrefix < nextEntries.length && this.orderedEntries[unchangedPrefix] === nextEntries[unchangedPrefix]; )
59
+ unchangedPrefix += 1;
60
+ let macros = unchangedPrefix > 0 ? { ...this.macroSnapshots[unchangedPrefix - 1] } : {};
61
+ this.orderedEntries = nextEntries, this.macroSnapshots.length = unchangedPrefix;
62
+ for (let index = unchangedPrefix; index < nextEntries.length; index += 1) {
63
+ let entry = nextEntries[index], preparation = null;
64
+ if (entry.complete) {
65
+ let nextMacros = { ...macros };
66
+ try {
67
+ let html = katex.renderToString(entry.tex, {
68
+ displayMode: entry.displayMode,
69
+ throwOnError: !1,
70
+ trust: !1,
71
+ maxExpand: 1e3,
72
+ macros: nextMacros
73
+ });
74
+ macros = nextMacros, preparation = { tex: entry.tex, displayMode: entry.displayMode, scope: this, html };
75
+ } catch (reason) {
76
+ preparation = {
77
+ tex: entry.tex,
78
+ displayMode: entry.displayMode,
79
+ scope: this,
80
+ error: reason instanceof Error ? reason : new Error(String(reason))
81
+ };
82
+ }
83
+ }
84
+ this.macroSnapshots[index] = { ...macros }, entry.publish(preparation);
85
+ }
86
+ }
87
+ }, DocumentMathMacroScopeContext = createContext(null), MathExpressionCompletenessContext = createContext(!0), MathEngineOutputView = ({ engine, tex, displayMode }) => {
88
+ let output = useMemo(
89
+ () => engine.render(tex, { displayMode }),
90
+ [engine, engine.id, tex, displayMode]
91
+ );
92
+ return /* @__PURE__ */ jsx(Fragment, { children: output });
93
+ };
94
+ function useMathPreparation(tex, displayMode, elementRef, enabled) {
95
+ let scope = useContext(DocumentMathMacroScopeContext), complete = useContext(MathExpressionCompletenessContext), [preparation, setPreparation] = useState(null);
96
+ return useEffect(() => {
97
+ if (!enabled) return;
98
+ let active = !0;
99
+ if (scope) {
100
+ let element = elementRef.current;
101
+ return element ? scope.register(element, tex, displayMode, complete, (result) => {
102
+ active && setPreparation(result);
103
+ }) : () => {
104
+ active = !1;
105
+ };
106
+ }
107
+ return (async () => {
108
+ try {
109
+ let html = (await loadKaTeX()).renderToString(tex, {
110
+ displayMode,
111
+ throwOnError: !1,
112
+ trust: !1,
113
+ maxExpand: 1e3
114
+ });
115
+ active && setPreparation({ tex, displayMode, scope: null, html });
116
+ } catch (reason) {
117
+ active && setPreparation({
118
+ tex,
119
+ displayMode,
120
+ scope: null,
121
+ error: reason instanceof Error ? reason : new Error(String(reason))
122
+ });
123
+ }
124
+ })(), () => {
125
+ active = !1;
126
+ };
127
+ }, [tex, displayMode, scope, complete, elementRef, enabled]), scope && !complete ? null : preparation?.tex === tex && preparation.displayMode === displayMode && preparation.scope === scope ? preparation : null;
128
+ }
129
+ var KaTeXInlineMath = ({ tex, className = "", fallback, engine }) => {
130
+ let elementRef = useRef(null), preparation = useMathPreparation(tex, !1, elementRef, !engine);
131
+ return engine ? /* @__PURE__ */ jsx("span", { className: `math-inline ${className}`.trim(), "data-tex": tex, children: /* @__PURE__ */ jsx(MathEngineOutputView, { engine, tex, displayMode: !1 }) }) : preparation && "error" in preparation && fallback ? /* @__PURE__ */ jsx(Fragment, { children: fallback(tex, preparation.error) }) : preparation && "html" in preparation ? /* @__PURE__ */ jsx(
132
+ "span",
133
+ {
134
+ ref: elementRef,
135
+ className: `math-inline katex-inline ${className}`.trim(),
136
+ "data-tex": tex,
137
+ dangerouslySetInnerHTML: { __html: preparation.html }
138
+ }
139
+ ) : /* @__PURE__ */ jsxs("span", { ref: elementRef, className: `math-inline ${className}`.trim(), "data-tex": tex, children: [
140
+ "$",
141
+ tex,
142
+ "$"
143
+ ] });
144
+ };
145
+ KaTeXInlineMath.displayName = "KaTeXInlineMath";
146
+ var KaTeXBlockMath = ({ tex, className = "", fallback, engine }) => {
147
+ let elementRef = useRef(null), preparation = useMathPreparation(tex, !0, elementRef, !engine);
148
+ return engine ? /* @__PURE__ */ jsx(
149
+ "div",
150
+ {
151
+ className: `math-display markdown-math-display ${className}`.trim(),
152
+ "data-tex": tex,
153
+ style: { overflowX: "auto" },
154
+ children: /* @__PURE__ */ jsx(MathEngineOutputView, { engine, tex, displayMode: !0 })
155
+ }
156
+ ) : preparation && "error" in preparation && fallback ? /* @__PURE__ */ jsx(Fragment, { children: fallback(tex, preparation.error) }) : preparation && "html" in preparation ? /* @__PURE__ */ jsx(
157
+ "div",
158
+ {
159
+ ref: elementRef,
160
+ className: `math-display katex-block markdown-math-display ${className}`.trim(),
161
+ "data-tex": tex,
162
+ style: { overflowX: "auto" },
163
+ dangerouslySetInnerHTML: { __html: preparation.html }
164
+ }
165
+ ) : /* @__PURE__ */ jsx(
166
+ "div",
167
+ {
168
+ ref: elementRef,
169
+ className: `math-display markdown-math-display ${className}`.trim(),
170
+ "data-tex": tex,
171
+ style: { overflowX: "auto" },
172
+ children: /* @__PURE__ */ jsxs("span", { className: "math-display-content", children: [
173
+ "$$",
174
+ tex,
175
+ "$$"
176
+ ] })
177
+ }
178
+ );
179
+ };
180
+ KaTeXBlockMath.displayName = "KaTeXBlockMath";
181
+ export {
182
+ DocumentMathMacroScope,
183
+ DocumentMathMacroScopeContext,
184
+ KaTeXBlockMath,
185
+ KaTeXInlineMath,
186
+ MathExpressionCompletenessContext,
187
+ loadKaTeX
188
+ };