diff --git a/frontend/src/components/HtmlAnnotator.tsx b/frontend/src/components/HtmlAnnotator.tsx index c952266..c69befe 100644 --- a/frontend/src/components/HtmlAnnotator.tsx +++ b/frontend/src/components/HtmlAnnotator.tsx @@ -366,10 +366,18 @@ export default function HTMLAnnotator({ error, id }: HTMLAnnotatorProps) { )} src={`${iframeSrc}/openui/index.html?id=${iframeId}`} /> - {/* TODO: redo the scaffold */} - {error ? : undefined} - - + {/* TODO: redo the scaffold */} + {error ? : undefined} + {rawItem.commentary && ( +
+
+ {rawItem.commentary} +
+
📎
+
+ )} + +
tags if present + const indexMatch = cleanMarkdown.match(/([\s\S]*?)<\/index_html>/i) + const jsRegex = /```javascript\n([\s\S]*?)```/gi + let jsBlocks: string[] = [] - let htmlBlocks = parsed.children.filter( - c => - (c.type === 'code' && ['html', ''].includes(c.lang ?? '')) || - c.type === 'html' - ) as Code[] - // TODO: maybe do this first and only if the first paragraph is chill - for (const c of parsed.children) { - if (c.type === 'paragraph') { - let html = '' - if (c.children[0].type === 'html') { - for (const c2 of c.children) { - html = html + (c2 as unknown as Code).value || '' - } - } - htmlBlocks.push({ type: 'code', lang: 'html', value: html }) - } - } - // TODO: not convinced this is the best way to handle this - if ( - !rendering && - htmlBlocks.filter(h => h.value.trim() !== '').length === 0 - ) { - console.warn('No HTML found, parse results:', parsed.children) - htmlBlocks = [ - { - type: 'code', - lang: 'html', - value: `

Couldn't find any HTML, LLM Response:

${cleanMarkdown === '' ? `
${escapeHTML(markdown)}
` : cleanMarkdown}
` - } - ] - } - const jsBlocks = parsed.children.filter( - c => c.type === 'code' && c.lang === 'javascript' - ) as Code[] - result.html = fixHTML( - [ - ...htmlBlocks.map(h => h.value), - ...jsBlocks.map(j => ``) - ].join('\n') - ) - return result + let commentary = cleanMarkdown + + if (indexMatch) { + commentary = commentary.replace(indexMatch[0], '') + let match + while ((match = jsRegex.exec(commentary))) { + jsBlocks.push(match[1]) + } + commentary = commentary.replace(jsRegex, '').trim() + result.commentary = commentary + result.html = fixHTML( + [ + indexMatch[1].trim(), + ...jsBlocks.map(j => ``) + ].join('\n') + ) + return result + } + + // Fall back to parsing markdown for code fences + const htmlFence = cleanMarkdown.match(/```html\n([\s\S]*?)```/i) + if (htmlFence) { + commentary = commentary.replace(htmlFence[0], '') + } + + let match + while ((match = jsRegex.exec(commentary))) { + jsBlocks.push(match[1]) + } + commentary = commentary.replace(jsRegex, '').trim() + + result.commentary = commentary + const htmlContent = htmlFence ? htmlFence[1].trim() : '' + + if (!htmlContent && !rendering) { + console.warn('No HTML found in markdown') + } + + result.html = fixHTML( + [ + htmlContent, + ...jsBlocks.map(j => ``) + ].join('\n') + ) + + return result } diff --git a/frontend/src/state/atoms/history.ts b/frontend/src/state/atoms/history.ts index 25b2c69..f699533 100644 --- a/frontend/src/state/atoms/history.ts +++ b/frontend/src/state/atoms/history.ts @@ -39,8 +39,9 @@ export interface HistoryItem { // TODO: Deprecate react?: string components?: FrameworkMap - html?: string - comments?: string[] + html?: string + comments?: string[] + commentary?: string } interface Chapter {