mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 10:20:09 +02:00
fix(theme): allow empty code blocks and live playgrounds (#9704)
This commit is contained in:
parent
ca09f238f3
commit
16500436f7
6 changed files with 70 additions and 7 deletions
|
@ -363,6 +363,14 @@ declare module '@theme/CodeBlock' {
|
||||||
export default function CodeBlock(props: Props): JSX.Element;
|
export default function CodeBlock(props: Props): JSX.Element;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare module '@theme/CodeInline' {
|
||||||
|
import type {ComponentProps} from 'react';
|
||||||
|
|
||||||
|
export interface Props extends ComponentProps<'code'> {}
|
||||||
|
|
||||||
|
export default function CodeInline(props: Props): JSX.Element;
|
||||||
|
}
|
||||||
|
|
||||||
declare module '@theme/CodeBlock/CopyButton' {
|
declare module '@theme/CodeBlock/CopyButton' {
|
||||||
export interface Props {
|
export interface Props {
|
||||||
readonly code: string;
|
readonly code: string;
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import React from 'react';
|
||||||
|
import type {Props} from '@theme/CodeInline';
|
||||||
|
|
||||||
|
// Simple component used to render inline code blocks
|
||||||
|
// its purpose is to be swizzled and customized
|
||||||
|
// MDX 1 used to have a inlineCode comp, see https://mdxjs.com/migrating/v2/
|
||||||
|
export default function CodeInline(props: Props): JSX.Element {
|
||||||
|
return <code {...props} />;
|
||||||
|
}
|
|
@ -8,15 +8,23 @@
|
||||||
import type {ComponentProps} from 'react';
|
import type {ComponentProps} from 'react';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import CodeBlock from '@theme/CodeBlock';
|
import CodeBlock from '@theme/CodeBlock';
|
||||||
|
import CodeInline from '@theme/CodeInline';
|
||||||
import type {Props} from '@theme/MDXComponents/Code';
|
import type {Props} from '@theme/MDXComponents/Code';
|
||||||
|
|
||||||
export default function MDXCode(props: Props): JSX.Element {
|
function shouldBeInline(props: Props) {
|
||||||
const shouldBeInline = React.Children.toArray(props.children).every(
|
return (
|
||||||
(el) => typeof el === 'string' && !el.includes('\n'),
|
// empty code blocks have no props.children,
|
||||||
|
// see https://github.com/facebook/docusaurus/pull/9704
|
||||||
|
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 {
|
||||||
<code {...props} />
|
return shouldBeInline(props) ? (
|
||||||
|
<CodeInline {...props} />
|
||||||
) : (
|
) : (
|
||||||
<CodeBlock {...(props as ComponentProps<typeof CodeBlock>)} />
|
<CodeBlock {...(props as ComponentProps<typeof CodeBlock>)} />
|
||||||
);
|
);
|
||||||
|
|
|
@ -24,7 +24,8 @@ declare module '@theme/Playground' {
|
||||||
type LiveProviderProps = React.ComponentProps<typeof LiveProvider>;
|
type LiveProviderProps = React.ComponentProps<typeof LiveProvider>;
|
||||||
|
|
||||||
export interface Props extends CodeBlockProps, LiveProviderProps {
|
export interface Props extends CodeBlockProps, LiveProviderProps {
|
||||||
children: string;
|
// Allow empty live playgrounds
|
||||||
|
children?: string;
|
||||||
}
|
}
|
||||||
export default function Playground(props: LiveProviderProps): JSX.Element;
|
export default function Playground(props: LiveProviderProps): JSX.Element;
|
||||||
}
|
}
|
||||||
|
|
|
@ -120,7 +120,7 @@ export default function Playground({
|
||||||
return (
|
return (
|
||||||
<div className={styles.playgroundContainer}>
|
<div className={styles.playgroundContainer}>
|
||||||
<LiveProvider
|
<LiveProvider
|
||||||
code={children.replace(/\n$/, '')}
|
code={children?.replace(/\n$/, '')}
|
||||||
noInline={noInline}
|
noInline={noInline}
|
||||||
transformCode={transformCode ?? DEFAULT_TRANSFORM_CODE}
|
transformCode={transformCode ?? DEFAULT_TRANSFORM_CODE}
|
||||||
theme={prismTheme}
|
theme={prismTheme}
|
||||||
|
|
|
@ -458,3 +458,33 @@ See https://github.com/facebook/docusaurus/issues/9517
|
||||||
</head>
|
</head>
|
||||||
</html>
|
</html>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 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
|
||||||
|
|
||||||
|
```
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue