Commit 0cd81782 by liimin

fix(markdown): 修复链接图片预览功能并优化代码结构

- 添加ImagePreview组件用于图片链接预览
- 修改Link组件逻辑,支持直接点击图片链接预览
- 移除冗余的mermaid代码块处理
- 修复ErrorBoundary渲染逻辑错误
- 优化LaTeX预处理正则表达式
1 parent 0d5095f1
......@@ -26,6 +26,7 @@ import { Theme } from '@/types/app'
import useTheme from '@/hooks/use-theme'
import cn from '@/utils/classnames'
import SVGRenderer from './svg-gallery'
import ImagePreview from '@/app/components/base/image-uploader/image-preview'
// Available language https://github.com/react-syntax-highlighter/react-syntax-highlighter/blob/master/AVAILABLE_LANGUAGES_HLJS.MD
const capitalizationLanguageNameMap: Record<string, string> = {
......@@ -66,7 +67,7 @@ const preprocessLaTeX = (content: string) => {
const codeBlocks = content.match(codeBlockRegex) || []
let processedContent = content.replace(codeBlockRegex, 'CODE_BLOCK_PLACEHOLDER')
processedContent = flow([(str: string) => str.replace(/\\\[(.*?)\\\]/g, (_, equation) => `$$${equation}$$`), (str: string) => str.replace(/\\\[(.*?)\\\]/gs, (_, equation) => `$$${equation}$$`), (str: string) => str.replace(/\\\((.*?)\\\)/g, (_, equation) => `$$${equation}$$`), (str: string) => str.replace(/(^|[^\\])\$(.+?)\$/g, (_, prefix, equation) => `${prefix}$${equation}$`)])(processedContent)
processedContent = flow([(str: string) => str.replace(/\\\[(.*?)\\\]/g, (_, equation) => `$$${equation}$$`), (str: string) => str.replace(/\\\[(.*?)\\\]/g, (_, equation) => `$$${equation}$$`), (str: string) => str.replace(/\\\((.*?)\\\)/g, (_, equation) => `$$${equation}$$`), (str: string) => str.replace(/(^|[^\\])\$(.+?)\$/g, (_, prefix, equation) => `${prefix}$${equation}$`)])(processedContent)
codeBlocks.forEach(block => {
processedContent = processedContent.replace('CODE_BLOCK_PLACEHOLDER', block)
......@@ -128,9 +129,9 @@ const CodeBlock: any = memo(({ inline, className, children = '', ...props }: any
const renderCodeContent = useMemo(() => {
const content = String(children).replace(/\n$/, '')
switch (language) {
case 'mermaid':
if (isSVG) return <Flowchart PrimitiveCode={content} />
break
// case 'mermaid':
// if (isSVG) return <Flowchart PrimitiveCode={content} />
// break
case 'echarts':
return (
<div style={{ minHeight: '350px', minWidth: '100%', overflowX: 'scroll' }}>
......@@ -200,14 +201,22 @@ CodeBlock.displayName = 'CodeBlock'
const VideoBlock: any = memo(({ node }: any) => {
const srcs = node.children.filter((child: any) => 'properties' in child).map((child: any) => (child as any).properties.src)
if (srcs.length === 0) return null
if (srcs.length === 0) {
const src = node.properties?.src
if (src) return <VideoGallery key={src} srcs={[src]} />
return null
}
return <VideoGallery key={srcs.join()} srcs={srcs} />
})
VideoBlock.displayName = 'VideoBlock'
const AudioBlock: any = memo(({ node }: any) => {
const srcs = node.children.filter((child: any) => 'properties' in child).map((child: any) => (child as any).properties.src)
if (srcs.length === 0) return null
if (srcs.length === 0) {
const src = node.properties?.src
if (src) return <AudioGallery key={src} srcs={[src]} />
return null
}
return <AudioGallery key={srcs.join()} srcs={srcs} />
})
AudioBlock.displayName = 'AudioBlock'
......@@ -241,28 +250,31 @@ const Img = ({ src }: any) => {
}
const Link = ({ node, children, ...props }: any) => {
const [imagePreviewUrl, setImagePreviewUrl] = useState('')
if (node.properties?.href && node.properties.href?.toString().startsWith('abbr')) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const { onSend } = useChatContext()
const hidden_text = decodeURIComponent(node.properties.href.toString().split('abbr:')[1])
return (
<abbr className="cursor-pointer underline !decoration-primary-700 decoration-dashed" onClick={() => onSend?.(hidden_text)} title={node.children[0]?.value || ''}>
{node.children[0]?.value || ''}
</abbr>
)
} else if (node.properties?.href && /\.(jpg|jpeg|png|gif|webp)$/i.test(node.properties.href)) {
// 处理图片链接
return (
<>
<img src={node.properties.href} alt={node.children[0]?.value || ''} className="max-w-full h-auto rounded-lg cursor-pointer hover:opacity-90" onClick={() => setImagePreviewUrl(node.properties.href)} />
{imagePreviewUrl && <ImagePreview url={imagePreviewUrl} onCancel={() => setImagePreviewUrl('')} title={''} />}
</>
)
} else {
const href = node.properties?.href?.toString() || ''
const isImage = /\.(jpe?g|png|gif|webp|bmp|svg)(\?.*)?$/i.test(href)
return isImage ? (
<div className="markdown-img-wrapper">
<ImageGallery srcs={[href]} />
</div>
) : (
return (
<a {...props} target="_blank" className="cursor-pointer underline !decoration-primary-700 decoration-dashed">
{children || 'Download'}
</a>
)
// return <a {...props} target="_blank" className="cursor-pointer underline !decoration-primary-700 decoration-dashed">{children || 'Download'}</a>
}
}
......@@ -272,7 +284,7 @@ export function Markdown(props: { content: string; className?: string; customDis
return (
<div className={cn('markdown-body', '!text-text-primary', props.className)}>
<ReactMarkdown
remarkPlugins={[RemarkGfm, [RemarkMath, { singleDollarTextMath: false }], RemarkBreaks]}
remarkPlugins={[[RemarkMath, { singleDollarTextMath: false }], RemarkBreaks]}
rehypePlugins={[
RehypeKatex,
RehypeRaw as any,
......@@ -331,7 +343,7 @@ export default class ErrorBoundary extends Component {
render() {
// eslint-disable-next-line ts/ban-ts-comment
// @ts-expect-error
if (this.state.hasError)
if (!this.state.hasError)
return (
<div>
Oops! An error occurred. This could be due to an ECharts runtime error or invalid SVG content. <br />
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!