Skip to content

Commit d4f4f8c

Browse files
committed
Add config props to components
1 parent f37b4c3 commit d4f4f8c

File tree

8 files changed

+162
-23
lines changed

8 files changed

+162
-23
lines changed

packages/mdx/dev/content/section.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Duis aute irure dolor in reprehenderit in voluptate velit esse cillum [dolore](f
2828

2929
Lorem dolor sit amet, [javascript](focus://index.js#2:3) adipiscing elit, sed do eiusmod [styles](focus://styles.css#2:3) incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
3030

31-
<CH.Code>
31+
<CH.Code lineNumbers={true}>
3232

3333
```js index.js
3434
function lorem(ipsum, dolor) {

packages/mdx/dev/content/test.mdx

Lines changed: 117 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,124 @@
1-
Lorem ipsum dolor sit amet. _`console.log("foo bar")`_.
1+
Annotations allow you to change how the code is displayed and also to add interactivity.
22

3-
Lorem ipsum dolor sit amet. `console.log("foo bar")`.
3+
<CH.Spotlight lineNumbers={true}>
44

5-
_`console.log("foo bar")`_
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)
13+
```
14+
````
615

7-
_`console.log("foo bar")`_
16+
There are two ways to add annotations:
817

9-
X <CH.InlineCode>console.log("foo bar")</CH.InlineCode>
18+
---
1019

11-
Lorem ipsum `var x = 1`
20+
```mdx mark=1[7:13],5[1:7] focus=1
1221

13-
_Lorem ipsum `var x = 1`_
22+
```
1423

15-
**`console.log("foo bar")`**
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/mdx-client/code.tsx

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react"
2-
import { CodeSpring } from "../smooth-code"
2+
import { CodeConfig, CodeSpring } from "../smooth-code"
33
import {
44
EditorSpring,
55
EditorProps,
@@ -20,23 +20,29 @@ export function Code(
2020
return <InnerCode {...step} onTabClick={onTabClick} />
2121
}
2222

23-
export function InnerCode({
24-
onTabClick,
25-
...props
26-
}: EditorProps & {
27-
onTabClick?: (filename: string) => void
28-
} & Partial<CodeHikeConfig>) {
23+
// build the CodeConfig from props and props.codeConfig
24+
export function mergeCodeConfig<T>(
25+
props: Partial<CodeConfig> & {
26+
codeConfig: Partial<CodeConfig>
27+
} & T
28+
) {
2929
const {
3030
lineNumbers,
3131
showCopyButton,
3232
showExpandButton,
33-
className,
34-
style,
35-
...editorProps
33+
minZoom,
34+
maxZoom,
35+
...rest
3636
} = props
37-
3837
const codeConfig = {
3938
...props.codeConfig,
39+
maxZoom:
40+
maxZoom == null ? props.codeConfig?.maxZoom : maxZoom,
41+
minZoom:
42+
minZoom == null ? props.codeConfig?.minZoom : minZoom,
43+
horizontalCenter:
44+
props.codeConfig?.horizontalCenter ??
45+
props.horizontalCenter,
4046
lineNumbers:
4147
lineNumbers == null
4248
? props.codeConfig?.lineNumbers
@@ -50,6 +56,17 @@ export function InnerCode({
5056
? props.codeConfig?.showExpandButton
5157
: showExpandButton,
5258
}
59+
return { ...rest, codeConfig }
60+
}
61+
62+
export function InnerCode({
63+
onTabClick,
64+
...props
65+
}: EditorProps & {
66+
onTabClick?: (filename: string) => void
67+
} & Partial<CodeHikeConfig>) {
68+
const { className, style, codeConfig, ...editorProps } =
69+
mergeCodeConfig(props)
5370

5471
if (
5572
!props.southPanel &&

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export function Scrollycoding({
1111
codeConfig,
1212
presetConfig,
1313
start = 0,
14+
...rest
1415
}: {
1516
children: React.ReactNode
1617
editorSteps: EditorStep[]
@@ -88,10 +89,11 @@ export function Scrollycoding({
8889
</div>
8990
<div className="ch-scrollycoding-sticker">
9091
<InnerCode
92+
showExpandButton={true}
93+
{...rest}
9194
{...(tab as any)}
9295
codeConfig={codeConfig}
9396
onTabClick={onTabClick}
94-
showExpandButton={true}
9597
/>
9698
{presetConfig && (
9799
<Preview

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,21 @@ export function Section({
6363
)
6464
}
6565

66-
export function SectionCode() {
66+
export function SectionCode(innerProps) {
6767
const { props, setFocus } =
6868
React.useContext(SectionContext)
6969

7070
const onTabClick = (filename: string) => {
7171
setFocus({ fileName: filename, focus: null, id: "" })
7272
}
7373

74-
return <InnerCode {...props} onTabClick={onTabClick} />
74+
return (
75+
<InnerCode
76+
{...innerProps}
77+
{...props}
78+
onTabClick={onTabClick}
79+
/>
80+
)
7581
}
7682

7783
// ---

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export function Slideshow({
99
codeConfig,
1010
presetConfig,
1111
code,
12+
...rest
1213
}: {
1314
children: React.ReactNode
1415
editorSteps: EditorStep[]
@@ -45,6 +46,7 @@ export function Slideshow({
4546
>
4647
<div className="ch-slideshow-slide">
4748
<InnerCode
49+
{...rest}
4850
{...(tab as any)}
4951
codeConfig={{
5052
...codeConfig,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export function Spotlight({
99
codeConfig,
1010
start = 0,
1111
presetConfig,
12+
...rest
1213
}: {
1314
children: React.ReactNode
1415
editorSteps: EditorStep[]
@@ -68,6 +69,7 @@ export function Spotlight({
6869
</div>
6970
<div className="ch-spotlight-sticker">
7071
<InnerCode
72+
{...rest}
7173
{...(tab as any)}
7274
codeConfig={codeConfig}
7375
onTabClick={onTabClick}

packages/mdx/src/remark/transform.section.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ async function transformSection(
3434
)
3535
toJSX(editorNode, {
3636
name: "CH.SectionCode",
37+
appendProps: true,
3738
props: {},
3839
})
3940
}

0 commit comments

Comments
 (0)