Skip to content

Commit 18eafc4

Browse files
committed
Multiline mark
1 parent 6389d8e commit 18eafc4

File tree

5 files changed

+63
-51
lines changed

5 files changed

+63
-51
lines changed

packages/mdx/dev/content/comment-annotations.mdx

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ function adipiscing(...elit) {
2020
console.log("hey")
2121
```
2222

23-
Same with other annotations like `bg` and `box`.
23+
Same with other annotations like `mark` and `box`.
2424

2525
```js
26-
// bg(1:2)
26+
// mark(1:2)
2727
function foo() {
2828
// box[6:9]
2929
console.log("hover me")
3030
return 8
3131
}
3232
```
3333

34-
You can pass a string parameter to comment annotations. For `bg` and `box`, it will be used as a color.
34+
You can pass a string parameter to comment annotations. For `mark` and `box`, it will be used as a color.
3535

3636
```js index.js
3737
function lorem(ipsum, dolor = 1) {
38-
// bg(1:3) linear-gradient(90deg, #020024 0%, #090979 35%, #00d4ff 100%)
38+
// mark(1:3) linear-gradient(90deg, #020024 0%, #090979 35%, #00d4ff 100%)
3939
const sit = ipsum == null && 0
4040
dolor = sit - amet(dolor)
4141
return sit ? consectetur(ipsum) : []

packages/mdx/pages/themes.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const mdx = `
99
1010
<CH.Code>
1111
12-
~~~json package.json
12+
~~~json package.json mark=2[12:23]
1313
{
1414
"name": "package.json"
1515
}
@@ -31,6 +31,7 @@ function PostPage() {
3131
3232
~~~js pages/alpha.ts
3333
function AlphaPage() {
34+
// mark
3435
return 1
3536
}
3637
~~~

packages/mdx/src/mdx-client/annotations.tsx

+53-46
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,64 @@ export const annotationsMap: Record<
1111
CodeAnnotation["Component"]
1212
> = {
1313
box: Box,
14-
bg: Background,
14+
bg: MultilineMark,
1515
label: Label,
1616
link: CodeLink,
1717
mark: Mark,
1818
withClass: WithClass,
1919
}
2020

21-
function Mark({
21+
function Mark(props: any) {
22+
if (props.isInline) {
23+
return <InlineMark {...props} />
24+
} else {
25+
return <MultilineMark {...props} />
26+
}
27+
}
28+
29+
function MultilineMark({
30+
children,
31+
data,
32+
style,
33+
theme,
34+
}: {
35+
data: string
36+
children: React.ReactNode
37+
style?: React.CSSProperties
38+
theme?: any
39+
}) {
40+
const bg =
41+
data ||
42+
(((theme as any).colors[
43+
"editor.lineHighlightBackground"
44+
] ||
45+
(theme as any).colors[
46+
"editor.selectionHighlightBackground"
47+
]) as string)
48+
return (
49+
<div
50+
style={{
51+
...style,
52+
background: bg,
53+
}}
54+
className="ch-code-bg-annotation"
55+
>
56+
<span
57+
className="ch-code-bg-annotation-border"
58+
style={{
59+
background: "#00a2d3",
60+
width: "3px",
61+
height: "100%",
62+
position: "absolute",
63+
left: 0,
64+
}}
65+
/>
66+
{children}
67+
</div>
68+
)
69+
}
70+
71+
function InlineMark({
2272
children,
2373
data,
2474
theme,
@@ -64,7 +114,7 @@ function tryGuessColor(
64114
grandChild?.props?.children || []
65115
)[0] as any
66116

67-
const { color } = grandGrandChild?.props?.style
117+
const { color } = grandGrandChild?.props?.style || {}
68118

69119
if (color) {
70120
return transparent(color as string, 0.2)
@@ -117,49 +167,6 @@ function WithClass({
117167
)
118168
}
119169

120-
function Background({
121-
children,
122-
data,
123-
style,
124-
theme,
125-
}: {
126-
data: string
127-
children: React.ReactNode
128-
style?: React.CSSProperties
129-
theme?: any
130-
}) {
131-
const bg =
132-
data ||
133-
(((theme as any).colors[
134-
"editor.lineHighlightBackground"
135-
] ||
136-
(theme as any).colors[
137-
"editor.selectionHighlightBackground"
138-
]) as string)
139-
return (
140-
<div
141-
style={{
142-
...style,
143-
background: bg,
144-
// cursor: "pointer",
145-
}}
146-
className="ch-code-bg-annotation"
147-
>
148-
<span
149-
className="ch-code-bg-annotation-border"
150-
style={{
151-
background: "#00a2d3",
152-
width: "3px",
153-
height: "100%",
154-
position: "absolute",
155-
left: 0,
156-
}}
157-
/>
158-
{children}
159-
</div>
160-
)
161-
}
162-
163170
function Label({
164171
children,
165172
data,

packages/mdx/src/smooth-code/partial-step-parser.ts

+2
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export type MultiLineAnnotation = {
142142
children: React.ReactNode
143143
data: any
144144
theme: EditorTheme
145+
isInline: boolean
145146
}) => React.ReactElement
146147
}
147148

@@ -155,6 +156,7 @@ export type InlineAnnotation = {
155156
children: React.ReactNode
156157
data: any
157158
theme: EditorTheme
159+
isInline: boolean
158160
}) => React.ReactElement
159161
}
160162

packages/mdx/src/smooth-code/smooth-lines.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ function Lines({
108108
key={i}
109109
data={group.annotation.data}
110110
theme={group.annotation.theme}
111+
isInline={false}
111112
>
112113
<LineGroup
113114
lines={group.lines}
@@ -258,6 +259,7 @@ function AnnotatedTokens({
258259
children={children}
259260
data={annotated?.annotation?.data}
260261
theme={annotated?.annotation?.theme!}
262+
isInline={true}
261263
/>
262264
) : (
263265
<>{children}</>

0 commit comments

Comments
 (0)