import { loadFiddle } from './api.js'; import { getFrameworkRuntime } from './js-preprocessors.js'; const MODE_MAP = { javascript: 'html-css-js', typescript: 'typescript', react: 'react', 'react-ts': 'react-ts', vue: 'vue', svelte: 'svelte', }; async function init() { // Extract fiddle ID from URL: /embed/:id const match = location.pathname.match(/^\/embed\/([a-zA-Z0-9_-]+)$/); if (!match) { document.body.innerHTML = '
'; return; } // Theme const params = new URLSearchParams(location.search); if (params.get('theme') === 'dark') document.body.classList.add('dark'); try { const fiddle = await loadFiddle(match[1]); const mode = MODE_MAP[fiddle.js_type] || 'html-css-js'; const runtime = getFrameworkRuntime(mode); // For embed, we compile client-side using the same preprocessors const { compileCss } = await import('./preprocessors.js'); const { compileJs } = await import('./js-preprocessors.js'); const compiledCss = await compileCss(fiddle.css, fiddle.css_type || 'css'); const result = await compileJs(fiddle.js, mode); const allCss = result.extraCss ? `${compiledCss}\n${result.extraCss}` : compiledCss; let bodyContent; if (mode === 'vue' || mode === 'svelte') { bodyContent = fiddle.html ? `${fiddle.html}\n${runtime.bodyHtml}` : runtime.bodyHtml; } else if (runtime.bodyHtml) { bodyContent = `${fiddle.html}\n${runtime.bodyHtml}`; } else { bodyContent = fiddle.html; } let scripts = ''; if (result.js) { if (result.isModule) { scripts = `