Skip to content

Commit

Permalink
add shouldFormat prop
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Nov 18, 2024
1 parent eab583f commit e71de2f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
15 changes: 15 additions & 0 deletions .changeset/khaki-rice-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'renoun': minor
---

Adds `shouldFormat` prop to `CodeBlock` component to allow disabling code formatting. This is useful for MDX code blocks that are already formatted by an IDE or CI environment.

```tsx
export function useMDXComponents() {
return {
pre: (props) => {
return <CodeBlock shouldFormat={false} {...restProps} />
},
}
}
```
2 changes: 1 addition & 1 deletion apps/site/components/MDXComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ export const MDXComponents = {
)
},
pre: ({ children, ...restProps }) => {
return <CodeBlock {...restProps} />
return <CodeBlock shouldFormat={false} {...restProps} />
},
} satisfies MDXComponentsType
5 changes: 5 additions & 0 deletions packages/renoun/src/components/CodeBlock/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export type BaseCodeBlockProps = {
/** Show or hide the toolbar. */
showToolbar?: boolean

/** Whether or not to format the source code using `prettier` if installed. */
shouldFormat?: boolean

/** CSS styles to apply to code block elements. */
css?: {
container?: CSSObject
Expand Down Expand Up @@ -106,6 +109,7 @@ async function CodeBlockAsync({
showErrors,
showLineNumbers,
showToolbar,
shouldFormat,
...props
}: CodeBlockProps) {
const containerPadding = computeDirectionalStyles(
Expand Down Expand Up @@ -138,6 +142,7 @@ async function CodeBlockAsync({
language,
allowErrors,
showErrors,
shouldFormat,
...options,
})
const contextValue = {
Expand Down
7 changes: 4 additions & 3 deletions packages/renoun/src/components/CodeInline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export type CodeInlineProps = {
/** Show or hide a persistent button that copies the `value` to the clipboard. */
allowCopy?: boolean

/** Whether or not to allow errors. Accepts a boolean or comma-separated list of allowed error codes. */
allowErrors?: boolean | string

/** Horizontal padding to apply to the wrapping element. */
paddingX?: string

Expand All @@ -31,9 +34,6 @@ export type CodeInlineProps = {

/** Style to apply to the wrapping element. */
style?: React.CSSProperties

/** Whether or not to allow errors. Accepts a boolean or comma-separated list of allowed error codes. */
allowErrors?: boolean | string
}

function Token({ token }: { token: Token }) {
Expand Down Expand Up @@ -69,6 +69,7 @@ async function CodeInlineAsync({
}: CodeInlineProps) {
const { tokens } = await analyzeSourceText({
isInline: true,
shouldFormat: false,
value,
language,
allowErrors,
Expand Down
9 changes: 5 additions & 4 deletions packages/renoun/src/utils/parse-source-text-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type BaseParseMetadataOptions = {
filename?: string
language?: Languages
allowErrors?: boolean | string
shouldFormat?: boolean
isInline?: boolean
}

Expand Down Expand Up @@ -43,6 +44,7 @@ export async function parseSourceTextMetadata({
filename: filenameProp,
language,
allowErrors = false,
shouldFormat = true,
isInline = false,
...props
}: ParseMetadataOptions): Promise<ParseMetadataResult> {
Expand Down Expand Up @@ -99,7 +101,6 @@ export async function parseSourceTextMetadata({
const isJavaScriptLikeLanguage = ['js', 'jsx', 'ts', 'tsx'].includes(
finalLanguage
)
const isHtmlLanguage = 'html' === finalLanguage
const jsxOnly = isJavaScriptLikeLanguage ? isJsxOnly(finalValue) : false
let filename = 'source' in props ? props.source : filenameProp
let sourceFile: SourceFile | undefined
Expand All @@ -109,12 +110,12 @@ export async function parseSourceTextMetadata({
isGeneratedFilename = true
}

// Format JavaScript and HTML code blocks.
if (isJavaScriptLikeLanguage || isHtmlLanguage) {
// Format source text if enabled.
if (shouldFormat) {
try {
finalValue = await formatSourceText(filename, finalValue)
} catch (error) {
console.log(
throw new Error(
`[renoun] Error formatting "${componentName}" source text${filename ? ` at filename "${filename}"` : ''} ${error}`
)
}
Expand Down

0 comments on commit e71de2f

Please sign in to comment.