import { escapeHtml } from "./contracts/html.ts";
/*
* What an AI chat's code block or canvas is, and the page Полка saves from it.
* Shared by the «На Полку» extension (extensions/chrome/src/shared/payload.ts)
* and the server's ChatGPT import (apps/server/url-import/providers/chatgpt.ts),
* so both read a source the same way. Pure: no DOM, no Node APIs.
*/
export type SourceKind = "html" | "html-fragment" | "jsx" | "tsx" | "svg" | "markdown" | "text";
/** A saved page: an HTML document, or a React component for the runtime. */
export type SourceBody = { html: string } | { component: string; componentLanguage: "jsx" | "tsx" };
const lang = (hint: string | null) =>
(hint ?? "")
.toLowerCase()
.replace(/^language-/, "")
.trim();
export function detectKind(source: string, hint: string | null): SourceKind {
const text = source.replace(/^/, "").trim();
const language = lang(hint);
if (/^(\s*)*]/i.test(text) || /
]/i.test(text.slice(0, 20000))) return "html";
if (/^(<\?xml[^>]*>\s*)?