@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
@@ -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
+ };