@conloca/mdx 0.1.16 → 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.
Files changed (86) hide show
  1. package/README.md +50 -16
  2. package/dist/code-block-frame.js +1 -0
  3. package/dist/code-block-frame.js.map +1 -0
  4. package/dist/editor-core.d.ts +0 -2
  5. package/dist/editor-core.d.ts.map +1 -1
  6. package/dist/editor-core.js +50 -6
  7. package/dist/editor-core.js.map +1 -0
  8. package/dist/esm-passthrough.d.ts +36 -0
  9. package/dist/esm-passthrough.d.ts.map +1 -0
  10. package/dist/esm-passthrough.js +237 -0
  11. package/dist/esm-passthrough.js.map +1 -0
  12. package/dist/fragments.d.ts +37 -0
  13. package/dist/fragments.d.ts.map +1 -0
  14. package/dist/fragments.js +53 -0
  15. package/dist/fragments.js.map +1 -0
  16. package/dist/index.d.ts +0 -2
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +1 -1
  19. package/dist/index.js.map +1 -0
  20. package/dist/markdown-options.js +1 -0
  21. package/dist/markdown-options.js.map +1 -0
  22. package/dist/mdx/compile-request.d.ts +5 -0
  23. package/dist/mdx/compile-request.d.ts.map +1 -0
  24. package/dist/mdx/compile-request.js +40 -0
  25. package/dist/mdx/compile-request.js.map +1 -0
  26. package/dist/mdx/compile.d.ts.map +1 -1
  27. package/dist/mdx/compile.js +21 -1
  28. package/dist/mdx/compile.js.map +1 -0
  29. package/dist/mdx/evaluate.d.ts +18 -23
  30. package/dist/mdx/evaluate.d.ts.map +1 -1
  31. package/dist/mdx/evaluate.js +103 -29
  32. package/dist/mdx/evaluate.js.map +1 -0
  33. package/dist/mdx/heading-outline.d.ts +46 -0
  34. package/dist/mdx/heading-outline.d.ts.map +1 -0
  35. package/dist/mdx/heading-outline.js +114 -0
  36. package/dist/mdx/heading-outline.js.map +1 -0
  37. package/dist/mdx/restrict-expressions.d.ts +43 -0
  38. package/dist/mdx/restrict-expressions.d.ts.map +1 -0
  39. package/dist/mdx/restrict-expressions.js +160 -0
  40. package/dist/mdx/restrict-expressions.js.map +1 -0
  41. package/dist/mdx-editor.js +1 -0
  42. package/dist/mdx-editor.js.map +1 -0
  43. package/dist/node.d.ts +3 -2
  44. package/dist/node.d.ts.map +1 -1
  45. package/dist/node.js +3 -1
  46. package/dist/node.js.map +1 -0
  47. package/dist/sandbox-frame.d.ts +114 -0
  48. package/dist/sandbox-frame.d.ts.map +1 -0
  49. package/dist/sandbox-frame.js +234 -0
  50. package/dist/sandbox-frame.js.map +1 -0
  51. package/dist/types.d.ts +7 -4
  52. package/dist/types.d.ts.map +1 -1
  53. package/dist/types.js +1 -0
  54. package/dist/types.js.map +1 -0
  55. package/package.json +58 -41
  56. package/src/code-block-frame.tsx +262 -0
  57. package/src/editor-core.tsx +403 -0
  58. package/src/editor-styles.css +282 -0
  59. package/src/esm-passthrough.ts +269 -0
  60. package/src/fragments.ts +91 -0
  61. package/src/index.ts +5 -0
  62. package/src/markdown-options.ts +21 -0
  63. package/src/mdx/compile-request.ts +53 -0
  64. package/src/mdx/compile.ts +51 -0
  65. package/src/mdx/evaluate.ts +144 -0
  66. package/src/mdx/heading-outline.ts +153 -0
  67. package/src/mdx/restrict-expressions.ts +173 -0
  68. package/src/mdx-editor.tsx +15 -0
  69. package/src/node.ts +25 -0
  70. package/src/sandbox-frame.ts +277 -0
  71. package/src/types.ts +23 -0
  72. package/dist/blocks.d.ts +0 -37
  73. package/dist/blocks.d.ts.map +0 -1
  74. package/dist/blocks.js +0 -51
  75. package/dist/hooks/useMDXEvaluation.d.ts +0 -28
  76. package/dist/hooks/useMDXEvaluation.d.ts.map +0 -1
  77. package/dist/hooks/useMDXEvaluation.js +0 -117
  78. package/dist/index.d.mts +0 -127
  79. package/dist/index.d.mts.map +0 -1
  80. package/dist/index.mjs +0 -516
  81. package/dist/index.mjs.map +0 -1
  82. package/dist/node.d.mts +0 -102
  83. package/dist/node.d.mts.map +0 -1
  84. package/dist/node.mjs +0 -140
  85. package/dist/node.mjs.map +0 -1
  86. package/dist/style.css +0 -88
