/* esm.sh - mdast-util-math@3.0.0 */
import{ok as c}from"../../devlop@^1.0.0?target=es2022";import{longestStreak as w}from"../../longest-streak@^3.0.0?target=es2022";function M(){return{enter:{mathFlow:d,mathFlowFenceMeta:h,mathText:o},exit:{mathFlow:p,mathFlowFence:l,mathFlowFenceMeta:f,mathFlowValue:n,mathText:x,mathTextData:n}};function d(t){let e={type:"element",tagName:"code",properties:{className:["language-math","math-display"]},children:[]};this.enter({type:"math",meta:null,value:"",data:{hName:"pre",hChildren:[e]}},t)}function h(){this.buffer()}function f(){let t=this.resume(),e=this.stack[this.stack.length-1];c(e.type==="math"),e.meta=t}function l(){this.data.mathFlowInside||(this.buffer(),this.data.mathFlowInside=!0)}function p(t){let e=this.resume().replace(/^(\r?\n|\r)|(\r?\n|\r)$/g,""),a=this.stack[this.stack.length-1];c(a.type==="math"),this.exit(t),a.value=e;let i=a.data.hChildren[0];c(i.type==="element"),c(i.tagName==="code"),i.children.push({type:"text",value:e}),this.data.mathFlowInside=void 0}function o(t){this.enter({type:"inlineMath",value:"",data:{hName:"code",hProperties:{className:["language-math","math-inline"]},hChildren:[]}},t),this.buffer()}function x(t){let e=this.resume(),a=this.stack[this.stack.length-1];c(a.type==="inlineMath"),this.exit(t),a.value=e,a.data.hChildren.push({type:"text",value:e})}function n(t){this.config.enter.data.call(this,t),this.config.exit.data.call(this,t)}}function F(d){let h=(d||{}).singleDollarTextMath;return h==null&&(h=!0),l.peek=p,{unsafe:[{character:"\r",inConstruct:"mathFlowMeta"},{character:`
`,inConstruct:"mathFlowMeta"},{character:"$",after:h?void 0:"\\$",inConstruct:"phrasing"},{character:"$",inConstruct:"mathFlowMeta"},{atBreak:!0,character:"$",after:"\\$"}],handlers:{math:f,inlineMath:l}};function f(o,x,n,t){let e=o.value||"",a=n.createTracker(t),i="$".repeat(Math.max(w(e,"$")+1,2)),u=n.enter("mathFlow"),r=a.move(i);if(o.meta){let s=n.enter("mathFlowMeta");r+=a.move(n.safe(o.meta,{after:`
`,before:r,encode:["$"],...a.current()})),s()}return r+=a.move(`
`),e&&(r+=a.move(e+`
`)),r+=a.move(i),u(),r}function l(o,x,n){let t=o.value||"",e=1;for(h||e++;new RegExp("(^|[^$])"+"\\$".repeat(e)+"([^$]|$)").test(t);)e++;let a="$".repeat(e);/[^ \r\n]/.test(t)&&(/^[ \r\n]/.test(t)&&/[ \r\n]$/.test(t)||/^\$|\$$/.test(t))&&(t=" "+t+" ");let i=-1;for(;++i<n.unsafe.length;){let u=n.unsafe[i];if(!u.atBreak)continue;let r=n.compilePattern(u),s;for(;s=r.exec(t);){let m=s.index;t.codePointAt(m)===10&&t.codePointAt(m-1)===13&&m--,t=t.slice(0,m)+" "+t.slice(s.index+1)}}return a+t+a}function p(){return"$"}}export{M as mathFromMarkdown,F as mathToMarkdown};
//# sourceMappingURL=mdast-util-math.mjs.map