Skip to content

Commit aa2b791

Browse files
RoyBAcubic-dev-ai[bot]hayescode
authored
feat: Add native video support in markdown rendering (#2704)
## Summary Adds support for rendering video files directly in markdown when using image syntax (`![](video.mp4)`). The markdown parser now detects video file extensions and renders a native HTML5 video element instead of trying to display videos as images. <img width="721" height="461" alt="image" src="https://github.com/user-attachments/assets/7ba80009-2b80-482f-9de9-d3493b953eaf" /> ## Changes - Modified the `img` component handler in `Markdown.tsx` to detect video file extensions - Renders native HTML5 `<video>` element with controls for video URLs - Supports common video formats: `.mp4`, `.webm`, `.mov`, `.avi`, `.ogv`, `.m4v` - Falls back to standard image rendering for non-video files ## Motivation Previously, when users included video URLs in markdown syntax like `![](http://example.com/video.mp4)`, the frontend was replacing them with `<img>` elements, which couldn't display the videos properly. This fix enables proper video playback directly within markdown content. ## Example ```python from chainlit import AskUserMessage, Message, on_chat_start @on_chat_start async def main(): res = await AskUserMessage(content="Please enter a video URL:", timeout=30).send() if res: video_url = res['output'] await Message( content=f"Here's your video:\n\n![Video]({video_url})", ).send() ``` <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Add native video rendering in Markdown when users embed videos with image syntax. The renderer detects video URLs and shows an HTML5 video player with controls for inline playback. - **New Features** - Detects common video extensions in Markdown image nodes. - Renders a responsive HTML5 video element with controls. - Supports .mp4, .webm, .mov, .avi, .ogv, .m4v. - Falls back to standard image rendering for non-video URLs. <sup>Written for commit 83674db. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. --> --------- Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> Co-authored-by: Josh Hayes <[email protected]>
1 parent 08ea796 commit aa2b791

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

frontend/src/components/Markdown.tsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,39 @@ const Markdown = ({
158158
}
159159
},
160160
img: (image: any) => {
161+
// Check if the image source is actually a video file
162+
const src = image.src.startsWith('/public')
163+
? apiClient.buildEndpoint(image.src)
164+
: image.src;
165+
166+
const videoExtensions = ['.mp4', '.webm', '.mov', '.avi', '.ogv', '.m4v'];
167+
const isVideo = videoExtensions.some(ext =>
168+
src.toLowerCase().split(/[?#]/)[0].endsWith(ext)
169+
);
170+
171+
if (isVideo) {
172+
return (
173+
<div className="sm:max-w-sm md:max-w-md">
174+
<video
175+
src={src}
176+
controls
177+
className="w-full h-auto rounded-md"
178+
style={{ maxWidth: '100%' }}
179+
>
180+
Your browser does not support the video tag.
181+
</video>
182+
</div>
183+
);
184+
}
185+
161186
return (
162187
<div className="sm:max-w-sm md:max-w-md">
163188
<AspectRatio
164189
ratio={16 / 9}
165190
className="bg-muted rounded-md overflow-hidden"
166191
>
167192
<img
168-
src={
169-
image.src.startsWith('/public')
170-
? apiClient.buildEndpoint(image.src)
171-
: image.src
172-
}
193+
src={src}
173194
alt={image.alt}
174195
className="h-full w-full object-contain"
175196
/>

0 commit comments

Comments
 (0)