mirror of
https://github.com/langgenius/dify.git
synced 2025-11-19 06:25:05 +00:00
Signed-off-by: lyzno1 <yuanyouhuilyz@gmail.com> Co-authored-by: Stream <Stream_2@qq.com> Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com> Co-authored-by: zhsama <torvalds@linux.do> Co-authored-by: Harry <xh001x@hotmail.com> Co-authored-by: lyzno1 <yuanyouhuilyz@gmail.com> Co-authored-by: yessenia <yessenia.contact@gmail.com> Co-authored-by: hjlarry <hjlarry@163.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: WTW0313 <twwu@dify.ai> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
70 lines
2.1 KiB
TypeScript
70 lines
2.1 KiB
TypeScript
/**
|
|
* @fileoverview Paragraph component for rendering <p> tags in Markdown.
|
|
* Extracted from the main markdown renderer for modularity.
|
|
* Handles special rendering for paragraphs that directly contain an image.
|
|
*/
|
|
import ImageGallery from '@/app/components/base/image-gallery'
|
|
import { usePluginReadmeAsset } from '@/service/use-plugins'
|
|
import React, { useEffect, useMemo, useState } from 'react'
|
|
import type { SimplePluginInfo } from '../markdown/react-markdown-wrapper'
|
|
import { getMarkdownImageURL } from './utils'
|
|
|
|
type PluginParagraphProps = {
|
|
pluginInfo?: SimplePluginInfo
|
|
node?: any
|
|
children?: React.ReactNode
|
|
}
|
|
|
|
export const PluginParagraph: React.FC<PluginParagraphProps> = ({ pluginInfo, node, children }) => {
|
|
const { pluginUniqueIdentifier, pluginId } = pluginInfo || {}
|
|
const childrenNode = node?.children as Array<any> | undefined
|
|
const firstChild = childrenNode?.[0]
|
|
const isImageParagraph = firstChild?.tagName === 'img'
|
|
const imageSrc = isImageParagraph ? firstChild?.properties?.src : undefined
|
|
|
|
const { data: assetData } = usePluginReadmeAsset({
|
|
plugin_unique_identifier: pluginUniqueIdentifier,
|
|
file_name: isImageParagraph && imageSrc ? imageSrc : '',
|
|
})
|
|
|
|
const [blobUrl, setBlobUrl] = useState<string>()
|
|
|
|
useEffect(() => {
|
|
if (!assetData) {
|
|
setBlobUrl(undefined)
|
|
return
|
|
}
|
|
|
|
const objectUrl = URL.createObjectURL(assetData)
|
|
setBlobUrl(objectUrl)
|
|
|
|
return () => {
|
|
URL.revokeObjectURL(objectUrl)
|
|
}
|
|
}, [assetData])
|
|
|
|
const imageUrl = useMemo(() => {
|
|
if (blobUrl)
|
|
return blobUrl
|
|
|
|
if (isImageParagraph && imageSrc)
|
|
return getMarkdownImageURL(imageSrc, pluginId)
|
|
|
|
return ''
|
|
}, [blobUrl, imageSrc, isImageParagraph, pluginId])
|
|
|
|
if (isImageParagraph) {
|
|
const remainingChildren = Array.isArray(children) && children.length > 1 ? children.slice(1) : undefined
|
|
|
|
return (
|
|
<div className="markdown-img-wrapper">
|
|
<ImageGallery srcs={[imageUrl]} />
|
|
{remainingChildren && (
|
|
<div className="mt-2">{remainingChildren}</div>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|
|
return <p>{children}</p>
|
|
}
|