package/README.md CHANGED
@@ -9,20 +9,31 @@ Universal MDX package for both browser and Node.js environments.
9
9
  - **MDXEditor** - Embeddable MDX editor component (the page-route editor in the CMS SPA uses this directly)
10
10
  - **BaseMDXEditor** - Lower-level editor primitive that accepts the image-dialog hook used by the CMS SPA's
11
11
  media-library integration
12
- - **useMDXEvaluation** - Hook for running pre-compiled MDX code in the browser
12
+ - **codeBlockWrapperInfo$** - Editor realm cell a host publishes so the editor's fenced code blocks inherit the host
13
+ site's own code-block classes
13
14
 
14
15
  If you are building inside `@conloca/cms-spa`, prefer `CMSMDXEditor` there for the integrated media-library image
15
16
  picker.
16
17
 
17
18
  > **Note:** The legacy `MDXEditorModal` / `BaseMDXEditorModal` exports were removed when the CMS SPA standardized on the
18
- > page-route editor (`/blocks/:id`, `/pages/:id`). Mount `MDXEditor` directly inside your own layout instead of nesting
19
- > it in a fullscreen modal.
19
+ > page-route editor (`/fragments/:id`, `/pages/:id`). Mount `MDXEditor` directly inside your own layout instead of
20
+ > nesting it in a fullscreen modal.
20
21
 
21
22
  ### Node.js-only exports (`@conloca/mdx/node`)
22
23
 
23
24
  - **compileMDX** - Compile MDX strings to JavaScript
24
25
  - **evaluateMDXToComponent** - Evaluate MDX strings to React components (server-side)
25
- - **evaluateMDXBlocks** - Fetch and evaluate MDX blocks from a compatible content API
26
+ - **evaluateMDXFragments** - Fetch and evaluate MDX fragments from a compatible content API
27
+ - **handleMDXCompileRequest** - Answer the editor's `POST /mdx/compile` request with the whole compile result
28
+
29
+ ### Sandboxed preview exports (`@conloca/mdx/sandbox-frame`)
30
+
31
+ Compiled MDX is evaluated in an opaque-origin iframe, never in the page that hosts the editor.
32
+
33
+ - **buildSandboxSrcdoc** - Build the preview frame's HTML document, with its runtime inlined
34
+ - **SANDBOX_FRAME_CSP** - The content-security policy that confines the MDX evaluation to that frame
35
+ - **SANDBOX_FRAME_PATH** - The path a host serves the frame document from
36
+ - **SandboxRenderMessage** / **SandboxOutboundMessage** - The messages the host and the frame exchange
26
37
 
27
38
  ## Usage
28
39
 
@@ -31,25 +42,48 @@ picker.
31
42
  ```typescript
32
43
  // In admin/CMS interface
33
44
  import { MDXEditor } from '@conloca/mdx';
45
+ import '@conloca/mdx/style.css';
46
+ ```
47
+
48
+ The editor entry ships no stylesheet in its JavaScript; import `@conloca/mdx/style.css` once, alongside it. It pulls
49
+ MDXEditor's own stylesheet in first, so that is the only stylesheet import a host writes.
34
50
 
