import{NodeType as N,NodeProp as x,NodeSet as K,Tree as w,Parser as _e,parseMixed as Ue}from"@lezer/common";import{styleTags as Y,tags as p,Tag as qe}from"@lezer/highlight";class z{static create(e,s,r,n,i){let o=n+(n<<8)+e+(s<<4)|0;return new z(e,s,r,o,i,[],[])}constructor(e,s,r,n,i,o,a){this.type=e,this.value=s,this.from=r,this.hash=n,this.end=i,this.children=o,this.positions=a,this.hashProp=[[x.contextHash,n]]}addChild(e,s){e.prop(x.contextHash)!=this.hash&&(e=new w(e.type,e.children,e.positions,e.length,this.hashProp)),this.children.push(e),this.positions.push(s)}toTree(e,s=this.end){let r=this.children.length-1;return r>=0&&(s=Math.max(s,this.positions[r]+this.children[r].length+this.from)),new w(e.types[this.type],this.children,this.positions,s-this.from).balance({makeTree:(n,i,o)=>new w(N.none,n,i,o,this.hashProp)})}}var l;(function(t){t[t.Document=1]="Document",t[t.CodeBlock=2]="CodeBlock",t[t.FencedCode=3]="FencedCode",t[t.Blockquote=4]="Blockquote",t[t.HorizontalRule=5]="HorizontalRule",t[t.BulletList=6]="BulletList",t[t.OrderedList=7]="OrderedList",t[t.ListItem=8]="ListItem",t[t.ATXHeading1=9]="ATXHeading1",t[t.ATXHeading2=10]="ATXHeading2",t[t.ATXHeading3=11]="ATXHeading3",t[t.ATXHeading4=12]="ATXHeading4",t[t.ATXHeading5=13]="ATXHeading5",t[t.ATXHeading6=14]="ATXHeading6",t[t.SetextHeading1=15]="SetextHeading1",t[t.SetextHeading2=16]="SetextHeading2",t[t.HTMLBlock=17]="HTMLBlock",t[t.LinkReference=18]="LinkReference",t[t.Paragraph=19]="Paragraph",t[t.CommentBlock=20]="CommentBlock",t[t.ProcessingInstructionBlock=21]="ProcessingInstructionBlock",t[t.Escape=22]="Escape",t[t.Entity=23]="Entity",t[t.HardBreak=24]="HardBreak",t[t.Emphasis=25]="Emphasis",t[t.StrongEmphasis=26]="StrongEmphasis",t[t.Link=27]="Link",t[t.Image=28]="Image",t[t.InlineCode=29]="InlineCode",t[t.HTMLTag=30]="HTMLTag",t[t.Comment=31]="Comment",t[t.ProcessingInstruction=32]="ProcessingInstruction",t[t.Autolink=33]="Autolink",t[t.HeaderMark=34]="HeaderMark",t[t.QuoteMark=35]="QuoteMark",t[t.ListMark=36]="ListMark",t[t.LinkMark=37]="LinkMark",t[t.EmphasisMark=38]="EmphasisMark",t[t.CodeMark=39]="CodeMark",t[t.CodeText=40]="CodeText",t[t.CodeInfo=41]="CodeInfo",t[t.LinkTitle=42]="LinkTitle",t[t.LinkLabel=43]="LinkLabel",t[t.URL=44]="URL"})(l||(l={}));class W{constructor(e,s){this.start=e,this.content=s,this.marks=[],this.parsers=[]}}class ee{constructor(){this.text="",this.baseIndent=0,this.basePos=0,this.depth=0,this.markers=[],this.pos=0,this.indent=0,this.next=-1}forward(){this.basePos>this.pos&&this.forwardInner()}forwardInner(){let e=this.skipSpace(this.basePos);this.indent=this.countIndent(e,this.pos,this.indent),this.pos=e,this.next=e==this.text.length?-1:this.text.charCodeAt(e)}skipSpace(e){return M(this.text,e)}reset(e){for(this.text=e,this.baseIndent=this.basePos=this.pos=this.indent=0,this.forwardInner(),this.depth=1;this.markers.length;)this.markers.pop()}moveBase(e){this.basePos=e,this.baseIndent=this.countIndent(e,this.pos,this.indent)}moveBaseColumn(e){this.baseIndent=e,this.basePos=this.findColumn(e)}addMarker(e){this.markers.push(e)}countIndent(e,s=0,r=0){for(let n=s;n=e.stack[s.depth+1].value+s.baseIndent)return!0;if(s.indent>=s.baseIndent+4)return!1;let r=(t.type==l.OrderedList?$:j)(s,e,!1);return r>0&&(t.type!=l.BulletList||F(s,e,!1)<0)&&s.text.charCodeAt(s.pos+r-1)==t.value}const se={[l.Blockquote](t,e,s){return s.next!=62?!1:(s.markers.push(c(l.QuoteMark,e.lineStart+s.pos,e.lineStart+s.pos+1)),s.moveBase(s.pos+(b(s.text.charCodeAt(s.pos+1))?2:1)),t.end=e.lineStart+s.text.length,!0)},[l.ListItem](t,e,s){return s.indent-1?!1:(s.moveBaseColumn(s.baseIndent+t.value),!0)},[l.OrderedList]:te,[l.BulletList]:te,[l.Document](){return!0}};function b(t){return t==32||t==9||t==10||t==13}function M(t,e=0){for(;es&&b(t.charCodeAt(e-1));)e--;return e}function ne(t){if(t.next!=96&&t.next!=126)return-1;let e=t.pos+1;for(;e-1&&t.depth==e.stack.length||r<3?-1:1}function ae(t,e){for(let s=t.stack.length-1;s>=0;s--)if(t.stack[s].type==e)return!0;return!1}function j(t,e,s){return(t.next==45||t.next==43||t.next==42)&&(t.pos==t.text.length-1||b(t.text.charCodeAt(t.pos+1)))&&(!s||ae(e,l.BulletList)||t.skipSpace(t.pos+2)=48&&n<=57;){r++;if(r==t.text.length)return-1;n=t.text.charCodeAt(r)}return r==t.pos||r>t.pos+9||n!=46&&n!=41||rt.pos+1||t.next!=49)?-1:r+1-t.pos}function oe(t){if(t.next!=35)return-1;let e=t.pos+1;for(;e6?-1:s}function le(t){if(t.next!=45&&t.next!=61||t.indent>=t.baseIndent+4)return-1;let e=t.pos+1;for(;e/,fe=/\?>/,U=[[/^<(?:script|pre|style)(?:\s|>|$)/i,/<\/(?:script|pre|style)>/i],[/^\s*/i.exec(r);if(i)return t.append(c(l.Comment,s,s+1+i[0].length));let o=/^\?[^]*?\?>/.exec(r);if(o)return t.append(c(l.ProcessingInstruction,s,s+1+o[0].length));let a=/^(?:![A-Z][^]*?>|!\[CDATA\[[^]*?\]\]>|\/\s*[a-zA-Z][\w-]*\s*>|\s*[a-zA-Z][\w-]*(\s+[a-zA-Z:_][\w-.:]*(?:\s*=\s*(?:[^\s"'=<>`]+|'[^']*'|"[^"]*"))?)*\s*(\/\s*)?>)/.exec(r);return a?t.append(c(l.HTMLTag,s,s+1+a[0].length)):-1},Emphasis(t,e,s){if(e!=95&&e!=42)return-1;let r=s+1;for(;t.char(r)==e;)r++;let n=t.slice(s-1,s),i=t.slice(r,r+1),o=P.test(n),a=P.test(i),h=/\s|^$/.test(n),d=/\s|^$/.test(i),u=!d&&(!a||h||o),k=!h&&(!o||d||a),f=u&&(e==42||!k||o),g=k&&(e==42||!u||a);return t.append(new S(e==95?Le:be,s,r,(f?1:0)|(g?2:0)))},HardBreak(t,e,s){if(e==92&&t.char(s+1)==10)return t.append(c(l.HardBreak,s,s+2));if(e==32){let r=s+1;for(;t.char(r)==32;)r++;if(t.char(r)==10&&r>=s+2)return t.append(c(l.HardBreak,s,r+1))}return-1},Link(t,e,s){return e==91?t.append(new S(I,s,s+1,1)):-1},Image(t,e,s){return e==33&&t.char(s+1)==91?t.append(new S(Q,s,s+2,1)):-1},LinkEnd(t,e,s){if(e!=93)return-1;for(let r=t.parts.length-1;r>=0;r--){let n=t.parts[r];if(n instanceof S&&(n.type==I||n.type==Q)){if(!n.side||t.skipSpace(n.to)==s&&!/[(\[]/.test(t.slice(s+1,s+2)))return t.parts[r]=null,-1;let i=t.takeContent(r),o=t.parts[r]=Ke(t,i,n.type==I?l.Link:l.Image,n.from,s+1);if(n.type==I)for(let a=0;ae?c(l.URL,e+s,i+s):i==t.length?null:!1}}function Ce(t,e,s){let r=t.charCodeAt(e);if(r!=39&&r!=34&&r!=40)return!1;let n=r==40?41:r;for(let i=e+1,o=!1;i=this.end?-1:this.text.charCodeAt(e-this.offset)}get end(){return this.offset+this.text.length}slice(e,s){return this.text.slice(e-this.offset,s-this.offset)}append(e){return this.parts.push(e),e.to}addDelimiter(e,s,r,n,i){return this.append(new S(e,s,r,(n?1:0)|(i?2:0)))}get hasOpenLink(){for(let e=this.parts.length-1;e>=0;e--){let s=this.parts[e];if(s instanceof S&&(s.type==I||s.type==Q))return!0}return!1}addElement(e){return this.append(e)}resolveMarkers(e){for(let r=e;r=e;h--){let m=this.parts[h];if(m instanceof S&&m.side&1&&m.type==n.type&&!(i&&(n.side&1||m.side&2)&&(m.to-m.from+o)%3==0&&((m.to-m.from)%3||o%3))){a=m;break}}if(!a)continue;let d=n.type.resolve,u=[],k=a.from,f=n.to;if(i){let m=Math.min(2,a.to-a.from,o);k=a.to-m,f=n.from+m,d=m==1?"Emphasis":"StrongEmphasis"}a.type.mark&&u.push(this.elt(a.type.mark,k,a.to));for(let m=h+1;m=0;s--){let r=this.parts[s];if(r instanceof S&&r.type==e)return s}return null}takeContent(e){let s=this.resolveMarkers(e);return this.parts.length=e,s}skipSpace(e){return M(this.text,e-this.offset)+this.offset}elt(e,s,r,n){return typeof e=="string"?c(this.parser.getNodeType(e),s,r,n):new me(e,s)}}function V(t,e){if(!e.length)return t;if(!t.length)return e;let s=t.slice(),r=0;for(let n of e){for(;r(e?e-1:0))return!1;if(this.fragmentEnd<0){let i=this.fragment.to;for(;i>0&&this.input.read(i-1,i)!=` `;)i--;this.fragmentEnd=i?i-1:0}let r=this.cursor;r||(r=this.cursor=this.fragment.tree.cursor(),r.firstChild());let n=e+this.fragment.offset;for(;r.to<=n;)if(!r.parent())return!1;for(;;){if(r.from>=n)return this.fragment.from<=s;if(!r.childAfter(n))return!1}}matches(e){let s=this.cursor.tree;return s&&s.prop(x.contextHash)==e}takeNodes(e){let s=this.cursor,r=this.fragment.offset,n=this.fragmentEnd-(this.fragment.openEnd?1:0),i=e.absoluteLineStart,o=i,a=e.block.children.length,h=o,d=a;for(;;){if(s.to-r>n){if(s.type.isAnonymous&&s.firstChild())continue;break}let u=Ie(s.from-r,e.ranges);if(s.to-r<=e.ranges[e.rangeI].to)e.addNode(s.tree,u);else{let k=new w(e.parser.nodeSet.types[l.Paragraph],[],[],0,e.block.hashProp);e.reusePlaceholders.set(k,s.tree),e.addNode(k,u)}if(s.type.is("Block")&&(Ye.indexOf(s.type.id)<0?(o=s.to-r,a=e.block.children.length):(o=h,a=d,h=s.to-r,d=e.block.children.length)),!s.nextSibling())break}for(;e.block.children.length>a;)e.block.children.pop(),e.block.positions.pop();return o-i}}function Ie(t,e){let s=t;for(let r=1;rR[t]),Object.keys(R).map(t=>Ge[t]),Object.keys(R),Ve,se,Object.keys(G).map(t=>G[t]),Object.keys(G),[]);function st(t,e,s){let r=[];for(let n=t.firstChild,i=e;;n=n.nextSibling){let o=n?n.from:s;if(o>i&&r.push({from:i,to:o}),!n)break;i=n.to}return r}function rt(t){let{codeParser:e,htmlParser:s}=t;return{wrap:Ue((n,i)=>{let o=n.type.id;if(e&&(o==l.CodeBlock||o==l.FencedCode)){let a="";if(o==l.FencedCode){let d=n.node.getChild(l.CodeInfo);d&&(a=i.read(d.from,d.to))}let h=e(a);if(h)return{parser:h,overlay:d=>d.type.id==l.CodeText}}else if(s&&(o==l.HTMLBlock||o==l.HTMLTag))return{parser:s,overlay:st(n.node,n.from,n.to)};return null})}}const nt={resolve:"Strikethrough",mark:"StrikethroughMark"},Be={defineNodes:[{name:"Strikethrough",style:{"Strikethrough/...":p.strikethrough}},{name:"StrikethroughMark",style:p.processingInstruction}],parseInline:[{name:"Strikethrough",parse(t,e,s){if(e!=126||t.char(s+1)!=126||t.char(s+2)==126)return-1;let r=t.slice(s-1,s),n=t.slice(s+2,s+3),i=/\s|^$/.test(r),o=/\s|^$/.test(n),a=P.test(r),h=P.test(n);return t.addDelimiter(nt,s,s+2,!o&&(!h||i||a),!i&&(!a||o||h))},after:"Emphasis"}]};function v(t,e,s=0,r,n=0){let i=0,o=!0,a=-1,h=-1,d=!1,u=()=>{r.push(t.elt("TableCell",n+a,n+h,t.parser.parseInline(e.slice(a,h),n+a)))};for(let k=s;k-1)&&i++,o=!1,r&&(a>-1&&u(),r.push(t.elt("TableDelimiter",k+n,k+n+1))),a=h=-1):(d||f!=32&&f!=9)&&(a<0&&(a=k),h=k+1),d=!d&&f==92}return a>-1&&(i++,r&&u()),i}function xe(t,e){for(let s=e;sn instanceof He)||!xe(e.text,e.basePos))return!1;let r=t.scanLine(t.absoluteLineEnd+1).text;return Me.test(r)&&v(t,e.text,e.basePos)==v(t,r,e.basePos)},before:"SetextHeading"}]};class it{nextLine(){return!1}finish(e,s){return e.addLeafElement(s,e.elt("Task",s.start,s.start+s.content.length,[e.elt("TaskMarker",s.start,s.start+3),...e.parser.parseInline(s.content.slice(3),s.start+3)])),!0}}const ve={defineNodes:[{name:"Task",block:!0,style:p.list},{name:"TaskMarker",style:p.atom}],parseBlock:[{name:"TaskList",leaf(t,e){return/^\[[ xX]\][ \t]/.test(e.content)&&t.parentType().name=="ListItem"?new it:null},after:"SetextHeading"}]},Ne=/(www\.)|(https?:\/\/)|([\w.+-]+@)|(mailto:|xmpp:)/gy,Re=/[\w-]+(\.[\w-]+)+(\/[^\s<]*)?/gy,at=/[\w-]+\.[\w-]+($|\/)/,Oe=/[\w.+-]+@[\w-]+(\.[\w.-]+)+/gy,ze=/\/[a-zA-Z\d@.]+/gy;function ye(t,e,s,r){let n=0;for(let i=e;i-1)return-1;let r=e+s[0].length;for(;;){let n=t[r-1],i;if(/[?!.,:*_~]/.test(n)||n==")"&&ye(t,e,r,")")>ye(t,e,r,"("))r--;else if(n==";"&&(i=/&(?:#\d+|#x[a-f\d]+|\w+);$/.exec(t.slice(e,r))))r=e+i.index;else break}return r}function Te(t,e){Oe.lastIndex=e;let s=Oe.exec(t);if(!s)return-1;let r=s[0][s[0].length-1];return r=="_"||r=="-"?-1:e+s[0].length-(r=="."?1:0)}const De={parseInline:[{name:"Autolink",parse(t,e,s){let r=s-t.offset;Ne.lastIndex=r;let n=Ne.exec(t.text),i=-1;if(!n)return-1;if(n[1]||n[2]){if(i=ot(t.text,r+n[0].length),i>-1&&t.hasOpenLink){let o=/([^\[\]]|\[[^\]]*\])*/.exec(t.text.slice(r,i));i=r+o[0].length}}else n[3]?i=Te(t.text,r):(i=Te(t.text,r+n[0].length),i>-1&&n[0]=="xmpp:"&&(ze.lastIndex=i,n=ze.exec(t.text),n&&(i=n.index+n[0].length)));return i<0?-1:(t.addElement(t.elt("URL",s,i+t.offset)),i+t.offset)}}]},lt=[Pe,ve,Be,De];function Xe(t,e,s){return(r,n,i)=>{if(n!=t||r.char(i+1)==t)return-1;let o=[r.elt(s,i,i+1)];for(let a=i+1;a