Files
DeepTutor/web/lib/iframe-html.ts
Bingxi Zhao (Frank) 2745e8bde9 feat(book): introduce book capability and restructure guide/co_writer modules
- Add new `deeptutor/book/` module with multi-agent pipeline
  (ideation, spine, page planner, source explorer, synthesizer)
  and rich block system (text, code, figure, quiz, flash cards,
  callout, deep dive, timeline, animation, concept graph, etc.)
- Add `/api/routers/book.py` and `deeptutor_cli/book.py` entry points
- Add web UI under `web/app/(workspace)/book/` with sidebar,
  chat panel, page reader, spine editor, progress timeline, and
  block renderers
- Move `deeptutor/agents/co_writer` -> `deeptutor/co_writer` and
  `deeptutor/agents/guide` -> `deeptutor/guide` (then remove guide
  entirely now superseded by book)
- Remove legacy guide UI, hooks, types, and API router
- Update visualize agents/prompts, notebook service, prompt manager,
  session turn runtime, and shared services to support new flows
- Refresh chat composer, sidebar, notebook selector, and locales

Made-with: Cursor
2026-04-20 00:33:33 +08:00

90 lines
3.7 KiB
TypeScript

/**
* Helpers for rendering AI-generated HTML inside a sandboxed `<iframe>`:
* - {@link injectKaTeX} ensures the page can render `$...$` / `$$...$$`
* even if the model didn't include KaTeX itself.
* - {@link sanitizeIframeHtml} strips bare `<script>` blocks (except the
* KaTeX init shim) and any inline event handlers / `javascript:` URLs.
*
* These were originally written for the (now-deprecated) Guided Learning
* page; the visualize capability now reuses them for `render_mode=html`.
*/
const KATEX_RESOURCES = [
'<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css" crossorigin="anonymous">',
'<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js" crossorigin="anonymous"><' +
"/script>",
'<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/auto-render.min.js" crossorigin="anonymous"><' +
"/script>",
].join("\n ");
// eslint-disable-next-line no-template-curly-in-string
const KATEX_INIT_SCRIPT =
"<script data-katex-init>" +
'document.addEventListener("DOMContentLoaded",function(){var t=0,i=setInterval(function(){if(typeof renderMathInElement==="function"){clearInterval(i);try{renderMathInElement(document.body,{delimiters:[{left:"$$",right:"$$",display:true},{left:"$",right:"$",display:false},{left:"\\\\(",right:"\\\\)",display:false},{left:"\\\\[",right:"\\\\]",display:true}],throwOnError:false})}catch(e){console.error("[KaTeX] Error:",e)}}else if(++t>50){clearInterval(i);console.warn("[KaTeX] Timeout")}},100)});' +
"<" +
"/script>";
const KATEX_HEAD = KATEX_RESOURCES + "\n " + KATEX_INIT_SCRIPT;
/**
* Inject KaTeX (CSS + JS + auto-render init) into the document's `<head>`.
* No-op if the document already references KaTeX.
*/
export function injectKaTeX(html: string): string {
const lower = html.toLowerCase();
const hasKaTeX =
lower.includes("katex.min.css") ||
lower.includes("katex.min.js") ||
lower.includes("katex@") ||
lower.includes("cdn.jsdelivr.net/npm/katex") ||
lower.includes("unpkg.com/katex");
if (hasKaTeX) return html;
if (html.includes("</head>")) {
return html.replace("</head>", KATEX_HEAD + "\n</head>");
}
if (html.includes("<head>")) {
return html.replace(/<head([^>]*)>/i, "<head$1>\n" + KATEX_HEAD);
}
if (html.includes("<html")) {
return html.replace(
/(<html[^>]*>)/i,
'$1\n<head>\n <meta charset="UTF-8">\n <meta name="viewport" content="width=device-width, initial-scale=1.0">\n' +
KATEX_HEAD +
"\n</head>",
);
}
return (
'<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <meta name="viewport" content="width=device-width, initial-scale=1.0">\n' +
KATEX_HEAD +
"\n</head>\n<body>\n" +
html +
"\n</body>\n</html>"
);
}
/**
* Light defense-in-depth on top of `sandbox="allow-scripts"` (without
* `allow-same-origin`): strip `javascript:` URLs and (best-effort) any
* `<a target="_top">` / `target="_parent"` so a misbehaving model cannot
* navigate the parent frame. We deliberately keep `<script>` tags and
* inline `on*=` handlers because the model is *expected* to ship
* interactive JS — and the sandbox already isolates it in a null origin
* with no access to the host page.
*/
export function sanitizeIframeHtml(html: string): string {
return html
.replace(/\s(href|src|formaction)\s*=\s*(['"])\s*javascript:[\s\S]*?\2/gi, "")
.replace(/\starget\s*=\s*(['"])_(top|parent)\1/gi, ' target="_self"');
}
/**
* Convenience: inject KaTeX, then sanitize. Suitable for a one-shot iframe
* `srcdoc` write.
*/
export function prepareIframeHtml(html: string): string {
return sanitizeIframeHtml(injectKaTeX(html));
}