@conloca/mdx 0.1.15 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -18
- package/dist/code-block-frame.d.ts +41 -0
- package/dist/code-block-frame.d.ts.map +1 -0
- package/dist/code-block-frame.js +187 -0
- package/dist/code-block-frame.js.map +1 -0
- package/dist/editor-core.d.ts +56 -0
- package/dist/editor-core.d.ts.map +1 -0
- package/dist/editor-core.js +225 -0
- package/dist/editor-core.js.map +1 -0
- package/dist/esm-passthrough.d.ts +36 -0
- package/dist/esm-passthrough.d.ts.map +1 -0
- package/dist/esm-passthrough.js +237 -0
- package/dist/esm-passthrough.js.map +1 -0
- package/dist/fragments.d.ts +37 -0
- package/dist/fragments.d.ts.map +1 -0
- package/dist/fragments.js +53 -0
- package/dist/fragments.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/markdown-options.d.ts +3 -0
- package/dist/markdown-options.d.ts.map +1 -0
- package/dist/markdown-options.js +20 -0
- package/dist/markdown-options.js.map +1 -0
- package/dist/mdx/compile-request.d.ts +5 -0
- package/dist/mdx/compile-request.d.ts.map +1 -0
- package/dist/mdx/compile-request.js +40 -0
- package/dist/mdx/compile-request.js.map +1 -0
- package/dist/mdx/compile.d.ts +3 -0
- package/dist/mdx/compile.d.ts.map +1 -0
- package/dist/mdx/compile.js +47 -0
- package/dist/mdx/compile.js.map +1 -0
- package/dist/mdx/evaluate.d.ts +39 -0
- package/dist/mdx/evaluate.d.ts.map +1 -0
- package/dist/mdx/evaluate.js +131 -0
- package/dist/mdx/evaluate.js.map +1 -0
- package/dist/mdx/heading-outline.d.ts +46 -0
- package/dist/mdx/heading-outline.d.ts.map +1 -0
- package/dist/mdx/heading-outline.js +114 -0
- package/dist/mdx/heading-outline.js.map +1 -0
- package/dist/mdx/restrict-expressions.d.ts +43 -0
- package/dist/mdx/restrict-expressions.d.ts.map +1 -0
- package/dist/mdx/restrict-expressions.js +160 -0
- package/dist/mdx/restrict-expressions.js.map +1 -0
- package/dist/mdx-editor.d.ts +7 -0
- package/dist/mdx-editor.d.ts.map +1 -0
- package/dist/mdx-editor.js +8 -0
- package/dist/mdx-editor.js.map +1 -0
- package/dist/node.d.ts +18 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +18 -0
- package/dist/node.js.map +1 -0
- package/dist/sandbox-frame.d.ts +114 -0
- package/dist/sandbox-frame.d.ts.map +1 -0
- package/dist/sandbox-frame.js +234 -0
- package/dist/sandbox-frame.js.map +1 -0
- package/dist/types.d.ts +19 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +82 -26
- package/src/code-block-frame.tsx +262 -0
- package/src/editor-core.tsx +403 -0
- package/src/editor-styles.css +282 -0
- package/src/esm-passthrough.ts +269 -0
- package/src/fragments.ts +91 -0
- package/src/index.ts +5 -0
- package/src/markdown-options.ts +21 -0
- package/src/mdx/compile-request.ts +53 -0
- package/src/mdx/compile.ts +51 -0
- package/src/mdx/evaluate.ts +144 -0
- package/src/mdx/heading-outline.ts +153 -0
- package/src/mdx/restrict-expressions.ts +173 -0
- package/src/mdx-editor.tsx +15 -0
- package/src/node.ts +25 -0
- package/src/sandbox-frame.ts +277 -0
- package/src/types.ts +23 -0
- package/dist/index.d.mts +0 -96
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -425
- package/dist/index.mjs.map +0 -1
- package/dist/node.d.mts +0 -107
- package/dist/node.d.mts.map +0 -1
- package/dist/node.mjs +0 -125
- package/dist/node.mjs.map +0 -1
package/package.json
CHANGED
|
@@ -1,71 +1,127 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@conloca/mdx",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/conloca/conloca.git",
|
|
10
|
-
"directory": "mdx"
|
|
10
|
+
"directory": "packages/mdx"
|
|
11
11
|
},
|
|
12
|
-
"main": "./dist/index.
|
|
13
|
-
"module": "./dist/index.
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"module": "./dist/index.js",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@conloca/content-api": "
|
|
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": "^
|
|
18
|
+
"@mdxeditor/editor": "^4.2.3",
|
|
19
|
+
"github-slugger": "^2.0.0",
|
|
20
|
+
"gray-matter": "^4.0.3",
|
|
21
|
+
"hast-util-to-jsx-runtime": "^2.3.6",
|
|
19
22
|
"lucide-react": "^0.564.0",
|
|
23
|
+
"mdast-util-to-markdown": "^2.1.2",
|
|
20
24
|
"remark-frontmatter": "^5.0.0",
|
|
21
|
-
"remark-gfm": "^4.0.
|
|
25
|
+
"remark-gfm": "^4.0.1",
|
|
26
|
+
"remark-mdx": "^3.1.1",
|
|
22
27
|
"remark-mdx-frontmatter": "^5.2.0",
|
|
23
|
-
"
|
|
28
|
+
"remark-parse": "^11.0.0",
|
|
29
|
+
"remark-rehype": "^11.1.2",
|
|
30
|
+
"tslib": "^2.8.1",
|
|
31
|
+
"unified": "^11.0.5"
|
|
24
32
|
},
|
|
25
33
|
"peerDependencies": {
|
|
26
34
|
"react": "^19.0.0",
|
|
27
35
|
"react-dom": "^19.0.0"
|
|
28
36
|
},
|
|
29
37
|
"devDependencies": {
|
|
30
|
-
"@
|
|
31
|
-
"@
|
|
32
|
-
"@types/
|
|
33
|
-
"@
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
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",
|
|
45
|
+
"mdast-util-gfm": "^3.1.0",
|
|
46
|
+
"micromark-extension-gfm": "^3.0.0",
|
|
47
|
+
"react": "19.2.8",
|
|
48
|
+
"react-dom": "19.2.8"
|
|
37
49
|
},
|
|
38
50
|
"exports": {
|
|
39
51
|
"./package.json": "./package.json",
|
|
40
52
|
".": {
|
|
41
53
|
"types": "./dist/index.d.ts",
|
|
42
|
-
"import": "./dist/index.
|
|
43
|
-
"default": "./dist/index.
|
|
54
|
+
"import": "./dist/index.js",
|
|
55
|
+
"default": "./dist/index.js"
|
|
44
56
|
},
|
|
45
57
|
"./node": {
|
|
46
58
|
"types": "./dist/node.d.ts",
|
|
47
|
-
"import": "./dist/node.
|
|
48
|
-
"default": "./dist/node.
|
|
49
|
-
}
|
|
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"
|
|
50
68
|
},
|
|
51
69
|
"files": [
|
|
52
70
|
"dist",
|
|
71
|
+
"src",
|
|
72
|
+
"!src/__tests__",
|
|
53
73
|
"!**/*.tsbuildinfo"
|
|
54
74
|
],
|
|
55
75
|
"scripts": {
|
|
56
|
-
"
|
|
57
|
-
"test": "tsc-bun-test",
|
|
58
|
-
"test:watch": "bun test --watch"
|
|
76
|
+
"test": "nx run mdx:test --outputStyle=stream"
|
|
59
77
|
},
|
|
60
78
|
"nx": {
|
|
61
79
|
"targets": {
|
|
80
|
+
"clean": {},
|
|
62
81
|
"test": {
|
|
63
82
|
"dependsOn": [
|
|
83
|
+
"typecheck-tests",
|
|
84
|
+
"^build",
|
|
85
|
+
"test-packed"
|
|
86
|
+
],
|
|
87
|
+
"executor": "@smoothbricks/nx-plugin:bounded-exec",
|
|
88
|
+
"options": {
|
|
89
|
+
"command": "bun --conditions=production test",
|
|
90
|
+
"cwd": "{projectRoot}",
|
|
91
|
+
"timeoutMs": 120000,
|
|
92
|
+
"killAfterMs": 10000
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
"lint": {},
|
|
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.",
|
|
98
|
+
"executor": "nx:run-commands",
|
|
99
|
+
"cache": true,
|
|
100
|
+
"inputs": [
|
|
101
|
+
{
|
|
102
|
+
"dependentTasksOutputFiles": "**/*",
|
|
103
|
+
"transitive": true
|
|
104
|
+
},
|
|
105
|
+
"{projectRoot}/package.json",
|
|
106
|
+
"{workspaceRoot}/bun.lock",
|
|
107
|
+
"{workspaceRoot}/tooling/packaging/**/*"
|
|
108
|
+
],
|
|
109
|
+
"dependsOn": [
|
|
110
|
+
"build",
|
|
64
111
|
"^build"
|
|
65
|
-
]
|
|
112
|
+
],
|
|
113
|
+
"options": {
|
|
114
|
+
"command": "bun tooling/packaging/test-packed.ts mdx",
|
|
115
|
+
"cwd": "{workspaceRoot}"
|
|
116
|
+
}
|
|
66
117
|
}
|
|
67
|
-
}
|
|
118
|
+
},
|
|
119
|
+
"tags": [
|
|
120
|
+
"npm:public"
|
|
121
|
+
],
|
|
122
|
+
"name": "mdx"
|
|
68
123
|
},
|
|
69
124
|
"type": "module",
|
|
70
|
-
"types": "./dist/index.d.ts"
|
|
125
|
+
"types": "./dist/index.d.ts",
|
|
126
|
+
"license": "FSL-1.1-ALv2"
|
|
71
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
|
+
};
|