@conloca/mdx 0.1.14 → 0.1.16

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 (44) hide show
  1. package/README.md +31 -9
  2. package/dist/blocks.d.ts +37 -0
  3. package/dist/blocks.d.ts.map +1 -0
  4. package/dist/blocks.js +51 -0
  5. package/dist/code-block-frame.d.ts +41 -0
  6. package/dist/code-block-frame.d.ts.map +1 -0
  7. package/dist/code-block-frame.js +186 -0
  8. package/dist/editor-core.d.ts +58 -0
  9. package/dist/editor-core.d.ts.map +1 -0
  10. package/dist/editor-core.js +181 -0
  11. package/dist/hooks/useMDXEvaluation.d.ts +28 -0
  12. package/dist/hooks/useMDXEvaluation.d.ts.map +1 -0
  13. package/dist/hooks/useMDXEvaluation.js +117 -0
  14. package/dist/index.d.mts +104 -50
  15. package/dist/index.d.mts.map +1 -1
  16. package/dist/index.d.ts +8 -0
  17. package/dist/index.d.ts.map +1 -0
  18. package/dist/index.js +4 -0
  19. package/dist/index.mjs +440 -369
  20. package/dist/index.mjs.map +1 -1
  21. package/dist/markdown-options.d.ts +3 -0
  22. package/dist/markdown-options.d.ts.map +1 -0
  23. package/dist/markdown-options.js +19 -0
  24. package/dist/mdx/compile.d.ts +3 -0
  25. package/dist/mdx/compile.d.ts.map +1 -0
  26. package/dist/mdx/compile.js +27 -0
  27. package/dist/mdx/evaluate.d.ts +44 -0
  28. package/dist/mdx/evaluate.d.ts.map +1 -0
  29. package/dist/mdx/evaluate.js +57 -0
  30. package/dist/mdx-editor.d.ts +7 -0
  31. package/dist/mdx-editor.d.ts.map +1 -0
  32. package/dist/mdx-editor.js +7 -0
  33. package/dist/node.d.mts +46 -74
  34. package/dist/node.d.mts.map +1 -1
  35. package/dist/node.d.ts +17 -0
  36. package/dist/node.d.ts.map +1 -0
  37. package/dist/node.js +16 -0
  38. package/dist/node.mjs +35 -116
  39. package/dist/node.mjs.map +1 -1
  40. package/dist/style.css +88 -0
  41. package/dist/types.d.ts +16 -0
  42. package/dist/types.d.ts.map +1 -0
  43. package/dist/types.js +1 -0
  44. package/package.json +54 -15
