markdown-flow-ui 0.1.90 → 0.1.91-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/@braintree_sanitize-url@7.1.1/node_modules/@braintree/sanitize-url/dist/index.cjs.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/@braintree_sanitize-url@7.1.1/node_modules/@braintree/sanitize-url/dist/index.cjs.js.map +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/@braintree_sanitize-url@7.1.1/node_modules/@braintree/sanitize-url/dist/index.es.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/classnames@2.5.1/node_modules/classnames/index.cjs.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/classnames@2.5.1/node_modules/classnames/index.es.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/mermaid@11.12.1/node_modules/mermaid/dist/chunks/mermaid.core/c4Diagram-YG6GDRKO.cjs.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/mermaid@11.12.1/node_modules/mermaid/dist/chunks/mermaid.core/c4Diagram-YG6GDRKO.es.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/mermaid@11.12.1/node_modules/mermaid/dist/chunks/mermaid.core/chunk-S3R3BYOJ.cjs.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/mermaid@11.12.1/node_modules/mermaid/dist/chunks/mermaid.core/chunk-S3R3BYOJ.es.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/mermaid@11.12.1/node_modules/mermaid/dist/chunks/mermaid.core/chunk-TZMSLE5B.cjs.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/mermaid@11.12.1/node_modules/mermaid/dist/chunks/mermaid.core/chunk-TZMSLE5B.es.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/mermaid@11.12.1/node_modules/mermaid/dist/chunks/mermaid.core/ganttDiagram-LVOFAZNH.cjs.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/mermaid@11.12.1/node_modules/mermaid/dist/chunks/mermaid.core/ganttDiagram-LVOFAZNH.es.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/mermaid@11.12.1/node_modules/mermaid/dist/chunks/mermaid.core/sequenceDiagram-WL72ISMW.cjs.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/mermaid@11.12.1/node_modules/mermaid/dist/chunks/mermaid.core/sequenceDiagram-WL72ISMW.es.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/rc-input@1.8.0_react-dom@19.0.1_react@19.0.1__react@19.0.1/node_modules/rc-input/es/BaseInput.cjs.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/rc-input@1.8.0_react-dom@19.0.1_react@19.0.1__react@19.0.1/node_modules/rc-input/es/BaseInput.es.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/rc-textarea@1.10.2_react-dom@19.0.1_react@19.0.1__react@19.0.1/node_modules/rc-textarea/es/ResizableTextArea.cjs.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/rc-textarea@1.10.2_react-dom@19.0.1_react@19.0.1__react@19.0.1/node_modules/rc-textarea/es/ResizableTextArea.es.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/rc-textarea@1.10.2_react-dom@19.0.1_react@19.0.1__react@19.0.1/node_modules/rc-textarea/es/TextArea.cjs.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/rc-textarea@1.10.2_react-dom@19.0.1_react@19.0.1__react@19.0.1/node_modules/rc-textarea/es/TextArea.es.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/rc-textarea@1.10.2_react-dom@19.0.1_react@19.0.1__react@19.0.1/node_modules/rc-textarea/es/index.cjs.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/rc-textarea@1.10.2_react-dom@19.0.1_react@19.0.1__react@19.0.1/node_modules/rc-textarea/es/index.es.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/remark-flow@0.1.6/node_modules/remark-flow/dist/index.cjs.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/remark-flow@0.1.6/node_modules/remark-flow/dist/index.es.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/remark-flow@0.1.6/node_modules/remark-flow/dist/remark-custom-variable.cjs.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/remark-flow@0.1.6/node_modules/remark-flow/dist/remark-custom-variable.es.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/remark-flow@0.1.6/node_modules/remark-flow/dist/remark-interaction.cjs.js +1 -1
- package/dist/Documents/ai-shifu/markdown-flow-ui/node_modules/.pnpm/remark-flow@0.1.6/node_modules/remark-flow/dist/remark-interaction.es.js +1 -1
- package/dist/_virtual/index.cjs10.js +1 -1
- package/dist/_virtual/index.cjs2.js +1 -1
- package/dist/_virtual/index.cjs3.js +1 -1
- package/dist/_virtual/index.cjs4.js +1 -1
- package/dist/_virtual/index.cjs7.js +1 -1
- package/dist/_virtual/index.cjs9.js +1 -1
- package/dist/_virtual/index.es10.js +2 -2
- package/dist/_virtual/index.es2.js +5 -2
- package/dist/_virtual/index.es2.js.map +1 -1
- package/dist/_virtual/index.es3.js +2 -4
- package/dist/_virtual/index.es3.js.map +1 -1
- package/dist/_virtual/index.es4.js +4 -5
- package/dist/_virtual/index.es4.js.map +1 -1
- package/dist/_virtual/index.es7.js +2 -3
- package/dist/_virtual/index.es7.js.map +1 -1
- package/dist/_virtual/index.es9.js +3 -2
- package/dist/_virtual/index.es9.js.map +1 -1
- package/dist/assets/markdown-flow-ui.css +1 -1
- package/dist/components/ContentRender/IframeSandbox.cjs.js +3 -3
- package/dist/components/ContentRender/IframeSandbox.cjs.js.map +1 -1
- package/dist/components/ContentRender/IframeSandbox.es.js +19 -19
- package/dist/components/ContentRender/IframeSandbox.es.js.map +1 -1
- package/dist/components/ContentRender/utils/split-content.cjs.js +3 -3
- package/dist/components/ContentRender/utils/split-content.cjs.js.map +1 -1
- package/dist/components/ContentRender/utils/split-content.es.js +94 -81
- package/dist/components/ContentRender/utils/split-content.es.js.map +1 -1
- package/dist/components/ui/inputGroup/textarea.cjs.js +1 -1
- package/dist/components/ui/inputGroup/textarea.es.js +1 -1
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const h=require("../../_virtual/jsx-runtime.cjs.js"),n=require("react"),$=require("react-dom/client"),B=require("./SandboxApp.cjs.js"),P=require("./utils/split-content.cjs.js"),_=require("./ContentRender.cjs.js"),L=({content:f,type:S,className:Y,loadingText:C,styleLoadingText:H,scriptLoadingText:j,fullScreenButtonText:p,hideFullScreen:M=!1,mode:c="content"})=>{const q=n.useRef(null),
|
|
2
|
-
`))||""},[f,c]),N=n.useMemo(()=>{const e=u.trim();if(!e)return!1;const t=e.match(/^<([a-zA-Z][\w:-]*)(\s[^>]*?)?>/);if(!t)return!1;const r=t[2]||"",o=r.match(/\bheight\s*=\s*["']([^"']+)["']/i);if(o&&/vh$/i.test(o[1].trim()))return!0;const d=r.match(/\bstyle\s*=\s*["']([^"']+)["']/i);return d?/height\s*:\s*[^;]*vh\b/i.test(d[1]):!1},[u]);n.useEffect(()=>{if(c!=="blackboard"){v.current=u;return}const e=v.current;!(e&&u.startsWith(e))&&e&&O(r=>r+1),v.current=u},[u,c]),n.useEffect(()=>{const e=
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const h=require("../../_virtual/jsx-runtime.cjs.js"),n=require("react"),$=require("react-dom/client"),B=require("./SandboxApp.cjs.js"),P=require("./utils/split-content.cjs.js"),_=require("./ContentRender.cjs.js"),L=({content:f,type:S,className:Y,loadingText:C,styleLoadingText:H,scriptLoadingText:j,fullScreenButtonText:p,hideFullScreen:M=!1,mode:c="content"})=>{const q=n.useRef(null),i=n.useRef(null),x=n.useRef(null),k=n.useRef(null),R=n.useRef(()=>{}),[z,A]=n.useState(480),[F,O]=n.useState(0),[D,I]=n.useState(!1),v=n.useRef(""),u=n.useMemo(()=>{const t=P.splitContentSegments(f).filter(o=>o.type==="sandbox");return(c==="blackboard"?t[t.length-1]?.value||"":t.map(o=>o.value).join(`
|
|
2
|
+
`))||""},[f,c]),N=n.useMemo(()=>{const e=u.trim();if(!e)return!1;const t=e.match(/^<([a-zA-Z][\w:-]*)(\s[^>]*?)?>/);if(!t)return!1;const r=t[2]||"",o=r.match(/\bheight\s*=\s*["']([^"']+)["']/i);if(o&&/vh$/i.test(o[1].trim()))return!0;const d=r.match(/\bstyle\s*=\s*["']([^"']+)["']/i);return d?/height\s*:\s*[^;]*vh\b/i.test(d[1]):!1},[u]);n.useEffect(()=>{if(c!=="blackboard"){v.current=u;return}const e=v.current;!(e&&u.startsWith(e))&&e&&O(r=>r+1),v.current=u},[u,c]),n.useEffect(()=>{const e=i.current;if(!e)return;const t=e.contentDocument;if(!t)return;t.open(),t.write(`<!DOCTYPE html>
|
|
3
3
|
<html>
|
|
4
4
|
<head>
|
|
5
5
|
<meta charset="utf-8" />
|
|
@@ -11,5 +11,5 @@
|
|
|
11
11
|
<body>
|
|
12
12
|
<div id="root"></div>
|
|
13
13
|
</body>
|
|
14
|
-
</html>`),t.close(),k.current=t;const r=t.getElementById("root");if(!r)return;const o=$.createRoot(r);x.current=o;const d=(m,a)=>{const s=m.trim().toLowerCase();if(!s)return null;const
|
|
14
|
+
</html>`),t.close(),k.current=t;const r=t.getElementById("root");if(!r)return;const o=$.createRoot(r);x.current=o;const d=(m,a)=>{const s=m.trim().toLowerCase();if(!s)return null;const l=Number.parseFloat(s);return Number.isNaN(l)?null:s.endsWith("vh")?l/100*a:s.endsWith("px")||/^[0-9.]+$/.test(s)?l:null},W=()=>{if(!i.current||!t.body)return null;const a=t.body.querySelector(".sandbox-wrapper")?.firstElementChild;if(!a)return null;const s=Array.from(a.children);if(s.length!==1)return null;const l=s[0],b=l.style.height||l.getAttribute("height");if(!b)return null;const w=i.current.ownerDocument?.documentElement?.clientHeight||window.innerHeight,g=d(b,w);return g?Math.ceil(g):null},y=()=>{if(!i.current||!t.body)return;const m=t.body.getBoundingClientRect(),a=t.documentElement?.getBoundingClientRect(),s=m.height,l=a?.height||0,b=Math.max(s,l),w=W(),g=Math.max(200,w??Math.ceil(b));A(g)};R.current=y,y();const E=new ResizeObserver(()=>y());return E.observe(t.body),r&&E.observe(r),()=>{E.disconnect(),setTimeout(()=>{o.unmount(),x.current=null,k.current=null,R.current=()=>{}},0)}},[]),n.useEffect(()=>{const e=()=>{I(!!document.fullscreenElement)};return document.addEventListener("fullscreenchange",e),()=>document.removeEventListener("fullscreenchange",e)},[]);const V=()=>{const e=q.current||i.current;if(e){if(document.fullscreenElement){document.exitFullscreen().catch(()=>{});return}e.requestFullscreen&&e.requestFullscreen().catch(()=>{})}};return n.useEffect(()=>{const e=x.current;e&&(e.render(h.jsxRuntimeExports.jsx(B.default,{html:u,loadingText:C,styleLoadingText:H,scriptLoadingText:j,fullScreenButtonText:p,hideFullScreen:M,resetToken:F,hasRootVhHeight:N,mode:c})),requestAnimationFrame(()=>R.current?.()))},[f,u,C,H,j,p,F,c]),h.jsxRuntimeExports.jsxs("div",{ref:q,"data-root-vh":N?"true":"false",className:"w-full h-full overflow-auto relative flex flex-col content-render-iframe-sandbox",children:[!M&&h.jsxRuntimeExports.jsx("button",{type:"button",onClick:V,className:"absolute top-2 right-2 z-50 p-1.5 bg-black/75 text-white rounded-md cursor-pointer",children:D?"退出全屏":p||"全屏浏览"}),c==="blackboard"&&S==="markdown"?h.jsxRuntimeExports.jsx(_.default,{content:f}):h.jsxRuntimeExports.jsx("iframe",{ref:i,sandbox:"allow-scripts allow-same-origin",allow:"fullscreen",allowFullScreen:!0,className:"w-full",style:{height:c==="blackboard"?"100%":`${z}px`}})]})};exports.default=L;
|
|
15
15
|
//# sourceMappingURL=IframeSandbox.cjs.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IframeSandbox.cjs.js","sources":["../../../src/components/ContentRender/IframeSandbox.tsx"],"sourcesContent":["import React, { useEffect, useRef, useState } from \"react\";\nimport { createRoot, Root } from \"react-dom/client\";\nimport SandboxApp from \"./SandboxApp\";\nimport { splitContentSegments } from \"./utils/split-content\";\nimport ContentRender from \"./ContentRender\";\nexport interface IframeSandboxProps {\n content: string;\n className?: string;\n loadingText?: string;\n styleLoadingText?: string;\n scriptLoadingText?: string;\n fullScreenButtonText?: string;\n hideFullScreen?: boolean;\n mode?: \"content\" | \"blackboard\";\n type: \"sandbox\" | \"markdown\";\n}\n\nconst IframeSandbox: React.FC<IframeSandboxProps> = ({\n content,\n type,\n className,\n loadingText,\n styleLoadingText,\n scriptLoadingText,\n fullScreenButtonText,\n hideFullScreen = false,\n mode = \"content\",\n}) => {\n const containerRef = useRef<HTMLDivElement>(null);\n const iframeRef = useRef<HTMLIFrameElement>(null);\n const rootRef = useRef<Root | null>(null);\n const docRef = useRef<Document | null>(null);\n const updateHeightRef = useRef<() => void>(() => {});\n const [height, setHeight] = useState(480);\n const [resetToken, setResetToken] = useState(0);\n const [isFullscreen, setIsFullscreen] = useState(false);\n const prevHtmlRef = useRef<string>(\"\");\n const htmlContent = React.useMemo(() => {\n const segments = splitContentSegments(content);\n // console.log('segments=====', segments);\n const sandboxSegments = segments.filter((seg) => seg.type === \"sandbox\");\n const sandboxContent =\n mode === \"blackboard\"\n ? sandboxSegments[sandboxSegments.length - 1]?.value || \"\"\n : sandboxSegments.map((seg) => seg.value).join(\"\\n\");\n return sandboxContent || \"\";\n }, [content, mode]);\n const hasRootVhHeight = React.useMemo(() => {\n const normalized = htmlContent.trim();\n if (!normalized) return false;\n const rootMatch = normalized.match(/^<([a-zA-Z][\\w:-]*)(\\s[^>]*?)?>/);\n if (!rootMatch) return false;\n const attrs = rootMatch[2] || \"\";\n const heightAttrMatch = attrs.match(/\\bheight\\s*=\\s*[\"']([^\"']+)[\"']/i);\n if (heightAttrMatch && /vh$/i.test(heightAttrMatch[1].trim())) {\n return true;\n }\n const styleAttrMatch = attrs.match(/\\bstyle\\s*=\\s*[\"']([^\"']+)[\"']/i);\n if (!styleAttrMatch) return false;\n return /height\\s*:\\s*[^;]*vh\\b/i.test(styleAttrMatch[1]);\n }, [htmlContent]);\n useEffect(() => {\n if (mode !== \"blackboard\") {\n prevHtmlRef.current = htmlContent;\n return;\n }\n const prev = prevHtmlRef.current;\n const isContinuation = prev && htmlContent.startsWith(prev);\n if (!isContinuation && prev) {\n setResetToken((token) => token + 1);\n }\n prevHtmlRef.current = htmlContent;\n }, [htmlContent, mode]);\n\n useEffect(() => {\n const iframe = iframeRef.current;\n if (!iframe) return undefined;\n\n const doc = iframe.contentDocument;\n if (!doc) return undefined;\n\n doc.open();\n doc.write(`<!DOCTYPE html>\n<html>\n <head>\n <meta charset=\"utf-8\" />\n <style>\n html, body { margin: 0; padding: 0; }\n *, *::before, *::after { box-sizing: border-box; }\n </style>\n </head>\n <body>\n <div id=\"root\"></div>\n </body>\n</html>`);\n doc.close();\n docRef.current = doc;\n\n const rootEl = doc.getElementById(\"root\");\n if (!rootEl) return undefined;\n\n const root = createRoot(rootEl);\n rootRef.current = root;\n\n const parseExplicitHeight = (\n value: string,\n parentViewportHeight: number\n ) => {\n const normalized = value.trim().toLowerCase();\n if (!normalized) return null;\n const numeric = Number.parseFloat(normalized);\n if (Number.isNaN(numeric)) return null;\n if (normalized.endsWith(\"vh\")) {\n return (numeric / 100) * parentViewportHeight;\n }\n if (normalized.endsWith(\"px\") || /^[0-9.]+$/.test(normalized)) {\n return numeric;\n }\n return null;\n };\n\n const resolveExplicitHeight = () => {\n if (!iframeRef.current || !doc.body) return null;\n const wrapper = doc.body.querySelector(\n \".sandbox-wrapper\"\n ) as HTMLElement | null;\n const container = wrapper?.firstElementChild as HTMLElement | null;\n if (!container) return null;\n const elements = Array.from(container.children) as HTMLElement[];\n if (elements.length !== 1) return null;\n const target = elements[0];\n const heightValue = target.style.height || target.getAttribute(\"height\");\n if (!heightValue) return null;\n const parentViewportHeight =\n iframeRef.current.ownerDocument?.documentElement?.clientHeight ||\n window.innerHeight;\n const parsed = parseExplicitHeight(heightValue, parentViewportHeight);\n return parsed ? Math.ceil(parsed) : null;\n };\n\n const updateHeight = () => {\n if (!iframeRef.current || !doc.body) return;\n const bodyRect = doc.body.getBoundingClientRect();\n const htmlRect = doc.documentElement?.getBoundingClientRect();\n const bodyHeight = bodyRect.height;\n const htmlHeight = htmlRect?.height || 0;\n const contentHeight = Math.max(bodyHeight, htmlHeight);\n const explicitHeight = resolveExplicitHeight();\n const nextHeight = Math.max(\n 200,\n explicitHeight ?? Math.ceil(contentHeight)\n );\n setHeight(nextHeight);\n };\n updateHeightRef.current = updateHeight;\n\n updateHeight();\n\n const resizeObserver = new ResizeObserver(() => updateHeight());\n resizeObserver.observe(doc.body);\n if (rootEl) {\n resizeObserver.observe(rootEl);\n }\n\n return () => {\n resizeObserver.disconnect();\n // Defer unmount to avoid React warning when parent is mid-render\n setTimeout(() => {\n root.unmount();\n rootRef.current = null;\n docRef.current = null;\n updateHeightRef.current = () => {};\n }, 0);\n };\n }, []);\n\n useEffect(() => {\n const onFullscreenChange = () => {\n setIsFullscreen(Boolean(document.fullscreenElement));\n };\n document.addEventListener(\"fullscreenchange\", onFullscreenChange);\n return () =>\n document.removeEventListener(\"fullscreenchange\", onFullscreenChange);\n }, []);\n\n const toggleFullscreen = () => {\n const target = containerRef.current || iframeRef.current;\n if (!target) return;\n if (document.fullscreenElement) {\n document.exitFullscreen().catch(() => {});\n return;\n }\n if (target.requestFullscreen) {\n target.requestFullscreen().catch(() => {});\n }\n };\n\n useEffect(() => {\n const root = rootRef.current;\n if (!root) return;\n\n root.render(\n <SandboxApp\n html={htmlContent}\n loadingText={loadingText}\n styleLoadingText={styleLoadingText}\n scriptLoadingText={scriptLoadingText}\n fullScreenButtonText={fullScreenButtonText}\n hideFullScreen={hideFullScreen}\n resetToken={resetToken}\n hasRootVhHeight={hasRootVhHeight}\n mode={mode}\n />\n );\n requestAnimationFrame(() => updateHeightRef.current?.());\n }, [\n content,\n htmlContent,\n loadingText,\n styleLoadingText,\n scriptLoadingText,\n fullScreenButtonText,\n resetToken,\n mode,\n ]);\n\n return (\n <div\n ref={containerRef}\n data-root-vh={hasRootVhHeight ? \"true\" : \"false\"}\n className={\n \"w-full h-full overflow-auto relative flex flex-col justify-center content-render-iframe-sandbox\"\n }\n >\n {!hideFullScreen && (\n <button\n type=\"button\"\n onClick={toggleFullscreen}\n className={\n \"absolute top-2 right-2 z-50 p-1.5 bg-black/75 text-white rounded-md cursor-pointer\"\n }\n >\n {isFullscreen ? \"退出全屏\" : fullScreenButtonText || \"全屏浏览\"}\n </button>\n )}\n {mode === \"blackboard\" && type === \"markdown\" ? (\n <ContentRender content={content} />\n ) : (\n <iframe\n ref={iframeRef}\n sandbox=\"allow-scripts allow-same-origin\"\n allow=\"fullscreen\"\n allowFullScreen\n className={(className, \"w-full\")}\n style={{\n height: mode === \"blackboard\" ? \"100%\" : `${height}px`,\n // height: `${height}px`,\n // margin: \"16px 0\",\n }}\n />\n )}\n </div>\n );\n};\n\nexport default IframeSandbox;\n"],"names":["IframeSandbox","content","type","className","loadingText","styleLoadingText","scriptLoadingText","fullScreenButtonText","hideFullScreen","mode","containerRef","useRef","iframeRef","rootRef","docRef","updateHeightRef","height","setHeight","useState","resetToken","setResetToken","isFullscreen","setIsFullscreen","prevHtmlRef","htmlContent","React","sandboxSegments","splitContentSegments","seg","hasRootVhHeight","normalized","rootMatch","attrs","heightAttrMatch","styleAttrMatch","useEffect","prev","token","iframe","doc","rootEl","root","createRoot","parseExplicitHeight","value","parentViewportHeight","numeric","resolveExplicitHeight","container","elements","target","heightValue","parsed","updateHeight","bodyRect","htmlRect","bodyHeight","htmlHeight","contentHeight","explicitHeight","nextHeight","resizeObserver","onFullscreenChange","toggleFullscreen","jsx","SandboxApp","jsxs","ContentRender"],"mappings":"iUAiBMA,EAA8C,CAAC,CACnD,QAAAC,EACA,KAAAC,EACA,UAAAC,EACA,YAAAC,EACA,iBAAAC,EACA,kBAAAC,EACA,qBAAAC,EACA,eAAAC,EAAiB,GACjB,KAAAC,EAAO,SACT,IAAM,CACJ,MAAMC,EAAeC,EAAAA,OAAuB,IAAI,EAC1CC,EAAYD,EAAAA,OAA0B,IAAI,EAC1CE,EAAUF,EAAAA,OAAoB,IAAI,EAClCG,EAASH,EAAAA,OAAwB,IAAI,EACrCI,EAAkBJ,EAAAA,OAAmB,IAAM,CAAC,CAAC,EAC7C,CAACK,EAAQC,CAAS,EAAIC,EAAAA,SAAS,GAAG,EAClC,CAACC,EAAYC,CAAa,EAAIF,EAAAA,SAAS,CAAC,EACxC,CAACG,EAAcC,CAAe,EAAIJ,EAAAA,SAAS,EAAK,EAChDK,EAAcZ,EAAAA,OAAe,EAAE,EAC/Ba,EAAcC,EAAM,QAAQ,IAAM,CAGtC,MAAMC,EAFWC,EAAAA,qBAAqB1B,CAAO,EAEZ,OAAQ2B,GAAQA,EAAI,OAAS,SAAS,EAKvE,OAHEnB,IAAS,aACLiB,EAAgBA,EAAgB,OAAS,CAAC,GAAG,OAAS,GACtDA,EAAgB,IAAKE,GAAQA,EAAI,KAAK,EAAE,KAAK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D,SAAA1C,EAAe,OAASd,GAAwB,MAAA,CAAA,EAGpDE,IAAS,cAAgBP,IAAS,WACjC8D,EAAAA,kBAAAA,IAACG,UAAA,CAAc,QAAAlE,EAAkB,EAEjC+D,EAAAA,kBAAAA,IAAC,SAAA,CACC,IAAKpD,EACL,QAAQ,kCACR,MAAM,aACN,gBAAe,GACf,UAAuB,SACvB,MAAO,CACL,OAAQH,IAAS,aAAe,OAAS,GAAGO,CAAM,IAAA,CAGpD,CAAA,CACF,CAAA,CAAA,CAIR"}
|
|
1
|
+
{"version":3,"file":"IframeSandbox.cjs.js","sources":["../../../src/components/ContentRender/IframeSandbox.tsx"],"sourcesContent":["import React, { useEffect, useRef, useState } from \"react\";\nimport { createRoot, Root } from \"react-dom/client\";\nimport SandboxApp from \"./SandboxApp\";\nimport { splitContentSegments } from \"./utils/split-content\";\nimport ContentRender from \"./ContentRender\";\nexport interface IframeSandboxProps {\n content: string;\n className?: string;\n loadingText?: string;\n styleLoadingText?: string;\n scriptLoadingText?: string;\n fullScreenButtonText?: string;\n hideFullScreen?: boolean;\n mode?: \"content\" | \"blackboard\";\n type: \"sandbox\" | \"markdown\";\n}\n\nconst IframeSandbox: React.FC<IframeSandboxProps> = ({\n content,\n type,\n className,\n loadingText,\n styleLoadingText,\n scriptLoadingText,\n fullScreenButtonText,\n hideFullScreen = false,\n mode = \"content\",\n}) => {\n const containerRef = useRef<HTMLDivElement>(null);\n const iframeRef = useRef<HTMLIFrameElement>(null);\n const rootRef = useRef<Root | null>(null);\n const docRef = useRef<Document | null>(null);\n const updateHeightRef = useRef<() => void>(() => {});\n const [height, setHeight] = useState(480);\n const [resetToken, setResetToken] = useState(0);\n const [isFullscreen, setIsFullscreen] = useState(false);\n const prevHtmlRef = useRef<string>(\"\");\n const htmlContent = React.useMemo(() => {\n const segments = splitContentSegments(content);\n // console.log('segments=====', segments);\n const sandboxSegments = segments.filter((seg) => seg.type === \"sandbox\");\n const sandboxContent =\n mode === \"blackboard\"\n ? sandboxSegments[sandboxSegments.length - 1]?.value || \"\"\n : sandboxSegments.map((seg) => seg.value).join(\"\\n\");\n return sandboxContent || \"\";\n }, [content, mode]);\n const hasRootVhHeight = React.useMemo(() => {\n const normalized = htmlContent.trim();\n if (!normalized) return false;\n const rootMatch = normalized.match(/^<([a-zA-Z][\\w:-]*)(\\s[^>]*?)?>/);\n if (!rootMatch) return false;\n const attrs = rootMatch[2] || \"\";\n const heightAttrMatch = attrs.match(/\\bheight\\s*=\\s*[\"']([^\"']+)[\"']/i);\n if (heightAttrMatch && /vh$/i.test(heightAttrMatch[1].trim())) {\n return true;\n }\n const styleAttrMatch = attrs.match(/\\bstyle\\s*=\\s*[\"']([^\"']+)[\"']/i);\n if (!styleAttrMatch) return false;\n return /height\\s*:\\s*[^;]*vh\\b/i.test(styleAttrMatch[1]);\n }, [htmlContent]);\n useEffect(() => {\n if (mode !== \"blackboard\") {\n prevHtmlRef.current = htmlContent;\n return;\n }\n const prev = prevHtmlRef.current;\n const isContinuation = prev && htmlContent.startsWith(prev);\n if (!isContinuation && prev) {\n setResetToken((token) => token + 1);\n }\n prevHtmlRef.current = htmlContent;\n }, [htmlContent, mode]);\n\n useEffect(() => {\n const iframe = iframeRef.current;\n if (!iframe) return undefined;\n\n const doc = iframe.contentDocument;\n if (!doc) return undefined;\n\n doc.open();\n doc.write(`<!DOCTYPE html>\n<html>\n <head>\n <meta charset=\"utf-8\" />\n <style>\n html, body { margin: 0; padding: 0; }\n *, *::before, *::after { box-sizing: border-box; }\n </style>\n </head>\n <body>\n <div id=\"root\"></div>\n </body>\n</html>`);\n doc.close();\n docRef.current = doc;\n\n const rootEl = doc.getElementById(\"root\");\n if (!rootEl) return undefined;\n\n const root = createRoot(rootEl);\n rootRef.current = root;\n\n const parseExplicitHeight = (\n value: string,\n parentViewportHeight: number\n ) => {\n const normalized = value.trim().toLowerCase();\n if (!normalized) return null;\n const numeric = Number.parseFloat(normalized);\n if (Number.isNaN(numeric)) return null;\n if (normalized.endsWith(\"vh\")) {\n return (numeric / 100) * parentViewportHeight;\n }\n if (normalized.endsWith(\"px\") || /^[0-9.]+$/.test(normalized)) {\n return numeric;\n }\n return null;\n };\n\n const resolveExplicitHeight = () => {\n if (!iframeRef.current || !doc.body) return null;\n const wrapper = doc.body.querySelector(\n \".sandbox-wrapper\"\n ) as HTMLElement | null;\n const container = wrapper?.firstElementChild as HTMLElement | null;\n if (!container) return null;\n const elements = Array.from(container.children) as HTMLElement[];\n if (elements.length !== 1) return null;\n const target = elements[0];\n const heightValue = target.style.height || target.getAttribute(\"height\");\n if (!heightValue) return null;\n const parentViewportHeight =\n iframeRef.current.ownerDocument?.documentElement?.clientHeight ||\n window.innerHeight;\n const parsed = parseExplicitHeight(heightValue, parentViewportHeight);\n return parsed ? Math.ceil(parsed) : null;\n };\n\n const updateHeight = () => {\n if (!iframeRef.current || !doc.body) return;\n const bodyRect = doc.body.getBoundingClientRect();\n const htmlRect = doc.documentElement?.getBoundingClientRect();\n const bodyHeight = bodyRect.height;\n const htmlHeight = htmlRect?.height || 0;\n const contentHeight = Math.max(bodyHeight, htmlHeight);\n const explicitHeight = resolveExplicitHeight();\n const nextHeight = Math.max(\n 200,\n explicitHeight ?? Math.ceil(contentHeight)\n );\n setHeight(nextHeight);\n };\n updateHeightRef.current = updateHeight;\n\n updateHeight();\n\n const resizeObserver = new ResizeObserver(() => updateHeight());\n resizeObserver.observe(doc.body);\n if (rootEl) {\n resizeObserver.observe(rootEl);\n }\n\n return () => {\n resizeObserver.disconnect();\n // Defer unmount to avoid React warning when parent is mid-render\n setTimeout(() => {\n root.unmount();\n rootRef.current = null;\n docRef.current = null;\n updateHeightRef.current = () => {};\n }, 0);\n };\n }, []);\n\n useEffect(() => {\n const onFullscreenChange = () => {\n setIsFullscreen(Boolean(document.fullscreenElement));\n };\n document.addEventListener(\"fullscreenchange\", onFullscreenChange);\n return () =>\n document.removeEventListener(\"fullscreenchange\", onFullscreenChange);\n }, []);\n\n const toggleFullscreen = () => {\n const target = containerRef.current || iframeRef.current;\n if (!target) return;\n if (document.fullscreenElement) {\n document.exitFullscreen().catch(() => {});\n return;\n }\n if (target.requestFullscreen) {\n target.requestFullscreen().catch(() => {});\n }\n };\n\n useEffect(() => {\n const root = rootRef.current;\n if (!root) return;\n\n root.render(\n <SandboxApp\n html={htmlContent}\n loadingText={loadingText}\n styleLoadingText={styleLoadingText}\n scriptLoadingText={scriptLoadingText}\n fullScreenButtonText={fullScreenButtonText}\n hideFullScreen={hideFullScreen}\n resetToken={resetToken}\n hasRootVhHeight={hasRootVhHeight}\n mode={mode}\n />\n );\n requestAnimationFrame(() => updateHeightRef.current?.());\n }, [\n content,\n htmlContent,\n loadingText,\n styleLoadingText,\n scriptLoadingText,\n fullScreenButtonText,\n resetToken,\n mode,\n ]);\n\n return (\n <div\n ref={containerRef}\n data-root-vh={hasRootVhHeight ? \"true\" : \"false\"}\n className={\n \"w-full h-full overflow-auto relative flex flex-col content-render-iframe-sandbox\"\n }\n >\n {!hideFullScreen && (\n <button\n type=\"button\"\n onClick={toggleFullscreen}\n className={\n \"absolute top-2 right-2 z-50 p-1.5 bg-black/75 text-white rounded-md cursor-pointer\"\n }\n >\n {isFullscreen ? \"退出全屏\" : fullScreenButtonText || \"全屏浏览\"}\n </button>\n )}\n {mode === \"blackboard\" && type === \"markdown\" ? (\n <ContentRender content={content} />\n ) : (\n <iframe\n ref={iframeRef}\n sandbox=\"allow-scripts allow-same-origin\"\n allow=\"fullscreen\"\n allowFullScreen\n className={(className, \"w-full\")}\n style={{\n height: mode === \"blackboard\" ? \"100%\" : `${height}px`,\n // height: `${height}px`,\n // margin: \"16px 0\",\n }}\n />\n )}\n </div>\n );\n};\n\nexport default IframeSandbox;\n"],"names":["IframeSandbox","content","type","className","loadingText","styleLoadingText","scriptLoadingText","fullScreenButtonText","hideFullScreen","mode","containerRef","useRef","iframeRef","rootRef","docRef","updateHeightRef","height","setHeight","useState","resetToken","setResetToken","isFullscreen","setIsFullscreen","prevHtmlRef","htmlContent","React","sandboxSegments","splitContentSegments","seg","hasRootVhHeight","normalized","rootMatch","attrs","heightAttrMatch","styleAttrMatch","useEffect","prev","token","iframe","doc","rootEl","root","createRoot","parseExplicitHeight","value","parentViewportHeight","numeric","resolveExplicitHeight","container","elements","target","heightValue","parsed","updateHeight","bodyRect","htmlRect","bodyHeight","htmlHeight","contentHeight","explicitHeight","nextHeight","resizeObserver","onFullscreenChange","toggleFullscreen","jsx","SandboxApp","jsxs","ContentRender"],"mappings":"iUAiBMA,EAA8C,CAAC,CACnD,QAAAC,EACA,KAAAC,EACA,UAAAC,EACA,YAAAC,EACA,iBAAAC,EACA,kBAAAC,EACA,qBAAAC,EACA,eAAAC,EAAiB,GACjB,KAAAC,EAAO,SACT,IAAM,CACJ,MAAMC,EAAeC,EAAAA,OAAuB,IAAI,EAC1CC,EAAYD,EAAAA,OAA0B,IAAI,EAC1CE,EAAUF,EAAAA,OAAoB,IAAI,EAClCG,EAASH,EAAAA,OAAwB,IAAI,EACrCI,EAAkBJ,EAAAA,OAAmB,IAAM,CAAC,CAAC,EAC7C,CAACK,EAAQC,CAAS,EAAIC,EAAAA,SAAS,GAAG,EAClC,CAACC,EAAYC,CAAa,EAAIF,EAAAA,SAAS,CAAC,EACxC,CAACG,EAAcC,CAAe,EAAIJ,EAAAA,SAAS,EAAK,EAChDK,EAAcZ,EAAAA,OAAe,EAAE,EAC/Ba,EAAcC,EAAM,QAAQ,IAAM,CAGtC,MAAMC,EAFWC,EAAAA,qBAAqB1B,CAAO,EAEZ,OAAQ2B,GAAQA,EAAI,OAAS,SAAS,EAKvE,OAHEnB,IAAS,aACLiB,EAAgBA,EAAgB,OAAS,CAAC,GAAG,OAAS,GACtDA,EAAgB,IAAKE,GAAQA,EAAI,KAAK,EAAE,KAAK;AAAA,CAAI,IAC9B,EAC3B,EAAG,CAAC3B,EAASQ,CAAI,CAAC,EACZoB,EAAkBJ,EAAM,QAAQ,IAAM,CAC1C,MAAMK,EAAaN,EAAY,KAAA,EAC/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D,EAAAA,kBAAAA,IAAC,SAAA,CACC,IAAKpD,EACL,QAAQ,kCACR,MAAM,aACN,gBAAe,GACf,UAAuB,SACvB,MAAO,CACL,OAAQH,IAAS,aAAe,OAAS,GAAGO,CAAM,IAAA,CAGpD,CAAA,CACF,CAAA,CAAA,CAIR"}
|
|
@@ -8,15 +8,15 @@ const ee = ({
|
|
|
8
8
|
content: m,
|
|
9
9
|
type: D,
|
|
10
10
|
className: G,
|
|
11
|
-
loadingText:
|
|
12
|
-
styleLoadingText:
|
|
13
|
-
scriptLoadingText:
|
|
11
|
+
loadingText: k,
|
|
12
|
+
styleLoadingText: F,
|
|
13
|
+
scriptLoadingText: M,
|
|
14
14
|
fullScreenButtonText: x,
|
|
15
|
-
hideFullScreen:
|
|
15
|
+
hideFullScreen: j = !1,
|
|
16
16
|
mode: s = "content"
|
|
17
17
|
}) => {
|
|
18
|
-
const N = a(null), i = a(null), v = a(null), z = a(null),
|
|
19
|
-
}), [I, O] = E(480), [A, V] = E(0), [W, $] = E(!1),
|
|
18
|
+
const N = a(null), i = a(null), v = a(null), z = a(null), w = a(() => {
|
|
19
|
+
}), [I, O] = E(480), [A, V] = E(0), [W, $] = E(!1), y = a(""), c = S.useMemo(() => {
|
|
20
20
|
const t = Y(m).filter((o) => o.type === "sandbox");
|
|
21
21
|
return (s === "blackboard" ? t[t.length - 1]?.value || "" : t.map((o) => o.value).join(`
|
|
22
22
|
`)) || "";
|
|
@@ -33,11 +33,11 @@ const ee = ({
|
|
|
33
33
|
}, [c]);
|
|
34
34
|
p(() => {
|
|
35
35
|
if (s !== "blackboard") {
|
|
36
|
-
|
|
36
|
+
y.current = c;
|
|
37
37
|
return;
|
|
38
38
|
}
|
|
39
|
-
const e =
|
|
40
|
-
!(e && c.startsWith(e)) && e && V((n) => n + 1),
|
|
39
|
+
const e = y.current;
|
|
40
|
+
!(e && c.startsWith(e)) && e && V((n) => n + 1), y.current = c;
|
|
41
41
|
}, [c, s]), p(() => {
|
|
42
42
|
const e = i.current;
|
|
43
43
|
if (!e) return;
|
|
@@ -85,11 +85,11 @@ const ee = ({
|
|
|
85
85
|
);
|
|
86
86
|
O(g);
|
|
87
87
|
};
|
|
88
|
-
|
|
88
|
+
w.current = R, R();
|
|
89
89
|
const H = new ResizeObserver(() => R());
|
|
90
90
|
return H.observe(t.body), n && H.observe(n), () => {
|
|
91
91
|
H.disconnect(), setTimeout(() => {
|
|
92
|
-
o.unmount(), v.current = null, z.current = null,
|
|
92
|
+
o.unmount(), v.current = null, z.current = null, w.current = () => {
|
|
93
93
|
};
|
|
94
94
|
}, 0);
|
|
95
95
|
};
|
|
@@ -118,23 +118,23 @@ const ee = ({
|
|
|
118
118
|
P,
|
|
119
119
|
{
|
|
120
120
|
html: c,
|
|
121
|
-
loadingText:
|
|
122
|
-
styleLoadingText:
|
|
123
|
-
scriptLoadingText:
|
|
121
|
+
loadingText: k,
|
|
122
|
+
styleLoadingText: F,
|
|
123
|
+
scriptLoadingText: M,
|
|
124
124
|
fullScreenButtonText: x,
|
|
125
|
-
hideFullScreen:
|
|
125
|
+
hideFullScreen: j,
|
|
126
126
|
resetToken: A,
|
|
127
127
|
hasRootVhHeight: q,
|
|
128
128
|
mode: s
|
|
129
129
|
}
|
|
130
130
|
)
|
|
131
|
-
), requestAnimationFrame(() =>
|
|
131
|
+
), requestAnimationFrame(() => w.current?.()));
|
|
132
132
|
}, [
|
|
133
133
|
m,
|
|
134
134
|
c,
|
|
135
|
-
j,
|
|
136
135
|
k,
|
|
137
136
|
F,
|
|
137
|
+
M,
|
|
138
138
|
x,
|
|
139
139
|
A,
|
|
140
140
|
s
|
|
@@ -143,9 +143,9 @@ const ee = ({
|
|
|
143
143
|
{
|
|
144
144
|
ref: N,
|
|
145
145
|
"data-root-vh": q ? "true" : "false",
|
|
146
|
-
className: "w-full h-full overflow-auto relative flex flex-col
|
|
146
|
+
className: "w-full h-full overflow-auto relative flex flex-col content-render-iframe-sandbox",
|
|
147
147
|
children: [
|
|
148
|
-
!
|
|
148
|
+
!j && /* @__PURE__ */ h.jsx(
|
|
149
149
|
"button",
|
|
150
150
|
{
|
|
151
151
|
type: "button",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IframeSandbox.es.js","sources":["../../../src/components/ContentRender/IframeSandbox.tsx"],"sourcesContent":["import React, { useEffect, useRef, useState } from \"react\";\nimport { createRoot, Root } from \"react-dom/client\";\nimport SandboxApp from \"./SandboxApp\";\nimport { splitContentSegments } from \"./utils/split-content\";\nimport ContentRender from \"./ContentRender\";\nexport interface IframeSandboxProps {\n content: string;\n className?: string;\n loadingText?: string;\n styleLoadingText?: string;\n scriptLoadingText?: string;\n fullScreenButtonText?: string;\n hideFullScreen?: boolean;\n mode?: \"content\" | \"blackboard\";\n type: \"sandbox\" | \"markdown\";\n}\n\nconst IframeSandbox: React.FC<IframeSandboxProps> = ({\n content,\n type,\n className,\n loadingText,\n styleLoadingText,\n scriptLoadingText,\n fullScreenButtonText,\n hideFullScreen = false,\n mode = \"content\",\n}) => {\n const containerRef = useRef<HTMLDivElement>(null);\n const iframeRef = useRef<HTMLIFrameElement>(null);\n const rootRef = useRef<Root | null>(null);\n const docRef = useRef<Document | null>(null);\n const updateHeightRef = useRef<() => void>(() => {});\n const [height, setHeight] = useState(480);\n const [resetToken, setResetToken] = useState(0);\n const [isFullscreen, setIsFullscreen] = useState(false);\n const prevHtmlRef = useRef<string>(\"\");\n const htmlContent = React.useMemo(() => {\n const segments = splitContentSegments(content);\n // console.log('segments=====', segments);\n const sandboxSegments = segments.filter((seg) => seg.type === \"sandbox\");\n const sandboxContent =\n mode === \"blackboard\"\n ? sandboxSegments[sandboxSegments.length - 1]?.value || \"\"\n : sandboxSegments.map((seg) => seg.value).join(\"\\n\");\n return sandboxContent || \"\";\n }, [content, mode]);\n const hasRootVhHeight = React.useMemo(() => {\n const normalized = htmlContent.trim();\n if (!normalized) return false;\n const rootMatch = normalized.match(/^<([a-zA-Z][\\w:-]*)(\\s[^>]*?)?>/);\n if (!rootMatch) return false;\n const attrs = rootMatch[2] || \"\";\n const heightAttrMatch = attrs.match(/\\bheight\\s*=\\s*[\"']([^\"']+)[\"']/i);\n if (heightAttrMatch && /vh$/i.test(heightAttrMatch[1].trim())) {\n return true;\n }\n const styleAttrMatch = attrs.match(/\\bstyle\\s*=\\s*[\"']([^\"']+)[\"']/i);\n if (!styleAttrMatch) return false;\n return /height\\s*:\\s*[^;]*vh\\b/i.test(styleAttrMatch[1]);\n }, [htmlContent]);\n useEffect(() => {\n if (mode !== \"blackboard\") {\n prevHtmlRef.current = htmlContent;\n return;\n }\n const prev = prevHtmlRef.current;\n const isContinuation = prev && htmlContent.startsWith(prev);\n if (!isContinuation && prev) {\n setResetToken((token) => token + 1);\n }\n prevHtmlRef.current = htmlContent;\n }, [htmlContent, mode]);\n\n useEffect(() => {\n const iframe = iframeRef.current;\n if (!iframe) return undefined;\n\n const doc = iframe.contentDocument;\n if (!doc) return undefined;\n\n doc.open();\n doc.write(`<!DOCTYPE html>\n<html>\n <head>\n <meta charset=\"utf-8\" />\n <style>\n html, body { margin: 0; padding: 0; }\n *, *::before, *::after { box-sizing: border-box; }\n </style>\n </head>\n <body>\n <div id=\"root\"></div>\n </body>\n</html>`);\n doc.close();\n docRef.current = doc;\n\n const rootEl = doc.getElementById(\"root\");\n if (!rootEl) return undefined;\n\n const root = createRoot(rootEl);\n rootRef.current = root;\n\n const parseExplicitHeight = (\n value: string,\n parentViewportHeight: number\n ) => {\n const normalized = value.trim().toLowerCase();\n if (!normalized) return null;\n const numeric = Number.parseFloat(normalized);\n if (Number.isNaN(numeric)) return null;\n if (normalized.endsWith(\"vh\")) {\n return (numeric / 100) * parentViewportHeight;\n }\n if (normalized.endsWith(\"px\") || /^[0-9.]+$/.test(normalized)) {\n return numeric;\n }\n return null;\n };\n\n const resolveExplicitHeight = () => {\n if (!iframeRef.current || !doc.body) return null;\n const wrapper = doc.body.querySelector(\n \".sandbox-wrapper\"\n ) as HTMLElement | null;\n const container = wrapper?.firstElementChild as HTMLElement | null;\n if (!container) return null;\n const elements = Array.from(container.children) as HTMLElement[];\n if (elements.length !== 1) return null;\n const target = elements[0];\n const heightValue = target.style.height || target.getAttribute(\"height\");\n if (!heightValue) return null;\n const parentViewportHeight =\n iframeRef.current.ownerDocument?.documentElement?.clientHeight ||\n window.innerHeight;\n const parsed = parseExplicitHeight(heightValue, parentViewportHeight);\n return parsed ? Math.ceil(parsed) : null;\n };\n\n const updateHeight = () => {\n if (!iframeRef.current || !doc.body) return;\n const bodyRect = doc.body.getBoundingClientRect();\n const htmlRect = doc.documentElement?.getBoundingClientRect();\n const bodyHeight = bodyRect.height;\n const htmlHeight = htmlRect?.height || 0;\n const contentHeight = Math.max(bodyHeight, htmlHeight);\n const explicitHeight = resolveExplicitHeight();\n const nextHeight = Math.max(\n 200,\n explicitHeight ?? Math.ceil(contentHeight)\n );\n setHeight(nextHeight);\n };\n updateHeightRef.current = updateHeight;\n\n updateHeight();\n\n const resizeObserver = new ResizeObserver(() => updateHeight());\n resizeObserver.observe(doc.body);\n if (rootEl) {\n resizeObserver.observe(rootEl);\n }\n\n return () => {\n resizeObserver.disconnect();\n // Defer unmount to avoid React warning when parent is mid-render\n setTimeout(() => {\n root.unmount();\n rootRef.current = null;\n docRef.current = null;\n updateHeightRef.current = () => {};\n }, 0);\n };\n }, []);\n\n useEffect(() => {\n const onFullscreenChange = () => {\n setIsFullscreen(Boolean(document.fullscreenElement));\n };\n document.addEventListener(\"fullscreenchange\", onFullscreenChange);\n return () =>\n document.removeEventListener(\"fullscreenchange\", onFullscreenChange);\n }, []);\n\n const toggleFullscreen = () => {\n const target = containerRef.current || iframeRef.current;\n if (!target) return;\n if (document.fullscreenElement) {\n document.exitFullscreen().catch(() => {});\n return;\n }\n if (target.requestFullscreen) {\n target.requestFullscreen().catch(() => {});\n }\n };\n\n useEffect(() => {\n const root = rootRef.current;\n if (!root) return;\n\n root.render(\n <SandboxApp\n html={htmlContent}\n loadingText={loadingText}\n styleLoadingText={styleLoadingText}\n scriptLoadingText={scriptLoadingText}\n fullScreenButtonText={fullScreenButtonText}\n hideFullScreen={hideFullScreen}\n resetToken={resetToken}\n hasRootVhHeight={hasRootVhHeight}\n mode={mode}\n />\n );\n requestAnimationFrame(() => updateHeightRef.current?.());\n }, [\n content,\n htmlContent,\n loadingText,\n styleLoadingText,\n scriptLoadingText,\n fullScreenButtonText,\n resetToken,\n mode,\n ]);\n\n return (\n <div\n ref={containerRef}\n data-root-vh={hasRootVhHeight ? \"true\" : \"false\"}\n className={\n \"w-full h-full overflow-auto relative flex flex-col justify-center content-render-iframe-sandbox\"\n }\n >\n {!hideFullScreen && (\n <button\n type=\"button\"\n onClick={toggleFullscreen}\n className={\n \"absolute top-2 right-2 z-50 p-1.5 bg-black/75 text-white rounded-md cursor-pointer\"\n }\n >\n {isFullscreen ? \"退出全屏\" : fullScreenButtonText || \"全屏浏览\"}\n </button>\n )}\n {mode === \"blackboard\" && type === \"markdown\" ? (\n <ContentRender content={content} />\n ) : (\n <iframe\n ref={iframeRef}\n sandbox=\"allow-scripts allow-same-origin\"\n allow=\"fullscreen\"\n allowFullScreen\n className={(className, \"w-full\")}\n style={{\n height: mode === \"blackboard\" ? \"100%\" : `${height}px`,\n // height: `${height}px`,\n // margin: \"16px 0\",\n }}\n />\n )}\n </div>\n );\n};\n\nexport default IframeSandbox;\n"],"names":["IframeSandbox","content","type","className","loadingText","styleLoadingText","scriptLoadingText","fullScreenButtonText","hideFullScreen","mode","containerRef","useRef","iframeRef","rootRef","docRef","updateHeightRef","height","setHeight","useState","resetToken","setResetToken","isFullscreen","setIsFullscreen","prevHtmlRef","htmlContent","React","sandboxSegments","splitContentSegments","seg","hasRootVhHeight","normalized","rootMatch","attrs","heightAttrMatch","styleAttrMatch","useEffect","prev","token","iframe","doc","rootEl","root","createRoot","parseExplicitHeight","value","parentViewportHeight","numeric","resolveExplicitHeight","container","elements","target","heightValue","parsed","updateHeight","bodyRect","htmlRect","bodyHeight","htmlHeight","contentHeight","explicitHeight","nextHeight","resizeObserver","onFullscreenChange","toggleFullscreen","jsx","SandboxApp","jsxs","ContentRender"],"mappings":";;;;;;AAiBA,MAAMA,KAA8C,CAAC;AAAA,EACnD,SAAAC;AAAA,EACA,MAAAC;AAAA,EACA,WAAAC;AAAA,EACA,aAAAC;AAAA,EACA,kBAAAC;AAAA,EACA,mBAAAC;AAAA,EACA,sBAAAC;AAAA,EACA,gBAAAC,IAAiB;AAAA,EACjB,MAAAC,IAAO;AACT,MAAM;AACJ,QAAMC,IAAeC,EAAuB,IAAI,GAC1CC,IAAYD,EAA0B,IAAI,GAC1CE,IAAUF,EAAoB,IAAI,GAClCG,IAASH,EAAwB,IAAI,GACrCI,IAAkBJ,EAAmB,MAAM;AAAA,EAAC,CAAC,GAC7C,CAACK,GAAQC,CAAS,IAAIC,EAAS,GAAG,GAClC,CAACC,GAAYC,CAAa,IAAIF,EAAS,CAAC,GACxC,CAACG,GAAcC,CAAe,IAAIJ,EAAS,EAAK,GAChDK,IAAcZ,EAAe,EAAE,GAC/Ba,IAAcC,EAAM,QAAQ,MAAM;AAGtC,UAAMC,IAFWC,EAAqB1B,CAAO,EAEZ,OAAO,CAAC2B,MAAQA,EAAI,SAAS,SAAS;AAKvE,YAHEnB,MAAS,eACLiB,EAAgBA,EAAgB,SAAS,CAAC,GAAG,SAAS,KACtDA,EAAgB,IAAI,CAACE,MAAQA,EAAI,KAAK,EAAE,KAAK;AAAA,CAAI,MAC9B;AAAA,EAC3B,GAAG,CAAC3B,GAASQ,CAAI,CAAC,GACZoB,IAAkBJ,EAAM,QAAQ,MAAM;AAC1C,UAAMK,IAAaN,EAAY,KAAA;AAC/B,QAAI,CAACM,EAAY,QAAO;AACxB,UAAMC,IAAYD,EAAW,MAAM,iCAAiC;AACpE,QAAI,CAACC,EAAW,QAAO;AACvB,UAAMC,IAAQD,EAAU,CAAC,KAAK,IACxBE,IAAkBD,EAAM,MAAM,kCAAkC;AACtE,QAAIC,KAAmB,OAAO,K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,UAAA;AAAA,YACC,MAAK;AAAA,YACL,SAASD;AAAA,YACT,WACE;AAAA,YAGD,UAAA1C,IAAe,SAASd,KAAwB;AAAA,UAAA;AAAA,QAAA;AAAA,QAGpDE,MAAS,gBAAgBP,MAAS,aACjC8D,gBAAAA,EAAAA,IAACG,GAAA,EAAc,SAAAlE,GAAkB,IAEjC+D,gBAAAA,EAAAA;AAAAA,UAAC;AAAA,UAAA;AAAA,YACC,KAAKpD;AAAA,YACL,SAAQ;AAAA,YACR,OAAM;AAAA,YACN,iBAAe;AAAA,YACf,WAAuB;AAAA,YACvB,OAAO;AAAA,cACL,QAAQH,MAAS,eAAe,SAAS,GAAGO,CAAM;AAAA;AAAA;AAAA,YAAA;AAAA,UAGpD;AAAA,QAAA;AAAA,MACF;AAAA,IAAA;AAAA,EAAA;AAIR;"}
|
|
1
|
+
{"version":3,"file":"IframeSandbox.es.js","sources":["../../../src/components/ContentRender/IframeSandbox.tsx"],"sourcesContent":["import React, { useEffect, useRef, useState } from \"react\";\nimport { createRoot, Root } from \"react-dom/client\";\nimport SandboxApp from \"./SandboxApp\";\nimport { splitContentSegments } from \"./utils/split-content\";\nimport ContentRender from \"./ContentRender\";\nexport interface IframeSandboxProps {\n content: string;\n className?: string;\n loadingText?: string;\n styleLoadingText?: string;\n scriptLoadingText?: string;\n fullScreenButtonText?: string;\n hideFullScreen?: boolean;\n mode?: \"content\" | \"blackboard\";\n type: \"sandbox\" | \"markdown\";\n}\n\nconst IframeSandbox: React.FC<IframeSandboxProps> = ({\n content,\n type,\n className,\n loadingText,\n styleLoadingText,\n scriptLoadingText,\n fullScreenButtonText,\n hideFullScreen = false,\n mode = \"content\",\n}) => {\n const containerRef = useRef<HTMLDivElement>(null);\n const iframeRef = useRef<HTMLIFrameElement>(null);\n const rootRef = useRef<Root | null>(null);\n const docRef = useRef<Document | null>(null);\n const updateHeightRef = useRef<() => void>(() => {});\n const [height, setHeight] = useState(480);\n const [resetToken, setResetToken] = useState(0);\n const [isFullscreen, setIsFullscreen] = useState(false);\n const prevHtmlRef = useRef<string>(\"\");\n const htmlContent = React.useMemo(() => {\n const segments = splitContentSegments(content);\n // console.log('segments=====', segments);\n const sandboxSegments = segments.filter((seg) => seg.type === \"sandbox\");\n const sandboxContent =\n mode === \"blackboard\"\n ? sandboxSegments[sandboxSegments.length - 1]?.value || \"\"\n : sandboxSegments.map((seg) => seg.value).join(\"\\n\");\n return sandboxContent || \"\";\n }, [content, mode]);\n const hasRootVhHeight = React.useMemo(() => {\n const normalized = htmlContent.trim();\n if (!normalized) return false;\n const rootMatch = normalized.match(/^<([a-zA-Z][\\w:-]*)(\\s[^>]*?)?>/);\n if (!rootMatch) return false;\n const attrs = rootMatch[2] || \"\";\n const heightAttrMatch = attrs.match(/\\bheight\\s*=\\s*[\"']([^\"']+)[\"']/i);\n if (heightAttrMatch && /vh$/i.test(heightAttrMatch[1].trim())) {\n return true;\n }\n const styleAttrMatch = attrs.match(/\\bstyle\\s*=\\s*[\"']([^\"']+)[\"']/i);\n if (!styleAttrMatch) return false;\n return /height\\s*:\\s*[^;]*vh\\b/i.test(styleAttrMatch[1]);\n }, [htmlContent]);\n useEffect(() => {\n if (mode !== \"blackboard\") {\n prevHtmlRef.current = htmlContent;\n return;\n }\n const prev = prevHtmlRef.current;\n const isContinuation = prev && htmlContent.startsWith(prev);\n if (!isContinuation && prev) {\n setResetToken((token) => token + 1);\n }\n prevHtmlRef.current = htmlContent;\n }, [htmlContent, mode]);\n\n useEffect(() => {\n const iframe = iframeRef.current;\n if (!iframe) return undefined;\n\n const doc = iframe.contentDocument;\n if (!doc) return undefined;\n\n doc.open();\n doc.write(`<!DOCTYPE html>\n<html>\n <head>\n <meta charset=\"utf-8\" />\n <style>\n html, body { margin: 0; padding: 0; }\n *, *::before, *::after { box-sizing: border-box; }\n </style>\n </head>\n <body>\n <div id=\"root\"></div>\n </body>\n</html>`);\n doc.close();\n docRef.current = doc;\n\n const rootEl = doc.getElementById(\"root\");\n if (!rootEl) return undefined;\n\n const root = createRoot(rootEl);\n rootRef.current = root;\n\n const parseExplicitHeight = (\n value: string,\n parentViewportHeight: number\n ) => {\n const normalized = value.trim().toLowerCase();\n if (!normalized) return null;\n const numeric = Number.parseFloat(normalized);\n if (Number.isNaN(numeric)) return null;\n if (normalized.endsWith(\"vh\")) {\n return (numeric / 100) * parentViewportHeight;\n }\n if (normalized.endsWith(\"px\") || /^[0-9.]+$/.test(normalized)) {\n return numeric;\n }\n return null;\n };\n\n const resolveExplicitHeight = () => {\n if (!iframeRef.current || !doc.body) return null;\n const wrapper = doc.body.querySelector(\n \".sandbox-wrapper\"\n ) as HTMLElement | null;\n const container = wrapper?.firstElementChild as HTMLElement | null;\n if (!container) return null;\n const elements = Array.from(container.children) as HTMLElement[];\n if (elements.length !== 1) return null;\n const target = elements[0];\n const heightValue = target.style.height || target.getAttribute(\"height\");\n if (!heightValue) return null;\n const parentViewportHeight =\n iframeRef.current.ownerDocument?.documentElement?.clientHeight ||\n window.innerHeight;\n const parsed = parseExplicitHeight(heightValue, parentViewportHeight);\n return parsed ? Math.ceil(parsed) : null;\n };\n\n const updateHeight = () => {\n if (!iframeRef.current || !doc.body) return;\n const bodyRect = doc.body.getBoundingClientRect();\n const htmlRect = doc.documentElement?.getBoundingClientRect();\n const bodyHeight = bodyRect.height;\n const htmlHeight = htmlRect?.height || 0;\n const contentHeight = Math.max(bodyHeight, htmlHeight);\n const explicitHeight = resolveExplicitHeight();\n const nextHeight = Math.max(\n 200,\n explicitHeight ?? Math.ceil(contentHeight)\n );\n setHeight(nextHeight);\n };\n updateHeightRef.current = updateHeight;\n\n updateHeight();\n\n const resizeObserver = new ResizeObserver(() => updateHeight());\n resizeObserver.observe(doc.body);\n if (rootEl) {\n resizeObserver.observe(rootEl);\n }\n\n return () => {\n resizeObserver.disconnect();\n // Defer unmount to avoid React warning when parent is mid-render\n setTimeout(() => {\n root.unmount();\n rootRef.current = null;\n docRef.current = null;\n updateHeightRef.current = () => {};\n }, 0);\n };\n }, []);\n\n useEffect(() => {\n const onFullscreenChange = () => {\n setIsFullscreen(Boolean(document.fullscreenElement));\n };\n document.addEventListener(\"fullscreenchange\", onFullscreenChange);\n return () =>\n document.removeEventListener(\"fullscreenchange\", onFullscreenChange);\n }, []);\n\n const toggleFullscreen = () => {\n const target = containerRef.current || iframeRef.current;\n if (!target) return;\n if (document.fullscreenElement) {\n document.exitFullscreen().catch(() => {});\n return;\n }\n if (target.requestFullscreen) {\n target.requestFullscreen().catch(() => {});\n }\n };\n\n useEffect(() => {\n const root = rootRef.current;\n if (!root) return;\n\n root.render(\n <SandboxApp\n html={htmlContent}\n loadingText={loadingText}\n styleLoadingText={styleLoadingText}\n scriptLoadingText={scriptLoadingText}\n fullScreenButtonText={fullScreenButtonText}\n hideFullScreen={hideFullScreen}\n resetToken={resetToken}\n hasRootVhHeight={hasRootVhHeight}\n mode={mode}\n />\n );\n requestAnimationFrame(() => updateHeightRef.current?.());\n }, [\n content,\n htmlContent,\n loadingText,\n styleLoadingText,\n scriptLoadingText,\n fullScreenButtonText,\n resetToken,\n mode,\n ]);\n\n return (\n <div\n ref={containerRef}\n data-root-vh={hasRootVhHeight ? \"true\" : \"false\"}\n className={\n \"w-full h-full overflow-auto relative flex flex-col content-render-iframe-sandbox\"\n }\n >\n {!hideFullScreen && (\n <button\n type=\"button\"\n onClick={toggleFullscreen}\n className={\n \"absolute top-2 right-2 z-50 p-1.5 bg-black/75 text-white rounded-md cursor-pointer\"\n }\n >\n {isFullscreen ? \"退出全屏\" : fullScreenButtonText || \"全屏浏览\"}\n </button>\n )}\n {mode === \"blackboard\" && type === \"markdown\" ? (\n <ContentRender content={content} />\n ) : (\n <iframe\n ref={iframeRef}\n sandbox=\"allow-scripts allow-same-origin\"\n allow=\"fullscreen\"\n allowFullScreen\n className={(className, \"w-full\")}\n style={{\n height: mode === \"blackboard\" ? \"100%\" : `${height}px`,\n // height: `${height}px`,\n // margin: \"16px 0\",\n }}\n />\n )}\n </div>\n );\n};\n\nexport default IframeSandbox;\n"],"names":["IframeSandbox","content","type","className","loadingText","styleLoadingText","scriptLoadingText","fullScreenButtonText","hideFullScreen","mode","containerRef","useRef","iframeRef","rootRef","docRef","updateHeightRef","height","setHeight","useState","resetToken","setResetToken","isFullscreen","setIsFullscreen","prevHtmlRef","htmlContent","React","sandboxSegments","splitContentSegments","seg","hasRootVhHeight","normalized","rootMatch","attrs","heightAttrMatch","styleAttrMatch","useEffect","prev","token","iframe","doc","rootEl","root","createRoot","parseExplicitHeight","value","parentViewportHeight","numeric","resolveExplicitHeight","container","elements","target","heightValue","parsed","updateHeight","bodyRect","htmlRect","bodyHeight","htmlHeight","contentHeight","explicitHeight","nextHeight","resizeObserver","onFullscreenChange","toggleFullscreen","jsx","SandboxApp","jsxs","ContentRender"],"mappings":";;;;;;AAiBA,MAAMA,KAA8C,CAAC;AAAA,EACnD,SAAAC;AAAA,EACA,MAAAC;AAAA,EACA,WAAAC;AAAA,EACA,aAAAC;AAAA,EACA,kBAAAC;AAAA,EACA,mBAAAC;AAAA,EACA,sBAAAC;AAAA,EACA,gBAAAC,IAAiB;AAAA,EACjB,MAAAC,IAAO;AACT,MAAM;AACJ,QAAMC,IAAeC,EAAuB,IAAI,GAC1CC,IAAYD,EAA0B,IAAI,GAC1CE,IAAUF,EAAoB,IAAI,GAClCG,IAASH,EAAwB,IAAI,GACrCI,IAAkBJ,EAAmB,MAAM;AAAA,EAAC,CAAC,GAC7C,CAACK,GAAQC,CAAS,IAAIC,EAAS,GAAG,GAClC,CAACC,GAAYC,CAAa,IAAIF,EAAS,CAAC,GACxC,CAACG,GAAcC,CAAe,IAAIJ,EAAS,EAAK,GAChDK,IAAcZ,EAAe,EAAE,GAC/Ba,IAAcC,EAAM,QAAQ,MAAM;AAGtC,UAAMC,IAFWC,EAAqB1B,CAAO,EAEZ,OAAO,CAAC2B,MAAQA,EAAI,SAAS,SAAS;AAKvE,YAHEnB,MAAS,eACLiB,EAAgBA,EAAgB,SAAS,CAAC,GAAG,SAA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;AAAA,QAAA;AAAA,MAAA;AAAA,IACF,GAEF,sBAAsB,MAAMM,EAAgB,WAAW;AAAA,EACzD,GAAG;AAAA,IACDd;AAAA,IACAuB;AAAA,IACApB;AAAA,IACAC;AAAA,IACAC;AAAA,IACAC;AAAA,IACAY;AAAA,IACAV;AAAA,EAAA,CACD,GAGCyD,gBAAAA,EAAAA;AAAAA,IAAC;AAAA,IAAA;AAAA,MACC,KAAKxD;AAAA,MACL,gBAAcmB,IAAkB,SAAS;AAAA,MACzC,WACE;AAAA,MAGD,UAAA;AAAA,QAAA,CAACrB,KACAwD,gBAAAA,EAAAA;AAAAA,UAAC;AAAA,UAAA;AAAA,YACC,MAAK;AAAA,YACL,SAASD;AAAA,YACT,WACE;AAAA,YAGD,UAAA1C,IAAe,SAASd,KAAwB;AAAA,UAAA;AAAA,QAAA;AAAA,QAGpDE,MAAS,gBAAgBP,MAAS,aACjC8D,gBAAAA,EAAAA,IAACG,GAAA,EAAc,SAAAlE,GAAkB,IAEjC+D,gBAAAA,EAAAA;AAAAA,UAAC;AAAA,UAAA;AAAA,YACC,KAAKpD;AAAA,YACL,SAAQ;AAAA,YACR,OAAM;AAAA,YACN,iBAAe;AAAA,YACf,WAAuB;AAAA,YACvB,OAAO;AAAA,cACL,QAAQH,MAAS,eAAe,SAAS,GAAGO,CAAM;AAAA;AAAA;AAAA,YAAA;AAAA,UAGpD;AAAA,QAAA;AAAA,MACF;AAAA,IAAA;AAAA,EAAA;AAIR;"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
2
|
-
`),
|
|
3
|
-
`);return{start:
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const T=/<(script|style|link|iframe|html|head|body|meta|title|base|template|div|section|article|main)[\s>]/i,_=[/<svg[\s\S]*?<\/svg>/i,/<img\b[^>]*?>/i,/```mermaid[\s\S]*?```/i,/```[a-zA-Z0-9]+[\s\S]*?```/i],A=/!\[[^\]]*]\([^\s)\n]+(?:\s+"[^"]*")?\)/i,R=/<\/[a-z][^>]*>\s*\n(?=[^\s<])/gi,B=/<custom-button-after-content\b[\s\S]*?<\/custom-button-after-content>/gi,P=/^"[\s\S]*"$/,w=/```mermaid[\s\S]*?```/i,N=s=>s.split(/\r?\n/).map(n=>n.trim()).find(Boolean)??"",C=(s,n)=>{if(!P.test(s))return s;const t=s.slice(1,-1);if(!n)return t;const e=t.match(w);if(!e||typeof e.index!="number")return t;const c=t.slice(0,e.index),i=t.slice(e.index+e[0].length),o=N(c),r=N(i);return!o||!r?t:`${o}${e[0]}${r}`},$=s=>{const n=[],t=/```/g;let e;for(;(e=t.exec(s))!==null;){const c=e.index,i=t.exec(s);if(!i){n.push({start:c,end:s.length});break}n.push({start:c,end:i.index+3})}return n},F=(s,n)=>n.some(({start:t,end:e})=>s>=t&&s<e),y=(s,n,t)=>{const e=n.flags.includes("g")?n.flags:`${n.flags}g`,c=new RegExp(n.source,e);let i;for(;(i=c.exec(s))!==null;)if(!F(i.index,t))return i.index;return-1},L=(s,n)=>{let t=s.length,e;for(R.lastIndex=0;e=R.exec(s);)if(!(e.index<=n)){t=e.index+e[0].length;break}return t},D=s=>{if(!s.length)return s;const n=[];return s.forEach(t=>{if(t.type!=="sandbox"){n.push(t);return}B.lastIndex=0;let e=0,c;for(;(c=B.exec(t.value))!==null;){const o=t.value.slice(e,c.index);o.trim()&&n.push({type:"sandbox",value:o}),n.push({type:"markdown",value:c[0]}),e=c.index+c[0].length}const i=t.value.slice(e);i.trim()&&n.push({type:"sandbox",value:i})}),n},z=s=>{let n=null;return _.forEach(t=>{const e=t.exec(s);if(!e||typeof e.index!="number")return;const c=e.index,i=e.index+e[0].length;(!n||c<n.start)&&(n={start:c,end:i})}),n},U=(s,n)=>{const t=y(s,A,n);if(t===-1)return null;const e=s.slice(t).match(A);return e?{start:t,end:t+e[0].length}:null},W=s=>{const n=s.match(/^\s*\|.+\|\s*$/m);if(!n||typeof n.index!="number")return null;const t=n[0].match(/^\s*/)?.[0].length??0,e=n.index+t,c=s.slice(e).split(`
|
|
2
|
+
`),i=[];for(const r of c){if(!r.trim().startsWith("|"))break;i.push(r)}const o=i.join(`
|
|
3
|
+
`);return{start:e,block:o,end:e+o.length}},f=(s,n=!1)=>{const t=C(s,n),e=l=>D(l),c=t.indexOf("```");if(n&&c!==-1&&t.indexOf("```",c+3)===-1)return e([{type:"markdown",value:t}]);const i=$(t),o=y(t,T,i),r=y(t,/<svg\b/i,i),E=o!==-1&&r!==-1&&o<r;if(r!==-1&&!E){const l=t.slice(0,r),a=t.indexOf("</svg>",r),u=a===-1?`${t.slice(r)}</svg>`:t.slice(r,a+6),p=a===-1?"":t.slice(a+6);if(n){const b=[];return l.trim()&&b.push({type:"text",value:l}),b.push({type:"markdown",value:u}),p.trim()&&b.push(...f(p,!0)),e(b)}if(a===-1)return e([{type:"markdown",value:u}])}const m=W(t);if(m){const l=[],a=t.slice(0,m.start);n&&a.trim()&&l.push({type:"text",value:a}),l.push({type:"markdown",value:m.block});const u=t.slice(m.end),p=u.length<t.length;return u.trim()&&p&&l.push(...n?f(u,!0):f(u)),e(l)}const h=z(t),g=U(t,i),d=h&&g?h.start<=g.start?h:g:h??g;if(o===-1&&!d)return n&&t.trim()?e([{type:"text",value:t}]):[];const v=!!d&&(o===-1||d.start<o),S=v?d.start:o,I=v?d.end:L(t,S),x=[],M=t.slice(0,S),O=t.slice(S,I),k=t.slice(I);return n&&M.trim()&&x.push({type:"text",value:M}),x.push({type:v?"markdown":"sandbox",value:O}),k.trim()&&x.push(...f(k,n)),e(x)};exports.splitContentSegments=f;
|
|
4
4
|
//# sourceMappingURL=split-content.cjs.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"split-content.cjs.js","sources":["../../../../src/components/ContentRender/utils/split-content.ts"],"sourcesContent":["export type RenderSegment =\n | { type: \"markdown\"; value: string }\n | { type: \"sandbox\"; value: string }\n | { type: \"text\"; value: string };\n\nconst SANDBOX_START_PATTERN =\n /<(script|style|link|iframe|html|head|body|meta|title|base|template|div|section|article|main)[\\s>]/i;\n\nconst INLINE_SANDBOX_PATTERNS: RegExp[] = [\n /<svg[\\s\\S]*?<\\/svg>/i,\n /<img\\b[^>]*?>/i,\n /```mermaid[\\s\\S]*?```/i,\n /```[a-zA-Z0-9]+[\\s\\S]*?```/i,\n];\nconst MARKDOWN_IMAGE_PATTERN = /!\\[[^\\]]*]\\([^\\s)\\n]+(?:\\s+\"[^\"]*\")?\\)/i;\n\nconst closingBoundary = /<\\/[a-z][^>]*>\\s*\\n(?=[^\\s<])/gi;\nconst CUSTOM_BUTTON_PATTERN =\n /<custom-button-after-content\\b[\\s\\S]*?<\\/custom-button-after-content>/gi;\n\ntype MatchResult = { start: number; end: number };\ntype FenceRange = { start: number; end: number };\n\nconst getFenceRanges = (raw: string): FenceRange[] => {\n const ranges: FenceRange[] = [];\n const fencePattern = /```/g;\n let match: RegExpExecArray | null;\n\n while ((match = fencePattern.exec(raw)) !== null) {\n const start = match.index;\n const closeMatch = fencePattern.exec(raw);\n if (!closeMatch) {\n ranges.push({ start, end: raw.length });\n break;\n }\n ranges.push({ start, end: closeMatch.index + 3 });\n }\n\n return ranges;\n};\n\nconst isIndexInRanges = (index: number, ranges: FenceRange[]) =>\n ranges.some(({ start, end }) => index >= start && index < end);\n\nconst findFirstMatchOutsideFence = (\n raw: string,\n pattern: RegExp,\n fenceRanges: FenceRange[]\n) => {\n const flags = pattern.flags.includes(\"g\")\n ? pattern.flags\n : `${pattern.flags}g`;\n const matcher = new RegExp(pattern.source, flags);\n let match: RegExpExecArray | null;\n\n while ((match = matcher.exec(raw)) !== null) {\n if (!isIndexInRanges(match.index, fenceRanges)) {\n return match.index;\n }\n }\n\n return -1;\n};\n\nconst findHtmlBlockEnd = (raw: string, startIndex: number) => {\n let blockEnd = raw.length;\n let match: RegExpExecArray | null;\n closingBoundary.lastIndex = 0;\n\n while ((match = closingBoundary.exec(raw))) {\n if (match.index <= startIndex) continue;\n blockEnd = match.index + match[0].length;\n break;\n }\n\n return blockEnd;\n};\n\nconst splitCustomButtonsFromSandbox = (segments: RenderSegment[]) => {\n if (!segments.length) return segments;\n const output: RenderSegment[] = [];\n\n segments.forEach((segment) => {\n if (segment.type !== \"sandbox\") {\n output.push(segment);\n return;\n }\n\n CUSTOM_BUTTON_PATTERN.lastIndex = 0;\n let lastIndex = 0;\n let match: RegExpExecArray | null;\n\n while ((match = CUSTOM_BUTTON_PATTERN.exec(segment.value)) !== null) {\n const before = segment.value.slice(lastIndex, match.index);\n if (before.trim()) {\n output.push({ type: \"sandbox\", value: before });\n }\n output.push({ type: \"markdown\", value: match[0] });\n lastIndex = match.index + match[0].length;\n }\n\n const rest = segment.value.slice(lastIndex);\n if (rest.trim()) {\n output.push({ type: \"sandbox\", value: rest });\n }\n });\n\n return output;\n};\n\nconst findInlineSandboxMatch = (raw: string): MatchResult | null => {\n let earliest: MatchResult | null = null;\n\n INLINE_SANDBOX_PATTERNS.forEach((pattern) => {\n const match = pattern.exec(raw);\n if (!match || typeof match.index !== \"number\") return;\n const start = match.index;\n const end = match.index + match[0].length;\n\n if (!earliest || start < earliest.start) {\n earliest = { start, end };\n }\n });\n\n return earliest;\n};\n\nconst findMarkdownImageMatch = (\n raw: string,\n fenceRanges: FenceRange[]\n): MatchResult | null => {\n const start = findFirstMatchOutsideFence(\n raw,\n MARKDOWN_IMAGE_PATTERN,\n fenceRanges\n );\n\n if (start === -1) return null;\n const match = raw.slice(start).match(MARKDOWN_IMAGE_PATTERN);\n if (!match) return null;\n\n return { start, end: start + match[0].length };\n};\n\nconst extractTableBlock = (\n raw: string\n): { start: number; block: string; end: number } | null => {\n const tableMatch = raw.match(/^\\s*\\|.+\\|\\s*$/m);\n if (!tableMatch || typeof tableMatch.index !== \"number\") return null;\n\n const leadingSpaces = tableMatch[0].match(/^\\s*/)?.[0].length ?? 0;\n const tableStart = tableMatch.index + leadingSpaces;\n\n const lines = raw.slice(tableStart).split(\"\\n\");\n const tableLines: string[] = [];\n\n for (const line of lines) {\n const trimmed = line.trim();\n if (!trimmed.startsWith(\"|\")) break;\n tableLines.push(line);\n }\n\n const block = tableLines.join(\"\\n\");\n return { start: tableStart, block, end: tableStart + block.length };\n};\n\n// Split incoming markdown content into markdown and sandbox HTML segments\nexport const splitContentSegments = (\n raw: string,\n keepText = false\n): RenderSegment[] => {\n const finalizeSegments = (segments: RenderSegment[]) =>\n splitCustomButtonsFromSandbox(segments);\n\n const fenceStart = raw.indexOf(\"```\");\n if (keepText && fenceStart !== -1) {\n const closingFence = raw.indexOf(\"```\", fenceStart + 3);\n if (closingFence === -1) {\n return finalizeSegments([{ type: \"markdown\", value: raw }]);\n }\n }\n\n const fenceRanges = getFenceRanges(raw);\n // Avoid treating fenced code blocks as sandbox content.\n const sandboxStartIndex = findFirstMatchOutsideFence(\n raw,\n SANDBOX_START_PATTERN,\n fenceRanges\n );\n const svgOpenIndex = findFirstMatchOutsideFence(raw, /<svg\\b/i, fenceRanges);\n const hasSandboxBeforeSvg =\n sandboxStartIndex !== -1 &&\n svgOpenIndex !== -1 &&\n sandboxStartIndex < svgOpenIndex;\n if (svgOpenIndex !== -1 && !hasSandboxBeforeSvg) {\n const before = raw.slice(0, svgOpenIndex);\n const closeIdx = raw.indexOf(\"</svg>\", svgOpenIndex);\n const svgBlock =\n closeIdx === -1\n ? `${raw.slice(svgOpenIndex)}</svg>`\n : raw.slice(svgOpenIndex, closeIdx + \"</svg>\".length);\n const after = closeIdx === -1 ? \"\" : raw.slice(closeIdx + \"</svg>\".length);\n\n if (keepText) {\n const segments: RenderSegment[] = [];\n if (before.trim()) {\n segments.push({ type: \"text\", value: before });\n }\n segments.push({ type: \"markdown\", value: svgBlock });\n if (after.trim()) {\n segments.push(...splitContentSegments(after, true));\n }\n return finalizeSegments(segments);\n }\n\n if (closeIdx === -1) {\n return finalizeSegments([{ type: \"markdown\", value: svgBlock }]);\n }\n }\n\n const tableBlock = extractTableBlock(raw);\n if (tableBlock) {\n const segments: RenderSegment[] = [];\n const before = raw.slice(0, tableBlock.start);\n if (keepText && before.trim()) {\n segments.push({ type: \"text\", value: before });\n }\n segments.push({ type: \"markdown\", value: tableBlock.block });\n const after = raw.slice(tableBlock.end);\n const hasProgress = after.length < raw.length;\n if (after.trim() && hasProgress) {\n segments.push(\n ...(keepText\n ? splitContentSegments(after, true)\n : splitContentSegments(after))\n );\n }\n return finalizeSegments(segments);\n }\n\n const inlineMatch = findInlineSandboxMatch(raw);\n const markdownImageMatch = findMarkdownImageMatch(raw, fenceRanges);\n const inlineCandidate =\n inlineMatch && markdownImageMatch\n ? inlineMatch.start <= markdownImageMatch.start\n ? inlineMatch\n : markdownImageMatch\n : (inlineMatch ?? markdownImageMatch);\n\n if (sandboxStartIndex === -1 && !inlineCandidate) {\n if (keepText && raw.trim()) {\n return finalizeSegments([{ type: \"text\", value: raw }]);\n }\n return [];\n }\n\n const shouldUseInline =\n !!inlineCandidate &&\n (sandboxStartIndex === -1 || inlineCandidate.start < sandboxStartIndex);\n\n const startIndex = shouldUseInline\n ? inlineCandidate!.start\n : sandboxStartIndex;\n const blockEnd = shouldUseInline\n ? inlineCandidate!.end\n : findHtmlBlockEnd(raw, startIndex);\n\n const segments: RenderSegment[] = [];\n const before = raw.slice(0, startIndex);\n const matchedBlock = raw.slice(startIndex, blockEnd);\n const after = raw.slice(blockEnd);\n\n if (keepText && before.trim()) {\n segments.push({ type: \"text\", value: before });\n }\n\n segments.push({\n type: shouldUseInline ? \"markdown\" : \"sandbox\",\n value: matchedBlock,\n });\n\n if (after.trim()) {\n segments.push(...splitContentSegments(after, keepText));\n }\n\n return finalizeSegments(segments);\n};\n"],"names":["SANDBOX_START_PATTERN","INLINE_SANDBOX_PATTERNS","MARKDOWN_IMAGE_PATTERN","closingBoundary","CUSTOM_BUTTON_PATTERN","getFenceRanges","raw","ranges","fencePattern","match","start","closeMatch","isIndexInRanges","index","end","findFirstMatchOutsideFence","pattern","fenceRanges","flags","matcher","findHtmlBlockEnd","startIndex","blockEnd","splitCustomButtonsFromSandbox","segments","output","segment","lastIndex","before","rest","findInlineSandboxMatch","earliest","findMarkdownImageMatch","extractTableBlock","tableMatch","leadingSpaces","tableStart","lines","tableLines","line","block","splitContentSegments","keepText","finalizeSegments","fenceStart","sandboxStartIndex","svgOpenIndex","hasSandboxBeforeSvg","closeIdx","svgBlock","after","tableBlock","hasProgress","inlineMatch","markdownImageMatch","inlineCandidate","shouldUseInline","matchedBlock"],"mappings":"gFAKA,MAAMA,EACJ,qGAEIC,EAAoC,CACxC,uBACA,iBACA,yBACA,6BACF,EACMC,EAAyB,0CAEzBC,EAAkB,kCAClBC,EACJ,0EAKIC,EAAkBC,GAA8B,CACpD,MAAMC,EAAuB,CAAA,EACvBC,EAAe,OACrB,IAAIC,EAEJ,MAAQA,EAAQD,EAAa,KAAKF,CAAG,KAAO,MAAM,CAChD,MAAMI,EAAQD,EAAM,MACdE,EAAaH,EAAa,KAAKF,CAAG,EACxC,GAAI,CAACK,EAAY,CACfJ,EAAO,KAAK,CAAE,MAAAG,EAAO,IAAKJ,EAAI,OAAQ,EACtC,KACF,CACAC,EAAO,KAAK,CAAE,MAAAG,EAAO,IAAKC,EAAW,MAAQ,EAAG,CAClD,CAEA,OAAOJ,CACT,EAEMK,EAAkB,CAACC,EAAeN,IACtCA,EAAO,KAAK,CAAC,CAAE,MAAAG,EAAO,IAAAI,CAAA,IAAUD,GAASH,GAASG,EAAQC,CAAG,EAEzDC,EAA6B,CACjCT,EACAU,EACAC,IACG,CACH,MAAMC,EAAQF,EAAQ,MAAM,SAAS,GAAG,EACpCA,EAAQ,MACR,GAAGA,EAAQ,KAAK,IACdG,EAAU,IAAI,OAAOH,EAAQ,OAAQE,CAAK,EAChD,IAAIT,EAEJ,MAAQA,EAAQU,EAAQ,KAAKb,CAAG,KAAO,MACrC,GAAI,CAACM,EAAgBH,EAAM,MAAOQ,CAAW,EAC3C,OAAOR,EAAM,MAI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,EAAiBnB,CAAQ,CAClC,CAEA,MAAM6B,EAAcvB,EAAuBxB,CAAG,EACxCgD,EAAqBtB,EAAuB1B,EAAKW,CAAW,EAC5DsC,EACJF,GAAeC,EACXD,EAAY,OAASC,EAAmB,MACtCD,EACAC,EACDD,GAAeC,EAEtB,GAAIT,IAAsB,IAAM,CAACU,EAC/B,OAAIb,GAAYpC,EAAI,OACXqC,EAAiB,CAAC,CAAE,KAAM,OAAQ,MAAOrC,CAAA,CAAK,CAAC,EAEjD,CAAA,EAGT,MAAMkD,EACJ,CAAC,CAACD,IACDV,IAAsB,IAAMU,EAAgB,MAAQV,GAEjDxB,EAAamC,EACfD,EAAiB,MACjBV,EACEvB,EAAWkC,EACbD,EAAiB,IACjBnC,EAAiBd,EAAKe,CAAU,EAE9BG,EAA4B,CAAA,EAC5BI,EAAStB,EAAI,MAAM,EAAGe,CAAU,EAChCoC,EAAenD,EAAI,MAAMe,EAAYC,CAAQ,EAC7C4B,EAAQ5C,EAAI,MAAMgB,CAAQ,EAEhC,OAAIoB,GAAYd,EAAO,QACrBJ,EAAS,KAAK,CAAE,KAAM,OAAQ,MAAOI,EAAQ,EAG/CJ,EAAS,KAAK,CACZ,KAAMgC,EAAkB,WAAa,UACrC,MAAOC,CAAA,CACR,EAEGP,EAAM,QACR1B,EAAS,KAAK,GAAGiB,EAAqBS,EAAOR,CAAQ,CAAC,EAGjDC,EAAiBnB,CAAQ,CAClC"}
|
|
1
|
+
{"version":3,"file":"split-content.cjs.js","sources":["../../../../src/components/ContentRender/utils/split-content.ts"],"sourcesContent":["export type RenderSegment =\n | { type: \"markdown\"; value: string }\n | { type: \"sandbox\"; value: string }\n | { type: \"text\"; value: string };\n\nconst SANDBOX_START_PATTERN =\n /<(script|style|link|iframe|html|head|body|meta|title|base|template|div|section|article|main)[\\s>]/i;\n\nconst INLINE_SANDBOX_PATTERNS: RegExp[] = [\n /<svg[\\s\\S]*?<\\/svg>/i,\n /<img\\b[^>]*?>/i,\n /```mermaid[\\s\\S]*?```/i,\n /```[a-zA-Z0-9]+[\\s\\S]*?```/i,\n];\nconst MARKDOWN_IMAGE_PATTERN = /!\\[[^\\]]*]\\([^\\s)\\n]+(?:\\s+\"[^\"]*\")?\\)/i;\n\nconst closingBoundary = /<\\/[a-z][^>]*>\\s*\\n(?=[^\\s<])/gi;\nconst CUSTOM_BUTTON_PATTERN =\n /<custom-button-after-content\\b[\\s\\S]*?<\\/custom-button-after-content>/gi;\n\ntype MatchResult = { start: number; end: number };\ntype FenceRange = { start: number; end: number };\n\nconst WRAPPED_QUOTES_PATTERN = /^\"[\\s\\S]*\"$/;\nconst MERMAID_BLOCK_PATTERN = /```mermaid[\\s\\S]*?```/i;\n\nconst firstNonEmptyLine = (content: string) =>\n content\n .split(/\\r?\\n/)\n .map((line) => line.trim())\n .find(Boolean) ?? \"\";\n\nconst normalizeQuotedMermaidContent = (raw: string, keepText: boolean) => {\n if (!WRAPPED_QUOTES_PATTERN.test(raw)) return raw;\n\n const unwrapped = raw.slice(1, -1);\n if (!keepText) return unwrapped;\n\n const mermaidMatch = unwrapped.match(MERMAID_BLOCK_PATTERN);\n if (!mermaidMatch || typeof mermaidMatch.index !== \"number\") {\n return unwrapped;\n }\n\n const before = unwrapped.slice(0, mermaidMatch.index);\n const after = unwrapped.slice(mermaidMatch.index + mermaidMatch[0].length);\n const leadingLine = firstNonEmptyLine(before);\n const trailingLine = firstNonEmptyLine(after);\n\n if (!leadingLine || !trailingLine) {\n return unwrapped;\n }\n\n // Keep the essential nearby text around the mermaid block in wrapped payloads.\n return `${leadingLine}${mermaidMatch[0]}${trailingLine}`;\n};\n\nconst getFenceRanges = (raw: string): FenceRange[] => {\n const ranges: FenceRange[] = [];\n const fencePattern = /```/g;\n let match: RegExpExecArray | null;\n\n while ((match = fencePattern.exec(raw)) !== null) {\n const start = match.index;\n const closeMatch = fencePattern.exec(raw);\n if (!closeMatch) {\n ranges.push({ start, end: raw.length });\n break;\n }\n ranges.push({ start, end: closeMatch.index + 3 });\n }\n\n return ranges;\n};\n\nconst isIndexInRanges = (index: number, ranges: FenceRange[]) =>\n ranges.some(({ start, end }) => index >= start && index < end);\n\nconst findFirstMatchOutsideFence = (\n raw: string,\n pattern: RegExp,\n fenceRanges: FenceRange[]\n) => {\n const flags = pattern.flags.includes(\"g\")\n ? pattern.flags\n : `${pattern.flags}g`;\n const matcher = new RegExp(pattern.source, flags);\n let match: RegExpExecArray | null;\n\n while ((match = matcher.exec(raw)) !== null) {\n if (!isIndexInRanges(match.index, fenceRanges)) {\n return match.index;\n }\n }\n\n return -1;\n};\n\nconst findHtmlBlockEnd = (raw: string, startIndex: number) => {\n let blockEnd = raw.length;\n let match: RegExpExecArray | null;\n closingBoundary.lastIndex = 0;\n\n while ((match = closingBoundary.exec(raw))) {\n if (match.index <= startIndex) continue;\n blockEnd = match.index + match[0].length;\n break;\n }\n\n return blockEnd;\n};\n\nconst splitCustomButtonsFromSandbox = (segments: RenderSegment[]) => {\n if (!segments.length) return segments;\n const output: RenderSegment[] = [];\n\n segments.forEach((segment) => {\n if (segment.type !== \"sandbox\") {\n output.push(segment);\n return;\n }\n\n CUSTOM_BUTTON_PATTERN.lastIndex = 0;\n let lastIndex = 0;\n let match: RegExpExecArray | null;\n\n while ((match = CUSTOM_BUTTON_PATTERN.exec(segment.value)) !== null) {\n const before = segment.value.slice(lastIndex, match.index);\n if (before.trim()) {\n output.push({ type: \"sandbox\", value: before });\n }\n output.push({ type: \"markdown\", value: match[0] });\n lastIndex = match.index + match[0].length;\n }\n\n const rest = segment.value.slice(lastIndex);\n if (rest.trim()) {\n output.push({ type: \"sandbox\", value: rest });\n }\n });\n\n return output;\n};\n\nconst findInlineSandboxMatch = (raw: string): MatchResult | null => {\n let earliest: MatchResult | null = null;\n\n INLINE_SANDBOX_PATTERNS.forEach((pattern) => {\n const match = pattern.exec(raw);\n if (!match || typeof match.index !== \"number\") return;\n const start = match.index;\n const end = match.index + match[0].length;\n\n if (!earliest || start < earliest.start) {\n earliest = { start, end };\n }\n });\n\n return earliest;\n};\n\nconst findMarkdownImageMatch = (\n raw: string,\n fenceRanges: FenceRange[]\n): MatchResult | null => {\n const start = findFirstMatchOutsideFence(\n raw,\n MARKDOWN_IMAGE_PATTERN,\n fenceRanges\n );\n\n if (start === -1) return null;\n const match = raw.slice(start).match(MARKDOWN_IMAGE_PATTERN);\n if (!match) return null;\n\n return { start, end: start + match[0].length };\n};\n\nconst extractTableBlock = (\n raw: string\n): { start: number; block: string; end: number } | null => {\n const tableMatch = raw.match(/^\\s*\\|.+\\|\\s*$/m);\n if (!tableMatch || typeof tableMatch.index !== \"number\") return null;\n\n const leadingSpaces = tableMatch[0].match(/^\\s*/)?.[0].length ?? 0;\n const tableStart = tableMatch.index + leadingSpaces;\n\n const lines = raw.slice(tableStart).split(\"\\n\");\n const tableLines: string[] = [];\n\n for (const line of lines) {\n const trimmed = line.trim();\n if (!trimmed.startsWith(\"|\")) break;\n tableLines.push(line);\n }\n\n const block = tableLines.join(\"\\n\");\n return { start: tableStart, block, end: tableStart + block.length };\n};\n\n// Split incoming markdown content into markdown and sandbox HTML segments\nexport const splitContentSegments = (\n raw: string,\n keepText = false\n): RenderSegment[] => {\n const source = normalizeQuotedMermaidContent(raw, keepText);\n const finalizeSegments = (segments: RenderSegment[]) =>\n splitCustomButtonsFromSandbox(segments);\n\n const fenceStart = source.indexOf(\"```\");\n if (keepText && fenceStart !== -1) {\n const closingFence = source.indexOf(\"```\", fenceStart + 3);\n if (closingFence === -1) {\n return finalizeSegments([{ type: \"markdown\", value: source }]);\n }\n }\n\n const fenceRanges = getFenceRanges(source);\n // Avoid treating fenced code blocks as sandbox content.\n const sandboxStartIndex = findFirstMatchOutsideFence(\n source,\n SANDBOX_START_PATTERN,\n fenceRanges\n );\n const svgOpenIndex = findFirstMatchOutsideFence(\n source,\n /<svg\\b/i,\n fenceRanges\n );\n const hasSandboxBeforeSvg =\n sandboxStartIndex !== -1 &&\n svgOpenIndex !== -1 &&\n sandboxStartIndex < svgOpenIndex;\n if (svgOpenIndex !== -1 && !hasSandboxBeforeSvg) {\n const before = source.slice(0, svgOpenIndex);\n const closeIdx = source.indexOf(\"</svg>\", svgOpenIndex);\n const svgBlock =\n closeIdx === -1\n ? `${source.slice(svgOpenIndex)}</svg>`\n : source.slice(svgOpenIndex, closeIdx + \"</svg>\".length);\n const after =\n closeIdx === -1 ? \"\" : source.slice(closeIdx + \"</svg>\".length);\n\n if (keepText) {\n const segments: RenderSegment[] = [];\n if (before.trim()) {\n segments.push({ type: \"text\", value: before });\n }\n segments.push({ type: \"markdown\", value: svgBlock });\n if (after.trim()) {\n segments.push(...splitContentSegments(after, true));\n }\n return finalizeSegments(segments);\n }\n\n if (closeIdx === -1) {\n return finalizeSegments([{ type: \"markdown\", value: svgBlock }]);\n }\n }\n\n const tableBlock = extractTableBlock(source);\n if (tableBlock) {\n const segments: RenderSegment[] = [];\n const before = source.slice(0, tableBlock.start);\n if (keepText && before.trim()) {\n segments.push({ type: \"text\", value: before });\n }\n segments.push({ type: \"markdown\", value: tableBlock.block });\n const after = source.slice(tableBlock.end);\n const hasProgress = after.length < source.length;\n if (after.trim() && hasProgress) {\n segments.push(\n ...(keepText\n ? splitContentSegments(after, true)\n : splitContentSegments(after))\n );\n }\n return finalizeSegments(segments);\n }\n\n const inlineMatch = findInlineSandboxMatch(source);\n const markdownImageMatch = findMarkdownImageMatch(source, fenceRanges);\n const inlineCandidate =\n inlineMatch && markdownImageMatch\n ? inlineMatch.start <= markdownImageMatch.start\n ? inlineMatch\n : markdownImageMatch\n : (inlineMatch ?? markdownImageMatch);\n\n if (sandboxStartIndex === -1 && !inlineCandidate) {\n if (keepText && source.trim()) {\n return finalizeSegments([{ type: \"text\", value: source }]);\n }\n return [];\n }\n\n const shouldUseInline =\n !!inlineCandidate &&\n (sandboxStartIndex === -1 || inlineCandidate.start < sandboxStartIndex);\n\n const startIndex = shouldUseInline\n ? inlineCandidate!.start\n : sandboxStartIndex;\n const blockEnd = shouldUseInline\n ? inlineCandidate!.end\n : findHtmlBlockEnd(source, startIndex);\n\n const segments: RenderSegment[] = [];\n const before = source.slice(0, startIndex);\n const matchedBlock = source.slice(startIndex, blockEnd);\n const after = source.slice(blockEnd);\n\n if (keepText && before.trim()) {\n segments.push({ type: \"text\", value: before });\n }\n\n segments.push({\n type: shouldUseInline ? \"markdown\" : \"sandbox\",\n value: matchedBlock,\n });\n\n if (after.trim()) {\n segments.push(...splitContentSegments(after, keepText));\n }\n\n return finalizeSegments(segments);\n};\n"],"names":["SANDBOX_START_PATTERN","INLINE_SANDBOX_PATTERNS","MARKDOWN_IMAGE_PATTERN","closingBoundary","CUSTOM_BUTTON_PATTERN","WRAPPED_QUOTES_PATTERN","MERMAID_BLOCK_PATTERN","firstNonEmptyLine","content","line","normalizeQuotedMermaidContent","raw","keepText","unwrapped","mermaidMatch","before","after","leadingLine","trailingLine","getFenceRanges","ranges","fencePattern","match","start","closeMatch","isIndexInRanges","index","end","findFirstMatchOutsideFence","pattern","fenceRanges","flags","matcher","findHtmlBlockEnd","startIndex","blockEnd","splitCustomButtonsFromSandbox","segments","output","segment","lastIndex","rest","findInlineSandboxMatch","earliest","findMarkdownImageMatch","extractTableBlock","tableMatch","leadingSpaces","tableStart","lines","tableLines","block","splitContentSegments","source","finalizeSegments","fenceStart","sandboxStartIndex","svgOpenIndex","hasSandboxBeforeSvg","closeIdx","svgBlock","tableBlock","hasProgress","inlineMatch","markdownImageMatch","inlineCandidate","shouldUseInline","matchedBlock"],"mappings":"gFAKA,MAAMA,EACJ,qGAEIC,EAAoC,CACxC,uBACA,iBACA,yBACA,6BACF,EACMC,EAAyB,0CAEzBC,EAAkB,kCAClBC,EACJ,0EAKIC,EAAyB,cACzBC,EAAwB,yBAExBC,EAAqBC,GACzBA,EACG,MAAM,OAAO,EACb,IAAKC,GAASA,EAAK,KAAA,CAAM,EACzB,KAAK,OAAO,GAAK,GAEhBC,EAAgC,CAACC,EAAaC,IAAsB,CACxE,GAAI,CAACP,EAAuB,KA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,KAAA,GAAU8C,GAClBzB,EAAS,KACP,GAAIzB,EACAwC,EAAqBpC,EAAO,EAAI,EAChCoC,EAAqBpC,CAAK,CAAA,EAG3BsC,EAAiBjB,CAAQ,CAClC,CAEA,MAAM0B,EAAcrB,EAAuBW,CAAM,EAC3CW,EAAqBpB,EAAuBS,EAAQvB,CAAW,EAC/DmC,EACJF,GAAeC,EACXD,EAAY,OAASC,EAAmB,MACtCD,EACAC,EACDD,GAAeC,EAEtB,GAAIR,IAAsB,IAAM,CAACS,EAC/B,OAAIrD,GAAYyC,EAAO,OACdC,EAAiB,CAAC,CAAE,KAAM,OAAQ,MAAOD,CAAA,CAAQ,CAAC,EAEpD,CAAA,EAGT,MAAMa,EACJ,CAAC,CAACD,IACDT,IAAsB,IAAMS,EAAgB,MAAQT,GAEjDtB,EAAagC,EACfD,EAAiB,MACjBT,EACErB,EAAW+B,EACbD,EAAiB,IACjBhC,EAAiBoB,EAAQnB,CAAU,EAEjCG,EAA4B,CAAA,EAC5BtB,EAASsC,EAAO,MAAM,EAAGnB,CAAU,EACnCiC,EAAed,EAAO,MAAMnB,EAAYC,CAAQ,EAChDnB,EAAQqC,EAAO,MAAMlB,CAAQ,EAEnC,OAAIvB,GAAYG,EAAO,QACrBsB,EAAS,KAAK,CAAE,KAAM,OAAQ,MAAOtB,EAAQ,EAG/CsB,EAAS,KAAK,CACZ,KAAM6B,EAAkB,WAAa,UACrC,MAAOC,CAAA,CACR,EAEGnD,EAAM,QACRqB,EAAS,KAAK,GAAGe,EAAqBpC,EAAOJ,CAAQ,CAAC,EAGjD0C,EAAiBjB,CAAQ,CAClC"}
|
|
@@ -1,118 +1,131 @@
|
|
|
1
|
-
const
|
|
1
|
+
const T = /<(script|style|link|iframe|html|head|body|meta|title|base|template|div|section|article|main)[\s>]/i, _ = [
|
|
2
2
|
/<svg[\s\S]*?<\/svg>/i,
|
|
3
3
|
/<img\b[^>]*?>/i,
|
|
4
4
|
/```mermaid[\s\S]*?```/i,
|
|
5
5
|
/```[a-zA-Z0-9]+[\s\S]*?```/i
|
|
6
|
-
],
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
], A = /!\[[^\]]*]\([^\s)\n]+(?:\s+"[^"]*")?\)/i, R = /<\/[a-z][^>]*>\s*\n(?=[^\s<])/gi, B = /<custom-button-after-content\b[\s\S]*?<\/custom-button-after-content>/gi, P = /^"[\s\S]*"$/, w = /```mermaid[\s\S]*?```/i, N = (s) => s.split(/\r?\n/).map((n) => n.trim()).find(Boolean) ?? "", $ = (s, n) => {
|
|
7
|
+
if (!P.test(s)) return s;
|
|
8
|
+
const t = s.slice(1, -1);
|
|
9
|
+
if (!n) return t;
|
|
10
|
+
const e = t.match(w);
|
|
11
|
+
if (!e || typeof e.index != "number")
|
|
12
|
+
return t;
|
|
13
|
+
const c = t.slice(0, e.index), i = t.slice(e.index + e[0].length), o = N(c), r = N(i);
|
|
14
|
+
return !o || !r ? t : `${o}${e[0]}${r}`;
|
|
15
|
+
}, C = (s) => {
|
|
16
|
+
const n = [], t = /```/g;
|
|
17
|
+
let e;
|
|
18
|
+
for (; (e = t.exec(s)) !== null; ) {
|
|
19
|
+
const c = e.index, i = t.exec(s);
|
|
20
|
+
if (!i) {
|
|
21
|
+
n.push({ start: c, end: s.length });
|
|
13
22
|
break;
|
|
14
23
|
}
|
|
15
|
-
n.push({ start:
|
|
24
|
+
n.push({ start: c, end: i.index + 3 });
|
|
16
25
|
}
|
|
17
26
|
return n;
|
|
18
|
-
},
|
|
19
|
-
const
|
|
20
|
-
let
|
|
21
|
-
for (; (
|
|
22
|
-
if (!
|
|
23
|
-
return
|
|
27
|
+
}, F = (s, n) => n.some(({ start: t, end: e }) => s >= t && s < e), y = (s, n, t) => {
|
|
28
|
+
const e = n.flags.includes("g") ? n.flags : `${n.flags}g`, c = new RegExp(n.source, e);
|
|
29
|
+
let i;
|
|
30
|
+
for (; (i = c.exec(s)) !== null; )
|
|
31
|
+
if (!F(i.index, t))
|
|
32
|
+
return i.index;
|
|
24
33
|
return -1;
|
|
25
|
-
},
|
|
26
|
-
let
|
|
27
|
-
for (
|
|
28
|
-
if (!(
|
|
29
|
-
|
|
34
|
+
}, L = (s, n) => {
|
|
35
|
+
let t = s.length, e;
|
|
36
|
+
for (R.lastIndex = 0; e = R.exec(s); )
|
|
37
|
+
if (!(e.index <= n)) {
|
|
38
|
+
t = e.index + e[0].length;
|
|
30
39
|
break;
|
|
31
40
|
}
|
|
32
|
-
return
|
|
33
|
-
},
|
|
34
|
-
if (!
|
|
41
|
+
return t;
|
|
42
|
+
}, D = (s) => {
|
|
43
|
+
if (!s.length) return s;
|
|
35
44
|
const n = [];
|
|
36
|
-
return
|
|
37
|
-
if (
|
|
38
|
-
n.push(
|
|
45
|
+
return s.forEach((t) => {
|
|
46
|
+
if (t.type !== "sandbox") {
|
|
47
|
+
n.push(t);
|
|
39
48
|
return;
|
|
40
49
|
}
|
|
41
|
-
|
|
42
|
-
let
|
|
43
|
-
for (; (
|
|
44
|
-
const
|
|
45
|
-
|
|
50
|
+
B.lastIndex = 0;
|
|
51
|
+
let e = 0, c;
|
|
52
|
+
for (; (c = B.exec(t.value)) !== null; ) {
|
|
53
|
+
const o = t.value.slice(e, c.index);
|
|
54
|
+
o.trim() && n.push({ type: "sandbox", value: o }), n.push({ type: "markdown", value: c[0] }), e = c.index + c[0].length;
|
|
46
55
|
}
|
|
47
|
-
const
|
|
48
|
-
|
|
56
|
+
const i = t.value.slice(e);
|
|
57
|
+
i.trim() && n.push({ type: "sandbox", value: i });
|
|
49
58
|
}), n;
|
|
50
|
-
},
|
|
59
|
+
}, z = (s) => {
|
|
51
60
|
let n = null;
|
|
52
|
-
return
|
|
53
|
-
const
|
|
54
|
-
if (!
|
|
55
|
-
const
|
|
56
|
-
(!n ||
|
|
61
|
+
return _.forEach((t) => {
|
|
62
|
+
const e = t.exec(s);
|
|
63
|
+
if (!e || typeof e.index != "number") return;
|
|
64
|
+
const c = e.index, i = e.index + e[0].length;
|
|
65
|
+
(!n || c < n.start) && (n = { start: c, end: i });
|
|
57
66
|
}), n;
|
|
58
|
-
},
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
67
|
+
}, U = (s, n) => {
|
|
68
|
+
const t = y(
|
|
69
|
+
s,
|
|
70
|
+
A,
|
|
62
71
|
n
|
|
63
72
|
);
|
|
64
|
-
if (
|
|
65
|
-
const
|
|
66
|
-
return
|
|
67
|
-
},
|
|
68
|
-
const n =
|
|
73
|
+
if (t === -1) return null;
|
|
74
|
+
const e = s.slice(t).match(A);
|
|
75
|
+
return e ? { start: t, end: t + e[0].length } : null;
|
|
76
|
+
}, W = (s) => {
|
|
77
|
+
const n = s.match(/^\s*\|.+\|\s*$/m);
|
|
69
78
|
if (!n || typeof n.index != "number") return null;
|
|
70
|
-
const
|
|
71
|
-
`),
|
|
72
|
-
for (const
|
|
73
|
-
if (!
|
|
74
|
-
|
|
79
|
+
const t = n[0].match(/^\s*/)?.[0].length ?? 0, e = n.index + t, c = s.slice(e).split(`
|
|
80
|
+
`), i = [];
|
|
81
|
+
for (const r of c) {
|
|
82
|
+
if (!r.trim().startsWith("|")) break;
|
|
83
|
+
i.push(r);
|
|
75
84
|
}
|
|
76
|
-
const
|
|
85
|
+
const o = i.join(`
|
|
77
86
|
`);
|
|
78
|
-
return { start:
|
|
79
|
-
},
|
|
80
|
-
const e = (l) =>
|
|
81
|
-
if (n &&
|
|
87
|
+
return { start: e, block: o, end: e + o.length };
|
|
88
|
+
}, b = (s, n = !1) => {
|
|
89
|
+
const t = $(s, n), e = (l) => D(l), c = t.indexOf("```");
|
|
90
|
+
if (n && c !== -1 && t.indexOf("```", c + 3) === -1)
|
|
82
91
|
return e([{ type: "markdown", value: t }]);
|
|
83
|
-
const
|
|
92
|
+
const i = C(t), o = y(
|
|
93
|
+
t,
|
|
94
|
+
T,
|
|
95
|
+
i
|
|
96
|
+
), r = y(
|
|
84
97
|
t,
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
),
|
|
88
|
-
if (
|
|
89
|
-
const l = t.slice(0,
|
|
98
|
+
/<svg\b/i,
|
|
99
|
+
i
|
|
100
|
+
), E = o !== -1 && r !== -1 && o < r;
|
|
101
|
+
if (r !== -1 && !E) {
|
|
102
|
+
const l = t.slice(0, r), a = t.indexOf("</svg>", r), u = a === -1 ? `${t.slice(r)}</svg>` : t.slice(r, a + 6), x = a === -1 ? "" : t.slice(a + 6);
|
|
90
103
|
if (n) {
|
|
91
|
-
const
|
|
92
|
-
return l.trim() &&
|
|
104
|
+
const p = [];
|
|
105
|
+
return l.trim() && p.push({ type: "text", value: l }), p.push({ type: "markdown", value: u }), x.trim() && p.push(...b(x, !0)), e(p);
|
|
93
106
|
}
|
|
94
107
|
if (a === -1)
|
|
95
|
-
return e([{ type: "markdown", value:
|
|
108
|
+
return e([{ type: "markdown", value: u }]);
|
|
96
109
|
}
|
|
97
|
-
const
|
|
98
|
-
if (
|
|
99
|
-
const l = [], a = t.slice(0,
|
|
100
|
-
n && a.trim() && l.push({ type: "text", value: a }), l.push({ type: "markdown", value:
|
|
101
|
-
const
|
|
102
|
-
return
|
|
103
|
-
...n ?
|
|
110
|
+
const f = W(t);
|
|
111
|
+
if (f) {
|
|
112
|
+
const l = [], a = t.slice(0, f.start);
|
|
113
|
+
n && a.trim() && l.push({ type: "text", value: a }), l.push({ type: "markdown", value: f.block });
|
|
114
|
+
const u = t.slice(f.end), x = u.length < t.length;
|
|
115
|
+
return u.trim() && x && l.push(
|
|
116
|
+
...n ? b(u, !0) : b(u)
|
|
104
117
|
), e(l);
|
|
105
118
|
}
|
|
106
|
-
const h =
|
|
107
|
-
if (
|
|
119
|
+
const h = z(t), m = U(t, i), d = h && m ? h.start <= m.start ? h : m : h ?? m;
|
|
120
|
+
if (o === -1 && !d)
|
|
108
121
|
return n && t.trim() ? e([{ type: "text", value: t }]) : [];
|
|
109
|
-
const v = !!d && (
|
|
110
|
-
return n &&
|
|
122
|
+
const v = !!d && (o === -1 || d.start < o), S = v ? d.start : o, I = v ? d.end : L(t, S), g = [], M = t.slice(0, S), O = t.slice(S, I), k = t.slice(I);
|
|
123
|
+
return n && M.trim() && g.push({ type: "text", value: M }), g.push({
|
|
111
124
|
type: v ? "markdown" : "sandbox",
|
|
112
|
-
value:
|
|
113
|
-
}),
|
|
125
|
+
value: O
|
|
126
|
+
}), k.trim() && g.push(...b(k, n)), e(g);
|
|
114
127
|
};
|
|
115
128
|
export {
|
|
116
|
-
|
|
129
|
+
b as splitContentSegments
|
|
117
130
|
};
|
|
118
131
|
//# sourceMappingURL=split-content.es.js.map
|