Skip to content

Commit

Permalink
add parseCodeProps to CodeInline component
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Jan 30, 2025
1 parent b7895d2 commit d5a1b85
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 25 deletions.
18 changes: 18 additions & 0 deletions .changeset/loud-cars-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'renoun': minor
---

Adds a `parseCodeProps` helper to the `CodeInline` component making it easier to type custom MDX components correctly:

```tsx
import { CodeInline } from 'renoun/components'
import type { MDXComponents } from 'renoun/mdx'

export function useMDXComponents() {
return {
code: (props) => {
return <CodeInline {...CodeInline.parseCodeProps(props)} />
},
} satisfies MDXComponents
}
```
16 changes: 11 additions & 5 deletions apps/site/components/MDXComponents.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { CodeInline, PackageInstall } from 'renoun/components'
import {
CodeBlock as RenounCodeBlock,
CodeInline,
PackageInstall,
} from 'renoun/components'
import type { MDXComponents as MDXComponentsType } from 'renoun/mdx'
import { GeistMono } from 'geist/font/mono'

Expand Down Expand Up @@ -86,10 +90,11 @@ export const MDXComponents = {
)
},
code: (props) => {
const { value, language } = CodeInline.parseCodeProps(props)
return (
<CodeInline
value={props.children as string}
language={props.language}
value={value}
language={language}
paddingY="0"
css={{
lineHeight: 1.15,
Expand All @@ -100,7 +105,8 @@ export const MDXComponents = {
/>
)
},
pre: ({ children, ...restProps }) => {
return <CodeBlock shouldFormat={false} {...restProps} />
pre: (props) => {
const parsedProps = RenounCodeBlock.parsePreProps(props)
return <CodeBlock shouldFormat={false} {...parsedProps} />
},
} satisfies MDXComponentsType
5 changes: 2 additions & 3 deletions apps/site/guides/02.next.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,10 @@ import type { MDXComponents } from 'renoun/mdx'
export function useMDXComponents() {
return {
pre: (props) => {
const { value, language } = CodeBlock.parsePreProps(props)
return <CodeBlock allowErrors value={value} language={language} />
return <CodeBlock {...CodeBlock.parsePreProps(props)} />
},
code: (props) => {
return <CodeInline value={props.children} language="typescript" />
return <CodeInline {...CodeInline.parseCodeProps(props)} />
},
} satisfies MDXComponents
}
Expand Down
5 changes: 2 additions & 3 deletions examples/blog/mdx-components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import type { MDXComponents } from 'renoun/mdx'
export function useMDXComponents() {
return {
code: (props) => {
return (
<CodeInline value={props.children as string} language="typescript" />
)
const { value, language } = CodeInline.parseCodeProps(props)
return <CodeInline value={value} language={language} />
},
pre: (props) => {
const { value, language } = CodeBlock.parsePreProps(props)
Expand Down
7 changes: 2 additions & 5 deletions examples/design-system/mdx-components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import type { MDXComponents } from 'renoun/mdx'
export function useMDXComponents() {
return {
code: (props) => {
return (
<CodeInline value={props.children as string} language="typescript" />
)
return <CodeInline {...CodeInline.parseCodeProps(props)} />
},
pre: (props) => {
const { value, language } = CodeBlock.parsePreProps(props)
return <CodeBlock value={value} language={language} />
return <CodeBlock {...CodeBlock.parsePreProps(props)} />
},
} satisfies MDXComponents
}
3 changes: 1 addition & 2 deletions packages/renoun/src/components/APIReference.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import type { MDXComponents } from '../mdx/index.js'

const mdxComponents = {
pre: (props) => {
const { value, language } = CodeBlock.parsePreProps(props)
return <CodeBlock allowErrors value={value} language={language} />
return <CodeBlock {...CodeBlock.parsePreProps(props)} />
},
code: (props) => {
return <CodeInline value={props.children} language="typescript" />
Expand Down
15 changes: 10 additions & 5 deletions packages/renoun/src/components/CodeBlock/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,11 @@ const languageKey = 'language-'
const languageLength = languageKey.length

/** Parses the props of an MDX `pre` element for passing to `CodeBlock`. */
CodeBlock.parsePreProps = (
props: React.ComponentProps<NonNullable<MDXComponents['pre']>>
) => {
const code = props.children as React.ReactElement<{
CodeBlock.parsePreProps = ({
children,
...props
}: React.ComponentProps<NonNullable<MDXComponents['pre']>>) => {
const code = children as React.ReactElement<{
className: `language-${string}`
children: string
}>
Expand All @@ -481,7 +482,11 @@ CodeBlock.parsePreProps = (
language: (languageClassName
? languageClassName.slice(languageLength)
: 'plain') as Languages,
}
...props,
} as {
value: string
language?: Languages
} & Omit<React.ComponentProps<NonNullable<MDXComponents['pre']>>, 'children'>
}

const StyledContainer = styled('div')
Expand Down
15 changes: 15 additions & 0 deletions packages/renoun/src/components/CodeInline.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Fragment, Suspense } from 'react'
import { css, styled, type CSSObject } from 'restyle'

import type { MDXComponents } from '../mdx/index.js'
import { analyzeSourceText } from '../project/client.js'
import type { Languages } from '../utils/get-language.js'
import { getThemeColors } from '../utils/get-theme-colors.js'
Expand Down Expand Up @@ -179,3 +180,17 @@ export function CodeInline({
</Suspense>
)
}

/** Parses the props of an MDX `code` element for passing to `CodeInline`. */
CodeInline.parseCodeProps = ({
children,
...props
}: React.ComponentProps<NonNullable<MDXComponents['code']>>) => {
return {
value: (children as string).trim(),
...props,
} as {
value: string
language?: Languages
} & Omit<React.ComponentProps<NonNullable<MDXComponents['code']>>, 'children'>
}
3 changes: 1 addition & 2 deletions packages/renoun/src/file-system/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ export { Repository } from './Repository.js'

const mdxComponents = {
pre: (props) => {
const { value, language } = CodeBlock.parsePreProps(props)
return <CodeBlock value={value} language={language} />
return <CodeBlock {...CodeBlock.parsePreProps(props)} />
},
code: (props) => {
return <CodeInline value={props.children} language="typescript" />
Expand Down

0 comments on commit d5a1b85

Please sign in to comment.