35
- // For rendering MDX content
36
- import { useMDXEvaluation } from '@conloca/mdx';
51
+ ### Browser (Preview)
52
+
53
+ Serve the frame document at `SANDBOX_FRAME_PATH`, point an iframe at it, and post the compiled MDX to the frame:
54
+
55
+ ```typescript
56
+ import { buildSandboxSrcdoc, SANDBOX_FRAME_CSP, SANDBOX_FRAME_PATH } from '@conloca/mdx/sandbox-frame';
57
+
58
+ // In the host's request handler for SANDBOX_FRAME_PATH
59
+ new Response(buildSandboxSrcdoc(), {
60
+ headers: {
61
+ 'content-type': 'text/html; charset=utf-8',
62
+ 'content-security-policy': `${SANDBOX_FRAME_CSP}; frame-ancestors 'self'`,
63
+ },
64
+ });
65
+
66
+ // In the host page, once the frame has posted `{ kind: 'ready' }`. The frame's
67
+ // origin is opaque, so '*' is the only target origin that reaches it.
68
+ frame.contentWindow?.postMessage({ __conlocaSandbox: true, kind: 'render', mode: 'mdx', payload: compiledMdx }, '*');
37
69
  ```
38
70
 
71
+ `@conloca/astro-cms` already serves this route; a host that mounts the editor itself has to serve it.
72
+
39
73
  ### Node.js (Server-side)
40
74
 
41
75
  ```typescript
42
76
  // In Astro, Next.js, etc.
43
- import { evaluateMDXBlocks, evaluateMDXToComponent } from '@conloca/mdx/node';
77
+ import { evaluateMDXFragments, evaluateMDXToComponent } from '@conloca/mdx/node';
44
78
  import { createContentAPI } from '@conloca/content-api/node';
45
79
 
46
- // Evaluate all MDX blocks for a locale
80
+ // Evaluate all MDX fragments for a locale
47
81
  const api = await createContentAPI({ contentRoot: './content' });
48
- const mdxComponents = await evaluateMDXBlocks(api, 'en');
82
+ const mdxComponents = await evaluateMDXFragments(api, 'en');
49
83
 
50
- for (const block of mdxComponents) {
51
- if (!block.ok) {
52
- console.error(`Failed to evaluate ${block.id}:`, block.error.message);
84
+ for (const fragment of mdxComponents) {
85
+ if (!fragment.ok) {
86
+ console.error(`Failed to evaluate ${fragment.id}:`, fragment.error.message);
53
87
  }
54
88
  }
55
89
 
@@ -68,7 +102,7 @@ This package was renamed from `@conloca/mdx-client` to `@conloca/mdx` and consol
68
102
  import { MDXEditorModal } from '@conloca/mdx-client';
69
103
 
70
104
  // After — mount the editor inline (the fullscreen-modal variant was removed
71
- // when the CMS standardized on page-route editing at `/blocks/:id` / `/pages/:id`).
105
+ // when the CMS standardized on page-route editing at `/fragments/:id` / `/pages/:id`).
72
106
  import { MDXEditor } from '@conloca/mdx';
73
107
  ```
74
108
 
@@ -80,13 +114,13 @@ pass `value` / `onChange` directly. See the **Browser: MDX Editor** usage exampl
80
114
  ```typescript
81
115
  // Before (scattered across packages)
82
116
  import { evaluateMDXToComponent } from '@conloca/content-api/node';
83
- import { evaluateMDXBlocks } from '@conloca/astro-cms/components';
117
+ import { evaluateMDXFragments } from '@conloca/astro-cms/components';
84
118
 
85
119
  // After
86
- import { evaluateMDXBlocks, evaluateMDXToComponent } from '@conloca/mdx/node';
120
+ import { evaluateMDXFragments, evaluateMDXToComponent } from '@conloca/mdx/node';
87
121
 
88
122
  // Also re-exported from Astro helpers when you are already working there
89
- import { evaluateMDXBlocks } from '@conloca/astro-cms/components';
123
+ import { evaluateMDXFragments } from '@conloca/astro-cms/components';
90
124
  ```
91
125
 
92
126
  ## Building
@@ -184,3 +184,4 @@ export const conlocaCodeBlockDescriptor = {
184
184
  match: () => true,
185
185
  Editor: ConlocaCodeBlockEditor,
186
186
  };
