Skip to content

fix: prevent invalid HTML nesting from fenced code blocks breaking layout#8302

Open
octo-patch wants to merge 1 commit intoblock:mainfrom
octo-patch:fix/issue-8290-fenced-code-block-layout
Open

fix: prevent invalid HTML nesting from fenced code blocks breaking layout#8302
octo-patch wants to merge 1 commit intoblock:mainfrom
octo-patch:fix/issue-8290-fenced-code-block-layout

Conversation

@octo-patch
Copy link
Copy Markdown

Fixes #8290

Problem

In react-markdown v10, fenced code blocks with a language specifier (e.g. ```bash) are handled by the custom MarkdownCode component, which renders a CodeBlock — a <div>-based component using SyntaxHighlighter with PreTag="div".

However, react-markdown wraps all block code elements with a default <pre> element when components.pre is not customized. This creates invalid HTML:

<pre>      <!-- react-markdown default -->
  <div>  <!-- CodeBlock / SyntaxHighlighter with PreTag="div" -->
    <code>...</code>
  </div>
</pre>

Browsers auto-correct <div> inside <pre> by moving the <div> outside the <pre>, which disrupts the layout of surrounding text content. This explains why fenced code blocks sometimes break the message layout — the issue appears whenever a response contains code blocks with a language specifier.

Solution

Add pre: ({ children }) => <>{children}</> to the components prop of ReactMarkdown. This strips the default <pre> wrapper, letting the CodeBlock component render its own block-level presentation (via SyntaxHighlighter with PreTag="div") without being wrapped in an invalid <pre> element.

Testing

  • Added two new tests to MarkdownContent.test.tsx:
    1. Verifies no <pre> elements remain in the DOM for a single code block
    2. Verifies multiple fenced code blocks render correctly alongside surrounding text (reproducing the reporter's exact example)
  • All 26 existing tests continue to pass

…yout (fixes block#8290)

In react-markdown v10, block code with syntax highlighting (CodeBlock, which
renders as a <div>) gets wrapped by react-markdown default <pre> element,
creating invalid HTML: <pre><div>...</div></pre>.

Browsers auto-correct this by moving the <div> outside <pre>, which disrupts
the layout of surrounding text content. This is why fenced code blocks
occasionally break the message layout in the Desktop UI.

Fix: add pre passthrough to the ReactMarkdown components to strip the default
<pre> wrapper. The CodeBlock component handles its own block-level presentation
via SyntaxHighlighter with PreTag=div.
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 804b0fbe26

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

/>
);
},
pre: ({ children }) => <>{children}</>,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve <pre> for code fences without a language tag

Unwrapping every pre node here breaks fenced code blocks that omit a language specifier. MarkdownCode only uses CodeBlock when className matches language-*, so no-language fences fall back to a plain <code> element; with this unconditional pre removal, those blocks lose their block container and render like inline code (no block layout/overflow behavior). This is a functional regression for common markdown like triple backticks without a language.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Desktop markdown rendering bug: fenced code blocks can break message layout

1 participant