diff --git a/packages/docusaurus-theme-classic/src/theme/MDXComponents/Code.tsx b/packages/docusaurus-theme-classic/src/theme/MDXComponents/Code.tsx
index 2055b2916728..7494bf430e63 100644
--- a/packages/docusaurus-theme-classic/src/theme/MDXComponents/Code.tsx
+++ b/packages/docusaurus-theme-classic/src/theme/MDXComponents/Code.tsx
@@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
-import type {ComponentProps, ReactNode} from 'react';
+import type {ComponentProps, ReactElement, ReactNode} from 'react';
import React from 'react';
import CodeBlock from '@theme/CodeBlock';
import CodeInline from '@theme/CodeInline';
@@ -15,13 +15,30 @@ function shouldBeInline(props: Props) {
return (
// empty code blocks have no props.children,
// see https://github.com/meta/docusaurus/pull/9704
- typeof props.children !== 'undefined' &&
- React.Children.toArray(props.children).every(
- (el) => typeof el === 'string' && !el.includes('\n'),
- )
+ typeof props.children !== 'undefined' && isSingleLine(props.children)
);
}
+function isSingleLine(children: ReactNode): boolean {
+ return React.Children.toArray(children).every(isSingleLineChild);
+}
+
+// CommonMark raw HTML can nest elements in inline code,
+// like a b
+// See https://github.com/meta/docusaurus/issues/10533
+function isSingleLineChild(child: ReactNode): boolean {
+ if (typeof child === 'string') {
+ return !child.includes('\n');
+ }
+ return isHtmlElement(child) && isSingleLine(child.props.children);
+}
+
+function isHtmlElement(
+ child: ReactNode,
+): child is ReactElement<{children?: ReactNode}, string> {
+ return React.isValidElement(child) && typeof child.type === 'string';
+}
+
export default function MDXCode(props: Props): ReactNode {
return shouldBeInline(props) ? (
diff --git a/website/_dogfooding/_pages tests/_code-md.md b/website/_dogfooding/_pages tests/_code-md.md
new file mode 100644
index 000000000000..631db48ccfe9
--- /dev/null
+++ b/website/_dogfooding/_pages tests/_code-md.md
@@ -0,0 +1,13 @@
+This content comes from a CommonMark (`.md`) partial, where raw HTML `` elements are rendered with the `MDXComponents/Code` component.
+
+Inline code with elements stays inline: git clone repository, x y z and emphasis.
+
+Inline code with a string stays inline: inline text.
+
+Multi-line code with elements renders as a code block:
+
+
+ Front page
+
+ Input: a = "abcd"
+
diff --git a/website/_dogfooding/_pages tests/code-block-tests.mdx b/website/_dogfooding/_pages tests/code-block-tests.mdx
index 8812fa048fe3..decb6ccc1674 100644
--- a/website/_dogfooding/_pages tests/code-block-tests.mdx
+++ b/website/_dogfooding/_pages tests/code-block-tests.mdx
@@ -147,6 +147,12 @@ Multi-line text inside `pre` will turn into one-liner, but it's okay (https://gi
+### `code` in CommonMark
+
+import CodeCommonMark from './_code-md.md';
+
+
+
## `CodeBlock`
### `CodeBlock > string`