@conloca/mdx 0.1.15 → 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 +15 -5
  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 +74 -43
  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 +328 -237
  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 +0 -5
  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 +26 -11
  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,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,18 +1,79 @@
1
- import { JsxComponentDescriptor, MDXEditorMethods, MDXEditorMethods as MDXEditorMethods$1 } from "@mdxeditor/editor";
1
+ import { JsxComponentDescriptor, MDXEditorMethods, MDXEditorMethods as MDXEditorMethods$1, RealmPlugin } from "@mdxeditor/editor";
2
2
  import React from "react";
3
- import * as react_jsx_runtime0 from "react/jsx-runtime";
4
-
3
+ import "@mdxeditor/editor/style.css";
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
5
39
  //#region src/editor-core.d.ts
6
40
  interface BaseMDXEditorProps {
7
41
  value: string;
8
- onChange: (value: string) => void;
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;
9
50
  onSave?: (value: string) => void;
10
51
  readOnly?: boolean;
11
52
  imageDialog?: React.FC | null;
12
53
  disableImageSettingsButton?: boolean;
13
54
  onImageShortcut?: () => void;
14
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>;
15
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;
16
77
  /**
17
78
  * Class applied to the MDXEditor root element (library forwards it to the
18
79
  * toolbar and portaled popups too). Pass `"dark-theme"` to activate the
@@ -20,40 +81,15 @@ interface BaseMDXEditorProps {
20
81
  * See https://mdxeditor.dev/editor/docs/theming
21
82
  */
22
83
  className?: string;
23
- }
24
- interface BaseMDXEditorModalProps {
25
- isOpen: boolean;
26
- onClose: () => void;
27
- filePath?: string;
28
- initialContent: string;
29
- onSave: (content: string) => void | Promise<void>;
30
- headerExtra?: React.ReactNode;
31
- headerTools?: (tools: {
32
- content: string;
33
- setContent: React.Dispatch<React.SetStateAction<string>>;
34
- editorRef: React.RefObject<MDXEditorMethods$1 | null>;
35
- }) => React.ReactNode;
36
- onBeforeClose?: () => boolean;
37
- EditorComponent: React.ForwardRefExoticComponent<BaseMDXEditorProps & React.RefAttributes<MDXEditorMethods$1>>;
38
- /**
39
- * Forwarded to the MDXEditor root. Typically `"dark-theme"` when the host
40
- * app is in dark mode — see BaseMDXEditorProps.className.
41
- */
42
- editorClassName?: 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
+ };
43
91
  }
44
92
  declare const BaseMDXEditor: React.ForwardRefExoticComponent<BaseMDXEditorProps & React.RefAttributes<MDXEditorMethods$1>>;
45
- declare function BaseMDXEditorModal({
46
- isOpen,
47
- onClose,
48
- filePath,
49
- initialContent,
50
- onSave,
51
- headerExtra,
52
- headerTools,
53
- onBeforeClose,
54
- EditorComponent,
55
- editorClassName
56
- }: BaseMDXEditorModalProps): react_jsx_runtime0.JSX.Element | null;
57
93
  //#endregion
58
94
  //#region src/hooks/useMDXEvaluation.d.ts
