streamdown 1.1.7 → 1.1.8

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