Skip to content

Commit

Permalink
fix: ref attribute in markdown causes page crash (langgenius#11369)
Browse files Browse the repository at this point in the history
Co-authored-by: crazywoola <427733928@qq.com>
  • Loading branch information
iamjoel and crazywoola authored Dec 5, 2024
1 parent eca466b commit 284bb7a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 30 deletions.
3 changes: 1 addition & 2 deletions web/app/components/base/image-gallery/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ const ImageGallery: FC<Props> = ({
imagePreviewUrl && (
<ImagePreview
url={imagePreviewUrl}
onCancel={() => setImagePreviewUrl('')}
/>
onCancel={() => setImagePreviewUrl('')} title={''} />
)
}
</div>
Expand Down
32 changes: 4 additions & 28 deletions web/app/components/base/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ 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 type { RefObject } from 'react'
import { Component, memo, useEffect, useMemo, useRef, useState } from 'react'
import { Component, memo, useMemo, useRef, useState } from 'react'
import type { CodeComponent } from 'react-markdown/lib/ast-to-react'
import cn from '@/utils/classnames'
import CopyBtn from '@/app/components/base/copy-btn'
Expand Down Expand Up @@ -77,29 +76,6 @@ export function PreCode(props: { children: any }) {
)
}

// eslint-disable-next-line unused-imports/no-unused-vars
const useLazyLoad = (ref: RefObject<Element>): boolean => {
const [isIntersecting, setIntersecting] = useState<boolean>(false)

useEffect(() => {
const observer = new IntersectionObserver(([entry]) => {
if (entry.isIntersecting) {
setIntersecting(true)
observer.disconnect()
}
})

if (ref.current)
observer.observe(ref.current)

return () => {
observer.disconnect()
}
}, [ref])

return isIntersecting
}

// **Add code block
// Avoid error #185 (Maximum update depth exceeded.
// This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate.
Expand All @@ -123,7 +99,7 @@ const CodeBlock: CodeComponent = memo(({ inline, className, children, ...props }
try {
return JSON.parse(String(children).replace(/\n$/, ''))
}
catch (error) {}
catch (error) { }
}
return JSON.parse('{"title":{"text":"ECharts error - Wrong JSON format."}}')
}, [language, children])
Expand Down Expand Up @@ -181,7 +157,7 @@ const CodeBlock: CodeComponent = memo(({ inline, className, children, ...props }
>
<div className='text-[13px] text-gray-500 font-normal'>{languageShowName}</div>
<div style={{ display: 'flex' }}>
{(['mermaid', 'svg']).includes(language!) && <SVGBtn isSVG={isSVG} setIsSVG={setIsSVG}/>}
{(['mermaid', 'svg']).includes(language!) && <SVGBtn isSVG={isSVG} setIsSVG={setIsSVG} />}
<CopyBtn
className='mr-1'
value={String(children).replace(/\n$/, '')}
Expand Down Expand Up @@ -261,7 +237,7 @@ export function Markdown(props: { content: string; className?: string }) {
() => {
return (tree) => {
const iterate = (node: any) => {
if (node.type === 'element' && !node.properties?.src && node.properties?.ref && node.properties.ref.startsWith('{') && node.properties.ref.endsWith('}'))
if (node.type === 'element' && node.properties?.ref)
delete node.properties.ref

if (node.children)
Expand Down

0 comments on commit 284bb7a

Please sign in to comment.