소스 검색

fix: replace old-style <br> tags to fix Mermaid rendering issues (#13792)

Hao Cheng 1 개월 전
부모
커밋
49d0acd188
3개의 변경된 파일13개의 추가작업 그리고 1개의 파일을 삭제
  1. 2 1
      web/app/components/base/mermaid/index.tsx
  2. 8 0
      web/app/components/base/mermaid/utils.spec.ts
  3. 3 0
      web/app/components/base/mermaid/utils.ts

+ 2 - 1
web/app/components/base/mermaid/index.tsx

@@ -3,6 +3,7 @@ import mermaid from 'mermaid'
 import { usePrevious } from 'ahooks'
 import { useTranslation } from 'react-i18next'
 import { ExclamationTriangleIcon } from '@heroicons/react/24/outline'
+import { cleanUpSvgCode } from './utils'
 import LoadingAnim from '@/app/components/base/chat/chat/loading-anim'
 import cn from '@/utils/classnames'
 import ImagePreview from '@/app/components/base/image-uploader/image-preview'
@@ -44,7 +45,7 @@ const Flowchart = React.forwardRef((props: {
     try {
       if (typeof window !== 'undefined' && mermaidAPI) {
         const svgGraph = await mermaidAPI.render('flowchart', PrimitiveCode)
-        const base64Svg: any = await svgToBase64(svgGraph.svg)
+        const base64Svg: any = await svgToBase64(cleanUpSvgCode(svgGraph.svg))
         setSvgCode(base64Svg)
         setIsLoading(false)
       }

+ 8 - 0
web/app/components/base/mermaid/utils.spec.ts

@@ -0,0 +1,8 @@
+import { cleanUpSvgCode } from './utils'
+
+describe('cleanUpSvgCode', () => {
+  it('replaces old-style <br> tags with the new style', () => {
+    const result = cleanUpSvgCode('<br>test<br>')
+    expect(result).toEqual('<br/>test<br/>')
+  })
+})

+ 3 - 0
web/app/components/base/mermaid/utils.ts

@@ -0,0 +1,3 @@
+export function cleanUpSvgCode(svgCode: string): string {
+  return svgCode.replaceAll('<br>', '<br/>')
+}