Skip to content

Commit a0a521b

Browse files
committed
Fix code block syntax colors broken by MDX 2 bump
1 parent 76fda9b commit a0a521b

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ dist
88
coverage
99
examples/next/.next
1010
docs/src/pages/index.mdx
11+
12+
# Prettier doesn't know how to format MDX2 and makes silly mistakes.
13+
*.mdx

packages/docs/src/components/code.js renamed to packages/docs/src/components/code.tsx

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// @ts-check
22
import { Text } from 'theme-ui'
33
import { Themed } from '@theme-ui/mdx'
4-
import Prism from '@theme-ui/prism'
4+
import Prism, { ThemeUIPrismProps } from '@theme-ui/prism'
55
import { LiveProvider, LiveEditor, LivePreview, LiveError } from 'react-live'
66
import * as themeUI from 'theme-ui'
7+
import { ComponentPropsWithoutRef } from 'react'
78

89
const posts = [
910
{
@@ -117,38 +118,50 @@ export const LiveCode = ({ children, preview, xray }) => {
117118
)
118119
}
119120

120-
/**
121-
* @param {{
122-
* live?: boolean;
123-
* filename?: string;
124-
* } | import("react").ComponentProps<typeof LiveCode>
125-
* | import('@theme-ui/prism').ThemeUIPrismProps} props
126-
*/
127-
const CodeBlock = (props) => {
121+
type LiveCodeBlockProps = {
122+
live: true
123+
} & ComponentPropsWithoutRef<typeof LiveCode>
124+
type UsualCodeBlockProps = {
125+
live?: false
126+
filename?: string
127+
} & ThemeUIPrismProps
128+
type CodeBlockProps = LiveCodeBlockProps | UsualCodeBlockProps
129+
130+
const CodeBlock = (props: CodeBlockProps) => {
131+
if (typeof props.children === 'object') {
132+
props = {
133+
...props,
134+
...props.children.props,
135+
}
136+
}
137+
128138
if (props.live) {
129139
return <LiveCode {...props} />
140+
} else {
141+
const { live: _, filename, ...rest } = props as UsualCodeBlockProps
142+
if (filename) {
143+
return (
144+
<section>
145+
<Text
146+
as="span"
147+
sx={{
148+
display: 'block',
149+
bg: 'gray',
150+
color: 'background',
151+
px: 3,
152+
py: 2,
153+
fontWeight: 'bold',
154+
}}
155+
>
156+
{filename}
157+
</Text>
158+
<Prism {...rest} sx={{ mt: 0 }} />
159+
</section>
160+
)
161+
} else {
162+
return <Prism {...rest} />
163+
}
130164
}
131-
if (props.filename) {
132-
return (
133-
<section>
134-
<Text
135-
as="span"
136-
sx={{
137-
display: 'block',
138-
bg: 'gray',
139-
color: 'background',
140-
px: 3,
141-
py: 2,
142-
fontWeight: 'bold',
143-
}}
144-
>
145-
{props.filename}
146-
</Text>
147-
<Prism {...props} sx={{ mt: 0 }} />
148-
</section>
149-
)
150-
}
151-
return <Prism {...props} />
152165
}
153166

154167
export default CodeBlock

packages/docs/src/pages/theming.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Beyond these base colors, any additional values can be added to a theme to augme
2424

2525
Multiple color modes, i.e. _dark mode_, can be handled with a nested `modes` object that matches the shape of the default colors.
2626

27-
```js
27+
```tsx
2828
// example colors with dark mode
2929
colors: {
3030
text: '#000',

0 commit comments

Comments
 (0)