mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
feat(markdown): render inline images in messages and markdown files
Route markdown image nodes through a shared MarkdownImage component that loads local paths through the raw file API, and allow data:image/* sources through the sanitizer and URL transform so inline images survive rendering. Render image attachments as thumbnails in the message thread, and accept image/webp uploads.
This commit is contained in:
1 parent
93faf387d2
commit
be93975d90
13 files changed
+263
-18
No files matched your search
@@ -1,16 +1,19 @@
|
||||
import React, { useEffect, useState, useId, useCallback } from 'react'
|
||||
import React, { useEffect, useState, useId, useCallback, useMemo } from 'react'
|
||||
import ReactMarkdown from 'react-markdown'
|
||||
import remarkGfm from 'remark-gfm'
|
||||
import mermaid from 'mermaid'
|
||||
import { Maximize2, X, AlertCircle } from 'lucide-react'
|
||||
import { CopyButton } from '@/components/ui/copy-button'
|
||||
import { MarkdownLink } from '@/components/ui/markdown-link'
|
||||
import { MarkdownImage } from '@/components/ui/markdown-image'
|
||||
import { useTheme } from '@/hooks/useTheme'
|
||||
import { markdownRehypePlugins } from '@/lib/markdownRehypePlugins'
|
||||
import { getWorkspaceFilePath } from '@/lib/markdownLinks'
|
||||
import { markdownRehypePlugins, markdownUrlTransform } from '@/lib/markdownRehypePlugins'
|
||||
|
||||
interface TextPartProps {
|
||||
text: string
|
||||
onFileClick?: (filePath: string) => void
|
||||
directory?: string
|
||||
}
|
||||
|
||||
interface MermaidBlockProps {
|
||||
@@ -181,11 +184,16 @@ function isMermaidBlockComplete(text: string): boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
export function TextPart({ text, onFileClick }: TextPartProps) {
|
||||
export function TextPart({ text, onFileClick, directory }: TextPartProps) {
|
||||
const mermaidComplete = React.useMemo(() => {
|
||||
return text ? isMermaidBlockComplete(text) : false
|
||||
}, [text])
|
||||
|
||||
const resolveLocalPath = useMemo(
|
||||
() => (directory ? (path: string) => getWorkspaceFilePath(path, { directory }) : undefined),
|
||||
[directory],
|
||||
)
|
||||
|
||||
if (!text || text.trim() === '') {
|
||||
return null
|
||||
}
|
||||
@@ -195,6 +203,7 @@ export function TextPart({ text, onFileClick }: TextPartProps) {
|
||||
<ReactMarkdown
|
||||
remarkPlugins={[remarkGfm]}
|
||||
rehypePlugins={markdownRehypePlugins}
|
||||
urlTransform={markdownUrlTransform}
|
||||
components={{
|
||||
code({ className, children, ...props }) {
|
||||
const isInline = !className || !className.includes('language-')
|
||||
@@ -261,6 +270,9 @@ export function TextPart({ text, onFileClick }: TextPartProps) {
|
||||
a(props) {
|
||||
return <MarkdownLink {...props} onOpenLocalPath={onFileClick} />
|
||||
},
|
||||
img(props) {
|
||||
return <MarkdownImage {...props} resolveLocalPath={resolveLocalPath} />
|
||||
},
|
||||
}}
|
||||
>
|
||||
{text}
|
||||
|
||||
Reference in new issue
Block a user