fix(desktop): MEDIA-delivered .md opens in the preview rail, not as a download link

A `.md` delivered via MEDIA has no entry in MEDIA_BY_EXT, so mediaKind()
classified it as a generic 'file' and MarkdownLink rendered a download-style
anchor. Markdown is renderable content: route markdown document paths to
PreviewAttachment (source='tool-result'), which opens them in the right-rail
preview pane — where .md already renders with a rendered/source toggle and,
since #89381, full KaTeX math, tables, images, and links.

Resolves #84951 (the MEDIA delivery half; the rail-side rendering half landed
in #89381).
This commit is contained in:
Teknium
2026-08-18 13:37:05 -07:00
parent 1c4dc4cf5f
commit cb7dd6d2b8
3 changed files with 65 additions and 0 deletions
@@ -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(<MarkdownTextContent isRunning={false} text={`[report.md](${href})`} />)
// 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(<MarkdownTextContent isRunning={false} text={`[archive.zip](${href})`} />)
expect(await screen.findByText(/archive\.zip/)).toBeTruthy()
expect(screen.queryByRole('button')).toBeNull()
})
})
@@ -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 <PreviewAttachment source="tool-result" target={mediaPath} />
}
return <MediaAttachment path={mediaPath} />
}
+11
View File
@@ -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'
}