var N=Object.defineProperty,k=Object.defineProperties,D=Object.getOwnPropertyDescriptors,b=Object.getOwnPropertySymbols,U=Object.prototype.hasOwnProperty,H=Object.prototype.propertyIsEnumerable,I=(e,r,t)=>r in e?N(e,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[r]=t,T=(e,r)=>{for(var t in r||(r={}))U.call(r,t)&&I(e,t,r[t]);if(b)for(var t of b(r))H.call(r,t)&&I(e,t,r[t]);return e},S=(e,r)=>k(e,D(r)),G=/(\*\*)([^*]*\*?)$/,P=/(__)([^_]*?)$/,B=/(\*\*\*)([^*]*?)$/,M=/(\*)([^*]*?)$/,W=/(_)([^_]*?)$/,w=/(`)([^`]*?)$/,j=/(~~)([^~]*?)$/,h=/^[\s_~*`]*$/,L=/^[\s]*[-*+][\s]+$/,X=/[\p{L}\p{N}_]/u,z=/^```[^`\n]*```?$/,F=/^\*{4,}$/,Z=/(__)([^_]+)_$/,q=/(~~)([^~]+)~$/,O=/~~/g,n=e=>{if(!e)return!1;let r=e.charCodeAt(0);return r>=48&&r<=57||r>=65&&r<=90||r>=97&&r<=122||r===95?!0:X.test(e)},d=(e,r)=>{let t=!1;for(let l=0;l<r;l+=1)e[l]==="`"&&e[l+1]==="`"&&e[l+2]==="`"&&(t=!t,l+=2);return t},J=(e,r)=>{let t=1;for(let l=r-1;l>=0;l-=1)if(e[l]==="]")t+=1;else if(e[l]==="["&&(t-=1,t===0))return l;return-1},A=(e,r)=>{let t=1;for(let l=r+1;l<e.length;l+=1)if(e[l]==="[")t+=1;else if(e[l]==="]"&&(t-=1,t===0))return l;return-1},c=(e,r)=>{let t=!1,l=!1;for(let i=0;i<e.length&&i<r;i+=1){if(e[i]==="\\"&&e[i+1]==="$"){i+=1;continue}e[i]==="$"&&(e[i+1]==="$"?(l=!l,i+=1,t=!1):l||(t=!t))}return t||l},Q=(e,r)=>{for(let t=r;t<e.length;t+=1){if(e[t]===")")return!0;if(e[t]===`
`)return!1}return!1},R=(e,r)=>{for(let t=r-1;t>=0;t-=1){if(e[t]===")")return!1;if(e[t]==="(")return t>0&&e[t-1]==="]"?Q(e,r):!1;if(e[t]===`
`)return!1}return!1},V=(e,r)=>{for(let t=r-1;t>=0;t-=1){if(e[t]===">")return!1;if(e[t]==="<"){let l=t+1<e.length?e[t+1]:"";return l>="a"&&l<="z"||l>="A"&&l<="Z"||l==="/"}if(e[t]===`
`)return!1}return!1},$=(e,r,t)=>{let l=0;for(let s=r-1;s>=0;s-=1)if(e[s]===`
`){l=s+1;break}let i=e.length;for(let s=r;s<e.length;s+=1)if(e[s]===`
`){i=s;break}let u=e.substring(l,i),a=0,f=!1;for(let s of u)if(s===t)a+=1;else if(s!==" "&&s!=="	"){f=!0;break}return a>=3&&!f},Y=/^(\s*(?:[-*+]|\d+[.)]) +)>(=?\s*[$]?\d)/gm,ee=e=>!e||typeof e!="string"||!e.includes(">")?e:e.replace(Y,(r,t,l,i)=>d(e,i)?r:`${t}\\>${l}`),te=(e,r,t,l)=>t==="\\"||e.includes("$")&&c(e,r)?!0:t!=="*"&&l==="*"?(r<e.length-2?e[r+2]:"")!=="*":!!(t==="*"||t&&l&&n(t)&&n(l)||(!t||t===" "||t==="	"||t===`
`)&&(!l||l===" "||l==="	"||l===`
`)),x=e=>{let r=0,t=!1,l=e.length;for(let i=0;i<l;i+=1){if(e[i]==="`"&&i+2<l&&e[i+1]==="`"&&e[i+2]==="`"){t=!t,i+=2;continue}if(t||e[i]!=="*")continue;let u=i>0?e[i-1]:"",a=i<l-1?e[i+1]:"";te(e,i,u,a)||(r+=1)}return r},re=(e,r,t,l)=>!!(t==="\\"||e.includes("$")&&c(e,r)||R(e,r)||V(e,r)||t==="_"||l==="_"||t&&l&&n(t)&&n(l)),le=e=>{let r=0,t=!1,l=e.length;for(let i=0;i<l;i+=1){if(e[i]==="`"&&i+2<l&&e[i+1]==="`"&&e[i+2]==="`"){t=!t,i+=2;continue}if(t||e[i]!=="_")continue;let u=i>0?e[i-1]:"",a=i<l-1?e[i+1]:"";re(e,i,u,a)||(r+=1)}return r},ie=e=>{let r=0,t=0,l=!1;for(let i=0;i<e.length;i+=1){if(e[i]==="`"&&i+2<e.length&&e[i+1]==="`"&&e[i+2]==="`"){t>=3&&(r+=Math.floor(t/3)),t=0,l=!l,i+=2;continue}l||(e[i]==="*"?t+=1:(t>=3&&(r+=Math.floor(t/3)),t=0))}return t>=3&&(r+=Math.floor(t/3)),r},m=e=>{let r=0,t=!1;for(let l=0;l<e.length;l+=1){if(e[l]==="`"&&l+2<e.length&&e[l+1]==="`"&&e[l+2]==="`"){t=!t,l+=2;continue}t||e[l]==="*"&&l+1<e.length&&e[l+1]==="*"&&(r+=1,l+=1)}return r},v=e=>{let r=0,t=!1;for(let l=0;l<e.length;l+=1){if(e[l]==="`"&&l+2<e.length&&e[l+1]==="`"&&e[l+2]==="`"){t=!t,l+=2;continue}t||e[l]==="_"&&l+1<e.length&&e[l+1]==="_"&&(r+=1,l+=1)}return r},ue=(e,r,t)=>{if(!r||h.test(r))return!0;let l=e.substring(0,t).lastIndexOf(`
`),i=l===-1?0:l+1,u=e.substring(i,t);return L.test(u)&&r.includes(`
`)?!0:$(e,t,"*")},se=e=>{let r=e.match(G);if(!r)return e;let t=r[2],l=e.lastIndexOf(r[1]);return d(e,l)||ue(e,t,l)?e:m(e)%2===1?t.endsWith("*")?`${e}*`:`${e}**`:e},ae=(e,r,t)=>{if(!r||h.test(r))return!0;let l=e.substring(0,t).lastIndexOf(`
`),i=l===-1?0:l+1,u=e.substring(i,t);return L.test(u)&&r.includes(`
`)?!0:$(e,t,"_")},oe=e=>{let r=e.match(P);if(!r){let i=e.match(Z);if(i){let u=e.lastIndexOf(i[1]);if(!d(e,u)&&v(e)%2===1)return`${e}_`}return e}let t=r[2],l=e.lastIndexOf(r[1]);return d(e,l)||ae(e,t,l)?e:v(e)%2===1?`${e}__`:e},fe=e=>{let r=!1;for(let t=0;t<e.length;t+=1){if(e[t]==="`"&&t+2<e.length&&e[t+1]==="`"&&e[t+2]==="`"){r=!r,t+=2;continue}if(!r&&e[t]==="*"&&e[t-1]!=="*"&&e[t+1]!=="*"&&e[t-1]!=="\\"&&!c(e,t)){let l=t>0?e[t-1]:"",i=t<e.length-1?e[t+1]:"";if((!l||l===" "||l==="	"||l===`
`)&&(!i||i===" "||i==="	"||i===`
`)||l&&i&&n(l)&&n(i))continue;return t}}return-1},ne=e=>{if(!e.match(M))return e;let r=fe(e);if(r===-1)return e;let t=e.substring(r+1);return!t||h.test(t)?e:x(e)%2===1?`${e}*`:e},K=e=>{let r=!1;for(let t=0;t<e.length;t+=1){if(e[t]==="`"&&t+2<e.length&&e[t+1]==="`"&&e[t+2]==="`"){r=!r,t+=2;continue}if(!r&&e[t]==="_"&&e[t-1]!=="_"&&e[t+1]!=="_"&&e[t-1]!=="\\"&&!c(e,t)&&!R(e,t)){let l=t>0?e[t-1]:"",i=t<e.length-1?e[t+1]:"";if(l&&i&&n(l)&&n(i))continue;return t}}return-1},he=e=>{let r=e.length;for(;r>0&&e[r-1]===`
`;)r-=1;if(r<e.length){let t=e.slice(0,r),l=e.slice(r);return`${t}_${l}`}return`${e}_`},de=e=>{if(!e.endsWith("**"))return null;let r=e.slice(0,-2);if(m(r)%2!==1)return null;let t=r.indexOf("**"),l=K(r);return t!==-1&&l!==-1&&t<l?`${r}_**`:null},ge=e=>{if(!e.match(W))return e;let r=K(e);if(r===-1)return e;let t=e.substring(r+1);if(!t||h.test(t))return e;if(le(e)%2===1){let l=de(e);return l!==null?l:he(e)}return e},ce=e=>{let r=m(e),t=x(e);return r%2===0&&t%2===0},pe=(e,r,t)=>!r||h.test(r)||d(e,t)?!0:$(e,t,"*"),$e=e=>{if(F.test(e))return e;let r=e.match(B);if(!r)return e;let t=r[2],l=e.lastIndexOf(r[1]);return pe(e,t,l)?e:ie(e)%2===1?ce(e)?e:`${e}***`:e},g=(e,r)=>{let t=!1,l=!1;for(let i=0;i<r;i+=1){if(e.substring(i,i+3)==="```"){l=!l,i+=2;continue}!l&&e[i]==="`"&&(t=!t)}return t||l},me=(e,r)=>{let t=e.substring(r,r+3)==="```",l=r>0&&e.substring(r-1,r+2)==="```",i=r>1&&e.substring(r-2,r+1)==="```";return t||l||i},ye=e=>{let r=0;for(let t=0;t<e.length;t+=1)e[t]==="`"&&!me(e,t)&&(r+=1);return r},_e=/<[a-zA-Z/][^>]*$/,be=e=>{let r=e.match(_e);return!r||r.index===void 0||g(e,r.index)?e:e.substring(0,r.index).trimEnd()},Ie=e=>!e.match(z)||e.includes(`
`)?null:e.endsWith("``")&&!e.endsWith("```")?`${e}\``:e,Oe=e=>(e.match(/```/g)||[]).length%2===1,ve=e=>{let r=Ie(e);if(r!==null)return r;let t=e.match(w);if(t&&!Oe(e)){let l=t[2];if(!l||h.test(l))return e;if(ye(e)%2===1)return`${e}\``}return e},Ee=(e,r)=>r>=2&&e.substring(r-2,r+1)==="```"||r>=1&&e.substring(r-1,r+2)==="```"||r<=e.length-3&&e.substring(r,r+3)==="```",Te=e=>{let r=0,t=!1;for(let l=0;l<e.length-1;l+=1)e[l]==="`"&&!Ee(e,l)&&(t=!t),!t&&e[l]==="$"&&e[l+1]==="$"&&(r+=1,l+=1);return r},Se=e=>{let r=e.indexOf("$$");return r!==-1&&e.indexOf(`
`,r)!==-1&&!e.endsWith(`
`)?`${e}
$$`:`${e}$$`},Le=e=>Te(e)%2===0?e:Se(e),Ae=(e,r,t)=>{if(e.substring(r+2).includes(")"))return null;let l=J(e,r);if(l===-1||g(e,l))return null;let i=l>0&&e[l-1]==="!",u=i?l-1:l,a=e.substring(0,u);if(i)return a;let f=e.substring(l+1,r);return t==="text-only"?`${a}${f}`:`${a}[${f}](streamdown:incomplete-link)`},E=(e,r)=>{for(let t=0;t<r;t++)if(e[t]==="["&&!g(e,t)){if(t>0&&e[t-1]==="!")continue;let l=A(e,t);if(l===-1)return t;if(l+1<e.length&&e[l+1]==="("){let i=e.indexOf(")",l+2);i!==-1&&(t=i)}}return r},Re=(e,r,t)=>{let l=r>0&&e[r-1]==="!",i=l?r-1:r;if(!e.substring(r+1).includes("]")){let u=e.substring(0,i);if(l)return u;if(t==="text-only"){let a=E(e,r);return e.substring(0,a)+e.substring(a+1)}return`${e}](streamdown:incomplete-link)`}if(A(e,r)===-1){let u=e.substring(0,i);if(l)return u;if(t==="text-only"){let a=E(e,r);return e.substring(0,a)+e.substring(a+1)}return`${e}](streamdown:incomplete-link)`}return null},C=(e,r="protocol")=>{let t=e.lastIndexOf("](");if(t!==-1&&!g(e,t)){let l=Ae(e,t,r);if(l!==null)return l}for(let l=e.length-1;l>=0;l-=1)if(e[l]==="["&&!g(e,l)){let i=Re(e,l,r);if(i!==null)return i}return e},xe=/^-{1,2}$/,Ke=/^[\s]*-{1,2}[\s]+$/,Ce=/^={1,2}$/,Ne=/^[\s]*={1,2}[\s]+$/,ke=e=>{if(!e||typeof e!="string")return e;let r=e.lastIndexOf(`
`);if(r===-1)return e;let t=e.substring(r+1),l=e.substring(0,r),i=t.trim();if(xe.test(i)&&!t.match(Ke)){let u=l.split(`
`).at(-1);if(u&&u.trim().length>0)return`${e}​`}if(Ce.test(i)&&!t.match(Ne)){let u=l.split(`
`).at(-1);if(u&&u.trim().length>0)return`${e}​`}return e},De=e=>{var r,t;let l=e.match(j);if(l){let i=l[2];if(!i||h.test(i))return e;if(((r=e.match(O))==null?void 0:r.length)%2===1)return`${e}~~`}else if(e.match(q)&&((t=e.match(O))==null?void 0:t.length)%2===1)return`${e}~`;return e},p=e=>e!==!1,o={COMPARISON_OPERATORS:-10,HTML_TAGS:-5,SETEXT_HEADINGS:0,LINKS:10,BOLD_ITALIC:20,BOLD:30,ITALIC_DOUBLE_UNDERSCORE:40,ITALIC_SINGLE_ASTERISK:41,ITALIC_SINGLE_UNDERSCORE:42,INLINE_CODE:50,STRIKETHROUGH:60,KATEX:70,DEFAULT:100},Ue=[{handler:{name:"comparisonOperators",handle:ee,priority:o.COMPARISON_OPERATORS},optionKey:"comparisonOperators"},{handler:{name:"htmlTags",handle:be,priority:o.HTML_TAGS},optionKey:"htmlTags"},{handler:{name:"setextHeadings",handle:ke,priority:o.SETEXT_HEADINGS},optionKey:"setextHeadings"},{handler:{name:"links",handle:C,priority:o.LINKS},optionKey:"links",earlyReturn:e=>e.endsWith("](streamdown:incomplete-link)")},{handler:{name:"boldItalic",handle:$e,priority:o.BOLD_ITALIC},optionKey:"boldItalic"},{handler:{name:"bold",handle:se,priority:o.BOLD},optionKey:"bold"},{handler:{name:"italicDoubleUnderscore",handle:oe,priority:o.ITALIC_DOUBLE_UNDERSCORE},optionKey:"italic"},{handler:{name:"italicSingleAsterisk",handle:ne,priority:o.ITALIC_SINGLE_ASTERISK},optionKey:"italic"},{handler:{name:"italicSingleUnderscore",handle:ge,priority:o.ITALIC_SINGLE_UNDERSCORE},optionKey:"italic"},{handler:{name:"inlineCode",handle:ve,priority:o.INLINE_CODE},optionKey:"inlineCode"},{handler:{name:"strikethrough",handle:De,priority:o.STRIKETHROUGH},optionKey:"strikethrough"},{handler:{name:"katex",handle:Le,priority:o.KATEX},optionKey:"katex"}],He=e=>{var r;let t=(r=e?.linkMode)!=null?r:"protocol";return Ue.filter(({handler:l,optionKey:i})=>l.name==="links"?p(e?.links)||p(e?.images):p(e?.[i])).map(({handler:l,earlyReturn:i})=>l.name==="links"?{handler:S(T({},l),{handle:u=>C(u,t)}),earlyReturn:t==="protocol"?i:void 0}:{handler:l,earlyReturn:i})},Ge=(e,r)=>{var t;if(!e||typeof e!="string")return e;let l=e.endsWith(" ")&&!e.endsWith("  ")?e.slice(0,-1):e,i=He(r),u=((t=r?.handlers)!=null?t:[]).map(f=>{var s;return{handler:S(T({},f),{priority:(s=f.priority)!=null?s:o.DEFAULT}),earlyReturn:void 0}}),a=[...i,...u].sort((f,s)=>{var y,_;return((y=f.handler.priority)!=null?y:0)-((_=s.handler.priority)!=null?_:0)});for(let{handler:f,earlyReturn:s}of a)if(l=f.handle(l),s!=null&&s(l))return l;return l},Pe=Ge;export{Pe as p};
