import { serializeInlineScript } from "./html"; const VIEW_TRANSITION_STYLE = ``; export function injectViewTransitions(html: string, enabled: boolean): string { if (!enabled || /]*\bdata-tradjs-view-transitions\b/i.test(html)) return html; if (/<\/head\s*>/i.test(html)) { const bodyClose = [...html.matchAll(/<\/body\s*>/gi)].at(-1); let readinessLink = ""; if (bodyClose) { let markerId = "tradjs-document-end"; for (let index = 1; html.includes(markerId); index++) { markerId = `tradjs-document-end-${index}`; } const marker = ``; html = html.slice(0, bodyClose.index) + marker + html.slice(bodyClose.index); readinessLink = ``; } return html.replace( /<\/head\s*>/i, () => `${VIEW_TRANSITION_STYLE}${readinessLink}`, ); } return html.replace( /^(\s*)?/i, (prefix) => `${prefix}${VIEW_TRANSITION_STYLE}`, ); } export function createClientInitializer( urls: readonly string[], context: { params: Record; pathname: string; }, blockRendering: boolean, ): string { if (urls.length === 0) return ""; const modules = urls.map((_, i) => `client${i}`); const imports = urls .map( (url, i) => `import * as ${modules[i]} from ${serializeInlineScript(url)};`, ) .join("\n"); return ``; }