Skip to content

Commit d35381d

Browse files
committed
Add mark annotation
1 parent 224fea3 commit d35381d

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

packages/mdx/src/client/annotations.tsx

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import React from "react"
22
import { CodeAnnotation } from "@code-hike/smooth-code"
3+
import {
4+
getColor,
5+
transparent,
6+
ColorName,
7+
} from "@code-hike/utils"
38

49
export function Annotation() {
510
return "error: code hike remark plugin not running or annotation isn't at the right place"
@@ -13,6 +18,64 @@ export const annotationsMap: Record<
1318
bg: Background,
1419
label: Label,
1520
link: CodeLink,
21+
mark: Mark,
22+
}
23+
24+
function Mark({
25+
children,
26+
data,
27+
theme,
28+
}: {
29+
data: any
30+
children: React.ReactNode
31+
theme: any
32+
}) {
33+
console.log(children)
34+
const bg =
35+
typeof data === "string"
36+
? data
37+
: tryGuessColor(children) ||
38+
transparent(
39+
getColor(theme, ColorName.CodeForeground),
40+
0.2
41+
)
42+
return (
43+
<span
44+
className="ch-code-mark-annotation"
45+
style={{
46+
background: bg,
47+
borderRadius: "0.25rem",
48+
padding: "0.2rem 0.15rem 0.1rem",
49+
margin: "0 -0.15rem",
50+
}}
51+
>
52+
{children}
53+
</span>
54+
)
55+
}
56+
57+
function tryGuessColor(
58+
children: React.ReactNode
59+
): string | undefined {
60+
const child = React.Children.toArray(children)[0] as any
61+
62+
const grandChild = React.Children.toArray(
63+
child?.props?.children || []
64+
)[0] as any
65+
66+
const grandGrandChild = React.Children.toArray(
67+
grandChild?.props?.children || []
68+
)[0] as any
69+
70+
console.log({ grandGrandChild })
71+
72+
const { color } = grandGrandChild?.props?.style
73+
74+
if (color) {
75+
return transparent(color as string, 0.2)
76+
}
77+
78+
return undefined
1679
}
1780

1881
function Box({

packages/playground/content/test.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
# Hello
22

33
Foo bar <CH.InlineCode>Hello</CH.InlineCode> foo bar
4+
5+
```js mark=1[1:12],3[17:20]
6+
function lorem(ipsum, dolor = 1) {
7+
const sit = ipsum == null ? 0 : ipsum.sit
8+
dolor = sit - amet(dolor)
9+
return sit
10+
? consectetur(ipsum, 0, dolor < 0 ? 0 : dolor)
11+
: []
12+
}
13+
```

0 commit comments

Comments
 (0)