@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@conloca/mdx",
3
- "version": "0.1.16",
3
+ "version": "0.2.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -9,53 +9,67 @@
9
9
  "url": "git+https://github.com/conloca/conloca.git",
10
10
  "directory": "packages/mdx"
11
11
  },
12
- "main": "./dist/index.mjs",
13
- "module": "./dist/index.mjs",
12
+ "main": "./dist/index.js",
13
+ "module": "./dist/index.js",
14
14
  "dependencies": {
15
- "@conloca/content-api": "0.1.16",
15
+ "@conloca/content-api": "0.2.0",
16
16
  "@mdx-js/mdx": "^3.1.1",
17
17
  "@mdx-js/react": "^3.1.1",
18
- "@mdxeditor/editor": "^3.55.0",
18
+ "@mdxeditor/editor": "^4.2.3",
19
+ "github-slugger": "^2.0.0",
19
20
  "gray-matter": "^4.0.3",
21
+ "hast-util-to-jsx-runtime": "^2.3.6",
20
22
  "lucide-react": "^0.564.0",
21
23
  "mdast-util-to-markdown": "^2.1.2",
22
24
  "remark-frontmatter": "^5.0.0",
23
- "remark-gfm": "^4.0.0",
25
+ "remark-gfm": "^4.0.1",
26
+ "remark-mdx": "^3.1.1",
24
27
  "remark-mdx-frontmatter": "^5.2.0",
25
- "tslib": "^2.8.1"
28
+ "remark-parse": "^11.0.0",
29
+ "remark-rehype": "^11.1.2",
30
+ "tslib": "^2.8.1",
31
+ "unified": "^11.0.5"
26
32
  },
27
33
  "peerDependencies": {
28
- "react": "^19.2.5",
29
- "react-dom": "^19.2.5"
34
+ "react": "^19.0.0",
35
+ "react-dom": "^19.0.0"
30
36
  },
31
37
  "devDependencies": {
32
- "@happy-dom/global-registrator": "20.6.1",
33
- "@testing-library/react": "^16.3.2",
34
- "@tsdown/css": "^0.22.0",
35
- "@types/react": "19.2.14",
36
- "@types/react-dom": "19.2.3",
37
- "happy-dom": "20.6.1",
38
- "mdast-util-from-markdown": "^2.0.0",
38
+ "@happy-dom/global-registrator": "20.13.2",
39
+ "@testing-library/react": "^16.3.3",
40
+ "@types/mdast": "^4.0.4",
41
+ "@types/react": "19.2.18",
42
+ "@types/react-dom": "19.2.7",
43
+ "happy-dom": "20.13.2",
44
+ "mdast-util-from-markdown": "^2.0.3",
39
45
  "mdast-util-gfm": "^3.1.0",
40
46
  "micromark-extension-gfm": "^3.0.0",
41
- "react": "19.2.4",
42
- "react-dom": "19.2.4"
47
+ "react": "19.2.8",
48
+ "react-dom": "19.2.8"
43
49
  },
44
50
  "exports": {
45
51
  "./package.json": "./package.json",
46
52
  ".": {
47
- "types": "./dist/index.d.mts",
48
- "import": "./dist/index.mjs",
49
- "default": "./dist/index.mjs"
53
+ "types": "./dist/index.d.ts",
54
+ "import": "./dist/index.js",
55
+ "default": "./dist/index.js"
50
56
  },
51
57
  "./node": {
52
- "types": "./dist/node.d.mts",
53
- "import": "./dist/node.mjs",
54
- "default": "./dist/node.mjs"
55
- }
58
+ "types": "./dist/node.d.ts",
59
+ "import": "./dist/node.js",
60
+ "default": "./dist/node.js"
61
+ },
62
+ "./sandbox-frame": {
63
+ "types": "./dist/sandbox-frame.d.ts",
64
+ "import": "./dist/sandbox-frame.js",
65
+ "default": "./dist/sandbox-frame.js"
66
+ },
67
+ "./style.css": "./src/editor-styles.css"
56
68
  },
