diff --git a/frontend/src/components/message/MessagePart.test.tsx b/frontend/src/components/message/MessagePart.test.tsx index 8ff528b9..57272eeb 100644 --- a/frontend/src/components/message/MessagePart.test.tsx +++ b/frontend/src/components/message/MessagePart.test.tsx @@ -1,5 +1,5 @@ import { describe, it, expect, vi, beforeEach } from 'vitest' -import { render, screen, fireEvent } from '@testing-library/react' +import { render, screen, fireEvent, within } from '@testing-library/react' import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import { MemoryRouter } from 'react-router-dom' import { MessagePart } from './MessagePart' @@ -175,6 +175,22 @@ describe('MessagePart', () => { expect(container.querySelector('img')).toBeNull() }) + + it('renders a linked image without a nested expand button', () => { + renderWithProviders( + , + ) + + const link = screen.getByRole('link') + expect(within(link).getByAltText('logo')).toHaveAttribute('src', 'https://example.com/a.png') + expect(within(link).queryByRole('button')).toBeNull() + }) + + it('renders an expand button for an unlinked image', () => { + renderWithProviders() + + expect(screen.getByRole('button', { name: 'Expand image: logo' })).toBeInTheDocument() + }) }) describe('reasoning', () => { diff --git a/frontend/src/components/ui/inline-image.test.tsx b/frontend/src/components/ui/inline-image.test.tsx index fab0ef5f..d3ecde34 100644 --- a/frontend/src/components/ui/inline-image.test.tsx +++ b/frontend/src/components/ui/inline-image.test.tsx @@ -22,4 +22,33 @@ describe('InlineImage', () => { expect(screen.queryByRole('button')).not.toBeInTheDocument() expect(screen.getByText('shot.png')).toBeInTheDocument() }) + + it('retries a new source after an image failure', () => { + const { rerender } = render() + + fireEvent.error(screen.getByAltText('shot.png')) + + expect(screen.queryByRole('button')).not.toBeInTheDocument() + expect(screen.getByText('shot.png')).toBeInTheDocument() + + rerender() + + expect(screen.getByAltText('shot.png')).toHaveAttribute('src', 'data:image/png;base64,aGVsbG8=') + expect(screen.getByRole('button', { name: 'Expand image: shot.png' })).toBeInTheDocument() + }) + + it('renders only the image without an expand button when not expandable', () => { + render() + + expect(screen.getByAltText('shot.png')).toHaveAttribute('src', 'data:image/png;base64,aGVsbG8=') + expect(screen.queryByRole('button')).not.toBeInTheDocument() + }) + + it('falls back to the alt text for a non-expandable image that fails to load', () => { + render() + + fireEvent.error(screen.getByAltText('shot.png')) + + expect(screen.getByText('shot.png')).toBeInTheDocument() + }) }) diff --git a/frontend/src/components/ui/inline-image.tsx b/frontend/src/components/ui/inline-image.tsx index 7fd5f185..6870f9a9 100644 --- a/frontend/src/components/ui/inline-image.tsx +++ b/frontend/src/components/ui/inline-image.tsx @@ -3,17 +3,46 @@ import { cn } from '@/lib/utils' import { Dialog, DialogContent, DialogTitle } from '@/components/ui/dialog' /** - * Renders an image as a bounded thumbnail button that opens a full-size preview dialog. - * Falls back to a muted alt-text span (or nothing) when the image fails to load. + * Renders an image as a bounded thumbnail that opens a full-size preview dialog when expandable. + * Falls back to a muted alt-text span (or nothing) when the image fails to load, retrying when the source changes. */ -export function InlineImage({ src, alt, className }: { src: string; alt?: string; className?: string }) { +export function InlineImage({ + src, + alt, + className, + expandable = true, +}: { + src: string + alt?: string + className?: string + expandable?: boolean +}) { const [open, setOpen] = useState(false) - const [failed, setFailed] = useState(false) + const [failedSrc, setFailedSrc] = useState(null) + const failed = failedSrc === src if (failed) { return alt ? {alt} : null } + const image = ( + {alt setFailedSrc(src)} + className={cn( + 'max-w-full max-h-80 rounded-lg border border-border object-contain', + expandable && 'cursor-zoom-in', + className, + )} + /> + ) + + if (!expandable) { + return image + } + return ( <> diff --git a/frontend/src/components/ui/markdown-image.tsx b/frontend/src/components/ui/markdown-image.tsx index 614cfcb7..ee972b30 100644 --- a/frontend/src/components/ui/markdown-image.tsx +++ b/frontend/src/components/ui/markdown-image.tsx @@ -1,6 +1,7 @@ import type { ComponentPropsWithoutRef } from 'react' import type { ExtraProps } from 'react-markdown' import { InlineImage } from '@/components/ui/inline-image' +import { useIsInsideMarkdownLink } from '@/components/ui/markdown-link' import { getFileApiUrl } from '@/api/files' import { getLocalLinkPath } from '@/lib/markdownLinks' @@ -14,6 +15,8 @@ type MarkdownImageProps = ComponentPropsWithoutRef<'img'> & ExtraProps & { * `data:image/*` URIs, is rendered unchanged. */ export function MarkdownImage({ src, alt, resolveLocalPath }: MarkdownImageProps) { + const insideLink = useIsInsideMarkdownLink() + if (typeof src !== 'string' || src === '') { return null } @@ -30,5 +33,5 @@ export function MarkdownImage({ src, alt, resolveLocalPath }: MarkdownImageProps } } - return + return } diff --git a/frontend/src/components/ui/markdown-link.tsx b/frontend/src/components/ui/markdown-link.tsx index 7a5b4bf6..b3b3b9b8 100644 --- a/frontend/src/components/ui/markdown-link.tsx +++ b/frontend/src/components/ui/markdown-link.tsx @@ -1,7 +1,15 @@ +import { createContext, useContext } from 'react' import type { ComponentPropsWithoutRef } from 'react' import type { ExtraProps } from 'react-markdown' import { getLocalLinkPath, isExternalLink } from '@/lib/markdownLinks' +const MarkdownLinkContext = createContext(false) + +/** Returns whether the current subtree is rendered inside a markdown link. */ +export function useIsInsideMarkdownLink(): boolean { + return useContext(MarkdownLinkContext) +} + type MarkdownLinkProps = ComponentPropsWithoutRef<'a'> & ExtraProps & { onOpenLocalPath?: (linkPath: string) => void } @@ -13,6 +21,7 @@ type MarkdownLinkProps = ComponentPropsWithoutRef<'a'> & ExtraProps & { export function MarkdownLink({ href, children, onOpenLocalPath, ...rest }: MarkdownLinkProps) { delete (rest as Record).node const localPath = onOpenLocalPath ? getLocalLinkPath(href) : null + const wrappedChildren = {children} if (localPath && onOpenLocalPath) { return ( @@ -24,14 +33,14 @@ export function MarkdownLink({ href, children, onOpenLocalPath, ...rest }: Markd onOpenLocalPath(localPath) }} > - {children} + {wrappedChildren} ) } if (isExternalLink(href)) { - return {children} + return {wrappedChildren} } - return {children} + return {wrappedChildren} } diff --git a/frontend/src/lib/markdownLinks.test.ts b/frontend/src/lib/markdownLinks.test.ts index 99764104..51704920 100644 --- a/frontend/src/lib/markdownLinks.test.ts +++ b/frontend/src/lib/markdownLinks.test.ts @@ -42,6 +42,18 @@ describe('resolvePathFromFile', () => { it('returns absolute paths unchanged', () => { expect(resolvePathFromFile(FROM, '/workspace/repos/app/report.html')).toBe('/workspace/repos/app/report.html') }) + + it('resolves relative links against an absolute base file keeping the leading slash', () => { + expect(resolvePathFromFile('/workspace/docs/README.md', 'img.png')).toBe('/workspace/docs/img.png') + expect(resolvePathFromFile('/workspace/docs/README.md', './img.png')).toBe('/workspace/docs/img.png') + expect(resolvePathFromFile('/workspace/docs/README.md', 'sub/img.png')).toBe('/workspace/docs/sub/img.png') + }) + + it('applies parent segments against an absolute base without climbing above root', () => { + expect(resolvePathFromFile('/workspace/docs/README.md', '../img.png')).toBe('/workspace/img.png') + expect(resolvePathFromFile('/a/index.md', '../../x.md')).toBe('/x.md') + expect(resolvePathFromFile('/a/index.md', '..')).toBe('/') + }) }) describe('getWorkspaceFilePath', () => { diff --git a/frontend/src/lib/markdownLinks.ts b/frontend/src/lib/markdownLinks.ts index fd3474be..189005ba 100644 --- a/frontend/src/lib/markdownLinks.ts +++ b/frontend/src/lib/markdownLinks.ts @@ -19,24 +19,31 @@ export function getLocalLinkPath(href: string | undefined): string | null { /** * Resolves a local link path against the workspace path of the file that contains it. - * Absolute paths are returned unchanged for the files API to validate; leading parent segments are kept. + * Absolute link paths are returned unchanged for the files API to validate. Leading parent segments + * are kept for relative bases, while an absolute base keeps its leading slash and never climbs above root. */ export function resolvePathFromFile(fromFilePath: string, linkPath: string): string | null { if (linkPath.startsWith('/')) { return linkPath } + const isAbsolute = fromFilePath.startsWith('/') const segments = fromFilePath.split('/').filter(Boolean).slice(0, -1) for (const segment of linkPath.split('/')) { if (segment === '' || segment === '.') continue - if (segment === '..' && segments.length > 0 && segments[segments.length - 1] !== '..') { + if (segment !== '..') { + segments.push(segment) + } else if (segments.length === 0) { + if (!isAbsolute) segments.push(segment) + } else if (isAbsolute || segments[segments.length - 1] !== '..') { segments.pop() } else { segments.push(segment) } } - return segments.join('/') || null + const resolved = segments.join('/') + return isAbsolute ? `/${resolved}` : resolved || null } /**