Skip to content

Commit 1351c85

Browse files
committed
Parse mdxJsxAttributeValueExpression
1 parent 5562b2f commit 1351c85

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

packages/mdx/src/annotations.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,16 @@ function Box({
1414
data,
1515
theme,
1616
}: {
17-
data: {
18-
url: string
19-
title: string | undefined
20-
}
17+
data: any
2118
children: React.ReactNode
2219
theme: any
2320
}) {
2421
const border =
25-
data ||
26-
theme.tokenColors.find((tc: any) =>
27-
tc.scope?.includes("string")
28-
)?.settings?.foreground ||
29-
"yellow"
22+
typeof data === "string"
23+
? data
24+
: theme.tokenColors.find((tc: any) =>
25+
tc.scope?.includes("string")
26+
)?.settings?.foreground || "yellow"
3027
return (
3128
<span style={{ outline: `2px solid ${border}` }}>
3229
{children}

packages/mdx/src/to-estree.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ export function valueToEstree(
187187
return (mdastToEstree(tree) as any).body[0].expression
188188
}
189189

190+
if (
191+
(value as any)?.type ===
192+
"mdxJsxAttributeValueExpression"
193+
) {
194+
return (value as any).data.estree.body[0].expression
195+
}
196+
190197
return {
191198
type: "ObjectExpression",
192199
// @ts-expect-error: looks like an object.

packages/playground/content/custom-annotations.mdx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { MyTooltipAnnotation } from "./MyTooltipAnnotation"
2+
13
# JSX Annotations
24

35
```js
@@ -13,9 +15,4 @@ function adipiscing(...elit) {
1315
}
1416
```
1517

16-
<CH.Annotation
17-
as="link"
18-
focus="4[16:26]"
19-
url="https://github.com/code-hike"
20-
title="Code Hike"
21-
/>
18+
<CH.Annotation as={MyTooltipAnnotation} focus="4[16:26]" />
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
export function MyTooltipAnnotation() {
2-
return "I'm a tooltip"
1+
export function MyTooltipAnnotation({
2+
children,
3+
data,
4+
theme,
5+
}) {
6+
const border =
7+
typeof data === "string"
8+
? data
9+
: theme.tokenColors.find(tc =>
10+
tc.scope?.includes("string")
11+
)?.settings?.foreground || "yellow"
12+
return (
13+
<span style={{ outline: `2px solid ${border}` }}>
14+
{children}
15+
</span>
16+
)
317
}

packages/playground/src/page-data.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export async function toProps({ demo, theme }) {
6262
console.error("remark-code-hike error", e)
6363
const errorBundle = await bundle(
6464
`### remark-code-hike error \n ~~~\n${e.toString()}\n~~~`,
65+
undefined,
6566
[]
6667
)
6768

0 commit comments

Comments
 (0)