From 9a6bf78e14a13864e72db87426da4f5996abe716 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 22 Mar 2026 21:36:45 -0500 Subject: [PATCH] refac --- src/lib/components/chat/Chat.svelte | 18 +++++------ src/lib/utils/index.ts | 46 ++++++++++++++++++++++------- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 6fa1fa3ff..ac23a7ffb 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -961,13 +961,12 @@ if (message?.role !== 'user' && message?.content) { const { codeBlocks: codeBlocks, - html: htmlContent, - css: cssContent, - js: jsContent + htmlGroups: htmlGroups } = getCodeBlockContents(message.content); - if (htmlContent || cssContent || jsContent) { - const renderedContent = ` + if (htmlGroups && htmlGroups.length > 0) { + htmlGroups.forEach((group) => { + const renderedContent = ` @@ -978,19 +977,20 @@ background-color: white; /* Ensure the iframe has a white background */ } - ${cssContent} + ${group.css} - ${htmlContent} + ${group.html} <${''}script> - ${jsContent} + ${group.js} `; - contents = [...contents, { type: 'iframe', content: renderedContent }]; + contents = [...contents, { type: 'iframe', content: renderedContent }]; + }); } else { // Check for SVG content for (const block of codeBlocks) { diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 8f9d68eb0..de9994131 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -1729,9 +1729,18 @@ export const getCodeBlockContents = (content: string): object => { let codeBlocks = []; - let htmlContent = ''; - let cssContent = ''; - let jsContent = ''; + // Groups of related HTML/CSS/JS blocks. Each HTML block starts a new group; + // CSS and JS blocks attach to the current (most recent) group. + // This preserves the existing behaviour for "dumb" models that output + // separate html/css/js blocks meant to form a single page, while also + // allowing multiple distinct HTML blocks to produce separate artifacts. + let htmlGroups: Array<{ html: string; css: string; js: string }> = []; + + const initDefaultGroup = () => { + if (htmlGroups.length === 0) { + htmlGroups.push({ html: '', css: '', js: '' }); + } + }; if (codeBlockContents) { codeBlockContents.forEach((block) => { @@ -1744,11 +1753,14 @@ export const getCodeBlockContents = (content: string): object => { const { lang, code } = block; if (lang === 'html') { - htmlContent += code + '\n'; + // Each HTML block starts a new group + htmlGroups.push({ html: code + '\n', css: '', js: '' }); } else if (lang === 'css') { - cssContent += code + '\n'; + initDefaultGroup(); + htmlGroups[htmlGroups.length - 1].css += code + '\n'; } else if (lang === 'javascript' || lang === 'js') { - jsContent += code + '\n'; + initDefaultGroup(); + htmlGroups[htmlGroups.length - 1].js += code + '\n'; } }); } else { @@ -1763,28 +1775,42 @@ export const getCodeBlockContents = (content: string): object => { if (inlineHtml) { inlineHtml.forEach((block) => { const content = block.replace(/<\/?html>/gi, ''); // Remove tags - htmlContent += content + '\n'; + htmlGroups.push({ html: content + '\n', css: '', js: '' }); }); } if (inlineCss) { inlineCss.forEach((block) => { const content = block.replace(/<\/?style>/gi, ''); // Remove