streamdown 1.1.9 → 1.1.10
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/index.cjs +6 -6
- package/dist/index.js +6 -6
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";"use client";var te=Object.create;var B=Object.defineProperty;var re=Object.getOwnPropertyDescriptor;var ne=Object.getOwnPropertyNames;var oe=Object.getPrototypeOf,se=Object.prototype.hasOwnProperty;var ie=(e,t)=>{for(var r in t)B(e,r,{get:t[r],enumerable:!0})},_=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of ne(t))!se.call(e,o)&&o!==r&&B(e,o,{get:()=>t[o],enumerable:!(n=re(t,o))||n.enumerable});return e};var v=(e,t,r)=>(r=e!=null?te(oe(e)):{},_(t||!e||!e.__esModule?B(r,"default",{value:e,enumerable:!0}):r,e)),ae=e=>_(B({},"__esModule",{value:!0}),e);var ze={};ie(ze,{ShikiThemeContext:()=>I,Streamdown:()=>Z});module.exports=ae(ze);var b=require("react"),F=v(require("react-markdown"),1),X=v(require("rehype-katex"),1),J=v(require("remark-gfm"),1),Q=v(require("remark-math"),1),Ze=require("katex/dist/katex.min.css"),L=v(require("harden-react-markdown"),1);var G=require("react");var $=require("lucide-react"),h=require("react"),N=require("shiki"),W=require("shiki/engine/javascript");var R=require("clsx"),E=require("tailwind-merge"),i=(...e)=>(0,E.twMerge)((0,R.clsx)(e));var f=require("react/jsx-runtime"),le=/<pre(\s|>)/,z=(0,h.createContext)({code:""}),S=class{constructor(){this.lightHighlighter=null;this.darkHighlighter=null;this.lightTheme=null;this.darkTheme=null;this.loadedLanguages=new Set;this.initializationPromise=null}async ensureHighlightersInitialized(t,r){var m,u;let[n,o]=t,s=(0,W.createJavaScriptRegexEngine)({forgiving:!0}),a=!this.lightHighlighter||this.lightTheme!==n,c=!this.darkHighlighter||this.darkTheme!==o;(a||c)&&this.loadedLanguages.clear();let l=!this.loadedLanguages.has(r);if(a?(this.lightHighlighter=await(0,N.createHighlighter)({themes:[n],langs:[r],engine:s}),this.lightTheme=n,this.loadedLanguages.add(r)):l&&await((m=this.lightHighlighter)==null?void 0:m.loadLanguage(r)),c){let p=l?[...this.loadedLanguages,r]:Array.from(this.loadedLanguages);this.darkHighlighter=await(0,N.createHighlighter)({themes:[o],langs:p.length>0?p:[r],engine:s}),this.darkTheme=o}else l&&await((u=this.darkHighlighter)==null?void 0:u.loadLanguage(r));l&&this.loadedLanguages.add(r)}async highlightCode(t,r,n,o){var u,p;this.initializationPromise&&await this.initializationPromise,this.initializationPromise=this.ensureHighlightersInitialized(n,r),await this.initializationPromise,this.initializationPromise=null;let s=g=>o?g.replace(le,`<pre class="${o}"$1`):g,[a,c]=n,l=(u=this.lightHighlighter)==null?void 0:u.codeToHtml(t,{lang:r,theme:a}),m=(p=this.darkHighlighter)==null?void 0:p.codeToHtml(t,{lang:r,theme:c});return[D(s(l)),D(s(m))]}},de=new S;async function ce(e,t,r,n){return de.highlightCode(e,t,r,n)}function D(e){return e.replace(/(<pre[^>]*)(style="[^"]*background[^";]*;?[^"]*")([^>]*>)/g,"$1$3")}var O=({code:e,language:t,className:r,children:n,preClassName:o,...s})=>{let[a,c]=(0,h.useState)(""),[l,m]=(0,h.useState)(""),u=(0,h.useRef)(!1),[p,g]=(0,h.useContext)(I);return(0,h.useEffect)(()=>(u.current=!0,ce(e,t,[p,g],o).then(([w,y])=>{u.current&&(c(w),m(y))}),()=>{u.current=!1}),[e,t,p,g,o]),(0,f.jsx)(z.Provider,{value:{code:e},children:(0,f.jsxs)("div",{className:"my-4 w-full overflow-hidden rounded-xl border","data-code-block-container":!0,"data-language":t,children:[(0,f.jsxs)("div",{className:"flex items-center justify-between bg-muted/80 p-3 text-muted-foreground text-xs","data-code-block-header":!0,"data-language":t,children:[(0,f.jsx)("span",{className:"ml-1 font-mono lowercase",children:t}),(0,f.jsx)("div",{children:n})]}),(0,f.jsx)("div",{className:"w-full",children:(0,f.jsxs)("div",{className:"min-w-full",children:[(0,f.jsx)("div",{className:i("overflow-x-auto dark:hidden",r),dangerouslySetInnerHTML:{__html:a},"data-code-block":!0,"data-language":t,...s}),(0,f.jsx)("div",{className:i("hidden overflow-x-auto dark:block",r),dangerouslySetInnerHTML:{__html:l},"data-code-block":!0,"data-language":t,...s})]})})]})})},H=({onCopy:e,onError:t,timeout:r=2e3,children:n,className:o,code:s,...a})=>{let[c,l]=(0,h.useState)(!1),m=(0,h.useRef)(0),u=(0,h.useContext)(z).code,p=s!=null?s:u,g=async()=>{var y;if(typeof window=="undefined"||!((y=navigator==null?void 0:navigator.clipboard)!=null&&y.writeText)){t==null||t(new Error("Clipboard API not available"));return}try{c||(await navigator.clipboard.writeText(p),l(!0),e==null||e(),m.current=window.setTimeout(()=>l(!1),r))}catch(T){t==null||t(T)}};(0,h.useEffect)(()=>()=>{window.clearTimeout(m.current)},[]);let w=c?$.CheckIcon:$.CopyIcon;return(0,f.jsx)("button",{className:i("text-muted-foreground","p-1 transition-all",o),onClick:g,type:"button",...a,children:n!=null?n:(0,f.jsx)(w,{size:14})})};var M=require("react");var k=require("react/jsx-runtime"),U=!1,me=async()=>{if(!U){let r=(await import("mermaid")).default;return r.initialize({startOnLoad:!1,theme:"default",securityLevel:"strict",fontFamily:"monospace",suppressErrorRendering:!0}),U=!0,r}return(await import("mermaid")).default},q=({chart:e,className:t})=>{let[r,n]=(0,M.useState)(null),[o,s]=(0,M.useState)(!0),[a,c]=(0,M.useState)(""),[l,m]=(0,M.useState)("");if((0,M.useEffect)(()=>{(async()=>{try{n(null),s(!0);let g=await me(),w=e.split("").reduce((A,ee)=>(A<<5)-A+ee.charCodeAt(0)|0,0),y=`mermaid-${Math.abs(w)}-${Date.now()}-${Math.random().toString(36).substring(2,9)}`,{svg:T}=await g.render(y,e);c(T),m(T)}catch(g){if(!(l||a)){let w=g instanceof Error?g.message:"Failed to render Mermaid chart";n(w)}}finally{s(!1)}})()},[e]),o&&!a&&!l)return(0,k.jsx)("div",{className:i("my-4 flex justify-center p-4",t),children:(0,k.jsxs)("div",{className:"flex items-center space-x-2 text-muted-foreground",children:[(0,k.jsx)("div",{className:"h-4 w-4 animate-spin rounded-full border-current border-b-2"}),(0,k.jsx)("span",{className:"text-sm",children:"Loading diagram..."})]})});if(r&&!a&&!l)return(0,k.jsxs)("div",{className:i("rounded-lg border border-red-200 bg-red-50 p-4",t),children:[(0,k.jsxs)("p",{className:"font-mono text-red-700 text-sm",children:["Mermaid Error: ",r]}),(0,k.jsxs)("details",{className:"mt-2",children:[(0,k.jsx)("summary",{className:"cursor-pointer text-red-600 text-xs",children:"Show Code"}),(0,k.jsx)("pre",{className:"mt-2 overflow-x-auto rounded bg-red-100 p-2 text-red-800 text-xs",children:e})]})]});let u=a||l;return(0,k.jsx)("div",{"aria-label":"Mermaid chart",className:i("my-4 flex justify-center",t),dangerouslySetInnerHTML:{__html:u},role:"img"})};var d=require("react/jsx-runtime"),ue=/language-([^\s]+)/,he=({node:e,className:t,children:r,...n})=>{var l,m,u;if(((l=e==null?void 0:e.position)==null?void 0:l.start.line)===((m=e==null?void 0:e.position)==null?void 0:m.end.line))return(0,d.jsx)("code",{className:i("rounded bg-muted px-1.5 py-0.5 font-mono text-sm",t),"data-streamdown":"inline-code",...n,children:r});let s=t==null?void 0:t.match(ue),a=(u=s==null?void 0:s.at(1))!=null?u:"",c="";return(0,G.isValidElement)(r)&&r.props&&typeof r.props=="object"&&"children"in r.props&&typeof r.props.children=="string"?c=r.props.children:typeof r=="string"&&(c=r),a==="mermaid"?(0,d.jsxs)("div",{className:i("group relative my-4 h-auto rounded-xl border p-4",t),"data-streamdown":"mermaid-block",children:[(0,d.jsx)("div",{className:"flex items-center justify-end",children:(0,d.jsx)(H,{code:c})}),(0,d.jsx)(q,{chart:c})]}):(0,d.jsx)(O,{className:i("overflow-x-auto border-t",t),code:c,"data-language":a,"data-streamdown":"code-block",language:a,preClassName:"overflow-x-auto font-mono text-xs p-4 bg-muted/40",children:(0,d.jsx)(H,{})})},V={ol:({node:e,children:t,className:r,...n})=>(0,d.jsx)("ol",{className:i("ml-4 list-outside list-decimal whitespace-normal",r),"data-streamdown":"ordered-list",...n,children:t}),li:({node:e,children:t,className:r,...n})=>(0,d.jsx)("li",{className:i("py-1",r),"data-streamdown":"list-item",...n,children:t}),ul:({node:e,children:t,className:r,...n})=>(0,d.jsx)("ul",{className:i("ml-4 list-outside list-disc whitespace-normal",r),"data-streamdown":"unordered-list",...n,children:t}),hr:({node:e,className:t,...r})=>(0,d.jsx)("hr",{className:i("my-6 border-border",t),"data-streamdown":"horizontal-rule",...r}),strong:({node:e,children:t,className:r,...n})=>(0,d.jsx)("span",{className:i("font-semibold",r),"data-streamdown":"strong",...n,children:t}),a:({node:e,children:t,className:r,href:n,...o})=>{let s=n==="streamdown:incomplete-link";return(0,d.jsx)("a",{className:i("font-medium text-primary underline",r),"data-incomplete":s,"data-streamdown":"link",href:n,rel:"noreferrer",target:"_blank",...o,children:t})},h1:({node:e,children:t,className:r,...n})=>(0,d.jsx)("h1",{className:i("mt-6 mb-2 font-semibold text-3xl",r),"data-streamdown":"heading-1",...n,children:t}),h2:({node:e,children:t,className:r,...n})=>(0,d.jsx)("h2",{className:i("mt-6 mb-2 font-semibold text-2xl",r),"data-streamdown":"heading-2",...n,children:t}),h3:({node:e,children:t,className:r,...n})=>(0,d.jsx)("h3",{className:i("mt-6 mb-2 font-semibold text-xl",r),"data-streamdown":"heading-3",...n,children:t}),h4:({node:e,children:t,className:r,...n})=>(0,d.jsx)("h4",{className:i("mt-6 mb-2 font-semibold text-lg",r),"data-streamdown":"heading-4",...n,children:t}),h5:({node:e,children:t,className:r,...n})=>(0,d.jsx)("h5",{className:i("mt-6 mb-2 font-semibold text-base",r),"data-streamdown":"heading-5",...n,children:t}),h6:({node:e,children:t,className:r,...n})=>(0,d.jsx)("h6",{className:i("mt-6 mb-2 font-semibold text-sm",r),"data-streamdown":"heading-6",...n,children:t}),table:({node:e,children:t,className:r,...n})=>(0,d.jsx)("div",{className:"my-4 overflow-x-auto","data-streamdown":"table-wrapper",children:(0,d.jsx)("table",{className:i("w-full border-collapse border border-border",r),"data-streamdown":"table",...n,children:t})}),thead:({node:e,children:t,className:r,...n})=>(0,d.jsx)("thead",{className:i("bg-muted/50",r),"data-streamdown":"table-header",...n,children:t}),tbody:({node:e,children:t,className:r,...n})=>(0,d.jsx)("tbody",{className:i("divide-y divide-border",r),"data-streamdown":"table-body",...n,children:t}),tr:({node:e,children:t,className:r,...n})=>(0,d.jsx)("tr",{className:i("border-border border-b",r),"data-streamdown":"table-row",...n,children:t}),th:({node:e,children:t,className:r,...n})=>(0,d.jsx)("th",{className:i("px-4 py-2 text-left font-semibold text-sm",r),"data-streamdown":"table-header-cell",...n,children:t}),td:({node:e,children:t,className:r,...n})=>(0,d.jsx)("td",{className:i("px-4 py-2 text-sm",r),"data-streamdown":"table-cell",...n,children:t}),blockquote:({node:e,children:t,className:r,...n})=>(0,d.jsx)("blockquote",{className:i("my-4 border-muted-foreground/30 border-l-4 pl-4 text-muted-foreground italic",r),"data-streamdown":"blockquote",...n,children:t}),code:he,pre:({children:e})=>e,sup:({node:e,children:t,className:r,...n})=>(0,d.jsx)("sup",{className:i("text-sm",r),"data-streamdown":"superscript",...n,children:t}),sub:({node:e,children:t,className:r,...n})=>(0,d.jsx)("sub",{className:i("text-sm",r),"data-streamdown":"subscript",...n,children:t})};var x=require("marked"),K=e=>{let r=x.marked.use({gfm:!0}).lexer(e).map(o=>o.raw),n=[];for(let o=0;o<r.length;o++){let s=r[o];if(s.trim()==="$$"&&n.length>0){let a=n.at(-1),c=a.trimStart().startsWith("$$"),l=(a.match(/\$\$/g)||[]).length;if(c&&l%2===1){n[n.length-1]=a+s;continue}}if(n.length>0&&s.trimEnd().endsWith("$$")){let a=n.at(-1),c=a.trimStart().startsWith("$$"),l=(a.match(/\$\$/g)||[]).length,m=(s.match(/\$\$/g)||[]).length;if(c&&l%2===1&&!s.trimStart().startsWith("$$")&&m===1){n[n.length-1]=a+s;continue}}n.push(s)}return n};var ge=/(!?\[)([^\]]*?)$/,pe=/(\*\*)([^*]*?)$/,fe=/(__)([^_]*?)$/,ke=/(\*\*\*)([^*]*?)$/,be=/(\*)([^*]*?)$/,we=/(_)([^_]*?)$/,ye=/(`)([^`]*?)$/,ve=/(~~)([^~]*?)$/,P=e=>{let t=(e.match(/```/g)||[]).length;return t>0&&t%2===0&&e.includes(`
|
|
2
|
-
`)},Me=e=>{let t=e.match(
|
|
3
|
-
`){a=l+1;break}if(l===0){a=0;break}}if(e.substring(a,n).trim()===""&&(s===" "||s===" "))return t;if(o!=="*"&&s!=="*")return t+1}return t},0)
|
|
4
|
-
`))return e.endsWith("``")&&!e.endsWith("```")?`${e}\``:e;let r=(e.match(/```/g)||[]).length,n=r%2===1;
|
|
5
|
-
`)||(e.endsWith("```\n")||e.endsWith("```"))&&r%2===0
|
|
1
|
+
"use strict";"use client";var re=Object.create;var B=Object.defineProperty;var ne=Object.getOwnPropertyDescriptor;var oe=Object.getOwnPropertyNames;var se=Object.getPrototypeOf,ie=Object.prototype.hasOwnProperty;var ae=(e,t)=>{for(var r in t)B(e,r,{get:t[r],enumerable:!0})},_=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of oe(t))!ie.call(e,o)&&o!==r&&B(e,o,{get:()=>t[o],enumerable:!(n=ne(t,o))||n.enumerable});return e};var v=(e,t,r)=>(r=e!=null?re(se(e)):{},_(t||!e||!e.__esModule?B(r,"default",{value:e,enumerable:!0}):r,e)),le=e=>_(B({},"__esModule",{value:!0}),e);var ze={};ae(ze,{ShikiThemeContext:()=>I,Streamdown:()=>ee});module.exports=le(ze);var b=require("react"),J=v(require("react-markdown"),1),Q=v(require("rehype-katex"),1),Y=v(require("remark-gfm"),1),Z=v(require("remark-math"),1),xe=require("katex/dist/katex.min.css"),L=v(require("harden-react-markdown"),1);var G=require("react");var C=require("lucide-react"),h=require("react"),S=require("shiki"),W=require("shiki/engine/javascript");var R=require("clsx"),E=require("tailwind-merge"),i=(...e)=>(0,E.twMerge)((0,R.clsx)(e));var f=require("react/jsx-runtime"),de=/<pre(\s|>)/,z=(0,h.createContext)({code:""}),N=class{constructor(){this.lightHighlighter=null;this.darkHighlighter=null;this.lightTheme=null;this.darkTheme=null;this.loadedLanguages=new Set;this.initializationPromise=null}async ensureHighlightersInitialized(t,r){var m,u;let[n,o]=t,s=(0,W.createJavaScriptRegexEngine)({forgiving:!0}),a=!this.lightHighlighter||this.lightTheme!==n,c=!this.darkHighlighter||this.darkTheme!==o;(a||c)&&this.loadedLanguages.clear();let l=!this.loadedLanguages.has(r);if(a?(this.lightHighlighter=await(0,S.createHighlighter)({themes:[n],langs:[r],engine:s}),this.lightTheme=n,this.loadedLanguages.add(r)):l&&await((m=this.lightHighlighter)==null?void 0:m.loadLanguage(r)),c){let p=l?[...this.loadedLanguages,r]:Array.from(this.loadedLanguages);this.darkHighlighter=await(0,S.createHighlighter)({themes:[o],langs:p.length>0?p:[r],engine:s}),this.darkTheme=o}else l&&await((u=this.darkHighlighter)==null?void 0:u.loadLanguage(r));l&&this.loadedLanguages.add(r)}async highlightCode(t,r,n,o){var u,p;this.initializationPromise&&await this.initializationPromise,this.initializationPromise=this.ensureHighlightersInitialized(n,r),await this.initializationPromise,this.initializationPromise=null;let s=g=>o?g.replace(de,`<pre class="${o}"$1`):g,[a,c]=n,l=(u=this.lightHighlighter)==null?void 0:u.codeToHtml(t,{lang:r,theme:a}),m=(p=this.darkHighlighter)==null?void 0:p.codeToHtml(t,{lang:r,theme:c});return[D(s(l)),D(s(m))]}},ce=new N;async function me(e,t,r,n){return ce.highlightCode(e,t,r,n)}function D(e){return e.replace(/(<pre[^>]*)(style="[^"]*background[^";]*;?[^"]*")([^>]*>)/g,"$1$3")}var O=({code:e,language:t,className:r,children:n,preClassName:o,...s})=>{let[a,c]=(0,h.useState)(""),[l,m]=(0,h.useState)(""),u=(0,h.useRef)(!1),[p,g]=(0,h.useContext)(I);return(0,h.useEffect)(()=>(u.current=!0,me(e,t,[p,g],o).then(([w,y])=>{u.current&&(c(w),m(y))}),()=>{u.current=!1}),[e,t,p,g,o]),(0,f.jsx)(z.Provider,{value:{code:e},children:(0,f.jsxs)("div",{className:"my-4 w-full overflow-hidden rounded-xl border","data-code-block-container":!0,"data-language":t,children:[(0,f.jsxs)("div",{className:"flex items-center justify-between bg-muted/80 p-3 text-muted-foreground text-xs","data-code-block-header":!0,"data-language":t,children:[(0,f.jsx)("span",{className:"ml-1 font-mono lowercase",children:t}),(0,f.jsx)("div",{children:n})]}),(0,f.jsx)("div",{className:"w-full",children:(0,f.jsxs)("div",{className:"min-w-full",children:[(0,f.jsx)("div",{className:i("overflow-x-auto dark:hidden",r),dangerouslySetInnerHTML:{__html:a},"data-code-block":!0,"data-language":t,...s}),(0,f.jsx)("div",{className:i("hidden overflow-x-auto dark:block",r),dangerouslySetInnerHTML:{__html:l},"data-code-block":!0,"data-language":t,...s})]})})]})})},H=({onCopy:e,onError:t,timeout:r=2e3,children:n,className:o,code:s,...a})=>{let[c,l]=(0,h.useState)(!1),m=(0,h.useRef)(0),u=(0,h.useContext)(z).code,p=s!=null?s:u,g=async()=>{var y;if(typeof window=="undefined"||!((y=navigator==null?void 0:navigator.clipboard)!=null&&y.writeText)){t==null||t(new Error("Clipboard API not available"));return}try{c||(await navigator.clipboard.writeText(p),l(!0),e==null||e(),m.current=window.setTimeout(()=>l(!1),r))}catch(M){t==null||t(M)}};(0,h.useEffect)(()=>()=>{window.clearTimeout(m.current)},[]);let w=c?C.CheckIcon:C.CopyIcon;return(0,f.jsx)("button",{className:i("text-muted-foreground","p-1 transition-all",o),onClick:g,type:"button",...a,children:n!=null?n:(0,f.jsx)(w,{size:14})})};var $=require("react");var k=require("react/jsx-runtime"),U=!1,ue=async()=>{if(!U){let r=(await import("mermaid")).default;return r.initialize({startOnLoad:!1,theme:"default",securityLevel:"strict",fontFamily:"monospace",suppressErrorRendering:!0}),U=!0,r}return(await import("mermaid")).default},q=({chart:e,className:t})=>{let[r,n]=(0,$.useState)(null),[o,s]=(0,$.useState)(!0),[a,c]=(0,$.useState)(""),[l,m]=(0,$.useState)("");if((0,$.useEffect)(()=>{(async()=>{try{n(null),s(!0);let g=await ue(),w=e.split("").reduce((A,te)=>(A<<5)-A+te.charCodeAt(0)|0,0),y=`mermaid-${Math.abs(w)}-${Date.now()}-${Math.random().toString(36).substring(2,9)}`,{svg:M}=await g.render(y,e);c(M),m(M)}catch(g){if(!(l||a)){let w=g instanceof Error?g.message:"Failed to render Mermaid chart";n(w)}}finally{s(!1)}})()},[e]),o&&!a&&!l)return(0,k.jsx)("div",{className:i("my-4 flex justify-center p-4",t),children:(0,k.jsxs)("div",{className:"flex items-center space-x-2 text-muted-foreground",children:[(0,k.jsx)("div",{className:"h-4 w-4 animate-spin rounded-full border-current border-b-2"}),(0,k.jsx)("span",{className:"text-sm",children:"Loading diagram..."})]})});if(r&&!a&&!l)return(0,k.jsxs)("div",{className:i("rounded-lg border border-red-200 bg-red-50 p-4",t),children:[(0,k.jsxs)("p",{className:"font-mono text-red-700 text-sm",children:["Mermaid Error: ",r]}),(0,k.jsxs)("details",{className:"mt-2",children:[(0,k.jsx)("summary",{className:"cursor-pointer text-red-600 text-xs",children:"Show Code"}),(0,k.jsx)("pre",{className:"mt-2 overflow-x-auto rounded bg-red-100 p-2 text-red-800 text-xs",children:e})]})]});let u=a||l;return(0,k.jsx)("div",{"aria-label":"Mermaid chart",className:i("my-4 flex justify-center",t),dangerouslySetInnerHTML:{__html:u},role:"img"})};var d=require("react/jsx-runtime"),he=/language-([^\s]+)/,ge=({node:e,className:t,children:r,...n})=>{var l,m,u;if(((l=e==null?void 0:e.position)==null?void 0:l.start.line)===((m=e==null?void 0:e.position)==null?void 0:m.end.line))return(0,d.jsx)("code",{className:i("rounded bg-muted px-1.5 py-0.5 font-mono text-sm",t),"data-streamdown":"inline-code",...n,children:r});let s=t==null?void 0:t.match(he),a=(u=s==null?void 0:s.at(1))!=null?u:"",c="";return(0,G.isValidElement)(r)&&r.props&&typeof r.props=="object"&&"children"in r.props&&typeof r.props.children=="string"?c=r.props.children:typeof r=="string"&&(c=r),a==="mermaid"?(0,d.jsxs)("div",{className:i("group relative my-4 h-auto rounded-xl border p-4",t),"data-streamdown":"mermaid-block",children:[(0,d.jsx)("div",{className:"flex items-center justify-end",children:(0,d.jsx)(H,{code:c})}),(0,d.jsx)(q,{chart:c})]}):(0,d.jsx)(O,{className:i("overflow-x-auto border-t",t),code:c,"data-language":a,"data-streamdown":"code-block",language:a,preClassName:"overflow-x-auto font-mono text-xs p-4 bg-muted/40",children:(0,d.jsx)(H,{})})},V={ol:({node:e,children:t,className:r,...n})=>(0,d.jsx)("ol",{className:i("ml-4 list-outside list-decimal whitespace-normal",r),"data-streamdown":"ordered-list",...n,children:t}),li:({node:e,children:t,className:r,...n})=>(0,d.jsx)("li",{className:i("py-1",r),"data-streamdown":"list-item",...n,children:t}),ul:({node:e,children:t,className:r,...n})=>(0,d.jsx)("ul",{className:i("ml-4 list-outside list-disc whitespace-normal",r),"data-streamdown":"unordered-list",...n,children:t}),hr:({node:e,className:t,...r})=>(0,d.jsx)("hr",{className:i("my-6 border-border",t),"data-streamdown":"horizontal-rule",...r}),strong:({node:e,children:t,className:r,...n})=>(0,d.jsx)("span",{className:i("font-semibold",r),"data-streamdown":"strong",...n,children:t}),a:({node:e,children:t,className:r,href:n,...o})=>{let s=n==="streamdown:incomplete-link";return(0,d.jsx)("a",{className:i("font-medium text-primary underline",r),"data-incomplete":s,"data-streamdown":"link",href:n,rel:"noreferrer",target:"_blank",...o,children:t})},h1:({node:e,children:t,className:r,...n})=>(0,d.jsx)("h1",{className:i("mt-6 mb-2 font-semibold text-3xl",r),"data-streamdown":"heading-1",...n,children:t}),h2:({node:e,children:t,className:r,...n})=>(0,d.jsx)("h2",{className:i("mt-6 mb-2 font-semibold text-2xl",r),"data-streamdown":"heading-2",...n,children:t}),h3:({node:e,children:t,className:r,...n})=>(0,d.jsx)("h3",{className:i("mt-6 mb-2 font-semibold text-xl",r),"data-streamdown":"heading-3",...n,children:t}),h4:({node:e,children:t,className:r,...n})=>(0,d.jsx)("h4",{className:i("mt-6 mb-2 font-semibold text-lg",r),"data-streamdown":"heading-4",...n,children:t}),h5:({node:e,children:t,className:r,...n})=>(0,d.jsx)("h5",{className:i("mt-6 mb-2 font-semibold text-base",r),"data-streamdown":"heading-5",...n,children:t}),h6:({node:e,children:t,className:r,...n})=>(0,d.jsx)("h6",{className:i("mt-6 mb-2 font-semibold text-sm",r),"data-streamdown":"heading-6",...n,children:t}),table:({node:e,children:t,className:r,...n})=>(0,d.jsx)("div",{className:"my-4 overflow-x-auto","data-streamdown":"table-wrapper",children:(0,d.jsx)("table",{className:i("w-full border-collapse border border-border",r),"data-streamdown":"table",...n,children:t})}),thead:({node:e,children:t,className:r,...n})=>(0,d.jsx)("thead",{className:i("bg-muted/80",r),"data-streamdown":"table-header",...n,children:t}),tbody:({node:e,children:t,className:r,...n})=>(0,d.jsx)("tbody",{className:i("divide-y bg-muted/40 divide-border",r),"data-streamdown":"table-body",...n,children:t}),tr:({node:e,children:t,className:r,...n})=>(0,d.jsx)("tr",{className:i("border-border border-b",r),"data-streamdown":"table-row",...n,children:t}),th:({node:e,children:t,className:r,...n})=>(0,d.jsx)("th",{className:i("whitespace-nowrap px-4 py-2 text-left font-semibold text-sm",r),"data-streamdown":"table-header-cell",...n,children:t}),td:({node:e,children:t,className:r,...n})=>(0,d.jsx)("td",{className:i("px-4 py-2 text-sm",r),"data-streamdown":"table-cell",...n,children:t}),blockquote:({node:e,children:t,className:r,...n})=>(0,d.jsx)("blockquote",{className:i("my-4 border-muted-foreground/30 border-l-4 pl-4 text-muted-foreground italic",r),"data-streamdown":"blockquote",...n,children:t}),code:ge,pre:({children:e})=>e,sup:({node:e,children:t,className:r,...n})=>(0,d.jsx)("sup",{className:i("text-sm",r),"data-streamdown":"superscript",...n,children:t}),sub:({node:e,children:t,className:r,...n})=>(0,d.jsx)("sub",{className:i("text-sm",r),"data-streamdown":"subscript",...n,children:t})};var F=require("marked"),K=e=>{let r=F.marked.use({gfm:!0}).lexer(e).map(o=>o.raw),n=[];for(let o=0;o<r.length;o++){let s=r[o];if(s.trim()==="$$"&&n.length>0){let a=n.at(-1),c=a.trimStart().startsWith("$$"),l=(a.match(/\$\$/g)||[]).length;if(c&&l%2===1){n[n.length-1]=a+s;continue}}if(n.length>0&&s.trimEnd().endsWith("$$")){let a=n.at(-1),c=a.trimStart().startsWith("$$"),l=(a.match(/\$\$/g)||[]).length,m=(s.match(/\$\$/g)||[]).length;if(c&&l%2===1&&!s.trimStart().startsWith("$$")&&m===1){n[n.length-1]=a+s;continue}}n.push(s)}return n};var pe=/(!?\[)([^\]]*?)$/,fe=/(\*\*)([^*]*?)$/,ke=/(__)([^_]*?)$/,be=/(\*\*\*)([^*]*?)$/,we=/(\*)([^*]*?)$/,ye=/(_)([^_]*?)$/,ve=/(`)([^`]*?)$/,$e=/(~~)([^~]*?)$/,P=e=>{let t=(e.match(/```/g)||[]).length;return t>0&&t%2===0&&e.includes(`
|
|
2
|
+
`)},Me=e=>{let t=e.match(pe);if(t){if(t[1].startsWith("!")){let n=e.lastIndexOf(t[1]);return e.substring(0,n)}return`${e}](streamdown:incomplete-link)`}return e},Te=e=>{if(P(e))return e;let t=e.match(fe);if(t){let r=t[2];if(!r||/^[\s_~*`]*$/.test(r))return e;if((e.match(/\*\*/g)||[]).length%2===1)return`${e}**`}return e},Be=e=>{let t=e.match(ke);if(t){let r=t[2];if(!r||/^[\s_~*`]*$/.test(r))return e;if((e.match(/__/g)||[]).length%2===1)return`${e}__`}return e},Ce=e=>e.split("").reduce((t,r,n)=>{if(r==="*"){let o=e[n-1],s=e[n+1];if(o==="\\")return t;let a=n;for(let l=n-1;l>=0;l--){if(e[l]===`
|
|
3
|
+
`){a=l+1;break}if(l===0){a=0;break}}if(e.substring(a,n).trim()===""&&(s===" "||s===" "))return t;if(o!=="*"&&s!=="*")return t+1}return t},0),Ie=e=>{if(P(e))return e;if(e.match(we)){let r=-1;for(let s=0;s<e.length;s++)if(e[s]==="*"&&e[s-1]!=="*"&&e[s+1]!=="*"){r=s;break}if(r===-1)return e;let n=e.substring(r+1);if(!n||/^[\s_~*`]*$/.test(n))return e;if(Ce(e)%2===1)return`${e}*`}return e},j=(e,t)=>{let r=!1,n=!1;for(let o=0;o<e.length&&o<t;o++){if(e[o]==="\\"&&e[o+1]==="$"){o++;continue}e[o]==="$"&&(e[o+1]==="$"?(n=!n,o++,r=!1):n||(r=!r))}return r||n},Pe=e=>e.split("").reduce((t,r,n)=>{if(r==="_"){let o=e[n-1],s=e[n+1];if(o==="\\"||j(e,n))return t;if(o!=="_"&&s!=="_")return t+1}return t},0),Se=e=>{if(P(e))return e;if(e.match(ye)){let r=-1;for(let s=0;s<e.length;s++)if(e[s]==="_"&&e[s-1]!=="_"&&e[s+1]!=="_"&&!j(e,s)){r=s;break}if(r===-1)return e;let n=e.substring(r+1);if(!n||/^[\s_~*`]*$/.test(n))return e;if(Pe(e)%2===1)return`${e}_`}return e},Ne=(e,t)=>{let r=e.substring(t,t+3)==="```",n=t>0&&e.substring(t-1,t+2)==="```",o=t>1&&e.substring(t-2,t+1)==="```";return r||n||o},He=e=>{let t=0;for(let r=0;r<e.length;r++)e[r]==="`"&&!Ne(e,r)&&t++;return t},Le=e=>{if(e.match(/^```[^`\n]*```?$/)&&!e.includes(`
|
|
4
|
+
`))return e.endsWith("``")&&!e.endsWith("```")?`${e}\``:e;let r=(e.match(/```/g)||[]).length,n=r%2===1;if(r>0&&r%2===0&&e.includes(`
|
|
5
|
+
`)||(e.endsWith("```\n")||e.endsWith("```"))&&r%2===0)return e;let o=e.match(ve);if(o&&!n){let s=o[2];if(!s||/^[\s_~*`]*$/.test(s))return e;if(He(e)%2===1)return`${e}\``}return e},Ae=e=>{let t=e.match($e);if(t){let r=t[2];if(!r||/^[\s_~*`]*$/.test(r))return e;if((e.match(/~~/g)||[]).length%2===1)return`${e}~~`}return e};var _e=e=>{if((e.match(/\$\$/g)||[]).length%2===0)return e;let r=e.indexOf("$$");return r!==-1&&e.indexOf(`
|
|
6
6
|
`,r)!==-1&&!e.endsWith(`
|
|
7
7
|
`)?`${e}
|
|
8
|
-
$$`:`${e}$$`},Re=e=>{let t=0,r=e.match(/\*+/g)||[];for(let n of r){let o=n.length;o>=3&&(t+=Math.floor(o/3))}return t},Ee=e=>P(e)||/^\*{4,}$/.test(e)
|
|
8
|
+
$$`:`${e}$$`},Re=e=>{let t=0,r=e.match(/\*+/g)||[];for(let n of r){let o=n.length;o>=3&&(t+=Math.floor(o/3))}return t},Ee=e=>{if(P(e)||/^\*{4,}$/.test(e))return e;let t=e.match(be);if(t){let r=t[2];if(!r||/^[\s_~*`]*$/.test(r))return e;if(Re(e)%2===1)return`${e}***`}return e},X=e=>{if(!e||typeof e!="string")return e;let t=e,r=Me(t);return r.endsWith("](streamdown:incomplete-link)")?r:(t=r,t=Ee(t),t=Te(t),t=Be(t),t=Ie(t),t=Se(t),t=Le(t),t=Ae(t),t=_e(t),t)};var T=require("react/jsx-runtime"),De=L.default.default||L.default,We=De(J.default),I=(0,b.createContext)(["github-light","github-dark"]),x=(0,b.memo)(({content:e,shouldParseIncompleteMarkdown:t,...r})=>{let n=(0,b.useMemo)(()=>typeof e=="string"&&t?X(e.trim()):e,[e,t]);return(0,T.jsx)(We,{...r,children:n})},(e,t)=>e.content===t.content);x.displayName="Block";var ee=(0,b.memo)(({children:e,allowedImagePrefixes:t,allowedLinkPrefixes:r,defaultOrigin:n,parseIncompleteMarkdown:o=!0,components:s,rehypePlugins:a,remarkPlugins:c,className:l,shikiTheme:m=["github-light","github-dark"],...u})=>{let p=(0,b.useId)(),g=(0,b.useMemo)(()=>K(typeof e=="string"?e:""),[e]),w=(0,b.useMemo)(()=>()=>(0,Q.default)({errorColor:"var(--color-muted-foreground)"}),[]);return(0,T.jsx)(I.Provider,{value:m,children:(0,T.jsx)("div",{className:i("space-y-4",l),...u,children:g.map((y,M)=>(0,T.jsx)(x,{allowedImagePrefixes:t!=null?t:["*"],allowedLinkPrefixes:r!=null?r:["*"],components:{...V,...s},content:y,defaultOrigin:n,rehypePlugins:[w,...a!=null?a:[]],remarkPlugins:[Y.default,[Z.default,{singleDollarTextMath:!1}],...c!=null?c:[]],shouldParseIncompleteMarkdown:o},`${p}-block_${M}`))})})},(e,t)=>e.children===t.children&&e.shikiTheme===t.shikiTheme);ee.displayName="Streamdown";0&&(module.exports={ShikiThemeContext,Streamdown});
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use client";import{createContext as Le,memo as V,useId as Ae,useMemo as N}from"react";import _e from"react-markdown";import Re from"rehype-katex";import Ee from"remark-gfm";import De from"remark-math";import"katex/dist/katex.min.css";import G from"harden-react-markdown";import{isValidElement as oe}from"react";import{CheckIcon as X,CopyIcon as J}from"lucide-react";import{createContext as Q,useContext as A,useEffect as _,useRef as R,useState as B}from"react";import{createHighlighter as H}from"shiki";import{createJavaScriptRegexEngine as Y}from"shiki/engine/javascript";import{clsx as j}from"clsx";import{twMerge as F}from"tailwind-merge";var i=(...e)=>F(j(e));import{jsx as k,jsxs as C}from"react/jsx-runtime";var Z=/<pre(\s|>)/,E=Q({code:""}),$=class{constructor(){this.lightHighlighter=null;this.darkHighlighter=null;this.lightTheme=null;this.darkTheme=null;this.loadedLanguages=new Set;this.initializationPromise=null}async ensureHighlightersInitialized(t,r){var m,u;let[n,o]=t,s=Y({forgiving:!0}),a=!this.lightHighlighter||this.lightTheme!==n,c=!this.darkHighlighter||this.darkTheme!==o;(a||c)&&this.loadedLanguages.clear();let l=!this.loadedLanguages.has(r);if(a?(this.lightHighlighter=await H({themes:[n],langs:[r],engine:s}),this.lightTheme=n,this.loadedLanguages.add(r)):l&&await((m=this.lightHighlighter)==null?void 0:m.loadLanguage(r)),c){let g=l?[...this.loadedLanguages,r]:Array.from(this.loadedLanguages);this.darkHighlighter=await H({themes:[o],langs:g.length>0?g:[r],engine:s}),this.darkTheme=o}else l&&await((u=this.darkHighlighter)==null?void 0:u.loadLanguage(r));l&&this.loadedLanguages.add(r)}async highlightCode(t,r,n,o){var u,g;this.initializationPromise&&await this.initializationPromise,this.initializationPromise=this.ensureHighlightersInitialized(n,r),await this.initializationPromise,this.initializationPromise=null;let s=h=>o?h.replace(Z,`<pre class="${o}"$1`):h,[a,c]=n,l=(u=this.lightHighlighter)==null?void 0:u.codeToHtml(t,{lang:r,theme:a}),m=(g=this.darkHighlighter)==null?void 0:g.codeToHtml(t,{lang:r,theme:c});return[L(s(l)),L(s(m))]}},ee=new $;async function te(e,t,r,n){return ee.highlightCode(e,t,r,n)}function L(e){return e.replace(/(<pre[^>]*)(style="[^"]*background[^";]*;?[^"]*")([^>]*>)/g,"$1$3")}var D=({code:e,language:t,className:r,children:n,preClassName:o,...s})=>{let[a,c]=B(""),[l,m]=B(""),u=R(!1),[g,h]=A(P);return _(()=>(u.current=!0,te(e,t,[g,h],o).then(([p,f])=>{u.current&&(c(p),m(f))}),()=>{u.current=!1}),[e,t,g,h,o]),k(E.Provider,{value:{code:e},children:C("div",{className:"my-4 w-full overflow-hidden rounded-xl border","data-code-block-container":!0,"data-language":t,children:[C("div",{className:"flex items-center justify-between bg-muted/80 p-3 text-muted-foreground text-xs","data-code-block-header":!0,"data-language":t,children:[k("span",{className:"ml-1 font-mono lowercase",children:t}),k("div",{children:n})]}),k("div",{className:"w-full",children:C("div",{className:"min-w-full",children:[k("div",{className:i("overflow-x-auto dark:hidden",r),dangerouslySetInnerHTML:{__html:a},"data-code-block":!0,"data-language":t,...s}),k("div",{className:i("hidden overflow-x-auto dark:block",r),dangerouslySetInnerHTML:{__html:l},"data-code-block":!0,"data-language":t,...s})]})})]})})},I=({onCopy:e,onError:t,timeout:r=2e3,children:n,className:o,code:s,...a})=>{let[c,l]=B(!1),m=R(0),u=A(E).code,g=s!=null?s:u,h=async()=>{var f;if(typeof window=="undefined"||!((f=navigator==null?void 0:navigator.clipboard)!=null&&f.writeText)){t==null||t(new Error("Clipboard API not available"));return}try{c||(await navigator.clipboard.writeText(g),l(!0),e==null||e(),m.current=window.setTimeout(()=>l(!1),r))}catch(b){t==null||t(b)}};_(()=>()=>{window.clearTimeout(m.current)},[]);let p=c?X:J;return k("button",{className:i("text-muted-foreground","p-1 transition-all",o),onClick:h,type:"button",...a,children:n!=null?n:k(p,{size:14})})};import{useEffect as re,useState as y}from"react";import{jsx as w,jsxs as v}from"react/jsx-runtime";var W=!1,ne=async()=>{if(!W){let r=(await import("mermaid")).default;return r.initialize({startOnLoad:!1,theme:"default",securityLevel:"strict",fontFamily:"monospace",suppressErrorRendering:!0}),W=!0,r}return(await import("mermaid")).default},z=({chart:e,className:t})=>{let[r,n]=y(null),[o,s]=y(!0),[a,c]=y(""),[l,m]=y("");if(re(()=>{(async()=>{try{n(null),s(!0);let h=await ne(),p=e.split("").reduce((S,K)=>(S<<5)-S+K.charCodeAt(0)|0,0),f=`mermaid-${Math.abs(p)}-${Date.now()}-${Math.random().toString(36).substring(2,9)}`,{svg:b}=await h.render(f,e);c(b),m(b)}catch(h){if(!(l||a)){let p=h instanceof Error?h.message:"Failed to render Mermaid chart";n(p)}}finally{s(!1)}})()},[e]),o&&!a&&!l)return w("div",{className:i("my-4 flex justify-center p-4",t),children:v("div",{className:"flex items-center space-x-2 text-muted-foreground",children:[w("div",{className:"h-4 w-4 animate-spin rounded-full border-current border-b-2"}),w("span",{className:"text-sm",children:"Loading diagram..."})]})});if(r&&!a&&!l)return v("div",{className:i("rounded-lg border border-red-200 bg-red-50 p-4",t),children:[v("p",{className:"font-mono text-red-700 text-sm",children:["Mermaid Error: ",r]}),v("details",{className:"mt-2",children:[w("summary",{className:"cursor-pointer text-red-600 text-xs",children:"Show Code"}),w("pre",{className:"mt-2 overflow-x-auto rounded bg-red-100 p-2 text-red-800 text-xs",children:e})]})]});let u=a||l;return w("div",{"aria-label":"Mermaid chart",className:i("my-4 flex justify-center",t),dangerouslySetInnerHTML:{__html:u},role:"img"})};import{jsx as d,jsxs as ae}from"react/jsx-runtime";var se=/language-([^\s]+)/,ie=({node:e,className:t,children:r,...n})=>{var l,m,u;if(((l=e==null?void 0:e.position)==null?void 0:l.start.line)===((m=e==null?void 0:e.position)==null?void 0:m.end.line))return d("code",{className:i("rounded bg-muted px-1.5 py-0.5 font-mono text-sm",t),"data-streamdown":"inline-code",...n,children:r});let s=t==null?void 0:t.match(se),a=(u=s==null?void 0:s.at(1))!=null?u:"",c="";return oe(r)&&r.props&&typeof r.props=="object"&&"children"in r.props&&typeof r.props.children=="string"?c=r.props.children:typeof r=="string"&&(c=r),a==="mermaid"?ae("div",{className:i("group relative my-4 h-auto rounded-xl border p-4",t),"data-streamdown":"mermaid-block",children:[d("div",{className:"flex items-center justify-end",children:d(I,{code:c})}),d(z,{chart:c})]}):d(D,{className:i("overflow-x-auto border-t",t),code:c,"data-language":a,"data-streamdown":"code-block",language:a,preClassName:"overflow-x-auto font-mono text-xs p-4 bg-muted/40",children:d(I,{})})},O={ol:({node:e,children:t,className:r,...n})=>d("ol",{className:i("ml-4 list-outside list-decimal whitespace-normal",r),"data-streamdown":"ordered-list",...n,children:t}),li:({node:e,children:t,className:r,...n})=>d("li",{className:i("py-1",r),"data-streamdown":"list-item",...n,children:t}),ul:({node:e,children:t,className:r,...n})=>d("ul",{className:i("ml-4 list-outside list-disc whitespace-normal",r),"data-streamdown":"unordered-list",...n,children:t}),hr:({node:e,className:t,...r})=>d("hr",{className:i("my-6 border-border",t),"data-streamdown":"horizontal-rule",...r}),strong:({node:e,children:t,className:r,...n})=>d("span",{className:i("font-semibold",r),"data-streamdown":"strong",...n,children:t}),a:({node:e,children:t,className:r,href:n,...o})=>{let s=n==="streamdown:incomplete-link";return d("a",{className:i("font-medium text-primary underline",r),"data-incomplete":s,"data-streamdown":"link",href:n,rel:"noreferrer",target:"_blank",...o,children:t})},h1:({node:e,children:t,className:r,...n})=>d("h1",{className:i("mt-6 mb-2 font-semibold text-3xl",r),"data-streamdown":"heading-1",...n,children:t}),h2:({node:e,children:t,className:r,...n})=>d("h2",{className:i("mt-6 mb-2 font-semibold text-2xl",r),"data-streamdown":"heading-2",...n,children:t}),h3:({node:e,children:t,className:r,...n})=>d("h3",{className:i("mt-6 mb-2 font-semibold text-xl",r),"data-streamdown":"heading-3",...n,children:t}),h4:({node:e,children:t,className:r,...n})=>d("h4",{className:i("mt-6 mb-2 font-semibold text-lg",r),"data-streamdown":"heading-4",...n,children:t}),h5:({node:e,children:t,className:r,...n})=>d("h5",{className:i("mt-6 mb-2 font-semibold text-base",r),"data-streamdown":"heading-5",...n,children:t}),h6:({node:e,children:t,className:r,...n})=>d("h6",{className:i("mt-6 mb-2 font-semibold text-sm",r),"data-streamdown":"heading-6",...n,children:t}),table:({node:e,children:t,className:r,...n})=>d("div",{className:"my-4 overflow-x-auto","data-streamdown":"table-wrapper",children:d("table",{className:i("w-full border-collapse border border-border",r),"data-streamdown":"table",...n,children:t})}),thead:({node:e,children:t,className:r,...n})=>d("thead",{className:i("bg-muted/50",r),"data-streamdown":"table-header",...n,children:t}),tbody:({node:e,children:t,className:r,...n})=>d("tbody",{className:i("divide-y divide-border",r),"data-streamdown":"table-body",...n,children:t}),tr:({node:e,children:t,className:r,...n})=>d("tr",{className:i("border-border border-b",r),"data-streamdown":"table-row",...n,children:t}),th:({node:e,children:t,className:r,...n})=>d("th",{className:i("px-4 py-2 text-left font-semibold text-sm",r),"data-streamdown":"table-header-cell",...n,children:t}),td:({node:e,children:t,className:r,...n})=>d("td",{className:i("px-4 py-2 text-sm",r),"data-streamdown":"table-cell",...n,children:t}),blockquote:({node:e,children:t,className:r,...n})=>d("blockquote",{className:i("my-4 border-muted-foreground/30 border-l-4 pl-4 text-muted-foreground italic",r),"data-streamdown":"blockquote",...n,children:t}),code:ie,pre:({children:e})=>e,sup:({node:e,children:t,className:r,...n})=>d("sup",{className:i("text-sm",r),"data-streamdown":"superscript",...n,children:t}),sub:({node:e,children:t,className:r,...n})=>d("sub",{className:i("text-sm",r),"data-streamdown":"subscript",...n,children:t})};import{marked as le}from"marked";var U=e=>{let r=le.use({gfm:!0}).lexer(e).map(o=>o.raw),n=[];for(let o=0;o<r.length;o++){let s=r[o];if(s.trim()==="$$"&&n.length>0){let a=n.at(-1),c=a.trimStart().startsWith("$$"),l=(a.match(/\$\$/g)||[]).length;if(c&&l%2===1){n[n.length-1]=a+s;continue}}if(n.length>0&&s.trimEnd().endsWith("$$")){let a=n.at(-1),c=a.trimStart().startsWith("$$"),l=(a.match(/\$\$/g)||[]).length,m=(s.match(/\$\$/g)||[]).length;if(c&&l%2===1&&!s.trimStart().startsWith("$$")&&m===1){n[n.length-1]=a+s;continue}}n.push(s)}return n};var de=/(!?\[)([^\]]*?)$/,ce=/(\*\*)([^*]*?)$/,me=/(__)([^_]*?)$/,ue=/(\*\*\*)([^*]*?)$/,he=/(\*)([^*]*?)$/,ge=/(_)([^_]*?)$/,pe=/(`)([^`]*?)$/,fe=/(~~)([^~]*?)$/,M=e=>{let t=(e.match(/```/g)||[]).length;return t>0&&t%2===0&&e.includes(`
|
|
2
|
-
`)},
|
|
3
|
-
`){a=l+1;break}if(l===0){a=0;break}}if(e.substring(a,n).trim()===""&&(
|
|
4
|
-
`))return e.endsWith("``")&&!e.endsWith("```")?`${e}\``:e;let r=(e.match(/```/g)||[]).length,n=r%2===1;
|
|
5
|
-
`)||(e.endsWith("```\n")||e.endsWith("```"))&&r%2===0
|
|
1
|
+
"use client";import{createContext as Le,memo as F,useId as Ae,useMemo as S}from"react";import _e from"react-markdown";import Re from"rehype-katex";import Ee from"remark-gfm";import De from"remark-math";import"katex/dist/katex.min.css";import V from"harden-react-markdown";import{isValidElement as se}from"react";import{CheckIcon as Q,CopyIcon as Y}from"lucide-react";import{createContext as Z,useContext as A,useEffect as _,useRef as R,useState as B}from"react";import{createHighlighter as H}from"shiki";import{createJavaScriptRegexEngine as x}from"shiki/engine/javascript";import{clsx as X}from"clsx";import{twMerge as J}from"tailwind-merge";var i=(...e)=>J(X(e));import{jsx as k,jsxs as T}from"react/jsx-runtime";var ee=/<pre(\s|>)/,E=Z({code:""}),C=class{constructor(){this.lightHighlighter=null;this.darkHighlighter=null;this.lightTheme=null;this.darkTheme=null;this.loadedLanguages=new Set;this.initializationPromise=null}async ensureHighlightersInitialized(t,r){var m,u;let[n,s]=t,o=x({forgiving:!0}),a=!this.lightHighlighter||this.lightTheme!==n,c=!this.darkHighlighter||this.darkTheme!==s;(a||c)&&this.loadedLanguages.clear();let l=!this.loadedLanguages.has(r);if(a?(this.lightHighlighter=await H({themes:[n],langs:[r],engine:o}),this.lightTheme=n,this.loadedLanguages.add(r)):l&&await((m=this.lightHighlighter)==null?void 0:m.loadLanguage(r)),c){let g=l?[...this.loadedLanguages,r]:Array.from(this.loadedLanguages);this.darkHighlighter=await H({themes:[s],langs:g.length>0?g:[r],engine:o}),this.darkTheme=s}else l&&await((u=this.darkHighlighter)==null?void 0:u.loadLanguage(r));l&&this.loadedLanguages.add(r)}async highlightCode(t,r,n,s){var u,g;this.initializationPromise&&await this.initializationPromise,this.initializationPromise=this.ensureHighlightersInitialized(n,r),await this.initializationPromise,this.initializationPromise=null;let o=h=>s?h.replace(ee,`<pre class="${s}"$1`):h,[a,c]=n,l=(u=this.lightHighlighter)==null?void 0:u.codeToHtml(t,{lang:r,theme:a}),m=(g=this.darkHighlighter)==null?void 0:g.codeToHtml(t,{lang:r,theme:c});return[L(o(l)),L(o(m))]}},te=new C;async function re(e,t,r,n){return te.highlightCode(e,t,r,n)}function L(e){return e.replace(/(<pre[^>]*)(style="[^"]*background[^";]*;?[^"]*")([^>]*>)/g,"$1$3")}var D=({code:e,language:t,className:r,children:n,preClassName:s,...o})=>{let[a,c]=B(""),[l,m]=B(""),u=R(!1),[g,h]=A(P);return _(()=>(u.current=!0,re(e,t,[g,h],s).then(([p,f])=>{u.current&&(c(p),m(f))}),()=>{u.current=!1}),[e,t,g,h,s]),k(E.Provider,{value:{code:e},children:T("div",{className:"my-4 w-full overflow-hidden rounded-xl border","data-code-block-container":!0,"data-language":t,children:[T("div",{className:"flex items-center justify-between bg-muted/80 p-3 text-muted-foreground text-xs","data-code-block-header":!0,"data-language":t,children:[k("span",{className:"ml-1 font-mono lowercase",children:t}),k("div",{children:n})]}),k("div",{className:"w-full",children:T("div",{className:"min-w-full",children:[k("div",{className:i("overflow-x-auto dark:hidden",r),dangerouslySetInnerHTML:{__html:a},"data-code-block":!0,"data-language":t,...o}),k("div",{className:i("hidden overflow-x-auto dark:block",r),dangerouslySetInnerHTML:{__html:l},"data-code-block":!0,"data-language":t,...o})]})})]})})},I=({onCopy:e,onError:t,timeout:r=2e3,children:n,className:s,code:o,...a})=>{let[c,l]=B(!1),m=R(0),u=A(E).code,g=o!=null?o:u,h=async()=>{var f;if(typeof window=="undefined"||!((f=navigator==null?void 0:navigator.clipboard)!=null&&f.writeText)){t==null||t(new Error("Clipboard API not available"));return}try{c||(await navigator.clipboard.writeText(g),l(!0),e==null||e(),m.current=window.setTimeout(()=>l(!1),r))}catch(b){t==null||t(b)}};_(()=>()=>{window.clearTimeout(m.current)},[]);let p=c?Q:Y;return k("button",{className:i("text-muted-foreground","p-1 transition-all",s),onClick:h,type:"button",...a,children:n!=null?n:k(p,{size:14})})};import{useEffect as ne,useState as y}from"react";import{jsx as w,jsxs as v}from"react/jsx-runtime";var W=!1,oe=async()=>{if(!W){let r=(await import("mermaid")).default;return r.initialize({startOnLoad:!1,theme:"default",securityLevel:"strict",fontFamily:"monospace",suppressErrorRendering:!0}),W=!0,r}return(await import("mermaid")).default},z=({chart:e,className:t})=>{let[r,n]=y(null),[s,o]=y(!0),[a,c]=y(""),[l,m]=y("");if(ne(()=>{(async()=>{try{n(null),o(!0);let h=await oe(),p=e.split("").reduce((N,j)=>(N<<5)-N+j.charCodeAt(0)|0,0),f=`mermaid-${Math.abs(p)}-${Date.now()}-${Math.random().toString(36).substring(2,9)}`,{svg:b}=await h.render(f,e);c(b),m(b)}catch(h){if(!(l||a)){let p=h instanceof Error?h.message:"Failed to render Mermaid chart";n(p)}}finally{o(!1)}})()},[e]),s&&!a&&!l)return w("div",{className:i("my-4 flex justify-center p-4",t),children:v("div",{className:"flex items-center space-x-2 text-muted-foreground",children:[w("div",{className:"h-4 w-4 animate-spin rounded-full border-current border-b-2"}),w("span",{className:"text-sm",children:"Loading diagram..."})]})});if(r&&!a&&!l)return v("div",{className:i("rounded-lg border border-red-200 bg-red-50 p-4",t),children:[v("p",{className:"font-mono text-red-700 text-sm",children:["Mermaid Error: ",r]}),v("details",{className:"mt-2",children:[w("summary",{className:"cursor-pointer text-red-600 text-xs",children:"Show Code"}),w("pre",{className:"mt-2 overflow-x-auto rounded bg-red-100 p-2 text-red-800 text-xs",children:e})]})]});let u=a||l;return w("div",{"aria-label":"Mermaid chart",className:i("my-4 flex justify-center",t),dangerouslySetInnerHTML:{__html:u},role:"img"})};import{jsx as d,jsxs as le}from"react/jsx-runtime";var ie=/language-([^\s]+)/,ae=({node:e,className:t,children:r,...n})=>{var l,m,u;if(((l=e==null?void 0:e.position)==null?void 0:l.start.line)===((m=e==null?void 0:e.position)==null?void 0:m.end.line))return d("code",{className:i("rounded bg-muted px-1.5 py-0.5 font-mono text-sm",t),"data-streamdown":"inline-code",...n,children:r});let o=t==null?void 0:t.match(ie),a=(u=o==null?void 0:o.at(1))!=null?u:"",c="";return se(r)&&r.props&&typeof r.props=="object"&&"children"in r.props&&typeof r.props.children=="string"?c=r.props.children:typeof r=="string"&&(c=r),a==="mermaid"?le("div",{className:i("group relative my-4 h-auto rounded-xl border p-4",t),"data-streamdown":"mermaid-block",children:[d("div",{className:"flex items-center justify-end",children:d(I,{code:c})}),d(z,{chart:c})]}):d(D,{className:i("overflow-x-auto border-t",t),code:c,"data-language":a,"data-streamdown":"code-block",language:a,preClassName:"overflow-x-auto font-mono text-xs p-4 bg-muted/40",children:d(I,{})})},O={ol:({node:e,children:t,className:r,...n})=>d("ol",{className:i("ml-4 list-outside list-decimal whitespace-normal",r),"data-streamdown":"ordered-list",...n,children:t}),li:({node:e,children:t,className:r,...n})=>d("li",{className:i("py-1",r),"data-streamdown":"list-item",...n,children:t}),ul:({node:e,children:t,className:r,...n})=>d("ul",{className:i("ml-4 list-outside list-disc whitespace-normal",r),"data-streamdown":"unordered-list",...n,children:t}),hr:({node:e,className:t,...r})=>d("hr",{className:i("my-6 border-border",t),"data-streamdown":"horizontal-rule",...r}),strong:({node:e,children:t,className:r,...n})=>d("span",{className:i("font-semibold",r),"data-streamdown":"strong",...n,children:t}),a:({node:e,children:t,className:r,href:n,...s})=>{let o=n==="streamdown:incomplete-link";return d("a",{className:i("font-medium text-primary underline",r),"data-incomplete":o,"data-streamdown":"link",href:n,rel:"noreferrer",target:"_blank",...s,children:t})},h1:({node:e,children:t,className:r,...n})=>d("h1",{className:i("mt-6 mb-2 font-semibold text-3xl",r),"data-streamdown":"heading-1",...n,children:t}),h2:({node:e,children:t,className:r,...n})=>d("h2",{className:i("mt-6 mb-2 font-semibold text-2xl",r),"data-streamdown":"heading-2",...n,children:t}),h3:({node:e,children:t,className:r,...n})=>d("h3",{className:i("mt-6 mb-2 font-semibold text-xl",r),"data-streamdown":"heading-3",...n,children:t}),h4:({node:e,children:t,className:r,...n})=>d("h4",{className:i("mt-6 mb-2 font-semibold text-lg",r),"data-streamdown":"heading-4",...n,children:t}),h5:({node:e,children:t,className:r,...n})=>d("h5",{className:i("mt-6 mb-2 font-semibold text-base",r),"data-streamdown":"heading-5",...n,children:t}),h6:({node:e,children:t,className:r,...n})=>d("h6",{className:i("mt-6 mb-2 font-semibold text-sm",r),"data-streamdown":"heading-6",...n,children:t}),table:({node:e,children:t,className:r,...n})=>d("div",{className:"my-4 overflow-x-auto","data-streamdown":"table-wrapper",children:d("table",{className:i("w-full border-collapse border border-border",r),"data-streamdown":"table",...n,children:t})}),thead:({node:e,children:t,className:r,...n})=>d("thead",{className:i("bg-muted/80",r),"data-streamdown":"table-header",...n,children:t}),tbody:({node:e,children:t,className:r,...n})=>d("tbody",{className:i("divide-y bg-muted/40 divide-border",r),"data-streamdown":"table-body",...n,children:t}),tr:({node:e,children:t,className:r,...n})=>d("tr",{className:i("border-border border-b",r),"data-streamdown":"table-row",...n,children:t}),th:({node:e,children:t,className:r,...n})=>d("th",{className:i("whitespace-nowrap px-4 py-2 text-left font-semibold text-sm",r),"data-streamdown":"table-header-cell",...n,children:t}),td:({node:e,children:t,className:r,...n})=>d("td",{className:i("px-4 py-2 text-sm",r),"data-streamdown":"table-cell",...n,children:t}),blockquote:({node:e,children:t,className:r,...n})=>d("blockquote",{className:i("my-4 border-muted-foreground/30 border-l-4 pl-4 text-muted-foreground italic",r),"data-streamdown":"blockquote",...n,children:t}),code:ae,pre:({children:e})=>e,sup:({node:e,children:t,className:r,...n})=>d("sup",{className:i("text-sm",r),"data-streamdown":"superscript",...n,children:t}),sub:({node:e,children:t,className:r,...n})=>d("sub",{className:i("text-sm",r),"data-streamdown":"subscript",...n,children:t})};import{marked as de}from"marked";var U=e=>{let r=de.use({gfm:!0}).lexer(e).map(s=>s.raw),n=[];for(let s=0;s<r.length;s++){let o=r[s];if(o.trim()==="$$"&&n.length>0){let a=n.at(-1),c=a.trimStart().startsWith("$$"),l=(a.match(/\$\$/g)||[]).length;if(c&&l%2===1){n[n.length-1]=a+o;continue}}if(n.length>0&&o.trimEnd().endsWith("$$")){let a=n.at(-1),c=a.trimStart().startsWith("$$"),l=(a.match(/\$\$/g)||[]).length,m=(o.match(/\$\$/g)||[]).length;if(c&&l%2===1&&!o.trimStart().startsWith("$$")&&m===1){n[n.length-1]=a+o;continue}}n.push(o)}return n};var ce=/(!?\[)([^\]]*?)$/,me=/(\*\*)([^*]*?)$/,ue=/(__)([^_]*?)$/,he=/(\*\*\*)([^*]*?)$/,ge=/(\*)([^*]*?)$/,pe=/(_)([^_]*?)$/,fe=/(`)([^`]*?)$/,ke=/(~~)([^~]*?)$/,$=e=>{let t=(e.match(/```/g)||[]).length;return t>0&&t%2===0&&e.includes(`
|
|
2
|
+
`)},be=e=>{let t=e.match(ce);if(t){if(t[1].startsWith("!")){let n=e.lastIndexOf(t[1]);return e.substring(0,n)}return`${e}](streamdown:incomplete-link)`}return e},we=e=>{if($(e))return e;let t=e.match(me);if(t){let r=t[2];if(!r||/^[\s_~*`]*$/.test(r))return e;if((e.match(/\*\*/g)||[]).length%2===1)return`${e}**`}return e},ye=e=>{let t=e.match(ue);if(t){let r=t[2];if(!r||/^[\s_~*`]*$/.test(r))return e;if((e.match(/__/g)||[]).length%2===1)return`${e}__`}return e},ve=e=>e.split("").reduce((t,r,n)=>{if(r==="*"){let s=e[n-1],o=e[n+1];if(s==="\\")return t;let a=n;for(let l=n-1;l>=0;l--){if(e[l]===`
|
|
3
|
+
`){a=l+1;break}if(l===0){a=0;break}}if(e.substring(a,n).trim()===""&&(o===" "||o===" "))return t;if(s!=="*"&&o!=="*")return t+1}return t},0),$e=e=>{if($(e))return e;if(e.match(ge)){let r=-1;for(let o=0;o<e.length;o++)if(e[o]==="*"&&e[o-1]!=="*"&&e[o+1]!=="*"){r=o;break}if(r===-1)return e;let n=e.substring(r+1);if(!n||/^[\s_~*`]*$/.test(n))return e;if(ve(e)%2===1)return`${e}*`}return e},q=(e,t)=>{let r=!1,n=!1;for(let s=0;s<e.length&&s<t;s++){if(e[s]==="\\"&&e[s+1]==="$"){s++;continue}e[s]==="$"&&(e[s+1]==="$"?(n=!n,s++,r=!1):n||(r=!r))}return r||n},Me=e=>e.split("").reduce((t,r,n)=>{if(r==="_"){let s=e[n-1],o=e[n+1];if(s==="\\"||q(e,n))return t;if(s!=="_"&&o!=="_")return t+1}return t},0),Te=e=>{if($(e))return e;if(e.match(pe)){let r=-1;for(let o=0;o<e.length;o++)if(e[o]==="_"&&e[o-1]!=="_"&&e[o+1]!=="_"&&!q(e,o)){r=o;break}if(r===-1)return e;let n=e.substring(r+1);if(!n||/^[\s_~*`]*$/.test(n))return e;if(Me(e)%2===1)return`${e}_`}return e},Be=(e,t)=>{let r=e.substring(t,t+3)==="```",n=t>0&&e.substring(t-1,t+2)==="```",s=t>1&&e.substring(t-2,t+1)==="```";return r||n||s},Ce=e=>{let t=0;for(let r=0;r<e.length;r++)e[r]==="`"&&!Be(e,r)&&t++;return t},Ie=e=>{if(e.match(/^```[^`\n]*```?$/)&&!e.includes(`
|
|
4
|
+
`))return e.endsWith("``")&&!e.endsWith("```")?`${e}\``:e;let r=(e.match(/```/g)||[]).length,n=r%2===1;if(r>0&&r%2===0&&e.includes(`
|
|
5
|
+
`)||(e.endsWith("```\n")||e.endsWith("```"))&&r%2===0)return e;let s=e.match(fe);if(s&&!n){let o=s[2];if(!o||/^[\s_~*`]*$/.test(o))return e;if(Ce(e)%2===1)return`${e}\``}return e},Pe=e=>{let t=e.match(ke);if(t){let r=t[2];if(!r||/^[\s_~*`]*$/.test(r))return e;if((e.match(/~~/g)||[]).length%2===1)return`${e}~~`}return e};var Se=e=>{if((e.match(/\$\$/g)||[]).length%2===0)return e;let r=e.indexOf("$$");return r!==-1&&e.indexOf(`
|
|
6
6
|
`,r)!==-1&&!e.endsWith(`
|
|
7
7
|
`)?`${e}
|
|
8
|
-
$$`:`${e}$$`},
|
|
8
|
+
$$`:`${e}$$`},Ne=e=>{let t=0,r=e.match(/\*+/g)||[];for(let n of r){let s=n.length;s>=3&&(t+=Math.floor(s/3))}return t},He=e=>{if($(e)||/^\*{4,}$/.test(e))return e;let t=e.match(he);if(t){let r=t[2];if(!r||/^[\s_~*`]*$/.test(r))return e;if(Ne(e)%2===1)return`${e}***`}return e},G=e=>{if(!e||typeof e!="string")return e;let t=e,r=be(t);return r.endsWith("](streamdown:incomplete-link)")?r:(t=r,t=He(t),t=we(t),t=ye(t),t=$e(t),t=Te(t),t=Ie(t),t=Pe(t),t=Se(t),t)};import{jsx as M}from"react/jsx-runtime";var We=V.default||V,ze=We(_e),P=Le(["github-light","github-dark"]),K=F(({content:e,shouldParseIncompleteMarkdown:t,...r})=>{let n=S(()=>typeof e=="string"&&t?G(e.trim()):e,[e,t]);return M(ze,{...r,children:n})},(e,t)=>e.content===t.content);K.displayName="Block";var Oe=F(({children:e,allowedImagePrefixes:t,allowedLinkPrefixes:r,defaultOrigin:n,parseIncompleteMarkdown:s=!0,components:o,rehypePlugins:a,remarkPlugins:c,className:l,shikiTheme:m=["github-light","github-dark"],...u})=>{let g=Ae(),h=S(()=>U(typeof e=="string"?e:""),[e]),p=S(()=>()=>Re({errorColor:"var(--color-muted-foreground)"}),[]);return M(P.Provider,{value:m,children:M("div",{className:i("space-y-4",l),...u,children:h.map((f,b)=>M(K,{allowedImagePrefixes:t!=null?t:["*"],allowedLinkPrefixes:r!=null?r:["*"],components:{...O,...o},content:f,defaultOrigin:n,rehypePlugins:[p,...a!=null?a:[]],remarkPlugins:[Ee,[De,{singleDollarTextMath:!1}],...c!=null?c:[]],shouldParseIncompleteMarkdown:s},`${g}-block_${b}`))})})},(e,t)=>e.children===t.children&&e.shikiTheme===t.shikiTheme);Oe.displayName="Streamdown";export{P as ShikiThemeContext,Oe as Streamdown};
|