/* esm.sh - @milkdown/transformer@7.22.3 */
import{createNodeInParserFail as x,parserMatchError as M,serializerMatchError as g,stackOverFlow as d}from"../../../exception@7.22.3/es2022/exception.mjs";import{Mark as l}from"../../../prose@7.22.3/X-ZHByb3NlbWlycm9yLW1vZGVsQDEuMjUuMTI/es2022/model.mjs";var m=class{},f=class{constructor(){this.elements=[],this.size=()=>this.elements.length,this.top=()=>this.elements.at(-1),this.push=n=>{this.top()?.push(n)},this.open=n=>{this.elements.push(n)},this.close=()=>{let n=this.elements.pop();if(!n)throw d();return n}}},S=class k extends m{constructor(t,s,e){super(),this.type=t,this.content=s,this.attrs=e}push(t,...s){this.content.push(t,...s)}pop(){return this.content.pop()}static create(t,s,e){return new k(t,s,e)}},P=class extends f{#t;static{this.create=(n,t)=>{let s=new this(n);return e=>(s.run(t,e),s.toDoc())}}constructor(n){super(),this.#t=l.none,this.#s=t=>t.isText,this.#i=(t,s)=>{if(this.#s(t)&&this.#s(s)&&l.sameSet(t.marks,s.marks))return this.schema.text(t.text+s.text,t.marks)},this.#h=t=>{let s=Object.values({...this.schema.nodes,...this.schema.marks}).find(e=>e.spec.parseMarkdown.match(t));if(!s)throw M(t);return s},this.#n=t=>{let s=this.#h(t);s.spec.parseMarkdown.runner(this,t,s)},this.injectRoot=(t,s,e)=>(this.openNode(s,e),this.next(t.children),this),this.openNode=(t,s)=>(this.open(S.create(t,[],s)),this),this.#r=()=>{this.#t=l.none;let t=this.close();return this.#e(t.type,t.attrs,t.content)},this.closeNode=()=>{try{this.#r()}catch(t){console.error(t)}return this},this.#e=(t,s,e)=>{let r=t.createAndFill(s,e,this.#t);if(!r)throw x(t,s,e);return this.push(r),r},this.addNode=(t,s,e)=>{try{this.#e(t,s,e)}catch(r){console.error(r)}return this},this.openMark=(t,s)=>{let e=t.create(s);return this.#t=e.addToSet(this.#t),this},this.closeMark=t=>(this.#t=t.removeFromSet(this.#t),this),this.addText=t=>{try{let s=this.top();if(!s)throw d();let e=s.pop(),r=this.schema.text(t,this.#t);if(!e)return s.push(r),this;let i=this.#i(e,r);return i?(s.push(i),this):(s.push(e,r),this)}catch(s){return console.error(s),this}},this.build=()=>{let t;do t=this.#r();while(this.size());return t},this.next=(t=[])=>([t].flat().forEach(s=>this.#n(s)),this),this.toDoc=()=>this.build(),this.run=(t,s)=>{let e=t.runSync(t.parse(s),s);return this.next(e),this},this.schema=n}#s;#i;#h;#n;#r;#e},p=class v extends m{constructor(t,s,e,r={}){super(),this.type=t,this.children=s,this.value=e,this.props=r,this.push=(i,...h)=>{this.children||(this.children=[]),this.children.push(i,...h)},this.pop=()=>this.children?.pop()}static{this.create=(t,s,e,r={})=>new v(t,s,e,r)}},N=n=>Object.prototype.hasOwnProperty.call(n,"size"),b=class extends f{#t;static{this.create=(n,t)=>{let s=new this(n);return e=>(s.run(e),s.toString(t))}}constructor(n){super(),this.#t=[],this.#s=t=>{let s=Object.values({...this.schema.nodes,...this.schema.marks}).find(e=>e.spec.toMarkdown.match(t));if(!s)throw g(t.type);return s},this.#i=t=>this.#s(t).spec.toMarkdown.runner(this,t),this.#h=(t,s)=>this.#s(t).spec.toMarkdown.runner(this,t,s),this.#n=t=>{let s=i=>i.type.spec.priority??50,e=[...t].sort((i,h)=>s(i)-s(h)),r=[];return this.#t.forEach(({mark:i})=>{let h=e.findIndex(o=>o.eq(i));h>=0&&r.push(...e.splice(h,1))}),r.concat(e)},this.#r=t=>{let s=t?.marks,e=0;for(;e<this.#t.length;){let{mark:r,spanning:i}=this.#t[e];if(i&&s?.some(h=>h.eq(r)))e++;else break}for(let r=this.#t.length-1;r>=e;r--)this.#u(this.#t[r].mark)},this.#e=(t,s)=>{this.#n(t.marks).every(e=>!this.#h(e,t))&&this.#i(t),this.#r(s)},this.#a=t=>{let{children:s}=t;return s&&(t.children=s.reduce((e,r,i)=>{if(i===0)return[r];let h=e.at(-1);if(h&&h.isMark&&r.isMark){let{children:o,...u}=r,{children:c,...a}=h;if(r.type===h.type&&o&&c&&JSON.stringify(u)===JSON.stringify(a)){let y={...a,children:[...c,...o]};return e.slice(0,-1).concat(this.#a(y))}}return e.concat(r)},[])),t},this.#l=t=>{let s={...t.props,type:t.type};return t.children&&(s.children=t.children),t.value&&(s.value=t.value),s},this.openNode=(t,s,e)=>(this.open(p.create(t,void 0,s,e)),this),this.#p=(t,s)=>{let e="",r="",i=t.children;if(i){let o=i[0],u=i.at(-1);if(u&&u.type==="text"&&u.value.endsWith(" ")){let c=u.value,a=c.trimEnd();r=c.slice(a.length),u.value=a}if(o&&o.type==="text"&&o.value.startsWith(" ")){let c=o.value,a=c.trimStart();e=c.slice(0,c.length-a.length),o.value=a}}e.length&&this.#o("text",void 0,e);let h=s();return r.length&&this.#o("text",void 0,r),h},this.#c=(t=!1)=>{let s=this.close(),e=()=>this.#o(s.type,s.children,s.value,s.props);return t?this.#p(s,e):e()},this.closeNode=()=>(this.#c(),this),this.#o=(t,s,e,r)=>{let i=p.create(t,s,e,r),h=this.#a(this.#l(i));return this.push(h),h},this.addNode=(t,s,e,r)=>(this.#o(t,s,e,r),this),this.#d=(t,s,e,r)=>this.#t.some(i=>i.mark.eq(t))?this:(this.#t.push({mark:t,spanning:e==null}),this.openNode(s,e,{...r,isMark:!0})),this.#u=t=>{let s=-1;for(let e=this.#t.length-1;e>=0;e--)if(this.#t[e].mark.eq(t)){s=e;break}s<0||(this.#t.splice(s,1),this.#c(!0))},this.withMark=(t,s,e,r)=>(this.#d(t,s,e,r),this),this.closeMark=t=>(this.#u(t),this),this.build=()=>{let t=null;do t=this.#c();while(this.size());return t.children||(t.children=[]),t},this.next=t=>N(t)?(t.forEach((s,e,r)=>{this.#e(s,t.maybeChild(r+1)??void 0)}),this):(this.#e(t),this),this.toString=t=>t.stringify(this.build()),this.run=t=>(this.#t=[],this.next(t),this),this.schema=n}#s;#i;#h;#n;#r;#e;#a;#l;#p;#c;#o;#d;#u};export{P as ParserState,b as SerializerState,f as Stack,m as StackElement};
//# sourceMappingURL=transformer.mjs.map