Files
opencode-manager/frontend/src/components/file-browser/MarkdownRenderer.tsx
T
Chris Scott 7fbb7555b2 feat(files): fullscreen preview, relative markdown links, and preinstalled Chromium (#369)
* feat(files): add fullscreen preview with a minimal close control

Add a fullscreen toggle to the desktop file preview header. Fullscreen fills the viewport, hides the header, and shows only a small on-top X to exit.

Keep the mobile preview fullscreen by default with its close button layered above the content, and give the headerless mobile preview the same small on-top X.

* fix(files): open relative markdown links and preinstall Chromium

Resolve relative and absolute markdown links against the previewed file
and open them in the file browser; external links open in a new tab.
Install Playwright and Chromium into the Manager image with NODE_PATH
and PLAYWRIGHT_BROWSERS_PATH so agents can run browser automation
without a project-local install.

* refactor(files): share markdown link handling across renderers

* test(docker): cover the preinstalled playwright chromium step
2026-09-29 00:15:52 -04:00

106 lines
3.4 KiB
TypeScript

import { memo, useMemo, useCallback } from 'react'
import ReactMarkdown from 'react-markdown'
import remarkGfm from 'remark-gfm'
import rehypeHighlight from 'rehype-highlight'
import rehypeRaw from 'rehype-raw'
import { markdownComponents } from './MarkdownComponents'
import { MarkdownLink } from '@/components/ui/markdown-link'
import { resolvePathFromFile } from '@/lib/markdownLinks'
import type { Components } from 'react-markdown'
interface MarkdownRendererProps {
content: string
className?: string
onContentChange?: (newContent: string) => void
filePath?: string
onOpenFile?: (path: string) => void
}
interface TaskItem {
lineIndex: number
checked: boolean
}
function parseTaskItems(content: string): TaskItem[] {
const items: TaskItem[] = []
const lines = content.split('\n')
for (let i = 0; i < lines.length; i++) {
const match = lines[i].match(/^(\s*[-*+]\s+)\[([ xX])\]/i)
if (match) {
items.push({ lineIndex: i, checked: match[2].toLowerCase() === 'x' })
}
}
return items
}
export const MarkdownRenderer = memo(function MarkdownRenderer({ content, className = '', onContentChange, filePath, onOpenFile }: MarkdownRendererProps) {
const taskItems = useMemo(() => parseTaskItems(content), [content])
const handleToggle = useCallback((taskItem: TaskItem) => {
if (!onContentChange) return
const lines = content.split('\n')
const line = lines[taskItem.lineIndex]
const newLine = line.replace(
/^(\s*[-*+]\s+)\[([ xX])\]/i,
(_, prefix: string, checked: string) => `${prefix}[${checked.toLowerCase() === 'x' ? ' ' : 'x'}]`,
)
if (newLine !== line) {
lines[taskItem.lineIndex] = newLine
onContentChange(lines.join('\n'))
}
}, [content, onContentChange])
const handleInputToggle = useCallback((input: HTMLInputElement) => {
const inputs = Array.from(input.closest('.prose')?.querySelectorAll('input[type="checkbox"]') ?? [])
const taskItem = taskItems[inputs.indexOf(input)]
if (taskItem) {
handleToggle(taskItem)
}
}, [handleToggle, taskItems])
const handleOpenLocalPath = useMemo(() => {
if (!filePath || !onOpenFile) return undefined
return (linkPath: string) => {
const resolvedPath = resolvePathFromFile(filePath, linkPath)
if (resolvedPath) onOpenFile(resolvedPath)
}
}, [filePath, onOpenFile])
const components: Components = {
...markdownComponents,
input(props) {
const { type, checked, disabled, ...rest } = props
delete (rest as Record<string, unknown>).node
if (type === 'checkbox') {
return (
<input
type="checkbox"
checked={Boolean(checked)}
onChange={(event) => handleInputToggle(event.currentTarget)}
className="cursor-pointer accent-primary"
{...rest}
/>
)
}
return <input type={type} checked={checked} disabled={disabled} {...rest} />
},
a(props) {
return <MarkdownLink {...props} onOpenLocalPath={handleOpenLocalPath} />
},
}
return (
<div className={`pb-[200px] p-4 prose prose-invert prose-enhanced max-w-none text-foreground overflow-hidden break-words leading-snug ${className}`}>
<ReactMarkdown
remarkPlugins={[remarkGfm]}
rehypePlugins={[rehypeHighlight, rehypeRaw]}
components={components}
>
{content}
</ReactMarkdown>
</div>
)
})