@conloca/mdx 0.1.15 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -18
- package/dist/code-block-frame.d.ts +41 -0
- package/dist/code-block-frame.d.ts.map +1 -0
- package/dist/code-block-frame.js +187 -0
- package/dist/code-block-frame.js.map +1 -0
- package/dist/editor-core.d.ts +56 -0
- package/dist/editor-core.d.ts.map +1 -0
- package/dist/editor-core.js +225 -0
- package/dist/editor-core.js.map +1 -0
- package/dist/esm-passthrough.d.ts +36 -0
- package/dist/esm-passthrough.d.ts.map +1 -0
- package/dist/esm-passthrough.js +237 -0
- package/dist/esm-passthrough.js.map +1 -0
- package/dist/fragments.d.ts +37 -0
- package/dist/fragments.d.ts.map +1 -0
- package/dist/fragments.js +53 -0
- package/dist/fragments.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/markdown-options.d.ts +3 -0
- package/dist/markdown-options.d.ts.map +1 -0
- package/dist/markdown-options.js +20 -0
- package/dist/markdown-options.js.map +1 -0
- package/dist/mdx/compile-request.d.ts +5 -0
- package/dist/mdx/compile-request.d.ts.map +1 -0
- package/dist/mdx/compile-request.js +40 -0
- package/dist/mdx/compile-request.js.map +1 -0
- package/dist/mdx/compile.d.ts +3 -0
- package/dist/mdx/compile.d.ts.map +1 -0
- package/dist/mdx/compile.js +47 -0
- package/dist/mdx/compile.js.map +1 -0
- package/dist/mdx/evaluate.d.ts +39 -0
- package/dist/mdx/evaluate.d.ts.map +1 -0
- package/dist/mdx/evaluate.js +131 -0
- package/dist/mdx/evaluate.js.map +1 -0
- package/dist/mdx/heading-outline.d.ts +46 -0
- package/dist/mdx/heading-outline.d.ts.map +1 -0
- package/dist/mdx/heading-outline.js +114 -0
- package/dist/mdx/heading-outline.js.map +1 -0
- package/dist/mdx/restrict-expressions.d.ts +43 -0
- package/dist/mdx/restrict-expressions.d.ts.map +1 -0
- package/dist/mdx/restrict-expressions.js +160 -0
- package/dist/mdx/restrict-expressions.js.map +1 -0
- package/dist/mdx-editor.d.ts +7 -0
- package/dist/mdx-editor.d.ts.map +1 -0
- package/dist/mdx-editor.js +8 -0
- package/dist/mdx-editor.js.map +1 -0
- package/dist/node.d.ts +18 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +18 -0
- package/dist/node.js.map +1 -0
- package/dist/sandbox-frame.d.ts +114 -0
- package/dist/sandbox-frame.d.ts.map +1 -0
- package/dist/sandbox-frame.js +234 -0
- package/dist/sandbox-frame.js.map +1 -0
- package/dist/types.d.ts +19 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +82 -26
- package/src/code-block-frame.tsx +262 -0
- package/src/editor-core.tsx +403 -0
- package/src/editor-styles.css +282 -0
- package/src/esm-passthrough.ts +269 -0
- package/src/fragments.ts +91 -0
- package/src/index.ts +5 -0
- package/src/markdown-options.ts +21 -0
- package/src/mdx/compile-request.ts +53 -0
- package/src/mdx/compile.ts +51 -0
- package/src/mdx/evaluate.ts +144 -0
- package/src/mdx/heading-outline.ts +153 -0
- package/src/mdx/restrict-expressions.ts +173 -0
- package/src/mdx-editor.tsx +15 -0
- package/src/node.ts +25 -0
- package/src/sandbox-frame.ts +277 -0
- package/src/types.ts +23 -0
- package/dist/index.d.mts +0 -96
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -425
- package/dist/index.mjs.map +0 -1
- package/dist/node.d.mts +0 -107
- package/dist/node.d.mts.map +0 -1
- package/dist/node.mjs +0 -125
- package/dist/node.mjs.map +0 -1
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { BlockTypeSelect, BoldItalicUnderlineToggles, ButtonWithTooltip, ChangeCodeMirrorLanguage, ConditionalContents, CreateLink, codeBlockPlugin, codeMirrorPlugin, DiffSourceToggleWrapper, diffSourcePlugin, GenericJsxEditor, HighlightToggle, headingsPlugin, InsertCodeBlock, InsertImage, InsertTable, InsertThematicBreak, imagePlugin, jsxPlugin, ListsToggle, linkDialogPlugin, linkPlugin, listsPlugin, MDXEditor as MDXEditorLib, markdownShortcutPlugin, quotePlugin, Separator, StrikeThroughSupSubToggles, tablePlugin, thematicBreakPlugin, toolbarPlugin, UndoRedo, } from '@mdxeditor/editor';
|
|
3
|
+
import { Maximize2, Minimize2 } from 'lucide-react';
|
|
4
|
+
import React, { useCallback, useEffect, useImperativeHandle, useRef, useSyncExternalStore } from 'react';
|
|
5
|
+
import { conlocaCodeBlockDescriptor } from './code-block-frame.js';
|
|
6
|
+
import { joinEsm, splitEsm } from './esm-passthrough.js';
|
|
7
|
+
import { TO_MARKDOWN_OPTIONS } from './markdown-options.js';
|
|
8
|
+
const FOCUS_MODE_STORAGE_KEY = 'conloca.mdxeditor.focusMode';
|
|
9
|
+
const FOCUS_MODE_BODY_CLASS = 'mdxeditor-focus-mode';
|
|
10
|
+
/**
|
|
11
|
+
* Toggle focus-mode by writing the class on `document.body`. Direct DOM
|
|
12
|
+
* mutation (vs. piping a boolean through React state into the editor's
|
|
13
|
+
* `className` prop) avoids re-rendering Lexical on every toggle — the
|
|
14
|
+
* editor's prop is the only entry point and changing it forces a parse
|
|
15
|
+
* round-trip. Persisted in localStorage so the preference survives reloads.
|
|
16
|
+
*/
|
|
17
|
+
function readFocusMode() {
|
|
18
|
+
if (typeof document === 'undefined')
|
|
19
|
+
return false;
|
|
20
|
+
return document.body.classList.contains(FOCUS_MODE_BODY_CLASS);
|
|
21
|
+
}
|
|
22
|
+
function subscribeFocusMode(callback) {
|
|
23
|
+
if (typeof window === 'undefined')
|
|
24
|
+
return () => { };
|
|
25
|
+
const handler = () => callback();
|
|
26
|
+
window.addEventListener('conloca:focusModeChange', handler);
|
|
27
|
+
return () => window.removeEventListener('conloca:focusModeChange', handler);
|
|
28
|
+
}
|
|
29
|
+
function setFocusMode(next) {
|
|
30
|
+
if (typeof document === 'undefined')
|
|
31
|
+
return;
|
|
32
|
+
document.body.classList.toggle(FOCUS_MODE_BODY_CLASS, next);
|
|
33
|
+
try {
|
|
34
|
+
if (next) {
|
|
35
|
+
window.localStorage.setItem(FOCUS_MODE_STORAGE_KEY, '1');
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
window.localStorage.removeItem(FOCUS_MODE_STORAGE_KEY);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// localStorage may be unavailable (private mode, SSR) — ignore.
|
|
43
|
+
}
|
|
44
|
+
window.dispatchEvent(new Event('conloca:focusModeChange'));
|
|
45
|
+
}
|
|
46
|
+
function FocusModeToggle() {
|
|
47
|
+
const focusMode = useSyncExternalStore(subscribeFocusMode, readFocusMode, () => false);
|
|
48
|
+
const onClick = useCallback(() => setFocusMode(!focusMode), [focusMode]);
|
|
49
|
+
const Icon = focusMode ? Minimize2 : Maximize2;
|
|
50
|
+
return (_jsx(ButtonWithTooltip, { title: focusMode ? 'Exit focus mode (wider column)' : 'Enter focus mode (narrow column)', onClick: onClick, children: _jsx(Icon, { size: 16, "aria-hidden": true }) }));
|
|
51
|
+
}
|
|
52
|
+
// The empty-string entry is the documented escape hatch for code blocks
|
|
53
|
+
// authored without a language hint. Without it, fences like ```\nfoo\n``` (which
|
|
54
|
+
// the parser tags as language `null` / meta `'N/A'`) trip a hard error in
|
|
55
|
+
// older versions and emit a noisy warning even on v3.55+. See mdx-editor/editor
|
|
56
|
+
// issues #370, #423, #463, #885, #886, #901.
|
|
57
|
+
const codeBlockLanguages = {
|
|
58
|
+
'': 'Plain text',
|
|
59
|
+
text: 'Plain Text',
|
|
60
|
+
js: 'JavaScript',
|
|
61
|
+
jsx: 'JavaScript (React)',
|
|
62
|
+
ts: 'TypeScript',
|
|
63
|
+
tsx: 'TypeScript (React)',
|
|
64
|
+
css: 'CSS',
|
|
65
|
+
html: 'HTML',
|
|
66
|
+
json: 'JSON',
|
|
67
|
+
md: 'Markdown',
|
|
68
|
+
mdx: 'MDX',
|
|
69
|
+
bash: 'Bash',
|
|
70
|
+
python: 'Python',
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Wildcard fallback so any unknown JSX tag round-trips through GenericJsxEditor
|
|
74
|
+
* instead of crashing the importer with "Parsing of the following markdown
|
|
75
|
+
* structure failed". Two entries because the upstream visitor dispatches by
|
|
76
|
+
* `kind` (block vs inline JSX) — without the inline one, an inline `<Badge>`
|
|
77
|
+
* mid-paragraph would be matched by the flow wildcard and lifted out of its
|
|
78
|
+
* paragraph in Lexical. Consumers passing typed descriptors win via the
|
|
79
|
+
* exact-name match in the visitor; the wildcard only handles the leftovers.
|
|
80
|
+
*/
|
|
81
|
+
const wildcardJsxDescriptors = [
|
|
82
|
+
{ name: '*', kind: 'flow', props: [], hasChildren: true, Editor: GenericJsxEditor },
|
|
83
|
+
{ name: '*', kind: 'text', props: [], hasChildren: true, Editor: GenericJsxEditor },
|
|
84
|
+
];
|
|
85
|
+
function renderInsertImageButton(imageButtonRef) {
|
|
86
|
+
if (!imageButtonRef) {
|
|
87
|
+
return _jsx(InsertImage, {});
|
|
88
|
+
}
|
|
89
|
+
return React.createElement(InsertImage, {
|
|
90
|
+
ref: imageButtonRef,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Restore the persisted focus-mode preference on first import. Runs once at
|
|
95
|
+
* module load so the class is in place before any editor mounts — avoids a
|
|
96
|
+
* flash of wide column when the user previously selected focus mode.
|
|
97
|
+
*/
|
|
98
|
+
if (typeof window !== 'undefined') {
|
|
99
|
+
try {
|
|
100
|
+
if (window.localStorage.getItem(FOCUS_MODE_STORAGE_KEY) === '1') {
|
|
101
|
+
document.body.classList.add(FOCUS_MODE_BODY_CLASS);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
// ignore
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
export const BaseMDXEditor = React.forwardRef(({ value, onChange, onSave, readOnly = false, imageDialog, disableImageSettingsButton = false, onImageShortcut, imageButtonRef, imageUploadHandler, jsxComponentDescriptors, extraPlugins, extraToolbarItems, className, placeholder, autoFocus, }, ref) => {
|
|
109
|
+
// The library reads `markdown` once, as `initialMarkdown`, and consumers
|
|
110
|
+
// swap documents by remounting (see the `key=` on MdxPageEditor's and
|
|
111
|
+
// FragmentEditor's editors). So the ESM lifted off the document belongs to
|
|
112
|
+
// whatever the editor was *seeded* with — re-deriving it from `value` would
|
|
113
|
+
// parse the whole document on every keystroke and would be wrong if the
|
|
114
|
+
// prop ever changed without a remount.
|
|
115
|
+
const seededRef = useRef(null);
|
|
116
|
+
seededRef.current ??= splitEsm(value);
|
|
117
|
+
// Known consequence: the editor is seeded with the stripped body, so the
|
|
118
|
+
// Source and Diff views (diffSourcePlugin, below) show a document without
|
|
119
|
+
// the import block even though the committed file has it. Data is safe —
|
|
120
|
+
// retyping an import there is reconciled by `joinEsm` against the preserved
|
|
121
|
+
// bindings rather than declared twice — but the raw view is not a faithful
|
|
122
|
+
// picture of the file on disk. Fixing that needs the library to render ESM
|
|
123
|
+
// it cannot represent in rich-text mode.
|
|
124
|
+
const innerRef = useRef(null);
|
|
125
|
+
// Everything leaving the editor gets the author's import/export block put
|
|
126
|
+
// back verbatim. `onChange` is the save path; the imperative handle is
|
|
127
|
+
// intercepted too so `getMarkdown()` never reports a document that is
|
|
128
|
+
// missing statements the file on disk has.
|
|
129
|
+
const handleChange = useCallback((markdown, initialMarkdownNormalize) => {
|
|
130
|
+
const preserved = seededRef.current;
|
|
131
|
+
onChange(preserved === null ? markdown : joinEsm(preserved, markdown), initialMarkdownNormalize);
|
|
132
|
+
}, [onChange]);
|
|
133
|
+
useImperativeHandle(ref, () => ({
|
|
134
|
+
getMarkdown: () => {
|
|
135
|
+
const markdown = innerRef.current?.getMarkdown() ?? '';
|
|
136
|
+
const preserved = seededRef.current;
|
|
137
|
+
return preserved === null ? markdown : joinEsm(preserved, markdown);
|
|
138
|
+
},
|
|
139
|
+
// Replaces the whole document, so its ESM becomes the preserved block.
|
|
140
|
+
setMarkdown: (markdown) => {
|
|
141
|
+
const split = splitEsm(markdown);
|
|
142
|
+
seededRef.current = split;
|
|
143
|
+
innerRef.current?.setMarkdown(split.body);
|
|
144
|
+
},
|
|
145
|
+
// Inserts at the cursor rather than reseeding, so the preserved block is
|
|
146
|
+
// unaffected. ESM in inserted markdown is dropped by the library, as
|
|
147
|
+
// before — the editor has never been able to author new imports.
|
|
148
|
+
insertMarkdown: (markdown) => innerRef.current?.insertMarkdown(markdown),
|
|
149
|
+
focus: (callbackFn, opts) => innerRef.current?.focus(callbackFn, opts),
|
|
150
|
+
getContentEditableHTML: () => innerRef.current?.getContentEditableHTML() ?? '',
|
|
151
|
+
getSelectionMarkdown: () => innerRef.current?.getSelectionMarkdown() ?? '',
|
|
152
|
+
}), []);
|
|
153
|
+
useEffect(() => {
|
|
154
|
+
const handleKeyDown = (event) => {
|
|
155
|
+
if ((event.ctrlKey || event.metaKey) && event.key === 's') {
|
|
156
|
+
event.preventDefault();
|
|
157
|
+
onSave?.(value);
|
|
158
|
+
}
|
|
159
|
+
if ((event.ctrlKey || event.metaKey) && event.shiftKey && event.key.toLowerCase() === 'i') {
|
|
160
|
+
if (!onImageShortcut) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
event.preventDefault();
|
|
164
|
+
onImageShortcut();
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
document.addEventListener('keydown', handleKeyDown);
|
|
168
|
+
return () => document.removeEventListener('keydown', handleKeyDown);
|
|
169
|
+
}, [onImageShortcut, onSave, value]);
|
|
170
|
+
return (_jsx(MDXEditorLib, { ref: innerRef, markdown: seededRef.current.body, toMarkdownOptions: TO_MARKDOWN_OPTIONS, onChange: handleChange, onError: ({ error, source }) => {
|
|
171
|
+
// Library invokes this synchronously inside a gurx signal, so don't
|
|
172
|
+
// setState here — that triggers a render-phase warning (#872).
|
|
173
|
+
// Logging only is enough; the React error boundary still catches
|
|
174
|
+
// anything that escapes from rendering.
|
|
175
|
+
console.error('[MDXEditor] parse error:', error, '\nsource:', source);
|
|
176
|
+
}, readOnly: readOnly, className: className, placeholder: placeholder, autoFocus: autoFocus, contentEditableClassName: "conloca-prose conloca-prose--editor", plugins: [
|
|
177
|
+
headingsPlugin(),
|
|
178
|
+
listsPlugin(),
|
|
179
|
+
quotePlugin(),
|
|
180
|
+
linkPlugin(),
|
|
181
|
+
linkDialogPlugin(),
|
|
182
|
+
tablePlugin(),
|
|
183
|
+
thematicBreakPlugin(),
|
|
184
|
+
// frontmatterPlugin intentionally omitted: metadata (title,
|
|
185
|
+
// description, id, created, modified) is server-owned and stripped
|
|
186
|
+
// from `content.mdx` on load. Re-introducing the plugin would let
|
|
187
|
+
// the editor round-trip a YAML block as body text, corrupting the
|
|
188
|
+
// file when content is inserted above it.
|
|
189
|
+
codeBlockPlugin({
|
|
190
|
+
defaultCodeBlockLanguage: 'ts',
|
|
191
|
+
codeBlockEditorDescriptors: [conlocaCodeBlockDescriptor],
|
|
192
|
+
}),
|
|
193
|
+
codeMirrorPlugin({ codeBlockLanguages }),
|
|
194
|
+
diffSourcePlugin({ viewMode: 'rich-text' }),
|
|
195
|
+
markdownShortcutPlugin(),
|
|
196
|
+
jsxPlugin({
|
|
197
|
+
jsxComponentDescriptors: [...(jsxComponentDescriptors ?? []), ...wildcardJsxDescriptors],
|
|
198
|
+
}),
|
|
199
|
+
imagePlugin({
|
|
200
|
+
...(imageDialog ? { ImageDialog: imageDialog, disableImageSettingsButton } : {}),
|
|
201
|
+
...(imageUploadHandler ? { imageUploadHandler } : {}),
|
|
202
|
+
}),
|
|
203
|
+
...(extraPlugins ?? []),
|
|
204
|
+
toolbarPlugin({
|
|
205
|
+
// flex-wrap so the toolbar reflows on narrow viewports instead of
|
|
206
|
+
// overflowing horizontally — workaround documented in
|
|
207
|
+
// mdx-editor/editor#908.
|
|
208
|
+
toolbarClassName: 'mdxeditor-toolbar-wrap',
|
|
209
|
+
toolbarContents: () => (_jsx(DiffSourceToggleWrapper, { children: _jsx(ConditionalContents, { options: [
|
|
210
|
+
{
|
|
211
|
+
// When the user clicks into a code block, swap most of
|
|
212
|
+
// the rich-text toolbar for the language picker — those
|
|
213
|
+
// controls are no-ops inside CodeMirror anyway.
|
|
214
|
+
when: (editor) => editor?.editorType === 'codeblock',
|
|
215
|
+
contents: () => _jsx(ChangeCodeMirrorLanguage, {}),
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
fallback: () => (_jsxs(_Fragment, { children: [_jsx(UndoRedo, {}), _jsx(Separator, {}), _jsx(BoldItalicUnderlineToggles, {}), _jsx(StrikeThroughSupSubToggles, {}), _jsx(HighlightToggle, {}), _jsx(Separator, {}), _jsx(ListsToggle, {}), _jsx(Separator, {}), _jsx(BlockTypeSelect, {}), _jsx(Separator, {}), _jsx(CreateLink, {}), renderInsertImageButton(imageButtonRef), _jsx(InsertTable, {}), _jsx(InsertThematicBreak, {}), _jsx(Separator, {}), _jsx(InsertCodeBlock, {}), extraToolbarItems, _jsx(Separator, {}), _jsx(FocusModeToggle, {})] })),
|
|
219
|
+
},
|
|
220
|
+
] }) })),
|
|
221
|
+
}),
|
|
222
|
+
] }));
|
|
223
|
+
});
|
|
224
|
+
BaseMDXEditor.displayName = 'BaseMDXEditor';
|
|
225
|
+
//# sourceMappingURL=editor-core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"editor-core.js","sourceRoot":"","sources":["../src/editor-core.tsx"],"names":[],"mappings":";AAAA,OAAO,EACL,eAAe,EACf,0BAA0B,EAC1B,iBAAiB,EACjB,wBAAwB,EACxB,mBAAmB,EACnB,UAAU,EACV,eAAe,EACf,gBAAgB,EAChB,uBAAuB,EACvB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,cAAc,EAC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AC,WAAW,KAAG,EACf,KAAC,mBAAmB,KAAG,EACvB,KAAC,SAAS,KAAG,EACb,KAAC,eAAe,KAAG,EAClB,iBAAiB,EAClB,KAAC,SAAS,KAAG,EACb,KAAC,eAAe,KAAG,IAClB,CACJ;6BACF;yBACF,GACD,GACsB,CAC3B;aACF,CAAC;SACH,GACD,CACH,CAAC;AACJ,CAAC,CACF,CAAC;AAEF,aAAa,CAAC,WAAW,GAAG,eAAe,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** ESM lifted off a document, ready to be re-attached by {@link joinEsm}. */
|
|
2
|
+
export interface PreservedEsm {
|
|
3
|
+
/**
|
|
4
|
+
* Verbatim text to re-emit ahead of the edited body — the document's leading
|
|
5
|
+
* ESM block including the blank lines around it, so a no-op edit reproduces
|
|
6
|
+
* the original bytes exactly. `''` when the document had no ESM.
|
|
7
|
+
*/
|
|
8
|
+
readonly prefix: string;
|
|
9
|
+
/** The markdown to hand the editor: the source with `prefix` removed. */
|
|
10
|
+
readonly body: string;
|
|
11
|
+
/** Every binding name declared by the preserved statements. */
|
|
12
|
+
readonly bindings: ReadonlySet<string>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Lift every ESM statement off `source`.
|
|
16
|
+
*
|
|
17
|
+
* The document's leading ESM block is preserved as a literal slice, so
|
|
18
|
+
* re-attaching it reproduces the original bytes. Statements that appear *below*
|
|
19
|
+
* the first content block are hoisted into that same block: their original
|
|
20
|
+
* position is not recoverable once the user has restructured the body, and MDX
|
|
21
|
+
* compiles ESM to module scope regardless of where it sat, so hoisting keeps
|
|
22
|
+
* the site's build working at the cost of moving those lines. Relative
|
|
23
|
+
* order among the statements is preserved.
|
|
24
|
+
*/
|
|
25
|
+
export declare function splitEsm(source: string): PreservedEsm;
|
|
26
|
+
/**
|
|
27
|
+
* Re-attach preserved ESM to markdown coming out of the editor.
|
|
28
|
+
*
|
|
29
|
+
* Any import the library rebuilt for a descriptor carrying `import.from` is
|
|
30
|
+
* reconciled against the preserved bindings first, so nothing is declared
|
|
31
|
+
* twice: a rebuilt statement whose every binding is already preserved is
|
|
32
|
+
* dropped, and one that also carries a new binding is narrowed to just the new
|
|
33
|
+
* names. The author's own statements are never rewritten.
|
|
34
|
+
*/
|
|
35
|
+
export declare function joinEsm(preserved: PreservedEsm, body: string): string;
|
|
36
|
+
//# sourceMappingURL=esm-passthrough.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"esm-passthrough.d.ts","sourceRoot":"","sources":["../src/esm-passthrough.ts"],"names":[],"mappings":"AAwEA,6EAA6E;AAC7E,MAAM,WAAW,YAAY;IAC3B;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,yEAAyE;IACzE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,+DAA+D;IAC/D,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;CACxC;AA2ED;;;;;;;;;;GAUG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,CAiDrD;AAED;;;;;;;;GAQG;AACH,wBAAgB,OAAO,CAAC,SAAS,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAsCrE"}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import remarkMdx from 'remark-mdx';
|
|
2
|
+
import remarkParse from 'remark-parse';
|
|
3
|
+
import { unified } from 'unified';
|
|
4
|
+
/**
|
|
5
|
+
* Verbatim passthrough for MDX ESM statements (`import` / `export`).
|
|
6
|
+
*
|
|
7
|
+
* `@mdxeditor/editor` cannot represent an `mdxjsEsm` node: its shipped
|
|
8
|
+
* `MdastMdxJsEsmVisitor` matches the node and its `visitNode` body is empty, so
|
|
9
|
+
* nothing reaches the Lexical tree. On export the library *rebuilds* an import
|
|
10
|
+
* block from scratch, using only the components it saw referred to in the
|
|
11
|
+
* document (`exportMarkdownFromLexical.js`). Three ways that loses author data,
|
|
12
|
+
* all reproduced against v3.55.0 in `esm-passthrough.test.ts`:
|
|
13
|
+
*
|
|
14
|
+
* 1. A component nested inside another JSX element is registered by name with
|
|
15
|
+
* no import source attached, so its specifier is pruned from the rebuilt
|
|
16
|
+
* line — `import { Tabs, TabItem }` comes back as `import { Tabs }` while
|
|
17
|
+
* `<TabItem>` is still in the document. Astro then fails the site's
|
|
18
|
+
* build with `TabItem is not defined`.
|
|
19
|
+
* 2. `export const …` is dropped outright, taking any `{value}` expression
|
|
20
|
+
* that referenced it down with it.
|
|
21
|
+
* 3. Side-effect imports (`import './styles.css'`) and specifiers that are
|
|
22
|
+
* imported but never referenced are dropped.
|
|
23
|
+
*
|
|
24
|
+
* Even when the rebuild keeps every name it is not byte-faithful: the library
|
|
25
|
+
* emits `import { X } from 'y'` with no trailing semicolon and orders
|
|
26
|
+
* specifiers by first reference, so a no-op save still rewrites the line.
|
|
27
|
+
*
|
|
28
|
+
* The fix keeps ESM out of the editor entirely. `splitEsm` lifts the statements
|
|
29
|
+
* off the markdown before the editor parses it and `joinEsm` puts them back,
|
|
30
|
+
* byte for byte, on the way out. Because the editor never sees an `mdxjsEsm`
|
|
31
|
+
* node, the library's `gatherMetadata` finds no import declarations and its
|
|
32
|
+
* rebuild produces nothing to collide with — except for components registered
|
|
33
|
+
* with an explicit `import.from` (see `MdxJsxComponentDescriptor`), which
|
|
34
|
+
* `joinEsm` reconciles so a binding is never declared twice.
|
|
35
|
+
*
|
|
36
|
+
* This narrows what reaches `compileMDX`, so it cannot weaken the
|
|
37
|
+
* `remarkRestrictExpressions` gate: the editor's live markdown now carries
|
|
38
|
+
* strictly fewer ESM statements than before, never more.
|
|
39
|
+
*/
|
|
40
|
+
/**
|
|
41
|
+
* Cheap pre-filter. A real parse is the only way to tell an ESM statement from
|
|
42
|
+
* an `import` line inside a fenced code block, but the overwhelming majority of
|
|
43
|
+
* documents have no line starting with `import`/`export` at all, and this keeps
|
|
44
|
+
* those off the parser entirely. False positives are harmless — they fall
|
|
45
|
+
* through to the parse, which decides.
|
|
46
|
+
*/
|
|
47
|
+
const ESM_HINT = /^(?:import|export)\b/m;
|
|
48
|
+
const parser = unified().use(remarkParse).use(remarkMdx);
|
|
49
|
+
/** Type guard so `data.estree` narrows without a cast. */
|
|
50
|
+
function isEsmNode(node) {
|
|
51
|
+
return node.type === 'mdxjsEsm';
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Parse far enough to locate `mdxjsEsm` nodes. Returns `undefined` when the
|
|
55
|
+
* document does not parse — acorn rejects `import type { … }`, for instance,
|
|
56
|
+
* which also makes the editor itself fall back to source view. Bailing out
|
|
57
|
+
* leaves behaviour exactly as it was.
|
|
58
|
+
*/
|
|
59
|
+
function parseOrUndefined(source) {
|
|
60
|
+
try {
|
|
61
|
+
return parser.parse(source);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Resolve the bindings an `mdxjsEsm` node introduces, from the estree that
|
|
69
|
+
* `micromark-extension-mdxjs` attaches (it parses with `addResult: true`).
|
|
70
|
+
* A statement whose bindings cannot be resolved contributes none and is simply
|
|
71
|
+
* preserved as opaque text.
|
|
72
|
+
*/
|
|
73
|
+
function describeEsm(node, text) {
|
|
74
|
+
const bindings = [];
|
|
75
|
+
let source;
|
|
76
|
+
let bindsDefault = false;
|
|
77
|
+
for (const statement of node.data?.estree?.body ?? []) {
|
|
78
|
+
if (statement.type === 'ImportDeclaration') {
|
|
79
|
+
if (typeof statement.source.value === 'string')
|
|
80
|
+
source = statement.source.value;
|
|
81
|
+
for (const specifier of statement.specifiers) {
|
|
82
|
+
bindings.push(specifier.local.name);
|
|
83
|
+
if (specifier.type !== 'ImportSpecifier')
|
|
84
|
+
bindsDefault = true;
|
|
85
|
+
}
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (statement.type === 'ExportNamedDeclaration') {
|
|
89
|
+
const declaration = statement.declaration;
|
|
90
|
+
if (declaration?.type === 'VariableDeclaration') {
|
|
91
|
+
for (const declarator of declaration.declarations) {
|
|
92
|
+
if (declarator.id.type === 'Identifier')
|
|
93
|
+
bindings.push(declarator.id.name);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
else if (declaration?.id != null) {
|
|
97
|
+
bindings.push(declaration.id.name);
|
|
98
|
+
}
|
|
99
|
+
for (const specifier of statement.specifiers) {
|
|
100
|
+
if (specifier.exported.type === 'Identifier')
|
|
101
|
+
bindings.push(specifier.exported.name);
|
|
102
|
+
}
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (statement.type === 'ExportDefaultDeclaration')
|
|
106
|
+
bindings.push('default');
|
|
107
|
+
}
|
|
108
|
+
return { text, bindings, source, bindsDefault };
|
|
109
|
+
}
|
|
110
|
+
/** Number of leading root children that are ESM statements. */
|
|
111
|
+
function countLeadingEsm(children) {
|
|
112
|
+
let count = 0;
|
|
113
|
+
for (const node of children) {
|
|
114
|
+
if (!isEsmNode(node))
|
|
115
|
+
break;
|
|
116
|
+
count += 1;
|
|
117
|
+
}
|
|
118
|
+
return count;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Offset where the body proper starts, i.e. the first root child that is not an
|
|
122
|
+
* ESM statement. Equal to `fallback` for an all-ESM document.
|
|
123
|
+
*/
|
|
124
|
+
function firstContentOffset(children, leadingCount, fallback) {
|
|
125
|
+
const firstContent = leadingCount < children.length ? children[leadingCount] : undefined;
|
|
126
|
+
return firstContent?.position?.start.offset ?? fallback;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Lift every ESM statement off `source`.
|
|
130
|
+
*
|
|
131
|
+
* The document's leading ESM block is preserved as a literal slice, so
|
|
132
|
+
* re-attaching it reproduces the original bytes. Statements that appear *below*
|
|
133
|
+
* the first content block are hoisted into that same block: their original
|
|
134
|
+
* position is not recoverable once the user has restructured the body, and MDX
|
|
135
|
+
* compiles ESM to module scope regardless of where it sat, so hoisting keeps
|
|
136
|
+
* the site's build working at the cost of moving those lines. Relative
|
|
137
|
+
* order among the statements is preserved.
|
|
138
|
+
*/
|
|
139
|
+
export function splitEsm(source) {
|
|
140
|
+
const untouched = { prefix: '', body: source, bindings: new Set() };
|
|
141
|
+
if (!ESM_HINT.test(source))
|
|
142
|
+
return untouched;
|
|
143
|
+
const tree = parseOrUndefined(source);
|
|
144
|
+
if (tree === undefined)
|
|
145
|
+
return untouched;
|
|
146
|
+
const children = tree.children;
|
|
147
|
+
const leadingCount = countLeadingEsm(children);
|
|
148
|
+
// Everything up to the first non-ESM block: the statements plus the blank
|
|
149
|
+
// lines around them, verbatim. A document that is *only* ESM keeps all of it.
|
|
150
|
+
const leadingText = leadingCount === 0 ? '' : source.slice(0, firstContentOffset(children, leadingCount, source.length));
|
|
151
|
+
const leading = [];
|
|
152
|
+
const hoisted = [];
|
|
153
|
+
const cuts = [];
|
|
154
|
+
for (const [index, node] of children.entries()) {
|
|
155
|
+
if (!isEsmNode(node))
|
|
156
|
+
continue;
|
|
157
|
+
const start = node.position?.start.offset;
|
|
158
|
+
const end = node.position?.end.offset;
|
|
159
|
+
// Positions are always present from a string parse; without them the text
|
|
160
|
+
// cannot be recovered, so leave the document alone rather than guess.
|
|
161
|
+
if (start === undefined || end === undefined)
|
|
162
|
+
return untouched;
|
|
163
|
+
const statement = describeEsm(node, source.slice(start, end));
|
|
164
|
+
if (index < leadingCount) {
|
|
165
|
+
leading.push(statement);
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
hoisted.push(statement);
|
|
169
|
+
cuts.push({ start, end });
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (leading.length === 0 && hoisted.length === 0)
|
|
173
|
+
return untouched;
|
|
174
|
+
// Cut hoisted statements out of the body back-to-front so earlier offsets stay
|
|
175
|
+
// valid. Blank lines left behind are irrelevant: the editor re-serializes the
|
|
176
|
+
// body regardless.
|
|
177
|
+
let body = source.slice(leadingText.length);
|
|
178
|
+
for (const cut of [...cuts].reverse()) {
|
|
179
|
+
body = body.slice(0, cut.start - leadingText.length) + body.slice(cut.end - leadingText.length);
|
|
180
|
+
}
|
|
181
|
+
return {
|
|
182
|
+
prefix: hoisted.length === 0 ? leadingText : `${leadingText}${hoisted.map((s) => s.text).join('\n')}\n\n`,
|
|
183
|
+
body,
|
|
184
|
+
bindings: new Set([...leading, ...hoisted].flatMap((s) => s.bindings)),
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Re-attach preserved ESM to markdown coming out of the editor.
|
|
189
|
+
*
|
|
190
|
+
* Any import the library rebuilt for a descriptor carrying `import.from` is
|
|
191
|
+
* reconciled against the preserved bindings first, so nothing is declared
|
|
192
|
+
* twice: a rebuilt statement whose every binding is already preserved is
|
|
193
|
+
* dropped, and one that also carries a new binding is narrowed to just the new
|
|
194
|
+
* names. The author's own statements are never rewritten.
|
|
195
|
+
*/
|
|
196
|
+
export function joinEsm(preserved, body) {
|
|
197
|
+
if (preserved.prefix === '')
|
|
198
|
+
return body;
|
|
199
|
+
if (!ESM_HINT.test(body))
|
|
200
|
+
return preserved.prefix + body;
|
|
201
|
+
const tree = parseOrUndefined(body);
|
|
202
|
+
if (tree === undefined)
|
|
203
|
+
return preserved.prefix + body;
|
|
204
|
+
const children = tree.children;
|
|
205
|
+
const leadingCount = countLeadingEsm(children);
|
|
206
|
+
if (leadingCount === 0)
|
|
207
|
+
return preserved.prefix + body;
|
|
208
|
+
const kept = [];
|
|
209
|
+
for (const node of children.slice(0, leadingCount)) {
|
|
210
|
+
if (!isEsmNode(node))
|
|
211
|
+
continue;
|
|
212
|
+
const start = node.position?.start.offset;
|
|
213
|
+
const end = node.position?.end.offset;
|
|
214
|
+
if (start === undefined || end === undefined)
|
|
215
|
+
continue;
|
|
216
|
+
const statement = describeEsm(node, body.slice(start, end));
|
|
217
|
+
const fresh = statement.bindings.filter((name) => !preserved.bindings.has(name));
|
|
218
|
+
if (statement.bindings.length === 0 || fresh.length === statement.bindings.length) {
|
|
219
|
+
kept.push(statement.text);
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (fresh.length === 0)
|
|
223
|
+
continue;
|
|
224
|
+
// Partially covered. Only a rebuilt named import can land here, and it was
|
|
225
|
+
// generated by the library rather than authored, so narrowing it to the new
|
|
226
|
+
// names loses nothing the author wrote.
|
|
227
|
+
if (statement.source === undefined || statement.bindsDefault) {
|
|
228
|
+
kept.push(statement.text);
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
kept.push(`import { ${fresh.join(', ')} } from '${statement.source}'`);
|
|
232
|
+
}
|
|
233
|
+
// An all-ESM body leaves nothing after the statements.
|
|
234
|
+
const rest = body.slice(firstContentOffset(children, leadingCount, body.length));
|
|
235
|
+
return preserved.prefix + (kept.length === 0 ? rest : `${kept.join('\n')}\n\n${rest}`);
|
|
236
|
+
}
|
|
237
|
+
//# sourceMappingURL=esm-passthrough.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"esm-passthrough.js","sourceRoot":"","sources":["../src/esm-passthrough.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,WAAW,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH;;;;;;GAMG;AACH,MAAM,QAAQ,GAAG,uBAAuB,CAAC;AAEzC,MAAM,MAAM,GAAG,OAAO,EAAE,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;AAIzD,0DAA0D;AAC1D,SAAS,SAAS,CAAC,IAAiB;IAClC,OAAO,IAAI,CAAC,IAAI,KAAK,UAAU,CAAC;AAClC,CAAC;AA4BD;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,MAAc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,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC;YAClF,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;YAC1B,SAAS;QACX,CAAC;QACD,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;YAAE,SAAS;QACjC,2EAA2E;QAC3E,4EAA4E;QAC5E,wCAAwC;QACxC,IAAI,SAAS,CAAC,MAAM,KAAK,SAAS,IAAI,SAAS,CAAC,YAAY,EAAE,CAAC;YAC7D,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;YAC1B,SAAS;QACX,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,YAAY,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,YAAY,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC;IACzE,CAAC;IAED,uDAAuD;IACvD,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,kBAAkB,CAAC,QAAQ,EAAE,YAAY,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IACjF,OAAO,SAAS,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;AACzF,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { MDXFragmentEvaluationResult } from './types.js';
|
|
2
|
+
interface FragmentLocaleSummary {
|
|
3
|
+
meta?: {
|
|
4
|
+
title?: string;
|
|
5
|
+
};
|
|
6
|
+
name?: string;
|
|
7
|
+
}
|
|
8
|
+
interface FragmentManifest {
|
|
9
|
+
id: string;
|
|
10
|
+
locales: Record<string, FragmentLocaleSummary | undefined>;
|
|
11
|
+
}
|
|
12
|
+
interface FragmentLocalizedEntry {
|
|
13
|
+
localized?: {
|
|
14
|
+
meta?: {
|
|
15
|
+
title?: string;
|
|
16
|
+
};
|
|
17
|
+
name?: string;
|
|
18
|
+
content?: {
|
|
19
|
+
mdx?: string;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
interface FragmentAPI {
|
|
24
|
+
listAllContent(filters: {
|
|
25
|
+
kind: 'fragment';
|
|
26
|
+
}): Iterable<FragmentManifest>;
|
|
27
|
+
getLocalized(id: string, locale: string): Promise<FragmentLocalizedEntry | null>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Evaluate all MDX fragments from a compatible content API.
|
|
31
|
+
*
|
|
32
|
+
* Returns structured success/error results so callers can decide how to
|
|
33
|
+
* surface broken fragments instead of hardcoding fallback UI in this package.
|
|
34
|
+
*/
|
|
35
|
+
export declare function evaluateMDXFragments(api: FragmentAPI, locale: string): Promise<MDXFragmentEvaluationResult[]>;
|
|
36
|
+
export {};
|
|
37
|
+
//# sourceMappingURL=fragments.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fragments.d.ts","sourceRoot":"","sources":["../src/fragments.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,YAAY,CAAC;AAE9D,UAAU,qBAAqB;IAC7B,IAAI,CAAC,EAAE;QACL,KAAK,CAAC,EAAE,MAAM,CAAC;KAChB,CAAC;IACF,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,UAAU,gBAAgB;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,qBAAqB,GAAG,SAAS,CAAC,CAAC;CAC5D;AAED,UAAU,sBAAsB;IAC9B,SAAS,CAAC,EAAE;QACV,IAAI,CAAC,EAAE;YACL,KAAK,CAAC,EAAE,MAAM,CAAC;SAChB,CAAC;QACF,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,OAAO,CAAC,EAAE;YACR,GAAG,CAAC,EAAE,MAAM,CAAC;SACd,CAAC;KACH,CAAC;CACH;AAED,UAAU,WAAW;IACnB,cAAc,CAAC,OAAO,EAAE;QAAE,IAAI,EAAE,UAAU,CAAA;KAAE,GAAG,QAAQ,CAAC,gBAAgB,CAAC,CAAC;IAC1E,YAAY,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,sBAAsB,GAAG,IAAI,CAAC,CAAC;CAClF;AAED;;;;;GAKG;AACH,wBAAsB,oBAAoB,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,2BAA2B,EAAE,CAAC,CAoDnH"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { evaluateMDXToComponent } from './mdx/evaluate.js';
|
|
2
|
+
/**
|
|
3
|
+
* Evaluate all MDX fragments from a compatible content API.
|
|
4
|
+
*
|
|
5
|
+
* Returns structured success/error results so callers can decide how to
|
|
6
|
+
* surface broken fragments instead of hardcoding fallback UI in this package.
|
|
7
|
+
*/
|
|
8
|
+
export async function evaluateMDXFragments(api, locale) {
|
|
9
|
+
const fragments = Array.from(api.listAllContent({ kind: 'fragment' }));
|
|
10
|
+
return Promise.all(fragments.map(async (fragment) => {
|
|
11
|
+
const locales = Object.keys(fragment.locales);
|
|
12
|
+
const firstLocale = locales.length > 0 ? fragment.locales[locales[0]] : null;
|
|
13
|
+
const manifestTitle = firstLocale?.meta?.title || firstLocale?.name || fragment.id;
|
|
14
|
+
try {
|
|
15
|
+
const fragmentLocalized = await api.getLocalized(fragment.id, locale);
|
|
16
|
+
const fragmentTitle = fragmentLocalized?.localized?.meta?.title || fragmentLocalized?.localized?.name || manifestTitle;
|
|
17
|
+
const mdxContent = fragmentLocalized?.localized?.content?.mdx;
|
|
18
|
+
if (!mdxContent) {
|
|
19
|
+
return {
|
|
20
|
+
ok: false,
|
|
21
|
+
id: fragment.id,
|
|
22
|
+
title: fragmentTitle,
|
|
23
|
+
error: new Error(`No MDX content found for fragment: ${fragment.id}`),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const { Component: EvaluatedComponent, error, outline } = await evaluateMDXToComponent(mdxContent);
|
|
27
|
+
if (error || !EvaluatedComponent) {
|
|
28
|
+
return {
|
|
29
|
+
ok: false,
|
|
30
|
+
id: fragment.id,
|
|
31
|
+
title: fragmentTitle,
|
|
32
|
+
error: error || new Error(`MDX evaluation failed for fragment: ${fragment.id}`),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
ok: true,
|
|
37
|
+
id: fragment.id,
|
|
38
|
+
title: fragmentTitle,
|
|
39
|
+
Component: EvaluatedComponent,
|
|
40
|
+
outline,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
return {
|
|
45
|
+
ok: false,
|
|
46
|
+
id: fragment.id,
|
|
47
|
+
title: manifestTitle,
|
|
48
|
+
error: error instanceof Error ? error : new Error(`Failed to evaluate fragment: ${fragment.id}`),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=fragments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fragments.js","sourceRoot":"","sources":["../src/fragments.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAgC3D;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,GAAgB,EAAE,MAAc;IACzE,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC;IAEvE,OAAO,OAAO,CAAC,GAAG,CAChB,SAAS,CAAC,GAAG,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE;QAC/B,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QAC9C,MAA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EAAE,EAAE,QAAQ,CAAC,EAAE;gBACf,KAAK,EAAE,aAAa;gBACpB,SAAS,EAAE,kBAAkB;gBAC7B,OAAO;aACR,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO;gBACL,EAAE,EAAE,KAAK;gBACT,EAAE,EAAE,QAAQ,CAAC,EAAE;gBACf,KAAK,EAAE,aAAa;gBACpB,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,gCAAgC,QAAQ,CAAC,EAAE,EAAE,CAAC;aACjG,CAAC;QACJ,CAAC;IACH,CAAC,CAAC,CACH,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type { MDXEditorMethods } from '@mdxeditor/editor';
|
|
2
|
+
export { codeBlockWrapperInfo$ } from './code-block-frame.js';
|
|
3
|
+
export type { BaseMDXEditorProps } from './editor-core.js';
|
|
4
|
+
export { BaseMDXEditor } from './editor-core.js';
|
|
5
|
+
export * from './mdx-editor.js';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,YAAY,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,cAAc,iBAAiB,CAAC"}
|
package/dist/index.js
ADDED