MCPcopy
hub / github.com/OpenCoworkAI/open-codesign / buildPreviewDocument

Function buildPreviewDocument

packages/runtime/src/index.ts:595–624  ·  view source on GitHub ↗
(
  userSource: string,
  opts: BuildPreviewDocumentOptions = {},
)

Source from the content-addressed store, hash-verified

593}
594
595export function buildPreviewDocument(
596 userSource: string,
597 opts: BuildPreviewDocumentOptions = {},
598): string {
599 const stripped = removeCspMetaTags(userSource);
600 // Already-wrapped srcdoc (round-trip safe). When the workspace preview path
601 // supplies a base URL, inject it once so relative assets still resolve.
602 if (stripped.includes(JSX_TEMPLATE_BEGIN)) {
603 return injectBaseHrefIntoHtmlDocument(stripped, opts.baseHref);
604 }
605
606 const classified = classifyRenderableSource(stripped, opts.path);
607 const kind = classified === 'unknown' && opts.path === undefined ? 'jsx' : classified;
608 if (kind === 'unknown') {
609 throw new Error(`Unsupported preview file type: ${opts.path ?? 'unknown'}`);
610 }
611
612 if (kind === 'html') {
613 const withRuntime = needsJsxRuntimeInHtml(stripped)
614 ? injectJsxRuntimeIntoHtml(stripped)
615 : stripped;
616 return injectOverlayIntoHtmlDocument(
617 injectPreviewViewportSupportIntoHtmlDocument(
618 injectBaseHrefIntoHtmlDocument(withRuntime, opts.baseHref),
619 ),
620 );
621 }
622
623 return wrapJsxAsSrcdoc(stripped, { kind, baseHref: opts.baseHref });
624}
625
626function ensureStandaloneShell(html: string): string {
627 const trimmed = html.trim();

Callers 6

index.test.tsFile · 0.90
PreviewSlotFunction · 0.90
WorkspaceFilePreviewFunction · 0.90
DesignCardPreviewFunction · 0.90
runPreviewFunction · 0.90
buildSrcdocFunction · 0.85

Tested by

no test coverage detected