59
95
  interface UseMDXEvaluationOptions {
@@ -81,16 +117,11 @@ interface UseMDXEvaluationResult {
81
117
  *
82
118
  * @returns Object containing Component, error, isLoading, retry
83
119
  */
84
- declare function useMDXEvaluation({
85
- compiledCode,
86
- cacheKey
87
- }: UseMDXEvaluationOptions): UseMDXEvaluationResult;
120
+ declare function useMDXEvaluation({ compiledCode, cacheKey }: UseMDXEvaluationOptions): UseMDXEvaluationResult;
88
121
  //#endregion
89
122
  //#region src/mdx-editor.d.ts
90
123
  interface MDXEditorProps extends Omit<BaseMDXEditorProps, 'disableImageSettingsButton' | 'imageButtonRef' | 'imageDialog' | 'onImageShortcut'> {}
91
124
  declare const MDXEditor: React.ForwardRefExoticComponent<MDXEditorProps & React.RefAttributes<MDXEditorMethods$1>>;
92
- interface MDXEditorModalProps extends Omit<BaseMDXEditorModalProps, 'EditorComponent'> {}
93
- declare function MDXEditorModal(props: MDXEditorModalProps): react_jsx_runtime0.JSX.Element;
94
125
  //#endregion
95
- export { BaseMDXEditor, BaseMDXEditorModal, type BaseMDXEditorModalProps, type BaseMDXEditorProps, MDXEditor, type MDXEditorMethods, MDXEditorModal, MDXEditorModalProps, MDXEditorProps, type UseMDXEvaluationOptions, type UseMDXEvaluationResult, useMDXEvaluation };
126
+ export { BaseMDXEditor, type BaseMDXEditorProps, MDXEditor, type MDXEditorMethods, MDXEditorProps, type UseMDXEvaluationOptions, type UseMDXEvaluationResult, codeBlockWrapperInfo$, useMDXEvaluation };
96
127
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/editor-core.tsx","../src/hooks/useMDXEvaluation.ts","../src/mdx-editor.tsx"],"mappings":";;;;;UAkCiB,kBAAA;EACf,KAAA;EACA,QAAA,GAAW,KAAA;EACX,MAAA,IAAU,KAAA;EACV,QAAA;EACA,WAAA,GAAc,KAAA,CAAM,EAAA;EACpB,0BAAA;EACA,eAAA;EACA,cAAA,GAAiB,KAAA,CAAM,GAAA,CAAI,iBAAA;EAC3B,uBAAA,GAA0B,sBAAA;EAAsB;;;;;;EAOhD,SAAA;AAAA;AAAA,UAGe,uBAAA;EACf,MAAA;EACA,OAAA;EACA,QAAA;EACA,cAAA;EACA,MAAA,GAAS,OAAA,oBAA2B,OAAA;EACpC,WAAA,GAAc,KAAA,CAAM,SAAA;EACpB,WAAA,IAAe,KAAA;IACb,OAAA;IACA,UAAA,EAAY,KAAA,CAAM,QAAA,CAAS,KAAA,CAAM,cAAA;IACjC,SAAA,EAAW,KAAA,CAAM,SAAA,CAAU,kBAAA;EAAA,MACvB,KAAA,CAAM,SAAA;EACZ,aAAA;EACA,eAAA,EAAiB,KAAA,CAAM,yBAAA,CAA0B,kBAAA,GAAqB,KAAA,CAAM,aAAA,CAAc,kBAAA;EAb3E;;;;EAkBf,eAAA;AAAA;AAAA,cAiEW,aAAA,EAAa,KAAA,CAAA,yBAAA,CAAA,kBAAA,GAAA,KAAA,CAAA,aAAA,CAAA,kBAAA;AAAA,iBA+JV,kBAAA,CAAA;EACd,MAAA;EACA,OAAA;EACA,QAAA;EACA,cAAA;EACA,MAAA;EACA,WAAA;EACA,WAAA;EACA,aAAA;EACA,eAAA;EACA;AAAA,GACC,uBAAA,GAAuB,kBAAA,CAAA,GAAA,CAAA,OAAA;;;UCrQT,uBAAA;EACf,YAAA;EACA,QAAA;AAAA;AAAA,UAGe,sBAAA;EACf,SAAA,EAAW,KAAA,CAAM,aAAA;EACjB,KAAA,EAAO,KAAA;EACP,SAAA;EACA,KAAA;AAAA;;;;;;;;;;;;;;;;iBAkBc,gBAAA,CAAA;EAAmB,YAAA;EAAc;AAAA,GAAY,uBAAA,GAA0B,sBAAA;;;UC/DtE,cAAA,SACP,IAAA,CACN,kBAAA;AAAA,cAIS,SAAA,EAAS,KAAA,CAAA,yBAAA,CAAA,cAAA,GAAA,KAAA,CAAA,aAAA,CAAA,kBAAA;AAAA,UAML,mBAAA,SAA4B,IAAA,CAAK,uBAAA;AAAA,iBAElC,cAAA,CAAe,KAAA,EAAO,mBAAA,GAAmB,kBAAA,CAAA,GAAA,CAAA,OAAA"}
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';