This commit is contained in:
Timothy Jaeryang Baek
2026-03-22 21:36:45 -05:00
parent 59171daa35
commit 9a6bf78e14
2 changed files with 45 additions and 19 deletions
+9 -9
View File
@@ -961,13 +961,12 @@
if (message?.role !== 'user' && message?.content) { if (message?.role !== 'user' && message?.content) {
const { const {
codeBlocks: codeBlocks, codeBlocks: codeBlocks,
html: htmlContent, htmlGroups: htmlGroups
css: cssContent,
js: jsContent
} = getCodeBlockContents(message.content); } = getCodeBlockContents(message.content);
if (htmlContent || cssContent || jsContent) { if (htmlGroups && htmlGroups.length > 0) {
const renderedContent = ` htmlGroups.forEach((group) => {
const renderedContent = `
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
@@ -978,19 +977,20 @@
background-color: white; /* Ensure the iframe has a white background */ background-color: white; /* Ensure the iframe has a white background */
} }
${cssContent} ${group.css}
</${''}style> </${''}style>
</head> </head>
<body> <body>
${htmlContent} ${group.html}
<${''}script> <${''}script>
${jsContent} ${group.js}
</${''}script> </${''}script>
</body> </body>
</html> </html>
`; `;
contents = [...contents, { type: 'iframe', content: renderedContent }]; contents = [...contents, { type: 'iframe', content: renderedContent }];
});
} else { } else {
// Check for SVG content // Check for SVG content
for (const block of codeBlocks) { for (const block of codeBlocks) {
+36 -10
View File
@@ -1729,9 +1729,18 @@ export const getCodeBlockContents = (content: string): object => {
let codeBlocks = []; let codeBlocks = [];
let htmlContent = ''; // Groups of related HTML/CSS/JS blocks. Each HTML block starts a new group;
let cssContent = ''; // CSS and JS blocks attach to the current (most recent) group.
let jsContent = ''; // 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) { if (codeBlockContents) {
codeBlockContents.forEach((block) => { codeBlockContents.forEach((block) => {
@@ -1744,11 +1753,14 @@ export const getCodeBlockContents = (content: string): object => {
const { lang, code } = block; const { lang, code } = block;
if (lang === 'html') { if (lang === 'html') {
htmlContent += code + '\n'; // Each HTML block starts a new group
htmlGroups.push({ html: code + '\n', css: '', js: '' });
} else if (lang === 'css') { } else if (lang === 'css') {
cssContent += code + '\n'; initDefaultGroup();
htmlGroups[htmlGroups.length - 1].css += code + '\n';
} else if (lang === 'javascript' || lang === 'js') { } else if (lang === 'javascript' || lang === 'js') {
jsContent += code + '\n'; initDefaultGroup();
htmlGroups[htmlGroups.length - 1].js += code + '\n';
} }
}); });
} else { } else {
@@ -1763,28 +1775,42 @@ export const getCodeBlockContents = (content: string): object => {
if (inlineHtml) { if (inlineHtml) {
inlineHtml.forEach((block) => { inlineHtml.forEach((block) => {
const content = block.replace(/<\/?html>/gi, ''); // Remove <html> tags const content = block.replace(/<\/?html>/gi, ''); // Remove <html> tags
htmlContent += content + '\n'; htmlGroups.push({ html: content + '\n', css: '', js: '' });
}); });
} }
if (inlineCss) { if (inlineCss) {
inlineCss.forEach((block) => { inlineCss.forEach((block) => {
const content = block.replace(/<\/?style>/gi, ''); // Remove <style> tags const content = block.replace(/<\/?style>/gi, ''); // Remove <style> tags
cssContent += content + '\n'; initDefaultGroup();
htmlGroups[htmlGroups.length - 1].css += content + '\n';
}); });
} }
if (inlineJs) { if (inlineJs) {
inlineJs.forEach((block) => { inlineJs.forEach((block) => {
const content = block.replace(/<\/?script>/gi, ''); // Remove <script> tags const content = block.replace(/<\/?script>/gi, ''); // Remove <script> tags
jsContent += content + '\n'; initDefaultGroup();
htmlGroups[htmlGroups.length - 1].js += content + '\n';
}); });
} }
} }
// Backward-compatible flat fields (merged from all groups)
const htmlContent = htmlGroups.map((g) => g.html).join('');
const cssContent = htmlGroups.map((g) => g.css).join('');
const jsContent = htmlGroups.map((g) => g.js).join('');
return { return {
codeBlocks: codeBlocks, codeBlocks: codeBlocks,
html: htmlContent.trim(), html: htmlContent.trim(),
css: cssContent.trim(), css: cssContent.trim(),
js: jsContent.trim() js: jsContent.trim(),
htmlGroups: htmlGroups
.filter((g) => g.html.trim() || g.css.trim() || g.js.trim())
.map((g) => ({
html: g.html.trim(),
css: g.css.trim(),
js: g.js.trim()
}))
}; };
}; };
export const parseFrontmatter = (content) => { export const parseFrontmatter = (content) => {