mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-05-23 00:16:48 +00:00
fix(frontend): render html artifact previews from blob content
This commit is contained in:
@@ -288,6 +288,25 @@ export function ArtifactFilePreview({
|
|||||||
language: string;
|
language: string;
|
||||||
url?: string;
|
url?: string;
|
||||||
}) {
|
}) {
|
||||||
|
const [htmlPreviewUrl, setHtmlPreviewUrl] = useState<string>();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (language !== "html" || isWriteFile) {
|
||||||
|
setHtmlPreviewUrl(undefined);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const blob = new Blob([htmlWithBaseHref(content ?? "", url)], {
|
||||||
|
type: "text/html",
|
||||||
|
});
|
||||||
|
const objectUrl = URL.createObjectURL(blob);
|
||||||
|
setHtmlPreviewUrl(objectUrl);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
URL.revokeObjectURL(objectUrl);
|
||||||
|
};
|
||||||
|
}, [content, isWriteFile, language, url]);
|
||||||
|
|
||||||
if (language === "markdown") {
|
if (language === "markdown") {
|
||||||
return (
|
return (
|
||||||
<div className="size-full px-4">
|
<div className="size-full px-4">
|
||||||
@@ -311,10 +330,35 @@ export function ArtifactFilePreview({
|
|||||||
? "allow-scripts allow-forms"
|
? "allow-scripts allow-forms"
|
||||||
: "allow-scripts allow-forms allow-same-origin"
|
: "allow-scripts allow-forms allow-same-origin"
|
||||||
}
|
}
|
||||||
src={isWriteFile ? undefined : url}
|
src={isWriteFile ? undefined : htmlPreviewUrl}
|
||||||
srcDoc={isWriteFile ? content : undefined}
|
srcDoc={isWriteFile ? content : undefined}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function htmlWithBaseHref(content: string, url?: string) {
|
||||||
|
if (!url || content.match(/<base\s/i)) {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseHref = htmlBaseHref(url);
|
||||||
|
const baseElement = `<base href="${escapeHtmlAttribute(baseHref)}">`;
|
||||||
|
if (content.match(/<head[^>]*>/i)) {
|
||||||
|
return content.replace(/<head([^>]*)>/i, `<head$1>${baseElement}`);
|
||||||
|
}
|
||||||
|
return `${baseElement}${content}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function htmlBaseHref(url: string) {
|
||||||
|
const baseUrl = new URL(url, window.location.href);
|
||||||
|
baseUrl.pathname = baseUrl.pathname.replace(/\/[^/]*$/, "/");
|
||||||
|
baseUrl.search = "";
|
||||||
|
baseUrl.hash = "";
|
||||||
|
return baseUrl.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
function escapeHtmlAttribute(value: string) {
|
||||||
|
return value.replaceAll("&", "&").replaceAll('"', """);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user