Skip to content

Commit fae9afe

Browse files
committed
JSX annotations
1 parent a805afc commit fae9afe

File tree

7 files changed

+73
-55
lines changed

7 files changed

+73
-55
lines changed

packages/mdx/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
"files": [
1010
"dist"
1111
],
12+
"inputs": [
13+
"index",
14+
"components"
15+
],
1216
"scripts": {
1317
"x": "x"
1418
},

packages/mdx/src/annotations.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CodeAnnotation } from "@code-hike/smooth-code"
33
import { CodeLink } from "./links"
44
import { Code, relativeToAbsolute } from "@code-hike/utils"
55
import { Node, Parent } from "unist"
6+
import { wrapChildren } from "./to-estree"
67

78
export function Annotation() {
89
return "error: code hike remark plugin not running or annotation isn't at the right place"
@@ -72,11 +73,12 @@ function Label({
7273
style,
7374
theme,
7475
}: {
75-
data: string
76+
data: any
7677
children: React.ReactNode
7778
style?: React.CSSProperties
7879
theme?: any
7980
}) {
81+
console.log(data)
8082
const bg = ((theme as any).colors[
8183
"editor.lineHighlightBackground"
8284
] ||
@@ -104,7 +106,7 @@ function Label({
104106
opacity: 0.7,
105107
}}
106108
>
107-
{data}
109+
{data?.children || data}
108110
</div>
109111
</div>
110112
)
@@ -206,7 +208,9 @@ export function extractJSXAnnotations(
206208
props[attr.name] = attr.value
207209
})
208210
const { as, focus, ...data } = props
209-
// data.children = wrapChildren(jsxAnnotation.children);
211+
data.children = wrapChildren(
212+
jsxAnnotation.children || []
213+
)
210214

211215
const Component = annotationsMap[as] || as
212216
annotations.push({

packages/mdx/src/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
import "./index.scss"
22

33
export { remarkCodeHike } from "./plugin"
4-
export { CH } from "./components"

packages/mdx/src/plugin.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ function addImportNode(tree: Parent) {
6262
],
6363
source: {
6464
type: "Literal",
65-
value: "@code-hike/mdx",
66-
raw: '"@code-hike/mdx"',
65+
value:
66+
"@code-hike/mdx/dist/components.esm.js",
67+
raw:
68+
'"@code-hike/mdx/dist/components.esm.js"',
6769
},
6870
},
6971
],

packages/mdx/src/to-estree.ts

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Expression } from "estree"
22
import isPlainObject from "is-plain-obj"
33
import { annotationsMap } from "./annotations"
4-
// import unified from "unified"
5-
// import remarkRehype from "remark-rehype"
6-
// import { toEstree } from "hast-util-to-estree"
4+
import unified from "unified"
5+
import remarkRehype from "remark-rehype"
6+
import toEstree from "hast-util-to-estree"
7+
import { Node, Parent } from "unist"
78

89
// forked from https://github.com/remcohaszing/estree-util-value-to-estree/blob/main/src/index.ts
910

@@ -180,6 +181,12 @@ export function valueToEstree(
180181
}
181182
}
182183
if (options.instanceAsObject || isPlainObject(value)) {
184+
if ((value as any)?.name === MDX_CHILDREN) {
185+
const tree = { ...(value as any) }
186+
tree.name = null
187+
return (mdastToEstree(tree) as any).body[0].expression
188+
}
189+
183190
return {
184191
type: "ObjectExpression",
185192
// @ts-expect-error: looks like an object.
@@ -233,29 +240,36 @@ export function valueToEstree(
233240
throw new TypeError(`Unsupported value: ${String(value)}`)
234241
}
235242

236-
// export async function mdastToEstree(node: Node) {
237-
// const nodeTypes = [
238-
// "mdxFlowExpression",
239-
// "mdxJsxFlowElement",
240-
// "mdxJsxTextElement",
241-
// "mdxTextExpression",
242-
// "mdxjsEsm",
243-
// ]
244-
// const changedTree = unified()
245-
// .use(remarkRehype, {
246-
// allowDangerousHtml: true,
247-
// passThrough: nodeTypes,
248-
// })
249-
// .use(rehypeRecma as any)
250-
// .runSync(node as any)
243+
export function mdastToEstree(node: Node) {
244+
const nodeTypes = [
245+
"mdxFlowExpression",
246+
"mdxJsxFlowElement",
247+
"mdxJsxTextElement",
248+
"mdxTextExpression",
249+
"mdxjsEsm",
250+
]
251+
const changedTree = unified()
252+
.use(remarkRehype, {
253+
allowDangerousHtml: true,
254+
passThrough: nodeTypes,
255+
})
256+
.use(rehypeRecma as any)
257+
.runSync(node)
251258

252-
// return changedTree
253-
// }
259+
return changedTree
260+
}
254261

255-
// function rehypeRecma() {
256-
// return (tree: any) => toEstree(tree)
257-
// }
262+
function rehypeRecma() {
263+
return (tree: any) => (toEstree as any)(tree)
264+
}
258265

259-
// export function wrapChildren() {
260-
// return {}
261-
// }
266+
const MDX_CHILDREN = "MDX_CHILDREN"
267+
268+
export function wrapChildren(children: Node[]) {
269+
const tree = {
270+
type: "mdxJsxFlowElement",
271+
children,
272+
name: MDX_CHILDREN,
273+
}
274+
return tree
275+
}

packages/playground/content/custom-annotations.mdx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,3 @@ function adipiscing(...elit) {
1919
url="https://github.com/code-hike"
2020
title="Code Hike"
2121
/>
22-
<CH.Annotation as="label" focus="8">
23-
24-
**Hello**
25-
26-
</CH.Annotation>

packages/script/rollup.config.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,7 @@ import typescript from "rollup-plugin-typescript2"
66
import copy from "rollup-plugin-copy"
77
import path from "path"
88
import json from "@rollup/plugin-json"
9-
10-
const plugins = [
11-
json({ compact: true }),
12-
typescript(),
13-
postcss({
14-
extract: path.resolve("dist/index.css"),
15-
plugins: [autoprefixer(), cssnano()],
16-
}),
17-
copy({
18-
targets: [{ src: "src/index.scss", dest: "dist" }],
19-
}),
20-
]
9+
import fs from "fs"
2110

2211
const createConfig = filename => ({
2312
input: `src/${filename}.tsx`,
@@ -37,11 +26,22 @@ const createConfig = filename => ({
3726
},
3827
],
3928
// external: ["react"],
40-
plugins,
29+
plugins: [
30+
json({ compact: true }),
31+
typescript(),
32+
postcss({
33+
extract: path.resolve("dist/index.css"),
34+
plugins: [autoprefixer(), cssnano()],
35+
}),
36+
copy({
37+
targets: [{ src: "src/index.scss", dest: "dist" }],
38+
}),
39+
],
4140
})
4241

43-
const configs = ["index"].map(filename =>
44-
createConfig(filename)
45-
)
46-
47-
export default configs
42+
export default function makeConfig(commandOptions) {
43+
const { inputs = ["index"] } = JSON.parse(
44+
fs.readFileSync("package.json", "utf8")
45+
)
46+
return inputs.map(createConfig)
47+
}

0 commit comments

Comments
 (0)