[PR #32410] fix: avoid invalid DOM nesting when inline image is not first paragraph child #33722

Open
opened 2026-02-21 20:53:46 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/32410

State: open
Merged: No


Problem

When rendering Markdown where an inline image appears inside a paragraph but is not the first child (e.g. Text before ![img](url) text after), the Paragraph component falls through to rendering <p>, while the Img renderer wraps the image in a <div>. This creates invalid <p><div>...</div></p> nesting, which triggers Next.js hydration warnings:

In HTML, <div> cannot be a descendant of <p>. This will cause a hydration error.

Solution

Added a check for image tags among all children of the paragraph node, not just the first child. When any child is an image, the component renders a <div> wrapper instead of <p>, preventing invalid DOM nesting.

The existing first-child optimization (which extracts the image into an ImageGallery) is preserved for backwards compatibility.

Changes

  • web/app/components/base/markdown-blocks/paragraph.tsx: Added hasImageChild helper and fallback <div> rendering when images are found in non-first positions.

Fixes #32362

**Original Pull Request:** https://github.com/langgenius/dify/pull/32410 **State:** open **Merged:** No --- ## Problem When rendering Markdown where an inline image appears inside a paragraph but is **not** the first child (e.g. `Text before ![img](url) text after`), the `Paragraph` component falls through to rendering `<p>`, while the `Img` renderer wraps the image in a `<div>`. This creates invalid `<p><div>...</div></p>` nesting, which triggers Next.js hydration warnings: > In HTML, `<div>` cannot be a descendant of `<p>`. This will cause a hydration error. ## Solution Added a check for image tags among **all** children of the paragraph node, not just the first child. When any child is an image, the component renders a `<div>` wrapper instead of `<p>`, preventing invalid DOM nesting. The existing first-child optimization (which extracts the image into an `ImageGallery`) is preserved for backwards compatibility. ## Changes - `web/app/components/base/markdown-blocks/paragraph.tsx`: Added `hasImageChild` helper and fallback `<div>` rendering when images are found in non-first positions. Fixes #32362
yindo added the pull-request label 2026-02-21 20:53:46 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#33722