Files
hapi/web/src/components/MarkdownRenderer.tsx
T
eb49ccd62b fix(web): resolve relative Markdown links and images in file preview (#2211)
* fix(web): resolve relative Markdown links and images in file preview

The standalone Markdown preview rendered without the previewed file's path,
so relative .md links became inert and relative images resolved against the
web page URL. Pass the session file to the renderer and resolve relative
hrefs/srcs against its directory: links open the sibling file preview,
images load through the session file-read API. HTTPS images are unchanged
(direct browser load); paths that escape the root stay inert.

Fixes #1917

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(web): bound preview image reads and check workspace containment

Only read supported image types whose directory-listed size is at most
10 MiB, and require absolute resolved paths to stay inside the session
workspace so escapes remain inert.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(web): keep session links and browsing context in Markdown preview

Resolve session references before relative file paths, and carry the
preview's origin/tab/query into relative file links so Back returns to
where the user entered from.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 10:31:28 +08:00

144 lines
5.3 KiB
TypeScript

import type { MarkdownTextPrimitiveProps } from '@assistant-ui/react-markdown'
import { MarkdownTextPrimitive } from '@assistant-ui/react-markdown'
import { TextMessagePartProvider } from '@assistant-ui/react'
import { Children, isValidElement, useMemo, type ComponentPropsWithoutRef, type ComponentType } from 'react'
import ReactMarkdown, { type Components } from 'react-markdown'
import {
MARKDOWN_PLUGINS,
MARKDOWN_PLUGINS_STANDALONE,
MARKDOWN_PLUGINS_STANDALONE_WITH_BREAKS,
MARKDOWN_PLUGINS_WITH_BREAKS,
MARKDOWN_REHYPE_PLUGINS,
MARKDOWN_COMPONENTS_BY_LANGUAGE,
MARKDOWN_CLASSNAME,
defaultComponents,
denyOnlyTransform,
MarkdownFileBaseContext,
UriConfirmProvider,
type MarkdownFileSearch,
} from '@/components/assistant-ui/markdown-text'
import { SyntaxHighlighter } from '@/components/assistant-ui/shiki-highlighter'
import type { CodeHeaderProps, SyntaxHighlighterProps } from '@assistant-ui/react-markdown'
import { cn } from '@/lib/utils'
interface MarkdownRendererProps {
content: string
components?: MarkdownTextPrimitiveProps['components']
className?: string
preserveSingleLineBreaks?: boolean
/** Render outside assistant-ui thread context (file pane, fixtures). */
standalone?: boolean
/** Standalone only: session file the content came from, for relative links/images. */
sessionId?: string
filePath?: string
workspacePath?: string | null
fileSearch?: MarkdownFileSearch
}
function StandaloneCode(props: ComponentPropsWithoutRef<'code'>) {
const Code = defaultComponents.code!
return <Code {...props} />
}
function StandalonePre(props: ComponentPropsWithoutRef<'pre'>) {
const child = Children.toArray(props.children)[0]
if (!isValidElement<ComponentPropsWithoutRef<'code'>>(child)) {
const Pre = defaultComponents.pre!
return <Pre {...props} />
}
const className = String(child.props.className ?? '')
const language = /language-(\w+)/.exec(className)?.[1] ?? 'unknown'
const code = String(child.props.children ?? '').replace(/\n$/, '')
const Highlighter: ComponentType<SyntaxHighlighterProps> =
MARKDOWN_COMPONENTS_BY_LANGUAGE[language as keyof typeof MARKDOWN_COMPONENTS_BY_LANGUAGE]?.SyntaxHighlighter
?? SyntaxHighlighter
const CodeHeader = defaultComponents.CodeHeader as ComponentType<CodeHeaderProps>
const Pre = defaultComponents.pre!
const Code = defaultComponents.code!
return (
<>
<CodeHeader language={language} code={code} />
<Highlighter language={language} code={code} components={{ Pre, Code }} />
</>
)
}
function StandaloneMarkdownContent(props: MarkdownRendererProps) {
const mergedComponents = props.components
? { ...defaultComponents, ...props.components }
: defaultComponents
const {
pre: _pre,
code: _code,
SyntaxHighlighter: _sh,
CodeHeader: _header,
...componentsRest
} = mergedComponents as typeof mergedComponents & Record<string, unknown>
const components = useMemo<Components>(() => ({
...(componentsRest as Components),
pre: StandalonePre,
code: StandaloneCode,
}), [componentsRest])
const fileBase = useMemo(
() => props.sessionId && props.filePath
? {
sessionId: props.sessionId,
filePath: props.filePath,
workspacePath: props.workspacePath ?? null,
fileSearch: props.fileSearch ?? {},
}
: null,
[props.sessionId, props.filePath, props.workspacePath, props.fileSearch]
)
return (
<MarkdownFileBaseContext.Provider value={fileBase}>
<UriConfirmProvider>
<div className={cn(MARKDOWN_CLASSNAME, props.className)}>
<ReactMarkdown
remarkPlugins={props.preserveSingleLineBreaks ? MARKDOWN_PLUGINS_STANDALONE_WITH_BREAKS : MARKDOWN_PLUGINS_STANDALONE}
rehypePlugins={MARKDOWN_REHYPE_PLUGINS}
components={components}
urlTransform={denyOnlyTransform}
>
{props.content}
</ReactMarkdown>
</div>
</UriConfirmProvider>
</MarkdownFileBaseContext.Provider>
)
}
function MarkdownContent(props: MarkdownRendererProps) {
const mergedComponents = props.components
? { ...defaultComponents, ...props.components }
: defaultComponents
return (
<UriConfirmProvider>
<TextMessagePartProvider text={props.content}>
<MarkdownTextPrimitive
remarkPlugins={props.preserveSingleLineBreaks ? MARKDOWN_PLUGINS_WITH_BREAKS : MARKDOWN_PLUGINS}
rehypePlugins={MARKDOWN_REHYPE_PLUGINS}
components={mergedComponents}
componentsByLanguage={MARKDOWN_COMPONENTS_BY_LANGUAGE}
urlTransform={denyOnlyTransform}
className={cn(MARKDOWN_CLASSNAME, props.className)}
/>
</TextMessagePartProvider>
</UriConfirmProvider>
)
}
export function MarkdownRenderer(props: MarkdownRendererProps) {
if (props.standalone) {
return <StandaloneMarkdownContent {...props} />
}
return <MarkdownContent {...props} />
}