fix: prevent invalid HTML nesting from fenced code blocks breaking layout#8302
fix: prevent invalid HTML nesting from fenced code blocks breaking layout#8302octo-patch wants to merge 1 commit intoblock:mainfrom
Conversation
…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.
There was a problem hiding this comment.
💡 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}</>, |
There was a problem hiding this comment.
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 👍 / 👎.
Fixes #8290
Problem
In react-markdown v10, fenced code blocks with a language specifier (e.g.
```bash) are handled by the customMarkdownCodecomponent, which renders aCodeBlock— a<div>-based component usingSyntaxHighlighterwithPreTag="div".However, react-markdown wraps all block code elements with a default
<pre>element whencomponents.preis not customized. This creates invalid HTML: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 thecomponentsprop ofReactMarkdown. This strips the default<pre>wrapper, letting theCodeBlockcomponent render its own block-level presentation (viaSyntaxHighlighterwithPreTag="div") without being wrapped in an invalid<pre>element.Testing
MarkdownContent.test.tsx:<pre>elements remain in the DOM for a single code block