Skip to content

Commit

Permalink
extract CodeInline to make it simpler to customize
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Jan 5, 2024
1 parent e5116f5 commit 19e5dad
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
8 changes: 8 additions & 0 deletions packages/docusaurus-theme-classic/src/theme-classic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,14 @@ declare module '@theme/CodeBlock' {
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' {
export interface Props {
readonly code: string;
Expand Down
16 changes: 16 additions & 0 deletions packages/docusaurus-theme-classic/src/theme/CodeInline/index.tsx
Original file line number Diff line number Diff line change
@@ -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} />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import type {ComponentProps} from 'react';
import React from 'react';
import CodeBlock from '@theme/CodeBlock';
import CodeInline from '@theme/CodeInline';
import type {Props} from '@theme/MDXComponents/Code';

function shouldBeInline(props: Props) {
return (
// 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'),
Expand All @@ -20,9 +23,8 @@ function shouldBeInline(props: Props) {
}

export default function MDXCode(props: Props): JSX.Element {
const inline = shouldBeInline(props);
return inline ? (
<code {...props} />
return shouldBeInline(props) ? (
<CodeInline {...props} />
) : (
<CodeBlock {...(props as ComponentProps<typeof CodeBlock>)} />
);
Expand Down

0 comments on commit 19e5dad

Please sign in to comment.