@@ -0,0 +1,28 @@
1
+ import type React from 'react';
2
+ export interface UseMDXEvaluationOptions {
3
+ compiledCode: string | null;
4
+ cacheKey?: string;
5
+ }
6
+ export interface UseMDXEvaluationResult {
7
+ Component: React.ComponentType | null;
8
+ error: Error | null;
9
+ isLoading: boolean;
10
+ retry: () => void;
11
+ }
12
+ /**
13
+ * Hook for evaluating pre-compiled MDX code into React components.
14
+ *
15
+ * Uses the browser-side half of a server-compile + browser-run pattern:
16
+ * 1. The server compiles MDX → JavaScript function body
17
+ * 2. The browser executes the pre-compiled code with run() (zero compiler dependencies)
18
+ *
19
+ * This keeps the full MDX compilation pipeline on the server and only ships
20
+ * a tiny runtime helper to the browser.
21
+ *
22
+ * @param options.compiledCode - The pre-compiled MDX function body. If null, returns null component
23
+ * @param options.cacheKey - Optional cache key. If not provided, uses hash of code
24
+ *
25
+ * @returns Object containing Component, error, isLoading, retry
26
+ */
27
+ export declare function useMDXEvaluation({ compiledCode, cacheKey }: UseMDXEvaluationOptions): UseMDXEvaluationResult;
28
+ //# sourceMappingURL=useMDXEvaluation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useMDXEvaluation.d.ts","sourceRoot":"","sources":["../../src/hooks/useMDXEvaluation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AA6C/B,MAAM,WAAW,uBAAuB;IACtC,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,sBAAsB;IACrC,SAAS,EAAE,KAAK,CAAC,aAAa,GAAG,IAAI,CAAC;IACtC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,SAAS,EAAE,OAAO,CAAC;IACnB,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,gBAAgB,CAAC,EAAE,YAAY,EAAE,QAAQ,EAAE,EAAE,uBAAuB,GAAG,sBAAsB,CAsE5G"}
@@ -0,0 +1,117 @@
1
+ import { useEffect, useState } from 'react';
2
+ import * as devRuntime from 'react/jsx-dev-runtime';
3
+ import * as runtime from 'react/jsx-runtime';
4
+ // Cache for compiled MDX components
5
+ const compiledCache = new Map();
6
+ /**
7
+ * Execute pre-compiled MDX code (compiled with outputFormat: 'function-body').
8
+ * This is the same as @mdx-js/mdx's run() — inlined here to avoid importing
9
+ * the full @mdx-js/mdx package (which pulls acorn + remark + unified into the browser).
10
+ *
11
+ * Note: Uses AsyncFunction constructor which requires CSP `unsafe-eval`.
12
+ * This is acceptable since the CMS admin UI runs in a trusted environment.
13
+ */
14
+ const AsyncFunction = Object.getPrototypeOf(async () => { }).constructor;
15
+ async function runMDX(code, options) {
16
+ return new AsyncFunction(String(code))(options);
17
+ }
18
+ /**
19
+ * Create the JSX runtime options for runMDX().
20
+ * Checked once at module load to avoid repeated environment checks.
21
+ */
22
+ function createRunOptions() {
23
+ const isDevelopment = process.env.NODE_ENV === 'development';
24
+ return isDevelopment ? { ...devRuntime, development: true } : { ...runtime };
25
+ }
26
+ const runOptions = createRunOptions();
27
+ /**
28
+ * Simple hash function for generating cache keys from MDX content.
29
+ */
30
+ function hashString(str) {
31
+ let hash = 0;
32
+ for (let i = 0; i < str.length; i++) {
33
+ const char = str.charCodeAt(i);
34
+ hash = (hash << 5) - hash + char;
35
+ hash = hash & hash;
36
+ }
37
+ return hash.toString(36);
38
+ }
39
+ /**
40
+ * Hook for evaluating pre-compiled MDX code into React components.
41
+ *
42
+ * Uses the browser-side half of a server-compile + browser-run pattern:
43
+ * 1. The server compiles MDX → JavaScript function body
44
+ * 2. The browser executes the pre-compiled code with run() (zero compiler dependencies)
45
+ *
46
+ * This keeps the full MDX compilation pipeline on the server and only ships
47
+ * a tiny runtime helper to the browser.
48
+ *
49
+ * @param options.compiledCode - The pre-compiled MDX function body. If null, returns null component
50
+ * @param options.cacheKey - Optional cache key. If not provided, uses hash of code
51
+ *
52
+ * @returns Object containing Component, error, isLoading, retry
53
+ */
54
+ export function useMDXEvaluation({ compiledCode, cacheKey }) {
55
+ const [Component, setComponent] = useState(null);
56
+ const [error, setError] = useState(null);
57
+ const [isLoading, setIsLoading] = useState(() => {
58
+ if (!compiledCode)
59
+ return false;
60
+ const key = cacheKey || `mdx-${hashString(compiledCode)}`;
61
+ return !compiledCache.has(key);
62
+ });
63
+ const [retryCount, setRetryCount] = useState(0);
64
+ const retry = () => {
65
+ if (compiledCode) {
66
+ const key = cacheKey || `mdx-${hashString(compiledCode)}`;
67
+ compiledCache.delete(key);
68
+ }
69
+ setRetryCount((prev) => prev + 1);
70
+ };
71
+ useEffect(() => {
72
+ void retryCount;
73
+ if (!compiledCode) {
74
+ setComponent(null);
75
+ setError(null);
76
+ setIsLoading(false);
77
+ return;
78
+ }
79
+ const key = cacheKey || `mdx-${hashString(compiledCode)}`;
80
+ const cached = compiledCache.get(key);
81
+ if (cached) {
82
+ setComponent(() => cached);
83
+ setError(null);
84
+ setIsLoading(false);
85
+ return;
86
+ }
87
+ let cancelled = false;
88
+ const compileAndRun = async () => {
89
+ try {
90
+ setError(null);
91
+ setIsLoading(true);
92
+ const { default: MDXComponent } = await runMDX(compiledCode, runOptions);
93
+ if (cancelled)
94
+ return;
95
+ compiledCache.set(key, MDXComponent);
96
+ setComponent(() => MDXComponent);
97
+ }
98
+ catch (err) {
99
+ if (cancelled)
100
+ return;
101
+ const evaluationError = err instanceof Error ? err : new Error('MDX evaluation failed');
102
+ setError(evaluationError);
103
+ setComponent(null);
104
+ }
105
+ finally {
106
+ if (!cancelled) {
107
+ setIsLoading(false);
108
+ }
109
+ }
110
+ };
111
+ compileAndRun();
112
+ return () => {
113
+ cancelled = true;
114
+ };
115
+ }, [compiledCode, cacheKey, retryCount]);
116
+ return { Component, error, isLoading, retry };
117
+ }
package/dist/index.d.mts CHANGED
@@ -1,10 +1,99 @@
1
+ import { JsxComponentDescriptor, MDXEditorMethods, MDXEditorMethods as MDXEditorMethods$1, RealmPlugin } from "@mdxeditor/editor";
1
2
  import React from "react";
