diff --git a/apps/desktop/src/components/assistant-ui/markdown-text.media-md.test.tsx b/apps/desktop/src/components/assistant-ui/markdown-text.media-md.test.tsx new file mode 100644 index 0000000000..9ade3e8979 --- /dev/null +++ b/apps/desktop/src/components/assistant-ui/markdown-text.media-md.test.tsx @@ -0,0 +1,45 @@ +import { cleanup, render, screen } from '@testing-library/react' +import { afterEach, describe, expect, it } from 'vitest' + +import { isMarkdownDocumentPath, mediaMarkdownHref } from '@/lib/media' + +import { MarkdownTextContent } from './markdown-text' + +// Regression for #84951: a `.md` delivered via MEDIA has no entry in +// MEDIA_BY_EXT, so it classified as a generic 'file' and rendered as a +// download-style link. Markdown is renderable content — it must route to the +// preview rail (which renders .md with a rendered/source toggle) instead. +describe('markdown documents delivered via MEDIA', () => { + afterEach(cleanup) + + it('classifies markdown extensions as markdown documents', () => { + expect(isMarkdownDocumentPath('/tmp/report.md')).toBe(true) + expect(isMarkdownDocumentPath('/tmp/notes.markdown')).toBe(true) + expect(isMarkdownDocumentPath('C:\\Users\\a\\report.MD')).toBe(true) + expect(isMarkdownDocumentPath('/tmp/report.md?x=1')).toBe(true) + expect(isMarkdownDocumentPath('/tmp/archive.zip')).toBe(false) + expect(isMarkdownDocumentPath('/tmp/clip.mp4')).toBe(false) + expect(isMarkdownDocumentPath('/tmp/README')).toBe(false) + }) + + it('renders a MEDIA .md as a preview attachment, not a download link', async () => { + const href = mediaMarkdownHref('/home/user/out/report.md') + + render() + + // PreviewAttachment renders an "open preview" toggle button; the old + // MediaAttachment 'file' fallback rendered a bare "Open ..." anchor. + expect(await screen.findByRole('button')).toBeTruthy() + expect(screen.queryByText(/^Loading /)).toBeNull() + expect(screen.getByText('report.md')).toBeTruthy() + }) + + it('still renders a non-markdown MEDIA file through the media fallback', async () => { + const href = mediaMarkdownHref('/home/user/out/archive.zip') + + render() + + expect(await screen.findByText(/archive\.zip/)).toBeTruthy() + expect(screen.queryByRole('button')).toBeNull() + }) +}) diff --git a/apps/desktop/src/components/assistant-ui/markdown-text.tsx b/apps/desktop/src/components/assistant-ui/markdown-text.tsx index b2e6ba0c2b..f9eb78084a 100644 --- a/apps/desktop/src/components/assistant-ui/markdown-text.tsx +++ b/apps/desktop/src/components/assistant-ui/markdown-text.tsx @@ -23,6 +23,7 @@ import { preprocessMarkdown } from '@/lib/markdown-preprocess' import { downloadGatewayMediaFile, isInlineMediaSrc, + isMarkdownDocumentPath, isRemoteGateway, mediaExternalUrl, mediaKind, @@ -255,6 +256,14 @@ function MarkdownLink({ children, className, href, ...props }: ComponentProps<'a const mediaPath = mediaPathFromMarkdownHref(href) if (mediaPath) { + // A delivered markdown document is renderable content, not an opaque + // download: route it to the preview rail (which renders .md with a + // rendered/source toggle) instead of the download-link fallback that + // `mediaKind() === 'file'` would produce. (#84951) + if (isMarkdownDocumentPath(mediaPath)) { + return + } + return } diff --git a/apps/desktop/src/lib/media.ts b/apps/desktop/src/lib/media.ts index 97404eb189..3bc9808aa5 100644 --- a/apps/desktop/src/lib/media.ts +++ b/apps/desktop/src/lib/media.ts @@ -40,6 +40,17 @@ export function mediaKind(path: string): MediaKind { return mediaInfo(path)?.kind ?? 'file' } +// Markdown is renderable content, not an opaque download: the preview rail +// already knows how to render a `.md` file (rendered/source toggle), so the +// MEDIA delivery path routes these to a preview instead of a download link. +const MARKDOWN_EXTENSIONS = new Set(['md', 'markdown', 'mdown', 'mkd']) + +export function isMarkdownDocumentPath(path: string): boolean { + const ext = path.split(/[?#]/, 1)[0]?.split('.').pop()?.toLowerCase() + + return ext ? MARKDOWN_EXTENSIONS.has(ext) : false +} + export function mediaMime(path: string): string { return mediaInfo(path)?.mime ?? 'application/octet-stream' }