mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
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.
47 lines
1.5 KiB
TypeScript
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>
|
|
}
|