2
- import { MDXEditorMethods, MDXEditorMethods as MDXEditorMethods$1 } from "@mdxeditor/editor";
3
3
  import "@mdxeditor/editor/style.css";
4
-
4
+ //#region src/code-block-frame.d.ts
5
+ /**
6
+ * Frame chrome around the editor's fenced code blocks — header bar with
7
+ * filename, language tag, and a copy button. The outer wrapper picks up
8
+ * the host's code-block classes (published via `codeBlockWrapperInfo$`
9
+ * by the host-wrapper plugin in cms-spa), so the host's actual code-
10
+ * block CSS (Starlight's expressive-code, Tailwind's pre styling, etc.)
11
+ * reaches the editor's frame through the cascade — no shipped mirror.
12
+ *
13
+ * Host-agnostic: no Shiki / Starlight / ExpressiveCode imports. Class
14
+ * names are discovered at runtime from the host's live HTML.
15
+ */
16
+ /**
17
+ * Realm-published cell carrying the host's code-block wrapper chain,
18
+ * outermost first. For Starlight's expressive-code:
19
+ *
20
+ * [
21
+ * { tagName: 'div', className: 'expressive-code' },
22
+ * { tagName: 'figure', className: 'frame has-title not-content' },
23
+ * ]
24
+ *
25
+ * The cms-spa `hostWrapperPlugin` writes to this whenever the fetched
26
+ * wrapper info changes; the code-block frame reads from it to render
27
+ * each chain link as a nested element with the host's expected tag
28
+ * name. Same publish-pattern as the content-wrapper plugin in cms-spa.
29
+ *
30
+ * Null when the host has no code-block chrome (or the fetch is in
31
+ * flight). The frame then falls back to its base `.conloca-code-block`
32
+ * wrapper with no host inheritance — "looks plain but not broken."
33
+ */
34
+ declare const codeBlockWrapperInfo$: import("@mdxeditor/editor").NodeRef<{
35
+ tagName: string;
36
+ className: string;
37
+ }[] | null>;
38
+ //#endregion
39
+ //#region src/editor-core.d.ts
40
+ interface BaseMDXEditorProps {
41
+ value: string;
42
+ /**
43
+ * Called whenever the editor content changes. The second argument is `true`
44
+ * the first time the library normalizes the parsed markdown back to its
45
+ * canonical form (bullet style, trailing newline, etc.) — consumers tracking
46
+ * dirty state should treat that call as a re-baseline rather than a user
47
+ * edit, otherwise the editor looks dirty the moment the page loads.
48
+ */
49
+ onChange: (value: string, initialMarkdownNormalize?: boolean) => void;
50
+ onSave?: (value: string) => void;
51
+ readOnly?: boolean;
52
+ imageDialog?: React.FC | null;
53
+ disableImageSettingsButton?: boolean;
54
+ onImageShortcut?: () => void;
55
+ imageButtonRef?: React.Ref<HTMLButtonElement>;
56
+ /**
57
+ * Upload handler for images pasted or dropped directly into the editor. When
58
+ * provided, the underlying imagePlugin will route those events through this
59
+ * function and insert the returned URL as the image src. Independent of the
60
+ * library/picker `imageDialog` flow — both can be active simultaneously.
61
+ */
62
+ imageUploadHandler?: (file: File) => Promise<string>;
63
+ jsxComponentDescriptors?: JsxComponentDescriptor[];
64
+ /**
65
+ * Additional @mdxeditor/editor RealmPlugins appended to the built-in
66
+ * plugin list. Used by the SPA shell to register the MDX components
67
+ * registry-driven slash menu without coupling @conloca/mdx to
68
+ * @conloca/cms-spa.
69
+ */
70
+ extraPlugins?: RealmPlugin[];
71
+ /**
72
+ * Extra toolbar buttons inserted between `<InsertAdmonition />` and the
73
+ * separator preceding focus mode. Pass `null` (default) to keep the
74
+ * stock toolbar.
75
+ */
76
+ extraToolbarItems?: React.ReactNode;
77
+ /**
78
+ * Class applied to the MDXEditor root element (library forwards it to the
79
+ * toolbar and portaled popups too). Pass `"dark-theme"` to activate the
80
+ * library's built-in Radix dark palette.
81
+ * See https://mdxeditor.dev/editor/docs/theming
82
+ */
83
+ className?: string;
84
+ /** Placeholder shown when the editor is empty. Forwarded to MDXEditorLib. */
85
+ placeholder?: React.ReactNode;
86
+ /** Auto-focus the editor on mount. Forwarded to MDXEditorLib. */
87
+ autoFocus?: boolean | {
88
+ defaultSelection?: 'rootStart' | 'rootEnd';
89
+ preventScroll?: boolean;
90
+ };
91
+ }
92
+ declare const BaseMDXEditor: React.ForwardRefExoticComponent<BaseMDXEditorProps & React.RefAttributes<MDXEditorMethods$1>>;
93
+ //#endregion
5
94
  //#region src/hooks/useMDXEvaluation.d.ts
