Skip to content

Commit b29057f

Browse files
authoredJun 20, 2022
Merge pull request #203 from code-hike/stable-annotations
Stable annotations
2 parents 9c5922c + 603da42 commit b29057f

File tree

7 files changed

+45
-131
lines changed

7 files changed

+45
-131
lines changed
 

‎packages/mdx/dev/content/test.mdx

+9-123
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

+12
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

+8-5
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>

‎packages/mdx/src/smooth-code/annotations.tsx

+11-2
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ function annotateMultilineSide(
205205
while (lineIndex < lines.length) {
206206
const annotation = annotations[0]
207207
let line = lines[lineIndex]
208+
if (
209+
annotation &&
210+
getLineNumber(line) > annotation.lineNumbers.start
211+
) {
212+
throw "Code Hike can't handle two annotations for the same line"
213+
}
208214
if (
209215
annotation &&
210216
getLineNumber(line) === annotation.lineNumbers.start
@@ -505,8 +511,11 @@ function shiftGroups(
505511
): TokenGroup[] {
506512
const removedGroups = [] as TokenGroup[]
507513
let currentStartColumn = startColumn
508-
while (currentStartColumn < newStartColumn) {
509-
const currentTokenGroup = tokenGroups.shift()!
514+
while (
515+
currentStartColumn < newStartColumn &&
516+
tokenGroups.length > 0
517+
) {
518+
const currentTokenGroup = tokenGroups.shift()
510519
removedGroups.push(currentTokenGroup)
511520
const length = currentTokenGroup.tokens.reduce(
512521
(a, t) => a + t.content.length,

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

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type CodeAnnotation = {
2424
children: React.ReactNode
2525
data: any
2626
theme: EditorTheme
27+
isInline: boolean
2728
}) => React.ReactElement
2829
data?: any
2930
}

‎playground/src/app.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function App() {
5454
<CodeHikeLogo />
5555
<h1>
5656
Code Hike
57-
<span>v0.6.0</span>
57+
<span>v{__APP_VERSION__}</span>
5858
</h1>
5959
</a>
6060
<a href="https://codehike.org/docs">Docs</a>

‎playground/vite.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ import react from "@vitejs/plugin-react";
44
// https://vitejs.dev/config/
55
export default defineConfig({
66
plugins: [react()],
7+
define: {
8+
__APP_VERSION__: JSON.stringify(process.env.npm_package_version),
9+
},
710
});

0 commit comments

Comments
 (0)