57
69
  "files": [
58
70
  "dist",
71
+ "src",
72
+ "!src/__tests__",
59
73
  "!**/*.tsbuildinfo"
60
74
  ],
61
75
  "scripts": {
@@ -63,39 +77,42 @@
63
77
  },
64
78
  "nx": {
65
79
  "targets": {
80
+ "clean": {},
66
81
  "test": {
67
82
  "dependsOn": [
68
83
  "typecheck-tests",
69
- "^build"
84
+ "^build",
85
+ "test-packed"
70
86
  ],
71
87
  "executor": "@smoothbricks/nx-plugin:bounded-exec",
72
88
  "options": {
73
89
  "command": "bun --conditions=production test",
74
90
  "cwd": "{projectRoot}",
75
91
  "timeoutMs": 120000,
76
- "killAfterMs": 1e4
92
+ "killAfterMs": 10000
77
93
  }
78
94
  },
79
95
  "lint": {},
80
- "tsdown-js": {
96
+ "test-packed": {
97
+ "//": "The packed shape is a function of the built outputs, the manifest and the locked dependency closure; cached on exactly those.",
81
98
  "executor": "nx:run-commands",
82
99
  "cache": true,
83
- "dependsOn": [
84
- "^build"
85
- ],
86
100
  "inputs": [
87
- "{projectRoot}/src/**/*",
88
- "{projectRoot}/tsdown.config.ts",
89
- "{projectRoot}/tsconfig.lib.json",
90
- "{projectRoot}/package.json"
101
+ {
102
+ "dependentTasksOutputFiles": "**/*",
103
+ "transitive": true
104
+ },
105
+ "{projectRoot}/package.json",
106
+ "{workspaceRoot}/bun.lock",
107
+ "{workspaceRoot}/tooling/packaging/**/*"
91
108
  ],
92
- "outputs": [
93
- "{projectRoot}/dist/**/*.mjs",
94
- "{projectRoot}/dist/**/*.d.mts"
109
+ "dependsOn": [
110
+ "build",
111
+ "^build"
95
112
  ],
96
113
  "options": {
97
- "command": "tsdown",
98
- "cwd": "{projectRoot}"
114
+ "command": "bun tooling/packaging/test-packed.ts mdx",
115
+ "cwd": "{workspaceRoot}"
99
116
  }
100
117
  }
101
118
  },
@@ -105,6 +122,6 @@
105
122
  "name": "mdx"
106
123
  },
107
124
  "type": "module",
108
- "types": "./dist/index.d.mts",
125
+ "types": "./dist/index.d.ts",
109
126
  "license": "FSL-1.1-ALv2"
110
127
  }
@@ -0,0 +1,262 @@
1
+ import {
2
+ Cell,
3
+ type CodeBlockEditorDescriptor,
4
+ type CodeBlockEditorProps,
5
+ CodeMirrorEditor,
6
+ useCellValue,
7
+ useCodeBlockEditorContext,
8
+ } from '@mdxeditor/editor';
9
+ import { Check, Copy } from 'lucide-react';
10
+ import { type ChangeEvent, createElement, type ReactElement, useCallback, useEffect, useMemo, useState } from 'react';
11
+
12
+ /**
13
+ * Frame chrome around the editor's fenced code blocks — header bar with
14
+ * filename, language tag, and a copy button. The outer wrapper picks up
15
+ * the host's code-block classes (published via `codeBlockWrapperInfo$`
16
+ * by the host-wrapper plugin in cms-spa), so the host's actual code-
17
+ * block CSS (Starlight's expressive-code, Tailwind's pre styling, etc.)
18
+ * reaches the editor's frame through the cascade — no shipped mirror.
19
+ *
20
+ * Host-agnostic: no Shiki / Starlight / ExpressiveCode imports. Class
21
+ * names are discovered at runtime from the host's live HTML.
22
+ */
23
+
24
+ /**
25
+ * Realm-published cell carrying the host's code-block wrapper chain,
26
+ * outermost first. For Starlight's expressive-code:
27
+ *
28
+ * [
29
+ * { tagName: 'div', className: 'expressive-code' },
30
+ * { tagName: 'figure', className: 'frame has-title not-content' },
31
+ * ]
32
+ *
33
+ * The cms-spa `hostWrapperPlugin` writes to this whenever the fetched
34
+ * wrapper info changes; the code-block frame reads from it to render
35
+ * each chain link as a nested element with the host's expected tag
36
+ * name. Same publish-pattern as the content-wrapper plugin in cms-spa.
37
+ *
38
+ * Null when the host has no code-block chrome (or the fetch is in
39
+ * flight). The frame then falls back to its base `.conloca-code-block`
40
+ * wrapper with no host inheritance — "looks plain but not broken."
41
+ */
42
+ export const codeBlockWrapperInfo$ = Cell<{ tagName: string; className: string }[] | null>(null);
43
+
44
+ const TITLE_META_RE = /\btitle=(?:"([^"]*)"|'([^']*)'|(\S+))/;
45
+ // First-line filename comment, the convention ExpressiveCode's frames
46
+ // plugin recognizes when no explicit `title=` is set on the fence —
47
+ // `// foo.ts`, `# foo.py`, `<!-- foo.html -->`, `/* foo.css */`,
48
+ // `; foo.ini`, `-- foo.sql`. Requires a dot-extension so plain prose
49
+ // comments don't get mistaken for filenames.
50
+ const FILENAME_COMMENT_RE = /^\s*(?:\/\/|#|;|--|<!--|\/\*)\s*([\w.\-/@]+\.[\w.-]+)\s*(?:-->|\*\/)?\s*$/;
51
+
52
+ interface MetaParts {
53
+ /** The current title value (empty string when no title meta is set). */
54
+ title: string;
55
+ /** Everything else in the meta string, with the title slice removed. */
56
+ rest: string;
57
+ }
58
+
59
+ function parseMeta(meta: string): MetaParts {
60
+ const match = meta.match(TITLE_META_RE);
61
+ if (!match) return { title: '', rest: meta.trim() };
62
+ const value = match[1] ?? match[2] ?? match[3] ?? '';
63
+ const rest = (meta.slice(0, match.index) + meta.slice(match.index! + match[0].length)).replace(/\s+/g, ' ').trim();
64
+ return { title: value, rest };
65
+ }
66
+
67
+ /** Reassemble a meta string with `title` set (or stripped when empty). */
68
+ function buildMeta(title: string, rest: string): string {
69
+ const trimmedTitle = title.trim();
70
+ const titleFragment = trimmedTitle ? `title="${trimmedTitle.replace(/"/g, '\\"')}"` : '';
71
+ if (titleFragment && rest) return `${titleFragment} ${rest}`;
72
+ return titleFragment || rest;
73
+ }
74
+
75
+ /**
76
+ * Detect a leading filename comment, the convention EC's frames plugin
77
+ * uses on the published page when no explicit `title=` is set. Returns
78
+ * the filename when line 1 of the code is a single-line comment ending
79
+ * in a dot-extension; empty string otherwise.
80
+ *
81
+ * Used to pre-fill the editor's filename input on legacy fences so
82
+ * authors see the same value the reader sees in the rendered tab. Pure
83
+ * read — the underlying code/comment is never touched.
84
+ */
85
+ function detectFilenameComment(code: string): string {
86
+ const firstLine = code.split('\n', 1)[0] ?? '';
87
+ const match = firstLine.match(FILENAME_COMMENT_RE);
88
+ return match ? (match[1] ?? '') : '';
89
+ }
90
+
91
+ function CopyButton({ code }: { code: string }) {
92
+ const [copied, setCopied] = useState(false);
93
+ const onClick = useCallback(() => {
94
+ void navigator.clipboard.writeText(code).then(() => {
95
+ setCopied(true);
96
+ window.setTimeout(() => setCopied(false), 1200);
97
+ });
98
+ }, [code]);
99
+ return (
100
+ <button
101
+ type="button"
102
+ onClick={onClick}
103
+ className="conloca-code-copy"
104
+ aria-label={copied ? 'Copied' : 'Copy code'}
105
+ title={copied ? 'Copied' : 'Copy code'}
106
+ >
107
+ {copied ? <Check size={14} aria-hidden="true" /> : <Copy size={14} aria-hidden="true" />}
108
+ </button>
109
+ );
110
+ }
111
+
112
+ /**
113
+ * Editable filename slot. Rewrites the code block's meta string via
114
+ * `setMeta` from `useCodeBlockEditorContext` so authors can author the
115
+ * frame title (`\`\`\`ts title="src/foo.ts"`) directly from the editor —
116
+ * the meta string round-trips through MDXEditor's markdown serializer
117
+ * and ExpressiveCode picks it up as the frame header on the published
118
+ * page. Other meta fragments (ins/del/mark/wrap) are preserved.
119
+ *
120
+ * Legacy fences with a filename comment on line 1 (`// foo.ts`) — EC's
121
+ * pre-meta convention for tab titles — are detected and pre-filled in
122
+ * the input as a read-only display. Opening such a fence does NOT
123
+ * mutate the source. Editing the field is an explicit author intent:
124
+ * the new value is written as `title=` meta AND the original comment
125
+ * line is stripped, so the editor's input becomes the single source of
126
+ * truth without breaking the published render.
127
+ */
128
+ function FilenameInput({ meta, code }: { meta: string; code: string }) {
129
+ const { setMeta, setCode } = useCodeBlockEditorContext();
130
+ const parsed = useMemo(() => parseMeta(meta), [meta]);
131
+ const detectedFromComment = useMemo(() => (parsed.title ? '' : detectFilenameComment(code)), [parsed.title, code]);
132
+ const displayed = parsed.title || detectedFromComment;
133
+ const [title, setTitle] = useState(displayed);
134
+ const [interacted, setInteracted] = useState(false);
135
+
136
+ // External updates (HMR, language change, sibling code edits) reset
137
+ // the local view to the canonical value and clear the "user edited"
138
+ // flag — so the next blur after an external update doesn't write
139
+ // anything until the author actively types again.
140
+ useEffect(() => {
141
+ setTitle(displayed);
142
+ setInteracted(false);
143
+ }, [displayed]);
144
+
145
+ const commit = useCallback(
146
+ (next: string) => {
147
+ if (!interacted) return;
148
+ if (next === parsed.title) return;
149
+ setMeta(buildMeta(next, parsed.rest));
150
+ // If the pre-fill came from a leading filename comment and the
151
+ // author has now committed an explicit title, strip the comment
152
+ // so EC doesn't render the title twice (once as the tab, once as
153
+ // a stray code comment) on the published page.
154
+ if (detectedFromComment) {
155
+ const lines = code.split('\n');
156
+ if (lines[0] && FILENAME_COMMENT_RE.test(lines[0])) {
157
+ setCode(lines.slice(1).join('\n').replace(/^\n+/, ''));
158
+ }
159
+ }
160
+ },
161
+ [interacted, parsed.title, parsed.rest, detectedFromComment, code, setMeta, setCode],
162
+ );
163
+
164
+ const onChange = (e: ChangeEvent<HTMLInputElement>) => {
165
+ setInteracted(true);
166
+ setTitle(e.target.value);
167
+ };
168
+ const onBlur = () => commit(title);
169
+
170
+ return (
171
+ <input
172
+ type="text"
173
+ className="conloca-code-block__filename"
174
+ value={title}
175
+ placeholder="filename"
176
+ spellCheck={false}
177
+ onChange={onChange}
178
+ onBlur={onBlur}
179
+ aria-label="Code block filename"
180
+ />
181
+ );
182
+ }
183
+
184
+ function ConlocaCodeBlockEditor(props: CodeBlockEditorProps) {
185
+ // Materialise the host's discovered code-block chain as nested
186
+ // elements. The INNERMOST link in the chain becomes the "frame"
187
+ // element that directly holds the filename header, the code body,
188
+ // and the copy slot — matching ExpressiveCode's authoring shape:
189
+ //
190
+ // <div class="expressive-code"> ← outer link
191
+ // <figure class="frame has-title …"> ← inner link (frame)
192
+ // <figcaption class="header">
193
+ // <span class="title"><FilenameInput /></span>
194
+ // </figcaption>
195
+ // <pre><CodeMirror /></pre>
196
+ // <div class="copy"><CopyButton /></div>
197
+ // </figure>
198
+ // </div>
199
+ //
200
+ // Rendering with the host's expected tags (figure / figcaption / pre)
201
+ // and nesting depth lets descendant CSS selectors land — both single-
202
+ // class (`.frame { ... }`) and descendant (`.expressive-code .frame
203
+ // .header > .title { ... }`) rules paint without us shipping a
204
+ // parallel copy.
205
+ //
206
+ // When chain is null/empty (host has no code-block chrome, or the
207
+ // wrapper fetch hasn't resolved yet) we fall back to a bare
208
+ // `<div class="conloca-code-block">` containing the same inner
209
+ // shape — "looks plain but not broken."
210
+ const chain = useCellValue(codeBlockWrapperInfo$) ?? [];
211
+ const frameLink = chain.length > 0 ? chain[chain.length - 1] : null;
212
+ const outerLinks = frameLink ? chain.slice(0, -1) : [];
213
+
214
+ const frameInner = (
215
+ <>
216
+ <figcaption className="conloca-code-block__header header">
217
+ <span className="title">
218
+ <FilenameInput meta={props.meta} code={props.code} />
219
+ </span>
220
+ </figcaption>
221
+ <pre className="conloca-code-block__pre">
222
+ <CodeMirrorEditor {...props} />
223
+ </pre>
224
+ {/* `.copy` matches expressive-code's expected slot name so any
225
+ `.frame .copy` styling from the host paints. We host only the
226
+ copy button here — language selection lives in MDXEditor's
227
+ own `_codeMirrorToolbar_*` (combobox), positioned via
228
+ editor-styles.css so it doesn't overlap the code. */}
229
+ <div className="conloca-code-block__copy copy">
230
+ <CopyButton code={props.code} />
231
+ </div>
232
+ </>
233
+ );
234
+
235
+ // The frame element carries `conloca-code-block` (our own targeting
236
+ // hook) alongside the host's discovered classes. When the host has
237
+ // no chain, we still wrap in a plain `<div>` so the inner shape is
238
+ // consistent for our utility selectors.
239
+ const frame = frameLink ? (
240
+ createElement(frameLink.tagName, { className: `conloca-code-block ${frameLink.className}` }, frameInner)
241
+ ) : (
242
+ <div className="conloca-code-block">{frameInner}</div>
243
+ );
244
+
245
+ // Wrap outer links from inside out. `reduceRight` so the first
246
+ // chain link ends up as the outermost rendered element.
247
+ return outerLinks.reduceRight<ReactElement>(
248
+ (kids, link) => createElement(link.tagName, { className: link.className }, kids),
249
+ frame,
250
+ );
251
+ }
252
+
253
+ /**
254
+ * Catch-all descriptor — `priority: 100`, `match: () => true` — registered
255
+ * via `codeBlockPlugin({ codeBlockEditorDescriptors: [conlocaCodeBlockDescriptor] })`.
256
+ * Wraps the upstream `CodeMirrorEditor` with the host-themed frame.
257
+ */
258
+ export const conlocaCodeBlockDescriptor: CodeBlockEditorDescriptor = {
259
+ priority: 100,
260
+ match: () => true,
261
+ Editor: ConlocaCodeBlockEditor,
262
+ };