[PR #32366] fix(web): avoid invalid DOM nesting when inline images appear in paragraphs #33693

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

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

State: open
Merged: No


Summary

When a Markdown paragraph contains an inline image that is not the first child (e.g. Text before ![img](url) text after), the Img component renders a block-level <div> wrapper. This was placed inside a <p> tag, producing invalid HTML and triggering React/Next.js hydration warnings:

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

Changes

  • Updated Paragraph component (web/app/components/base/markdown-blocks/paragraph.tsx) to detect when any child node (not just the first) is an image element.
  • When a non-leading image is found, renders as <div class="markdown-paragraph"> instead of <p>, keeping the DOM valid.
  • The existing first-child-is-image logic (dedicated ImageGallery block) is unchanged.

Testing

  • Rendered Markdown with inline images in various positions within paragraphs
  • Confirmed no hydration warning in browser console
  • Confirmed existing image-as-first-child rendering is unaffected

Closes #32362

**Original Pull Request:** https://github.com/langgenius/dify/pull/32366 **State:** open **Merged:** No --- ## Summary When a Markdown paragraph contains an inline image that is **not** the first child (e.g. `Text before ![img](url) text after`), the `Img` component renders a block-level `<div>` wrapper. This was placed inside a `<p>` tag, producing invalid HTML and triggering React/Next.js hydration warnings: > `In HTML, <div> cannot be a descendant of <p>. This will cause a hydration error.` ## Changes - Updated `Paragraph` component (`web/app/components/base/markdown-blocks/paragraph.tsx`) to detect when **any** child node (not just the first) is an image element. - When a non-leading image is found, renders as `<div class="markdown-paragraph">` instead of `<p>`, keeping the DOM valid. - The existing first-child-is-image logic (dedicated `ImageGallery` block) is unchanged. ## Testing - Rendered Markdown with inline images in various positions within paragraphs - Confirmed no hydration warning in browser console - Confirmed existing image-as-first-child rendering is unaffected Closes #32362
yindo added the pull-request label 2026-02-21 20:53:43 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#33693