6
95
  interface UseMDXEvaluationOptions {
7
- mdxContent: string | null;
96
+ compiledCode: string | null;
8
97
  cacheKey?: string;
9
98
  }
10
99
  interface UseMDXEvaluationResult {
@@ -14,60 +103,25 @@ interface UseMDXEvaluationResult {
14
103
  retry: () => void;
15
104
  }
16
105
  /**
17
- * Hook for evaluating MDX content into React components.
18
- *
19
- * Features:
20
- * - Evaluates MDX strings into renderable React components using @mdx-js/mdx
21
- * - Caches compiled components to avoid re-evaluation of unchanged content
22
- * - Handles both development (jsx-dev-runtime) and production (jsx-runtime) environments
23
- * - Provides retry mechanism to clear cache and re-evaluate failed compilations
24
- * - Returns loading and error states for UI feedback
106
+ * Hook for evaluating pre-compiled MDX code into React components.
25
107
  *
26
- * @param options - Configuration options
27
- * @param options.mdxContent - The MDX string to evaluate. If null, returns null component
28
- * @param options.cacheKey - Optional cache key. If not provided, uses first 100 chars of content
108
+ * Uses the browser-side half of a server-compile + browser-run pattern:
109
+ * 1. The server compiles MDX → JavaScript function body
110
+ * 2. The browser executes the pre-compiled code with run() (zero compiler dependencies)
29
111
  *
30
- * @returns Object containing:
31
- * - Component: The compiled React component (null if not yet compiled or error occurred)
32
- * - error: Error object if evaluation failed (null otherwise)
33
- * - isLoading: Boolean indicating if evaluation is in progress
34
- * - retry: Function to clear cache and retry evaluation
112
+ * This keeps the full MDX compilation pipeline on the server and only ships
113
+ * a tiny runtime helper to the browser.
35
114
  *
36
- * @example
37
- * ```tsx
38
- * const { Component, error, isLoading, retry } = useMDXEvaluation({
39
- * mdxContent: '# Hello\n\nThis is **MDX** content',
40
- * cacheKey: 'my-unique-key'
41
- * });
115
+ * @param options.compiledCode - The pre-compiled MDX function body. If null, returns null component
116
+ * @param options.cacheKey - Optional cache key. If not provided, uses hash of code
42
117
  *
43
- * if (isLoading) return <Spinner />;
44
- * if (error) return <Error message={error.message} onRetry={retry} />;
45
- * if (Component) return <Component />;
46
- * ```
118
+ * @returns Object containing Component, error, isLoading, retry
47
119
  */
48
- declare function useMDXEvaluation({
49
- mdxContent,
50
- cacheKey
51
- }: UseMDXEvaluationOptions): UseMDXEvaluationResult;
120
+ declare function useMDXEvaluation({ compiledCode, cacheKey }: UseMDXEvaluationOptions): UseMDXEvaluationResult;
52
121
  //#endregion
53
122
  //#region src/mdx-editor.d.ts
54
- interface MDXEditorProps {
55
- value: string;
56
- onChange: (value: string) => void;
57
- onSave?: (value: string) => void;
58
- readOnly?: boolean;
59
- }
123
+ interface MDXEditorProps extends Omit<BaseMDXEditorProps, 'disableImageSettingsButton' | 'imageButtonRef' | 'imageDialog' | 'onImageShortcut'> {}
60
124
  declare const MDXEditor: React.ForwardRefExoticComponent<MDXEditorProps & React.RefAttributes<MDXEditorMethods$1>>;
61
- interface MDXEditorModalProps {
62
- isOpen: boolean;
63
- onClose: () => void;
64
- filePath?: string;
65
- initialContent: string;
66
- onSave: (content: string) => void | Promise<void>;
67
- headerExtra?: React.ReactNode;
68
- onBeforeClose?: () => boolean;
69
- }
70
- declare const MDXEditorModal: React.FC<MDXEditorModalProps>;
71
125
  //#endregion
72
- export { MDXEditor, type MDXEditorMethods, MDXEditorModal, MDXEditorModalProps, MDXEditorProps, useMDXEvaluation };
126
+ export { BaseMDXEditor, type BaseMDXEditorProps, MDXEditor, type MDXEditorMethods, MDXEditorProps, type UseMDXEvaluationOptions, type UseMDXEvaluationResult, codeBlockWrapperInfo$, useMDXEvaluation };
73
127
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/hooks/useMDXEvaluation.ts","../src/mdx-editor.tsx"],"mappings":";;;;;UAgDU,uBAAA;EACR,UAAA;EACA,QAAA;AAAA;AAAA,UAGQ,sBAAA;EACR,SAAA,EAAW,KAAA,CAAM,aAAA;EACjB,KAAA,EAAO,KAAA;EACP,SAAA;EACA,KAAA;AAAA;;;;;;;;;;;;;AAmCF;;;;;;;;;;;;;;;;;;;;iBAAgB,gBAAA,CAAA;EAAmB,UAAA;EAAY;AAAA,GAAY,uBAAA,GAA0B,sBAAA;;;UClBpE,cAAA;EACf,KAAA;EACA,QAAA,GAAW,KAAA;EACX,MAAA,IAAU,KAAA;EACV,QAAA;AAAA;AAAA,cAGW,SAAA,EAAS,KAAA,CAAA,yBAAA,CAAA,cAAA,GAAA,KAAA,CAAA,aAAA,CAAA,kBAAA;AAAA,UAmML,mBAAA;EACf,MAAA;EACA,OAAA;EACA,QAAA;EACA,cAAA;EACA,MAAA,GAAS,OAAA,oBAA2B,OAAA;EACpC,WAAA,GAAc,KAAA,CAAM,SAAA;EACpB,aAAA;AAAA;AAAA,cAGW,cAAA,EAAgB,KAAA,CAAM,EAAA,CAAG,mBAAA"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/code-block-frame.tsx","../src/editor-core.tsx","../src/hooks/useMDXEvaluation.ts","../src/mdx-editor.tsx"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAyCa,mDAAqB;EAAmB;EAAmB;;;;UCsDvD;EACf;;;;;;;;EAQA,WAAW,eAAe;EAC1B,UAAU;EACV;EACA,cAAc,MAAM;EACpB;EACA;EACA,iBAAiB,MAAM,IAAI;;;;;;;EAO3B,sBAAsB,MAAM,SAAS;EACrC,0BAA0B;;;;;;;EAO1B,eAAe;;;;;;EAMf,oBAAoB,MAAM;;;;;;;EAO1B;;EAEA,cAAc,MAAM;;EAEpB;IAAwB;IAA4C;;;cA+DzD,eAAa,MAAA,0BAAA,qBAAA,MAAA,cAAA;;;UChKT;EACf;EACA;;UAGe;EACf,WAAW,MAAM;EACjB,OAAO;EACP;EACA;;;;;;;;;;;;;;;;;iBAkBc,mBAAmB,cAAc,YAAY,0BAA0B;;;UCpEtE,uBACP,KACN;cAIS,WAAS,MAAA,0BAAA,iBAAA,MAAA,cAAA"}
@@ -0,0 +1,8 @@
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 type { UseMDXEvaluationOptions, UseMDXEvaluationResult } from './hooks/useMDXEvaluation.js';
6
+ export { useMDXEvaluation } from './hooks/useMDXEvaluation.js';
7
+ export * from './mdx-editor.js';
8
+ //# 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,YAAY,EAAE,uBAAuB,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACnG,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,cAAc,iBAAiB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { codeBlockWrapperInfo$ } from './code-block-frame.js';
2
+ export { BaseMDXEditor } from './editor-core.js';
3
+ export { useMDXEvaluation } from './hooks/useMDXEvaluation.js';
4
+ export * from './mdx-editor.js';