187
+ //# sourceMappingURL=code-block-frame.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"code-block-frame.js","sourceRoot":"","s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5B,IACL,CACJ,CAAC;IAEF,oEAAoE;IACpE,mEAAmE;IACnE,mEAAmE;IACnE,wCAAwC;IACxC,MAAM,KAAK,GAAG,SAAS,CAAC,CAAC,CAAC,CACxB,aAAa,CAAC,SAAS,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,sBAAsB,SAAS,CAAC,SAAS,EAAE,EAAE,EAAE,UAAU,CAAC,CACzG,CAAC,CAAC,CAAC,CACF,cAAK,SAAS,EAAC,oBAAoB,YAAE,UAAU,GAAO,CACvD,CAAC;IAEF,+DAA+D;IAC/D,wDAAwD;IACxD,OAAO,UAAU,CAAC,WAAW,CAC3B,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,CAAC,SAAS,EAAE,EAAE,IAAI,CAAC,EAChF,KAAK,CACN,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAA8B;IACnE,QAAQ,EAAE,GAAG;IACb,KAAK,EAAE,GAAG,EAAE,CAAC,IAAI;IACjB,MAAM,EAAE,sBAAsB;CAC/B,CAAC"}
@@ -1,7 +1,5 @@
1
1
  import { type JsxComponentDescriptor, type MDXEditorMethods, type RealmPlugin } from '@mdxeditor/editor';
2
2
  import React from 'react';
3
- import '@mdxeditor/editor/style.css';
4
- import './editor-styles.css';
5
3
  export interface BaseMDXEditorProps {
6
4
  value: string;
7
5
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"editor-core.d.ts","sourceRoot":"","sources":["../src/editor-core.tsx"],"names":[],"mappings":"AAAA,OAAO,EAmBL,KAAK,sBAAsB,EAO3B,KAAK,gBAAgB,EAGrB,KAAK,WAAW,EAOjB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAuD,MAAM,OAAO,CAAC;AAC5E,OAAO,6BAA6B,CAAC;AACrC,OAAO,qBAAqB,CAAC;AAuD7B,MAAM,WAAW,kBAAkB;IAC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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
3
  import { Maximize2, Minimize2 } from 'lucide-react';
4
- import React, { useCallback, useEffect, useSyncExternalStore } from 'react';
5
- import '@mdxeditor/editor/style.css';
6
- import './editor-styles.css';
7
- import { conlocaCodeBlockDescriptor } from './code-block-frame';
8
- import { TO_MARKDOWN_OPTIONS } from './markdown-options';
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';
9
8
  const FOCUS_MODE_STORAGE_KEY = 'conloca.mdxeditor.focusMode';
10
9
  const FOCUS_MODE_BODY_CLASS = 'mdxeditor-focus-mode';
11
10
  /**
@@ -107,6 +106,50 @@ if (typeof window !== 'undefined') {
107
106
  }
108
107
  }
109
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
+ }), []);
110
153
  useEffect(() => {
111
154
  const handleKeyDown = (event) => {
112
155
  if ((event.ctrlKey || event.metaKey) && event.key === 's') {
@@ -124,7 +167,7 @@ export const BaseMDXEditor = React.forwardRef(({ value, onChange, onSave, readOn
124
167
  document.addEventListener('keydown', handleKeyDown);
125
168
  return () => document.removeEventListener('keydown', handleKeyDown);
126
169
  }, [onImageShortcut, onSave, value]);
127
- return (_jsx(MDXEditorLib, { ref: ref, markdown: value, toMarkdownOptions: TO_MARKDOWN_OPTIONS, onChange: onChange, onError: ({ error, source }) => {
170
+ return (_jsx(MDXEditorLib, { ref: innerRef, markdown: seededRef.current.body, toMarkdownOptions: TO_MARKDOWN_OPTIONS, onChange: handleChange, onError: ({ error, source }) => {
128
171
  // Library invokes this synchronously inside a gurx signal, so don't
129
172
  // setState here — that triggers a render-phase warning (#872).
130
173
  // Logging only is enough; the React error boundary still catches
@@ -179,3 +222,4 @@ export const BaseMDXEditor = React.forwardRef(({ value, onChange, onSave, readOn
179
222
  ] }));
180
223
  });
181
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,EACd,eAAe,EACf,WAAW,EACX,WAAW,EACX,mBAAmB,EACnB,WAAW,EAEX,SAAS,EACT,WAAW,EACX,gBAAgB,EAChB,UAAU,EACV,WAAW,EACX,SAAS,IAAI,YAAY,EAEzB,sBAAsB,EACtB,WAAW,EAEX,SAAS,EACT,0BAA0B,EAC1B,WAAW,EACX,mBAAmB,EACnB,aAAa,EACb,QAAQ,GACT,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,KAAK,EAAE,EAAE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+ 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,IA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jF,IAAI,SAAS,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,KAAK,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