From 872fd2e44fb69abc7fd3234a8f5fd3b961ed5db2 Mon Sep 17 00:00:00 2001 From: Vj Date: Fri, 20 Nov 2026 12:20:55 -0500 Subject: [PATCH] fix(theme-classic): render CommonMark inline code with nested HTML elements inline (AI-assisted) In CommonMark docs, raw HTML like a b rendered as a code block inside the paragraph, because MDXComponents/Code only rendered strings inline. Also consider nested HTML elements: code renders inline when all its text is on a single line. Add a CommonMark partial to the code-block-tests dogfood page. Fixes #10533 Co-Authored-By: Claude Opus 5.5 --- .../src/theme/MDXComponents/Code.tsx | 27 +++++++++++++++---- website/_dogfooding/_pages tests/_code-md.md | 13 +++++++++ .../_pages tests/code-block-tests.mdx | 6 +++++ 3 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 website/_dogfooding/_pages tests/_code-md.md 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`