diff --git a/packages/docusaurus-theme-classic/src/theme/MDXComponents/Code.tsx b/packages/docusaurus-theme-classic/src/theme/MDXComponents/Code.tsx index 97e1ac4d8dd7..6eab58ba4de9 100644 --- a/packages/docusaurus-theme-classic/src/theme/MDXComponents/Code.tsx +++ b/packages/docusaurus-theme-classic/src/theme/MDXComponents/Code.tsx @@ -10,12 +10,18 @@ import React from 'react'; import CodeBlock from '@theme/CodeBlock'; import type {Props} from '@theme/MDXComponents/Code'; -export default function MDXCode(props: Props): JSX.Element { - const shouldBeInline = React.Children.toArray(props.children).every( - (el) => typeof el === 'string' && !el.includes('\n'), +function shouldBeInline(props: Props) { + return ( + typeof props.children !== 'undefined' && + React.Children.toArray(props.children).every( + (el) => typeof el === 'string' && !el.includes('\n'), + ) ); +} - return shouldBeInline ? ( +export default function MDXCode(props: Props): JSX.Element { + const inline = shouldBeInline(props); + return inline ? ( ) : ( )} /> diff --git a/packages/docusaurus-theme-live-codeblock/src/theme-live-codeblock.d.ts b/packages/docusaurus-theme-live-codeblock/src/theme-live-codeblock.d.ts index b3670f476ddf..12bd27e8c786 100644 --- a/packages/docusaurus-theme-live-codeblock/src/theme-live-codeblock.d.ts +++ b/packages/docusaurus-theme-live-codeblock/src/theme-live-codeblock.d.ts @@ -24,7 +24,8 @@ declare module '@theme/Playground' { type LiveProviderProps = React.ComponentProps; export interface Props extends CodeBlockProps, LiveProviderProps { - children: string; + // Allow empty live playgrounds + children?: string; } export default function Playground(props: LiveProviderProps): JSX.Element; } diff --git a/packages/docusaurus-theme-live-codeblock/src/theme/Playground/index.tsx b/packages/docusaurus-theme-live-codeblock/src/theme/Playground/index.tsx index a55915086532..1f36e69f570c 100644 --- a/packages/docusaurus-theme-live-codeblock/src/theme/Playground/index.tsx +++ b/packages/docusaurus-theme-live-codeblock/src/theme/Playground/index.tsx @@ -120,7 +120,7 @@ export default function Playground({ return (
``` + +## Empty code blocks edge cases + +Empty inline code block: `` + +Single space inline code block: ` ` + +Empty code block + +{/* prettier-ignore */} +``` +``` + +Empty 1 line code block + +``` + +``` + +Empty 2 line code block + +``` + +``` + +Empty live code block + +```js live + +```