@conloca/mdx 0.1.14 → 0.1.15
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/README.md +17 -5
- package/dist/index.d.mts +70 -47
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +223 -243
- package/dist/index.mjs.map +1 -1
- package/dist/node.d.mts +48 -71
- package/dist/node.d.mts.map +1 -1
- package/dist/node.mjs +37 -133
- package/dist/node.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -1,142 +1,29 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BlockTypeSelect, BoldItalicUnderlineToggles, CreateLink, DiffSourceToggleWrapper, InsertCodeBlock, InsertImage, InsertTable, InsertThematicBreak, ListsToggle, MDXEditor as MDXEditor$1, Separator, UndoRedo, codeBlockPlugin, codeMirrorPlugin, diffSourcePlugin, frontmatterPlugin, headingsPlugin, imagePlugin, jsxPlugin, linkDialogPlugin, linkPlugin, listsPlugin, markdownShortcutPlugin, quotePlugin, tablePlugin, thematicBreakPlugin, toolbarPlugin } from "@mdxeditor/editor";
|
|
2
2
|
import React, { useEffect, useState } from "react";
|
|
3
|
-
import
|
|
3
|
+
import "@mdxeditor/editor/style.css";
|
|
4
4
|
import * as runtime from "react/jsx-runtime";
|
|
5
5
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
-
import
|
|
7
|
-
import remarkGfm from "remark-gfm";
|
|
8
|
-
import remarkMdxFrontmatter from "remark-mdx-frontmatter";
|
|
9
|
-
import { BlockTypeSelect, BoldItalicUnderlineToggles, CreateLink, DiffSourceToggleWrapper, InsertCodeBlock, InsertImage, InsertTable, InsertThematicBreak, ListsToggle, MDXEditor as MDXEditor$1, Separator, UndoRedo, codeBlockPlugin, codeMirrorPlugin, diffSourcePlugin, frontmatterPlugin, headingsPlugin, imagePlugin, jsxPlugin, linkDialogPlugin, linkPlugin, listsPlugin, markdownShortcutPlugin, quotePlugin, tablePlugin, thematicBreakPlugin, toolbarPlugin } from "@mdxeditor/editor";
|
|
10
|
-
import "@mdxeditor/editor/style.css";
|
|
6
|
+
import * as devRuntime from "react/jsx-dev-runtime";
|
|
11
7
|
|
|
12
|
-
//#region src/
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
]
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
const mdxEvaluationOptions = createMDXEvaluationOptions();
|
|
34
|
-
/**
|
|
35
|
-
* Simple hash function for generating cache keys from MDX content.
|
|
36
|
-
* Uses string hashing to avoid collisions when cacheKey isn't provided.
|
|
37
|
-
*/
|
|
38
|
-
function hashString(str) {
|
|
39
|
-
let hash = 0;
|
|
40
|
-
for (let i = 0; i < str.length; i++) {
|
|
41
|
-
const char = str.charCodeAt(i);
|
|
42
|
-
hash = (hash << 5) - hash + char;
|
|
43
|
-
hash = hash & hash;
|
|
44
|
-
}
|
|
45
|
-
return hash.toString(36);
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Hook for evaluating MDX content into React components.
|
|
49
|
-
*
|
|
50
|
-
* Features:
|
|
51
|
-
* - Evaluates MDX strings into renderable React components using @mdx-js/mdx
|
|
52
|
-
* - Caches compiled components to avoid re-evaluation of unchanged content
|
|
53
|
-
* - Handles both development (jsx-dev-runtime) and production (jsx-runtime) environments
|
|
54
|
-
* - Provides retry mechanism to clear cache and re-evaluate failed compilations
|
|
55
|
-
* - Returns loading and error states for UI feedback
|
|
56
|
-
*
|
|
57
|
-
* @param options - Configuration options
|
|
58
|
-
* @param options.mdxContent - The MDX string to evaluate. If null, returns null component
|
|
59
|
-
* @param options.cacheKey - Optional cache key. If not provided, uses first 100 chars of content
|
|
60
|
-
*
|
|
61
|
-
* @returns Object containing:
|
|
62
|
-
* - Component: The compiled React component (null if not yet compiled or error occurred)
|
|
63
|
-
* - error: Error object if evaluation failed (null otherwise)
|
|
64
|
-
* - isLoading: Boolean indicating if evaluation is in progress
|
|
65
|
-
* - retry: Function to clear cache and retry evaluation
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```tsx
|
|
69
|
-
* const { Component, error, isLoading, retry } = useMDXEvaluation({
|
|
70
|
-
* mdxContent: '# Hello\n\nThis is **MDX** content',
|
|
71
|
-
* cacheKey: 'my-unique-key'
|
|
72
|
-
* });
|
|
73
|
-
*
|
|
74
|
-
* if (isLoading) return <Spinner />;
|
|
75
|
-
* if (error) return <Error message={error.message} onRetry={retry} />;
|
|
76
|
-
* if (Component) return <Component />;
|
|
77
|
-
* ```
|
|
78
|
-
*/
|
|
79
|
-
function useMDXEvaluation({ mdxContent, cacheKey }) {
|
|
80
|
-
const [Component, setComponent] = useState(null);
|
|
81
|
-
const [error, setError] = useState(null);
|
|
82
|
-
const [isLoading, setIsLoading] = useState(!!mdxContent);
|
|
83
|
-
const [retryCount, setRetryCount] = useState(0);
|
|
84
|
-
const retry = () => {
|
|
85
|
-
if (mdxContent) {
|
|
86
|
-
const key = cacheKey || `mdx-${hashString(mdxContent)}`;
|
|
87
|
-
compiledCache.delete(key);
|
|
88
|
-
}
|
|
89
|
-
setRetryCount((prev) => prev + 1);
|
|
90
|
-
};
|
|
91
|
-
useEffect(() => {
|
|
92
|
-
if (!mdxContent) {
|
|
93
|
-
setComponent(null);
|
|
94
|
-
setError(null);
|
|
95
|
-
setIsLoading(false);
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
const key = cacheKey || `mdx-${hashString(mdxContent)}`;
|
|
99
|
-
const cached = compiledCache.get(key);
|
|
100
|
-
if (cached) {
|
|
101
|
-
setComponent(() => cached);
|
|
102
|
-
setError(null);
|
|
103
|
-
setIsLoading(false);
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
const evaluateMDX = async () => {
|
|
107
|
-
try {
|
|
108
|
-
setError(null);
|
|
109
|
-
setIsLoading(true);
|
|
110
|
-
const { default: MDXComponent } = await evaluate(mdxContent, mdxEvaluationOptions);
|
|
111
|
-
compiledCache.set(key, MDXComponent);
|
|
112
|
-
setComponent(() => MDXComponent);
|
|
113
|
-
} catch (err) {
|
|
114
|
-
setError(err instanceof Error ? err : /* @__PURE__ */ new Error("MDX evaluation failed"));
|
|
115
|
-
setComponent(null);
|
|
116
|
-
} finally {
|
|
117
|
-
setIsLoading(false);
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
evaluateMDX();
|
|
121
|
-
}, [
|
|
122
|
-
mdxContent,
|
|
123
|
-
cacheKey,
|
|
124
|
-
retryCount
|
|
125
|
-
]);
|
|
126
|
-
return {
|
|
127
|
-
Component,
|
|
128
|
-
error,
|
|
129
|
-
isLoading,
|
|
130
|
-
retry
|
|
131
|
-
};
|
|
8
|
+
//#region src/editor-core.tsx
|
|
9
|
+
const codeBlockLanguages = {
|
|
10
|
+
text: "Plain Text",
|
|
11
|
+
js: "JavaScript",
|
|
12
|
+
jsx: "JavaScript (React)",
|
|
13
|
+
ts: "TypeScript",
|
|
14
|
+
tsx: "TypeScript (React)",
|
|
15
|
+
css: "CSS",
|
|
16
|
+
html: "HTML",
|
|
17
|
+
json: "JSON",
|
|
18
|
+
md: "Markdown",
|
|
19
|
+
mdx: "MDX",
|
|
20
|
+
bash: "Bash",
|
|
21
|
+
python: "Python"
|
|
22
|
+
};
|
|
23
|
+
function renderInsertImageButton(imageButtonRef) {
|
|
24
|
+
if (!imageButtonRef) return /* @__PURE__ */ jsx(InsertImage, {});
|
|
25
|
+
return React.createElement(InsertImage, { ref: imageButtonRef });
|
|
132
26
|
}
|
|
133
|
-
|
|
134
|
-
//#endregion
|
|
135
|
-
//#region src/mdx-editor.tsx
|
|
136
|
-
/**
|
|
137
|
-
* Error Boundary for catching React errors in MDXEditor.
|
|
138
|
-
* Prevents editor crashes from breaking the entire CMS.
|
|
139
|
-
*/
|
|
140
27
|
var MDXEditorErrorBoundary = class extends React.Component {
|
|
141
28
|
constructor(props) {
|
|
142
29
|
super(props);
|
|
@@ -149,7 +36,7 @@ var MDXEditorErrorBoundary = class extends React.Component {
|
|
|
149
36
|
};
|
|
150
37
|
}
|
|
151
38
|
componentDidCatch(error, errorInfo) {
|
|
152
|
-
console.error(
|
|
39
|
+
console.error(`${this.props.logPrefix} Error boundary caught:`, error, errorInfo);
|
|
153
40
|
}
|
|
154
41
|
render() {
|
|
155
42
|
if (this.state.hasError) return /* @__PURE__ */ jsxs("div", {
|
|
@@ -178,23 +65,33 @@ var MDXEditorErrorBoundary = class extends React.Component {
|
|
|
178
65
|
return this.props.children;
|
|
179
66
|
}
|
|
180
67
|
};
|
|
181
|
-
const
|
|
68
|
+
const BaseMDXEditor = React.forwardRef(({ value, onChange, onSave, readOnly = false, imageDialog, disableImageSettingsButton = false, onImageShortcut, imageButtonRef, jsxComponentDescriptors, className }, ref) => {
|
|
182
69
|
useEffect(() => {
|
|
183
|
-
const handleKeyDown = (
|
|
184
|
-
if ((
|
|
185
|
-
|
|
186
|
-
|
|
70
|
+
const handleKeyDown = (event) => {
|
|
71
|
+
if ((event.ctrlKey || event.metaKey) && event.key === "s") {
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
onSave?.(value);
|
|
74
|
+
}
|
|
75
|
+
if ((event.ctrlKey || event.metaKey) && event.shiftKey && event.key.toLowerCase() === "i") {
|
|
76
|
+
if (!onImageShortcut) return;
|
|
77
|
+
event.preventDefault();
|
|
78
|
+
onImageShortcut();
|
|
187
79
|
}
|
|
188
80
|
};
|
|
189
81
|
document.addEventListener("keydown", handleKeyDown);
|
|
190
82
|
return () => document.removeEventListener("keydown", handleKeyDown);
|
|
191
|
-
}, [
|
|
83
|
+
}, [
|
|
84
|
+
onImageShortcut,
|
|
85
|
+
onSave,
|
|
86
|
+
value
|
|
87
|
+
]);
|
|
192
88
|
return /* @__PURE__ */ jsx(MDXEditor$1, {
|
|
193
89
|
ref,
|
|
194
90
|
markdown: value,
|
|
195
91
|
onChange,
|
|
196
92
|
readOnly,
|
|
197
|
-
|
|
93
|
+
className,
|
|
94
|
+
contentEditableClassName: "conloca-prose conloca-prose--editor max-w-none",
|
|
198
95
|
plugins: [
|
|
199
96
|
headingsPlugin(),
|
|
200
97
|
listsPlugin(),
|
|
@@ -205,24 +102,14 @@ const MDXEditor = React.forwardRef(({ value, onChange, onSave, readOnly = false
|
|
|
205
102
|
thematicBreakPlugin(),
|
|
206
103
|
frontmatterPlugin(),
|
|
207
104
|
codeBlockPlugin({ defaultCodeBlockLanguage: "ts" }),
|
|
208
|
-
codeMirrorPlugin({ codeBlockLanguages
|
|
209
|
-
text: "Plain Text",
|
|
210
|
-
js: "JavaScript",
|
|
211
|
-
jsx: "JavaScript (React)",
|
|
212
|
-
ts: "TypeScript",
|
|
213
|
-
tsx: "TypeScript (React)",
|
|
214
|
-
css: "CSS",
|
|
215
|
-
html: "HTML",
|
|
216
|
-
json: "JSON",
|
|
217
|
-
md: "Markdown",
|
|
218
|
-
mdx: "MDX",
|
|
219
|
-
bash: "Bash",
|
|
220
|
-
python: "Python"
|
|
221
|
-
} }),
|
|
105
|
+
codeMirrorPlugin({ codeBlockLanguages }),
|
|
222
106
|
diffSourcePlugin({ viewMode: "rich-text" }),
|
|
223
107
|
markdownShortcutPlugin(),
|
|
224
|
-
jsxPlugin(),
|
|
225
|
-
imagePlugin(
|
|
108
|
+
jsxPlugin({ jsxComponentDescriptors: jsxComponentDescriptors ?? [] }),
|
|
109
|
+
imagePlugin(imageDialog ? {
|
|
110
|
+
ImageDialog: imageDialog,
|
|
111
|
+
disableImageSettingsButton
|
|
112
|
+
} : void 0),
|
|
226
113
|
toolbarPlugin({ toolbarContents: () => /* @__PURE__ */ jsxs(DiffSourceToggleWrapper, { children: [
|
|
227
114
|
/* @__PURE__ */ jsx(UndoRedo, {}),
|
|
228
115
|
/* @__PURE__ */ jsx(Separator, {}),
|
|
@@ -233,7 +120,7 @@ const MDXEditor = React.forwardRef(({ value, onChange, onSave, readOnly = false
|
|
|
233
120
|
/* @__PURE__ */ jsx(BlockTypeSelect, {}),
|
|
234
121
|
/* @__PURE__ */ jsx(Separator, {}),
|
|
235
122
|
/* @__PURE__ */ jsx(CreateLink, {}),
|
|
236
|
-
|
|
123
|
+
renderInsertImageButton(imageButtonRef),
|
|
237
124
|
/* @__PURE__ */ jsx(InsertTable, {}),
|
|
238
125
|
/* @__PURE__ */ jsx(InsertThematicBreak, {}),
|
|
239
126
|
/* @__PURE__ */ jsx(Separator, {}),
|
|
@@ -242,61 +129,8 @@ const MDXEditor = React.forwardRef(({ value, onChange, onSave, readOnly = false
|
|
|
242
129
|
]
|
|
243
130
|
});
|
|
244
131
|
});
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
* DeferredMDXEditor - A wrapper component that defers MDXEditor initialization to avoid React 19 render-phase state updates
|
|
248
|
-
*
|
|
249
|
-
* ## Why This Wrapper Exists
|
|
250
|
-
*
|
|
251
|
-
* MDXEditor (v3.38.0) uses Lexical under the hood, which performs synchronous state updates during its initial render
|
|
252
|
-
* when provided with markdown content. This violates React 19's stricter rules about state updates during render.
|
|
253
|
-
*
|
|
254
|
-
* ## The Specific React 19 Issue
|
|
255
|
-
*
|
|
256
|
-
* In React 19, the framework is more strict about preventing state updates during the render phase. When MDXEditor
|
|
257
|
-
* initializes with the `markdown` prop, Lexical immediately:
|
|
258
|
-
* 1. Parses the markdown content
|
|
259
|
-
* 2. Converts it to Lexical's internal state representation
|
|
260
|
-
* 3. Attempts to commit these changes via `$commitPendingUpdates`
|
|
261
|
-
*
|
|
262
|
-
* This happens synchronously during render, causing the error:
|
|
263
|
-
* "Can't perform a React state update on a component that hasn't mounted yet"
|
|
264
|
-
*
|
|
265
|
-
* ## How This Wrapper Solves It
|
|
266
|
-
*
|
|
267
|
-
* By deferring the actual MDXEditor component render by 10ms using setTimeout, we ensure:
|
|
268
|
-
* 1. React completes the current render cycle
|
|
269
|
-
* 2. The component tree is fully mounted
|
|
270
|
-
* 3. Only then does MDXEditor initialize with its content
|
|
271
|
-
*
|
|
272
|
-
* This prevents Lexical from trying to update state during the render phase.
|
|
273
|
-
*
|
|
274
|
-
* ## When This Issue Occurs
|
|
275
|
-
*
|
|
276
|
-
* This issue is particularly prevalent when:
|
|
277
|
-
* - Using MDXEditor in a modal that mounts/unmounts frequently
|
|
278
|
-
* - Dynamically loading content into the editor
|
|
279
|
-
* - Using React 19 with its stricter concurrent rendering rules
|
|
280
|
-
*
|
|
281
|
-
* ## Technical Details
|
|
282
|
-
*
|
|
283
|
-
* The error stack trace shows the issue originates from:
|
|
284
|
-
* - triggerListeners @ Lexical.dev.mjs:8156
|
|
285
|
-
* - $commitPendingUpdates @ Lexical.dev.mjs:8116
|
|
286
|
-
*
|
|
287
|
-
* These Lexical internal methods are called during the initial render when markdown content is provided.
|
|
288
|
-
*
|
|
289
|
-
* ## Future Considerations
|
|
290
|
-
*
|
|
291
|
-
* This wrapper is a workaround for what should ideally be fixed in the MDXEditor/Lexical library itself.
|
|
292
|
-
* The library should defer its own state initialization to a useEffect or similar to be React 19 compliant.
|
|
293
|
-
*
|
|
294
|
-
* Related issues:
|
|
295
|
-
* - https://github.com/mdx-editor/editor/issues/494 (React 19 compatibility)
|
|
296
|
-
* - Various modal-related issues (#584, #581, #758, #276)
|
|
297
|
-
*/
|
|
298
|
-
const DeferredMDXEditor = ({ value, onChange, onSave, readOnly }) => {
|
|
299
|
-
const editorRef = React.useRef(null);
|
|
132
|
+
BaseMDXEditor.displayName = "BaseMDXEditor";
|
|
133
|
+
function DeferredMDXEditor({ EditorComponent, editorRef, ...props }) {
|
|
300
134
|
const [isReady, setIsReady] = useState(false);
|
|
301
135
|
useEffect(() => {
|
|
302
136
|
const timer = setTimeout(() => {
|
|
@@ -311,47 +145,46 @@ const DeferredMDXEditor = ({ value, onChange, onSave, readOnly }) => {
|
|
|
311
145
|
});
|
|
312
146
|
return /* @__PURE__ */ jsx("div", {
|
|
313
147
|
"data-testid": "deferred-editor-ready",
|
|
314
|
-
children: /* @__PURE__ */ jsx(
|
|
148
|
+
children: /* @__PURE__ */ jsx(EditorComponent, {
|
|
315
149
|
ref: editorRef,
|
|
316
|
-
|
|
317
|
-
onChange,
|
|
318
|
-
onSave,
|
|
319
|
-
readOnly
|
|
150
|
+
...props
|
|
320
151
|
})
|
|
321
152
|
});
|
|
322
|
-
}
|
|
323
|
-
|
|
153
|
+
}
|
|
154
|
+
function Dialog({ open, onOpenChange, children }) {
|
|
324
155
|
if (!open) return null;
|
|
325
156
|
return /* @__PURE__ */ jsxs("div", {
|
|
326
157
|
className: "fixed inset-0 z-50 flex items-center justify-center",
|
|
327
158
|
children: [/* @__PURE__ */ jsx("div", {
|
|
159
|
+
role: "presentation",
|
|
328
160
|
className: "fixed inset-0 bg-black/50",
|
|
329
161
|
onClick: () => onOpenChange(false)
|
|
330
162
|
}), /* @__PURE__ */ jsx("div", {
|
|
331
|
-
className: "relative z-50 bg-white rounded-lg shadow-lg max-w-[90vw] max-h-[90vh] overflow-hidden",
|
|
163
|
+
className: "relative z-50 bg-white dark:bg-grey-03 rounded-lg shadow-lg max-w-[90vw] max-h-[90vh] overflow-hidden",
|
|
332
164
|
children
|
|
333
165
|
})]
|
|
334
166
|
});
|
|
335
|
-
}
|
|
336
|
-
|
|
167
|
+
}
|
|
168
|
+
function DialogContent({ children, className = "" }) {
|
|
337
169
|
return /* @__PURE__ */ jsx("div", {
|
|
338
170
|
className: `p-6 ${className}`,
|
|
339
171
|
children
|
|
340
172
|
});
|
|
341
|
-
}
|
|
342
|
-
|
|
173
|
+
}
|
|
174
|
+
function DialogHeader({ children }) {
|
|
343
175
|
return /* @__PURE__ */ jsx("div", {
|
|
344
176
|
className: "mb-4",
|
|
345
177
|
children
|
|
346
178
|
});
|
|
347
|
-
}
|
|
348
|
-
|
|
179
|
+
}
|
|
180
|
+
function DialogTitle({ children }) {
|
|
349
181
|
return /* @__PURE__ */ jsx("h2", {
|
|
350
|
-
className: "text-xl font-semibold",
|
|
182
|
+
className: "text-xl font-semibold text-grey-01 dark:text-grey-12",
|
|
351
183
|
children
|
|
352
184
|
});
|
|
353
|
-
}
|
|
354
|
-
|
|
185
|
+
}
|
|
186
|
+
function BaseMDXEditorModal({ isOpen, onClose, filePath, initialContent, onSave, headerExtra, headerTools, onBeforeClose, EditorComponent, editorClassName }) {
|
|
187
|
+
const editorRef = React.useRef(null);
|
|
355
188
|
const [content, setContent] = useState(initialContent);
|
|
356
189
|
const [isEditorReady, setIsEditorReady] = useState(false);
|
|
357
190
|
const [isSaving, setIsSaving] = useState(false);
|
|
@@ -394,13 +227,18 @@ const MDXEditorModal = ({ isOpen, onClose, filePath, initialContent, onSave, hea
|
|
|
394
227
|
className: "flex items-center justify-between gap-4",
|
|
395
228
|
children: [
|
|
396
229
|
/* @__PURE__ */ jsx(DialogTitle, { children: filePath ? `Edit: ${filePath}` : "New MDX File" }),
|
|
397
|
-
headerExtra && /* @__PURE__ */
|
|
230
|
+
(headerExtra || headerTools) && /* @__PURE__ */ jsxs("div", {
|
|
398
231
|
className: "flex items-center gap-2",
|
|
399
|
-
children: headerExtra
|
|
232
|
+
children: [headerExtra, headerTools?.({
|
|
233
|
+
content,
|
|
234
|
+
setContent,
|
|
235
|
+
editorRef
|
|
236
|
+
})]
|
|
400
237
|
}),
|
|
401
238
|
/* @__PURE__ */ jsx("button", {
|
|
239
|
+
type: "button",
|
|
402
240
|
onClick: handleClose,
|
|
403
|
-
className: "p-2 hover:bg-gray-100
|
|
241
|
+
className: "p-2 rounded text-grey-04 dark:text-grey-07 hover:bg-gray-100 dark:hover:bg-grey-04 hover:text-grey-01 dark:hover:text-grey-12",
|
|
404
242
|
"aria-label": "Close",
|
|
405
243
|
children: "✕"
|
|
406
244
|
})
|
|
@@ -408,14 +246,20 @@ const MDXEditorModal = ({ isOpen, onClose, filePath, initialContent, onSave, hea
|
|
|
408
246
|
}) }),
|
|
409
247
|
/* @__PURE__ */ jsx("div", {
|
|
410
248
|
className: "flex-1 overflow-hidden",
|
|
411
|
-
children: isEditorReady ? /* @__PURE__ */ jsx(MDXEditorErrorBoundary, {
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
249
|
+
children: isEditorReady ? /* @__PURE__ */ jsx(MDXEditorErrorBoundary, {
|
|
250
|
+
logPrefix: "[MDXEditor]",
|
|
251
|
+
children: /* @__PURE__ */ jsx(DeferredMDXEditor, {
|
|
252
|
+
EditorComponent,
|
|
253
|
+
editorRef,
|
|
254
|
+
value: content,
|
|
255
|
+
onChange: setContent,
|
|
256
|
+
onSave: handleSave,
|
|
257
|
+
className: editorClassName
|
|
258
|
+
})
|
|
259
|
+
}) : /* @__PURE__ */ jsx("div", {
|
|
416
260
|
className: "flex items-center justify-center h-full",
|
|
417
261
|
children: /* @__PURE__ */ jsx("div", {
|
|
418
|
-
className: "text-gray-500",
|
|
262
|
+
className: "text-gray-500 dark:text-grey-07",
|
|
419
263
|
"data-testid": "modal-editor-loading",
|
|
420
264
|
children: "Loading editor..."
|
|
421
265
|
})
|
|
@@ -424,11 +268,13 @@ const MDXEditorModal = ({ isOpen, onClose, filePath, initialContent, onSave, hea
|
|
|
424
268
|
/* @__PURE__ */ jsxs("div", {
|
|
425
269
|
className: "flex justify-end gap-2 mt-4",
|
|
426
270
|
children: [/* @__PURE__ */ jsx("button", {
|
|
271
|
+
type: "button",
|
|
427
272
|
onClick: handleClose,
|
|
428
|
-
className: "px-4 py-2 border border-gray-300 rounded hover:bg-gray-50",
|
|
273
|
+
className: "px-4 py-2 border border-gray-300 dark:border-grey-04 rounded text-grey-01 dark:text-grey-12 hover:bg-gray-50 dark:hover:bg-grey-04",
|
|
429
274
|
disabled: isSaving,
|
|
430
275
|
children: "Cancel"
|
|
431
276
|
}), /* @__PURE__ */ jsx("button", {
|
|
277
|
+
type: "button",
|
|
432
278
|
onClick: handleSave,
|
|
433
279
|
className: "px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 disabled:opacity-50 disabled:cursor-not-allowed",
|
|
434
280
|
disabled: isSaving,
|
|
@@ -438,8 +284,142 @@ const MDXEditorModal = ({ isOpen, onClose, filePath, initialContent, onSave, hea
|
|
|
438
284
|
]
|
|
439
285
|
})
|
|
440
286
|
});
|
|
441
|
-
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
//#endregion
|
|
290
|
+
//#region src/hooks/useMDXEvaluation.ts
|
|
291
|
+
const compiledCache = /* @__PURE__ */ new Map();
|
|
292
|
+
/**
|
|
293
|
+
* Execute pre-compiled MDX code (compiled with outputFormat: 'function-body').
|
|
294
|
+
* This is the same as @mdx-js/mdx's run() — inlined here to avoid importing
|
|
295
|
+
* the full @mdx-js/mdx package (which pulls acorn + remark + unified into the browser).
|
|
296
|
+
*
|
|
297
|
+
* Note: Uses AsyncFunction constructor which requires CSP `unsafe-eval`.
|
|
298
|
+
* This is acceptable since the CMS admin UI runs in a trusted environment.
|
|
299
|
+
*/
|
|
300
|
+
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor;
|
|
301
|
+
async function runMDX(code, options) {
|
|
302
|
+
return new AsyncFunction(String(code))(options);
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Create the JSX runtime options for runMDX().
|
|
306
|
+
* Checked once at module load to avoid repeated environment checks.
|
|
307
|
+
*/
|
|
308
|
+
function createRunOptions() {
|
|
309
|
+
return process.env.NODE_ENV === "development" ? {
|
|
310
|
+
...devRuntime,
|
|
311
|
+
development: true
|
|
312
|
+
} : { ...runtime };
|
|
313
|
+
}
|
|
314
|
+
const runOptions = createRunOptions();
|
|
315
|
+
/**
|
|
316
|
+
* Simple hash function for generating cache keys from MDX content.
|
|
317
|
+
*/
|
|
318
|
+
function hashString(str) {
|
|
319
|
+
let hash = 0;
|
|
320
|
+
for (let i = 0; i < str.length; i++) {
|
|
321
|
+
const char = str.charCodeAt(i);
|
|
322
|
+
hash = (hash << 5) - hash + char;
|
|
323
|
+
hash = hash & hash;
|
|
324
|
+
}
|
|
325
|
+
return hash.toString(36);
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Hook for evaluating pre-compiled MDX code into React components.
|
|
329
|
+
*
|
|
330
|
+
* Uses the browser-side half of a server-compile + browser-run pattern:
|
|
331
|
+
* 1. The server compiles MDX → JavaScript function body
|
|
332
|
+
* 2. The browser executes the pre-compiled code with run() (zero compiler dependencies)
|
|
333
|
+
*
|
|
334
|
+
* This keeps the full MDX compilation pipeline on the server and only ships
|
|
335
|
+
* a tiny runtime helper to the browser.
|
|
336
|
+
*
|
|
337
|
+
* @param options.compiledCode - The pre-compiled MDX function body. If null, returns null component
|
|
338
|
+
* @param options.cacheKey - Optional cache key. If not provided, uses hash of code
|
|
339
|
+
*
|
|
340
|
+
* @returns Object containing Component, error, isLoading, retry
|
|
341
|
+
*/
|
|
342
|
+
function useMDXEvaluation({ compiledCode, cacheKey }) {
|
|
343
|
+
const [Component, setComponent] = useState(null);
|
|
344
|
+
const [error, setError] = useState(null);
|
|
345
|
+
const [isLoading, setIsLoading] = useState(() => {
|
|
346
|
+
if (!compiledCode) return false;
|
|
347
|
+
const key = cacheKey || `mdx-${hashString(compiledCode)}`;
|
|
348
|
+
return !compiledCache.has(key);
|
|
349
|
+
});
|
|
350
|
+
const [retryCount, setRetryCount] = useState(0);
|
|
351
|
+
const retry = () => {
|
|
352
|
+
if (compiledCode) {
|
|
353
|
+
const key = cacheKey || `mdx-${hashString(compiledCode)}`;
|
|
354
|
+
compiledCache.delete(key);
|
|
355
|
+
}
|
|
356
|
+
setRetryCount((prev) => prev + 1);
|
|
357
|
+
};
|
|
358
|
+
useEffect(() => {
|
|
359
|
+
if (!compiledCode) {
|
|
360
|
+
setComponent(null);
|
|
361
|
+
setError(null);
|
|
362
|
+
setIsLoading(false);
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
const key = cacheKey || `mdx-${hashString(compiledCode)}`;
|
|
366
|
+
const cached = compiledCache.get(key);
|
|
367
|
+
if (cached) {
|
|
368
|
+
setComponent(() => cached);
|
|
369
|
+
setError(null);
|
|
370
|
+
setIsLoading(false);
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
let cancelled = false;
|
|
374
|
+
const compileAndRun = async () => {
|
|
375
|
+
try {
|
|
376
|
+
setError(null);
|
|
377
|
+
setIsLoading(true);
|
|
378
|
+
const { default: MDXComponent } = await runMDX(compiledCode, runOptions);
|
|
379
|
+
if (cancelled) return;
|
|
380
|
+
compiledCache.set(key, MDXComponent);
|
|
381
|
+
setComponent(() => MDXComponent);
|
|
382
|
+
} catch (err) {
|
|
383
|
+
if (cancelled) return;
|
|
384
|
+
setError(err instanceof Error ? err : /* @__PURE__ */ new Error("MDX evaluation failed"));
|
|
385
|
+
setComponent(null);
|
|
386
|
+
} finally {
|
|
387
|
+
if (!cancelled) setIsLoading(false);
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
compileAndRun();
|
|
391
|
+
return () => {
|
|
392
|
+
cancelled = true;
|
|
393
|
+
};
|
|
394
|
+
}, [
|
|
395
|
+
compiledCode,
|
|
396
|
+
cacheKey,
|
|
397
|
+
retryCount
|
|
398
|
+
]);
|
|
399
|
+
return {
|
|
400
|
+
Component,
|
|
401
|
+
error,
|
|
402
|
+
isLoading,
|
|
403
|
+
retry
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
//#endregion
|
|
408
|
+
//#region src/mdx-editor.tsx
|
|
409
|
+
const MDXEditor = React.forwardRef((props, ref) => {
|
|
410
|
+
return /* @__PURE__ */ jsx(BaseMDXEditor, {
|
|
411
|
+
ref,
|
|
412
|
+
...props
|
|
413
|
+
});
|
|
414
|
+
});
|
|
415
|
+
MDXEditor.displayName = "MDXEditor";
|
|
416
|
+
function MDXEditorModal(props) {
|
|
417
|
+
return /* @__PURE__ */ jsx(BaseMDXEditorModal, {
|
|
418
|
+
...props,
|
|
419
|
+
EditorComponent: MDXEditor
|
|
420
|
+
});
|
|
421
|
+
}
|
|
442
422
|
|
|
443
423
|
//#endregion
|
|
444
|
-
export { MDXEditor, MDXEditorModal, useMDXEvaluation };
|
|
424
|
+
export { BaseMDXEditor, BaseMDXEditorModal, MDXEditor, MDXEditorModal, useMDXEvaluation };
|
|
445
425
|
//# sourceMappingURL=index.mjs.map
|