From f5f83f1924b101e0e2f0917965e806daad5bdd5b Mon Sep 17 00:00:00 2001 From: Joel Date: Mon, 17 Feb 2025 18:11:07 +0800 Subject: [PATCH] fix: markdown merge error (#13853) --- web/app/components/base/markdown.tsx | 83 ++++++++++++---------------- 1 file changed, 34 insertions(+), 49 deletions(-) diff --git a/web/app/components/base/markdown.tsx b/web/app/components/base/markdown.tsx index d21277cbb..37691416e 100644 --- a/web/app/components/base/markdown.tsx +++ b/web/app/components/base/markdown.tsx @@ -1,4 +1,3 @@ -import type { Components } from 'react-markdown' import ReactMarkdown from 'react-markdown' import ReactEcharts from 'echarts-for-react' import 'katex/dist/katex.min.css' @@ -9,8 +8,8 @@ import RemarkGfm from 'remark-gfm' import RehypeRaw from 'rehype-raw' import SyntaxHighlighter from 'react-syntax-highlighter' import { atelierHeathLight } from 'react-syntax-highlighter/dist/esm/styles/hljs' -import { Component, createContext, memo, useContext, useMemo, useRef, useState } from 'react' -import { flow } from 'lodash/fp' +import { Component, memo, useMemo, useRef, useState } from 'react' +import { flow } from 'lodash-es' import cn from '@/utils/classnames' import CopyBtn from '@/app/components/base/copy-btn' import SVGBtn from '@/app/components/base/svg' @@ -22,7 +21,7 @@ import AudioGallery from '@/app/components/base/audio-gallery' import SVGRenderer from '@/app/components/base/svg-gallery' import MarkdownButton from '@/app/components/base/markdown-blocks/button' import MarkdownForm from '@/app/components/base/markdown-blocks/form' -import type { ElementContentMap } from 'hast' +import ThinkBlock from '@/app/components/base/markdown-blocks/think-block' // Available language https://github.com/react-syntax-highlighter/react-syntax-highlighter/blob/master/AVAILABLE_LANGUAGES_HLJS.MD const capitalizationLanguageNameMap: Record = { @@ -57,7 +56,7 @@ const getCorrectCapitalizationLanguageName = (language: string) => { return language.charAt(0).toUpperCase() + language.substring(1) } -const preprocessLaTeX = (content?: string) => { +const preprocessLaTeX = (content: string) => { if (typeof content !== 'string') return content @@ -91,20 +90,6 @@ export function PreCode(props: { children: any }) { ) } -const PreContext = createContext({ - // if children not in PreContext, just leave inline true - inline: true, -}) - -const PreBlock: Components['pre'] = (props) => { - const { ...rest } = props - return -
-  
-}
-
 // **Add code block
 // Avoid error #185 (Maximum update depth exceeded.
 // This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate.
@@ -118,8 +103,7 @@ const PreBlock: Components['pre'] = (props) => {
 // visit https://reactjs.org/docs/error-decoder.html?invariant=185 for the full message
 // or use the non-minified dev environment for full errors and additional helpful warnings.
 
-const CodeBlock: Components['code'] = memo(({ className, children, ...props }) => {
-  const { inline } = useContext(PreContext)
+const CodeBlock: any = memo(({ inline, className, children, ...props }) => {
   const [isSVG, setIsSVG] = useState(true)
   const match = /language-(\w+)/.exec(className || '')
   const language = match?.[1]
@@ -158,7 +142,7 @@ const CodeBlock: Components['code'] = memo(({ className, children, ...props }) =
     else {
       return (
         
   )
 })
-// CodeBlock.displayName = 'CodeBlock'
+CodeBlock.displayName = 'CodeBlock'
 
-const VideoBlock: Components['video'] = memo(({ node }) => {
-  const srcs = node!.children.filter(child => 'properties' in child).map(child => (child as any).properties.src)
+const VideoBlock: any = memo(({ node }) => {
+  const srcs = node.children.filter(child => 'properties' in child).map(child => (child as any).properties.src)
   if (srcs.length === 0)
     return null
   return 
 })
-// VideoBlock.displayName = 'VideoBlock'
+VideoBlock.displayName = 'VideoBlock'
 
-const AudioBlock: Components['audio'] = memo(({ node }) => {
-  const srcs = node!.children.filter(child => 'properties' in child).map(child => (child as any).properties.src)
+const AudioBlock: any = memo(({ node }) => {
+  const srcs = node.children.filter(child => 'properties' in child).map(child => (child as any).properties.src)
   if (srcs.length === 0)
     return null
   return 
 })
-// AudioBlock.displayName = 'AudioBlock'
+AudioBlock.displayName = 'AudioBlock'
 
 const ScriptBlock = memo(({ node }: any) => {
   const scriptContent = node.children[0]?.value || ''
@@ -223,32 +207,34 @@ const ScriptBlock = memo(({ node }: any) => {
 })
 ScriptBlock.displayName = 'ScriptBlock'
 
-const Paragraph: Components['p'] = ({ node, children }) => {
-  const children_node = node!.children
-  if (children_node && children_node[0] && 'tagName' in children_node[0] && children_node[0].tagName === 'img')
-    return 
-  return 

{children}

+const Paragraph = (paragraph: any) => { + const { node }: any = paragraph + const children_node = node.children + if (children_node && children_node[0] && 'tagName' in children_node[0] && children_node[0].tagName === 'img') { + return ( + <> + +

{paragraph.children.slice(1)}

+ + ) + } + return

{paragraph.children}

} -const Img: Components['img'] = ({ src }) => { - return () +const Img = ({ src }: any) => { + return () } -const Link: Components['a'] = ({ node, ...props }) => { - if (node!.properties?.href && node!.properties.href?.toString().startsWith('abbr')) { +const Link = ({ node, ...props }: any) => { + 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]) - const title = (node!.children[0] as ElementContentMap['text'])?.value - return onSend?.(hidden_text)} title={title}>{title} + const hidden_text = decodeURIComponent(node.properties.href.toString().split('abbr:')[1]) + + return onSend?.(hidden_text)} title={node.children[0]?.value}>{node.children[0]?.value} } else { - const firstChild = node?.children?.[0] as ElementContentMap['text'] | undefined - return { - firstChild - ? firstChild.value - : 'Download' - } + return {node.children[0] ? node.children[0]?.value : 'Download'} } } @@ -258,7 +244,7 @@ export function Markdown(props: { content: string; className?: string }) { preprocessLaTeX, ])(props.content) return ( -
+