fix: fix markdown embed image issue (#857)

* fix: fix markdown embed image issue

* fix urlTransform filter condition

* fix code format

* add image/gif filter

* Format code

---------

Co-authored-by: 饺子w (Yumechi) <35571479+eternal-flame-AD@users.noreply.github.com>
This commit is contained in:
ggqshr 2025-10-13 10:19:33 +08:00 committed by GitHub
parent 970302106b
commit 9879280a2c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 2 deletions

View File

@ -1,7 +1,21 @@
import React from 'react'; import React from 'react';
import ReactMarkdown from 'react-markdown'; import ReactMarkdown, {defaultUrlTransform} from 'react-markdown';
import type {UrlTransform} from 'react-markdown';
import gfm from 'remark-gfm'; import gfm from 'remark-gfm';
// Copy from mlflow/server/js/src/shared/web-shared/genai-markdown-renderer/GenAIMarkdownRenderer.tsx
// Related PR: https://github.com/mlflow/mlflow/pull/16761
const urlTransform: UrlTransform = (value) => {
if (
value.startsWith('data:image/png;') ||
value.startsWith('data:image/jpeg;') ||
value.startsWith('data:image/gif;')
) {
return value;
}
return defaultUrlTransform(value);
};
export const Markdown = ({ export const Markdown = ({
children, children,
onImageLoaded = () => {}, onImageLoaded = () => {},
@ -11,7 +25,8 @@ export const Markdown = ({
}) => ( }) => (
<ReactMarkdown <ReactMarkdown
components={{img: ({...props}) => <img onLoad={onImageLoaded} {...props} />}} components={{img: ({...props}) => <img onLoad={onImageLoaded} {...props} />}}
remarkPlugins={[gfm]}> remarkPlugins={[gfm]}
urlTransform={urlTransform}>
{children} {children}
</ReactMarkdown> </ReactMarkdown>
); );