Skip to content

Commit 6e6e539

Browse files
committed
Support more props in CH.Code
1 parent 71ab3ca commit 6e6e539

File tree

3 files changed

+88
-8
lines changed

3 files changed

+88
-8
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
`<CH.Code lineNumbers showCopyButton={false} className="foobar">`
2+
3+
<CH.Code lineNumbers showCopyButton={false} className="foobar">
4+
5+
```js
6+
console.log(1)
7+
```
8+
9+
</CH.Code>
10+
11+
`<CH.Code lineNumbers={false} showCopyButton={true} className="foo">`
12+
13+
<CH.Code lineNumbers={false} showCopyButton={true} className="foo">
14+
15+
```js index.js
16+
console.log(2)
17+
```
18+
19+
```python foo.py
20+
print(3)
21+
```
22+
23+
---
24+
25+
```html index.html
26+
<div>Hi</div>
27+
```
28+
29+
</CH.Code>
30+
31+
`<CH.Code lineNumbers={false} showCopyButton={true} className="foo">`
32+
33+
<CH.Code className="foobarbazbarbar" style={{border: "2px solid red", height: 160}}>
34+
35+
```js index.js
36+
console.log(2)
37+
console.log(2)
38+
console.log(2)
39+
console.log(2)
40+
console.log(2)
41+
console.log(2)
42+
console.log(2)
43+
console.log(2)
44+
console.log(2)
45+
console.log(2)
46+
```
47+
48+
</CH.Code>

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

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import {
55
EditorProps,
66
EditorStep,
77
} from "../mini-editor"
8+
import { CodeHikeConfig } from "remark/config"
89

9-
export function Code(props: EditorProps) {
10+
export function Code(
11+
props: EditorProps & Partial<CodeHikeConfig>
12+
) {
1013
const [step, setStep] = React.useState(props)
1114

1215
function onTabClick(filename: string) {
@@ -22,29 +25,57 @@ export function InnerCode({
2225
...props
2326
}: EditorProps & {
2427
onTabClick?: (filename: string) => void
25-
}) {
28+
} & Partial<CodeHikeConfig>) {
29+
const {
30+
lineNumbers,
31+
showCopyButton,
32+
className,
33+
style,
34+
...editorProps
35+
} = props
36+
37+
const codeConfig = {
38+
...props.codeConfig,
39+
lineNumbers,
40+
showCopyButton,
41+
}
42+
2643
if (
2744
!props.southPanel &&
2845
props.files.length === 1 &&
2946
!props.files[0].name
3047
) {
3148
return (
32-
<div className="ch-codeblock not-prose">
49+
<div
50+
className={`ch-codeblock not-prose ${
51+
className || ""
52+
}`}
53+
style={style}
54+
>
3355
<CodeSpring
3456
className="ch-code"
35-
config={props.codeConfig}
36-
step={props.files[0]}
57+
config={codeConfig}
58+
step={editorProps.files[0]}
3759
/>
3860
</div>
3961
)
4062
} else {
4163
const frameProps = {
42-
...props?.frameProps,
64+
...editorProps?.frameProps,
4365
onTabClick,
4466
}
4567
return (
46-
<div className="ch-codegroup not-prose">
47-
<EditorSpring {...props} frameProps={frameProps} />
68+
<div
69+
className={`ch-codegroup not-prose ${
70+
className || ""
71+
}`}
72+
style={style}
73+
>
74+
<EditorSpring
75+
{...editorProps}
76+
frameProps={frameProps}
77+
codeConfig={codeConfig}
78+
/>
4879
</div>
4980
)
5081
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ async function transformCode(
3232
toJSX(nodeInfo.node, {
3333
name: "CH.Code",
3434
props: await mapAnyCodeNode(nodeInfo, config),
35+
appendProps: true,
3536
addConfigProp: true,
3637
})
3738
}

0 commit comments

Comments
 (0)