Files
opencode-manager/frontend/src/components/ui/markdown-link.tsx
T
Chris Scott f488b8762d fix(markdown): address inline image review feedback
Keep the leading slash when resolving relative paths against an absolute
markdown file, retry an inline image when its source changes after a load
failure, and render linked markdown images without a nested expand button.
2026-10-13 01:46:51 +00:00

47 lines
1.5 KiB
TypeScript

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
}
/**
* Renders a markdown link. Local file links call `onOpenLocalPath` instead of navigating the app,
* and external http(s) links open in a new tab.
*/
export function MarkdownLink({ href, children, onOpenLocalPath, ...rest }: MarkdownLinkProps) {
delete (rest as Record<string, unknown>).node
const localPath = onOpenLocalPath ? getLocalLinkPath(href) : null
const wrappedChildren = <MarkdownLinkContext.Provider value={true}>{children}</MarkdownLinkContext.Provider>
if (localPath && onOpenLocalPath) {
return (
<a
href={href}
{...rest}
onClick={(event) => {
event.preventDefault()
onOpenLocalPath(localPath)
}}
>
{wrappedChildren}
</a>
)
}
if (isExternalLink(href)) {
return <a href={href} target="_blank" rel="noopener noreferrer" {...rest}>{wrappedChildren}</a>
}
return <a href={href} {...rest}>{wrappedChildren}</a>
}