Skip to content

Commit 5f686f9

Browse files
committed
Support multiline links
1 parent 28485e2 commit 5f686f9

File tree

3 files changed

+29
-128
lines changed

3 files changed

+29
-128
lines changed

packages/mdx/dev/content/test.mdx

Lines changed: 9 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,10 @@
1-
Annotations allow you to change how the code is displayed and also to add interactivity.
2-
3-
<CH.Spotlight lineNumbers={true}>
4-
5-
````mdx mark=1[7:13],5[1:7]
6-
```py focus=2
7-
print(1)
8-
print(2)
9-
print(3)
10-
# focus
11-
print(4)
12-
print(5)
1+
```js
2+
foo
3+
// link(1:2) https://codehike.org
4+
const hi = 'hi'
5+
const hi = 'hi'
6+
const hi = 'hi'
7+
// link[2:18] https://codehike.org
8+
hello world
9+
hello
1310
```
14-
````
15-
16-
There are two ways to add annotations:
17-
18-
---
19-
20-
```mdx mark=1[7:13],5[1:7] focus=1
21-
22-
```
23-
24-
- using the codeblock metastring
25-
26-
---
27-
28-
```mdx mark=1[7:13],5[1:7] focus=5
29-
30-
```
31-
32-
- using comments inside the code
33-
34-
</CH.Spotlight>
35-
36-
_`focus`_ is only one of the possible annotations. The full list is:
37-
38-
- _`focus`_: keep the targeted code bright while dimming the rest
39-
- _`mark`_: add a background color to the targeted tokens
40-
- _`link`_: add links inside the code
41-
42-
First let's see the syntax to target different parts of the code.
43-
44-
## Targeting lines and columns
45-
46-
<CH.Spotlight maxZoom={1.5} lineNumbers={true} >
47-
48-
<CH.Code lineNumbers={true}>
49-
50-
```text
51-
123456789
52-
123456789
53-
123456789
54-
123456789
55-
123456789
56-
123456789
57-
123456789
58-
```
59-
60-
</CH.Code>
61-
62-
---
63-
64-
```text focus=2
65-
123456789
66-
123456789
67-
123456789
68-
123456789
69-
123456789
70-
123456789
71-
123456789
72-
```
73-
74-
To select a specific line, use the line number:
75-
_`focus=2`_
76-
77-
---
78-
79-
```text focus=3:5
80-
123456789
81-
123456789
82-
123456789
83-
123456789
84-
123456789
85-
123456789
86-
123456789
87-
```
88-
89-
To select a range of lines use a colon:
90-
_`focus=3:5`_
91-
92-
---
93-
94-
```text focus=5[3:6]
95-
123456789
96-
123456789
97-
123456789
98-
123456789
99-
123456789
100-
123456789
101-
123456789
102-
```
103-
104-
Select a range of column from a line using brackets:
105-
_`focus=5[3:6]`_
106-
107-
---
108-
109-
```text focus=1,3:5,7[1:4,7:9]
110-
123456789
111-
123456789
112-
123456789
113-
123456789
114-
123456789
115-
123456789
116-
123456789
117-
```
118-
119-
Combine selectors using a comma separated list:
120-
_`focus=1,3:5,7[1:4,7:9]`_
121-
122-
</CH.Spotlight>
123-
124-
## Comment syntax

packages/mdx/src/index.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,15 @@
6666
position: absolute;
6767
left: 0;
6868
}
69+
70+
.ch-code-inline-link {
71+
text-decoration: underline;
72+
text-decoration-style: dotted;
73+
color: inherit;
74+
}
75+
76+
.ch-code-link :not(span) > span {
77+
text-decoration: underline;
78+
text-decoration-style: dotted;
79+
color: inherit;
80+
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ function Label({
196196

197197
function CodeLink({
198198
children,
199+
isInline,
200+
style,
199201
data,
200202
}: {
201203
data:
@@ -205,18 +207,19 @@ function CodeLink({
205207
}
206208
| string
207209
children: React.ReactNode
210+
isInline: boolean
211+
style?: React.CSSProperties
208212
}) {
209213
const url = (data as any)?.url || data
210214
const title = (data as any)?.title
211215
return (
212216
<a
213217
href={url}
214218
title={title}
215-
style={{
216-
textDecoration: "underline",
217-
textDecorationStyle: "dotted",
218-
color: "inherit",
219-
}}
219+
className={
220+
isInline ? "ch-code-inline-link" : "ch-code-link"
221+
}
222+
style={style}
220223
>
221224
{children}
222225
</a>

0 commit comments

Comments
 (0)