import{NodeProp as e,Tree as t,NodeType as s,Parser as n,NodeSet as r,parseMixed as i}from"@lezer/common";import{Tag as l,styleTags as a,tags as o}from"@lezer/highlight";class CompositeBlock{static create(e,t,s,n,r){let i=n+(n<<8)+e+(t<<4)|0;return new CompositeBlock(e,t,s,i,r,[],[])}constructor(t,s,n,r,i,l,a){this.type=t;this.value=s;this.from=n;this.hash=r;this.end=i;this.children=l;this.positions=a;this.hashProp=[[e.contextHash,r]]}addChild(s,n){s.prop(e.contextHash)!=this.hash&&(s=new t(s.type,s.children,s.positions,s.length,this.hashProp));this.children.push(s);this.positions.push(n)}toTree(e,n=this.end){let r=this.children.length-1;r>=0&&(n=Math.max(n,this.positions[r]+this.children[r].length+this.from));return new t(e.types[this.type],this.children,this.positions,n-this.from).balance({makeTree:(e,n,r)=>new t(s.none,e,n,r,this.hashProp)})}}var h;(function(e){e[e.Document=1]="Document";e[e.CodeBlock=2]="CodeBlock";e[e.FencedCode=3]="FencedCode";e[e.Blockquote=4]="Blockquote";e[e.HorizontalRule=5]="HorizontalRule";e[e.BulletList=6]="BulletList";e[e.OrderedList=7]="OrderedList";e[e.ListItem=8]="ListItem";e[e.ATXHeading1=9]="ATXHeading1";e[e.ATXHeading2=10]="ATXHeading2";e[e.ATXHeading3=11]="ATXHeading3";e[e.ATXHeading4=12]="ATXHeading4";e[e.ATXHeading5=13]="ATXHeading5";e[e.ATXHeading6=14]="ATXHeading6";e[e.SetextHeading1=15]="SetextHeading1";e[e.SetextHeading2=16]="SetextHeading2";e[e.HTMLBlock=17]="HTMLBlock";e[e.LinkReference=18]="LinkReference";e[e.Paragraph=19]="Paragraph";e[e.CommentBlock=20]="CommentBlock";e[e.ProcessingInstructionBlock=21]="ProcessingInstructionBlock";e[e.Escape=22]="Escape";e[e.Entity=23]="Entity";e[e.HardBreak=24]="HardBreak";e[e.Emphasis=25]="Emphasis";e[e.StrongEmphasis=26]="StrongEmphasis";e[e.Link=27]="Link";e[e.Image=28]="Image";e[e.InlineCode=29]="InlineCode";e[e.HTMLTag=30]="HTMLTag";e[e.Comment=31]="Comment";e[e.ProcessingInstruction=32]="ProcessingInstruction";e[e.Autolink=33]="Autolink";e[e.HeaderMark=34]="HeaderMark";e[e.QuoteMark=35]="QuoteMark";e[e.ListMark=36]="ListMark";e[e.LinkMark=37]="LinkMark";e[e.EmphasisMark=38]="EmphasisMark";e[e.CodeMark=39]="CodeMark";e[e.CodeText=40]="CodeText";e[e.CodeInfo=41]="CodeInfo";e[e.LinkTitle=42]="LinkTitle";e[e.LinkLabel=43]="LinkLabel";e[e.URL=44]="URL"})(h||(h={}));class LeafBlock{constructor(e,t){this.start=e;this.content=t;this.marks=[];this.parsers=[]}}class Line{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 skipSpace(this.text,e)}reset(e){this.text=e;this.baseIndent=this.basePos=this.pos=this.indent=0;this.forwardInner();this.depth=1;while(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,t=0,s=0){for(let n=t;n=t.stack[s.depth+1].value+s.baseIndent)return true;if(s.indent>=s.baseIndent+4)return false;let n=(e.type==h.OrderedList?isOrderedList:isBulletList)(s,t,false);return n>0&&(e.type!=h.BulletList||isHorizontalRule(s,t,false)<0)&&s.text.charCodeAt(s.pos+n-1)==e.value}const f={[h.Blockquote](e,t,s){if(s.next!=62)return false;s.markers.push(elt(h.QuoteMark,t.lineStart+s.pos,t.lineStart+s.pos+1));s.moveBase(s.pos+(space(s.text.charCodeAt(s.pos+1))?2:1));e.end=t.lineStart+s.text.length;return true},[h.ListItem](e,t,s){if(s.indent-1)return false;s.moveBaseColumn(s.baseIndent+e.value);return true},[h.OrderedList]:skipForList,[h.BulletList]:skipForList,[h.Document](){return true}};function space(e){return e==32||e==9||e==10||e==13}function skipSpace(e,t=0){while(ts&&space(e.charCodeAt(t-1)))t--;return t}function isFencedCode(e){if(e.next!=96&&e.next!=126)return-1;let t=e.pos+1;while(t-1&&e.depth==t.stack.length||n<3?-1:1}function inList(e,t){for(let s=e.stack.length-1;s>=0;s--)if(e.stack[s].type==t)return true;return false}function isBulletList(e,t,s){return e.next!=45&&e.next!=43&&e.next!=42||e.pos!=e.text.length-1&&!space(e.text.charCodeAt(e.pos+1))||!(!s||inList(t,h.BulletList)||e.skipSpace(e.pos+2)=48&&r<=57))break;n++;if(n==e.text.length)return-1;r=e.text.charCodeAt(n)}return n==e.pos||n>e.pos+9||r!=46&&r!=41||ne.pos+1||e.next!=49)?-1:n+1-e.pos}function isAtxHeading(e){if(e.next!=35)return-1;let t=e.pos+1;while(t6?-1:s}function isSetextUnderline(e){if(e.next!=45&&e.next!=61||e.indent>=e.baseIndent+4)return-1;let t=e.pos+1;while(t/,c=/\?>/;const u=[[/^<(?:script|pre|style)(?:\s|>|$)/i,/<\/(?:script|pre|style)>/i],[/^\s*/i.exec(n);if(i)return e.append(elt(h.Comment,s,s+1+i[0].length));let l=/^\?[^]*?\?>/.exec(n);if(l)return e.append(elt(h.ProcessingInstruction,s,s+1+l[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(n);return a?e.append(elt(h.HTMLTag,s,s+1+a[0].length)):-1},Emphasis(e,t,s){if(t!=95&&t!=42)return-1;let n=s+1;while(e.char(n)==t)n++;let r=e.slice(s-1,s),i=e.slice(n,n+1);let l=I.test(r),a=I.test(i);let o=/\s|^$/.test(r),h=/\s|^$/.test(i);let f=!h&&(!a||o||l);let d=!o&&(!l||h||a);let p=f&&(t==42||!d||l);let c=d&&(t==42||!f||a);return e.append(new InlineDelimiter(t==95?C:S,s,n,(p?1:0)|(c?2:0)))},HardBreak(e,t,s){if(t==92&&e.char(s+1)==10)return e.append(elt(h.HardBreak,s,s+2));if(t==32){let t=s+1;while(e.char(t)==32)t++;if(e.char(t)==10&&t>=s+2)return e.append(elt(h.HardBreak,s,t+1))}return-1},Link(e,t,s){return t==91?e.append(new InlineDelimiter(w,s,s+1,1)):-1},Image(e,t,s){return t==33&&e.char(s+1)==91?e.append(new InlineDelimiter(y,s,s+2,1)):-1},LinkEnd(e,t,s){if(t!=93)return-1;for(let t=e.parts.length-1;t>=0;t--){let n=e.parts[t];if(n instanceof InlineDelimiter&&(n.type==w||n.type==y)){if(!n.side||e.skipSpace(n.to)==s&&!/[(\[]/.test(e.slice(s+1,s+2))){e.parts[t]=null;return-1}let r=e.takeContent(t);let i=e.parts[t]=finishLink(e,r,n.type==w?h.Link:h.Image,n.from,s+1);if(n.type==w)for(let s=0;st?elt(h.URL,t+s,r+s):r==e.length&&null}}function parseLinkTitle(e,t,s){let n=e.charCodeAt(t);if(n!=39&&n!=34&&n!=40)return false;let r=n==40?41:n;for(let n=t+1,i=false;n=this.end?-1:this.text.charCodeAt(e-this.offset)}get end(){return this.offset+this.text.length}slice(e,t){return this.text.slice(e-this.offset,t-this.offset)}append(e){this.parts.push(e);return e.to}addDelimiter(e,t,s,n,r){return this.append(new InlineDelimiter(e,t,s,(n?1:0)|(r?2:0)))}addElement(e){return this.append(e)}resolveMarkers(e){for(let t=e;t=e;l--){let e=this.parts[l];if(e instanceof InlineDelimiter&&e.side&1&&e.type==s.type&&!(n&&(s.side&1||e.side&2)&&(e.to-e.from+r)%3==0&&((e.to-e.from)%3||r%3))){i=e;break}}if(!i)continue;let a=s.type.resolve,o=[];let h=i.from,f=s.to;if(n){let e=Math.min(2,i.to-i.from,r);h=i.to-e;f=s.from+e;a=e==1?"Emphasis":"StrongEmphasis"}i.type.mark&&o.push(this.elt(i.type.mark,h,i.to));for(let e=l+1;e=0;t--){let s=this.parts[t];if(s instanceof InlineDelimiter&&s.type==e)return t}return null}takeContent(e){let t=this.resolveMarkers(e);this.parts.length=e;return t}skipSpace(e){return skipSpace(this.text,e-this.offset)+this.offset}elt(e,t,s,n){return typeof e=="string"?elt(this.parser.getNodeType(e),t,s,n):new TreeElement(e,t)}}function injectMarks(e,t){if(!t.length)return e;if(!e.length)return t;let s=e.slice(),n=0;for(let e of t){while(n(e?e-1:0))return false;if(this.fragmentEnd<0){let e=this.fragment.to;while(e>0&&this.input.read(e-1,e)!="\n")e--;this.fragmentEnd=e?e-1:0}let s=this.cursor;if(!s){s=this.cursor=this.fragment.tree.cursor();s.firstChild()}let n=e+this.fragment.offset;while(s.to<=n)if(!s.parent())return false;for(;;){if(s.from>=n)return this.fragment.from<=t;if(!s.childAfter(n))return false}}matches(t){let s=this.cursor.tree;return s&&s.prop(e.contextHash)==t}takeNodes(e){let s=this.cursor,n=this.fragment.offset,r=this.fragmentEnd-(this.fragment.openEnd?1:0);let i=e.absoluteLineStart,l=i,a=e.block.children.length;let o=l,f=a;for(;;){if(s.to-n>r){if(s.type.isAnonymous&&s.firstChild())continue;break}let i=toRelative(s.from-n,e.ranges);if(s.to-n<=e.ranges[e.rangeI].to)e.addNode(s.tree,i);else{let n=new t(e.parser.nodeSet.types[h.Paragraph],[],[],0,e.block.hashProp);e.reusePlaceholders.set(n,s.tree);e.addNode(n,i)}if(s.type.is("Block"))if(B.indexOf(s.type.id)<0){l=s.to-n;a=e.block.children.length}else{l=o;a=f;o=s.to-n;f=e.block.children.length}if(!s.nextSibling())break}while(e.block.children.length>a){e.block.children.pop();e.block.positions.pop()}return l-i}}function toRelative(e,t){let s=e;for(let n=1;nm[e])),Object.keys(m).map((e=>k[e])),Object.keys(m),g,f,Object.keys(E).map((e=>E[e])),Object.keys(E),[]);function leftOverSpace(e,t,s){let n=[];for(let r=e.firstChild,i=t;;r=r.nextSibling){let e=r?r.from:s;e>i&&n.push({from:i,to:e});if(!r)break;i=r.to}return n}function parseCode(e){let{codeParser:t,htmlParser:s}=e;let n=i(((e,n)=>{let r=e.type.id;if(!t||r!=h.CodeBlock&&r!=h.FencedCode){if(s&&(r==h.HTMLBlock||r==h.HTMLTag))return{parser:s,overlay:leftOverSpace(e.node,e.from,e.to)}}else{let s="";if(r==h.FencedCode){let t=e.node.getChild(h.CodeInfo);t&&(s=n.read(t.from,t.to))}let i=t(s);if(i)return{parser:i,overlay:e=>e.type.id==h.CodeText}}return null}));return{wrap:n}}const M={resolve:"Strikethrough",mark:"StrikethroughMark"};const P={defineNodes:[{name:"Strikethrough",style:{"Strikethrough/...":o.strikethrough}},{name:"StrikethroughMark",style:o.processingInstruction}],parseInline:[{name:"Strikethrough",parse(e,t,s){if(t!=126||e.char(s+1)!=126||e.char(s+2)==126)return-1;let n=e.slice(s-1,s),r=e.slice(s+2,s+3);let i=/\s|^$/.test(n),l=/\s|^$/.test(r);let a=I.test(n),o=I.test(r);return e.addDelimiter(M,s,s+2,!l&&(!o||i||a),!i&&(!a||l||o))},after:"Emphasis"}]};function parseRow(e,t,s=0,n,r=0){let i=0,l=true,a=-1,o=-1,h=false;let parseCell=()=>{n.push(e.elt("TableCell",r+a,r+o,e.parser.parseInline(t.slice(a,o),r+a)))};for(let f=s;f-1)&&i++;l=false;if(n){a>-1&&parseCell();n.push(e.elt("TableDelimiter",f+r,f+r+1))}a=o=-1}h=!h&&s==92}if(a>-1){i++;n&&parseCell()}return i}function hasPipe(e,t){for(let s=t;se instanceof TableParser))||!hasPipe(t.text,t.basePos))return false;let n=e.scanLine(e.absoluteLineEnd+1).text;return v.test(n)&&parseRow(e,t.text,t.basePos)==parseRow(e,n,t.basePos)},before:"SetextHeading"}]};class TaskParser{nextLine(){return false}finish(e,t){e.addLeafElement(t,e.elt("Task",t.start,t.start+t.content.length,[e.elt("TaskMarker",t.start,t.start+3),...e.parser.parseInline(t.content.slice(3),t.start+3)]));return true}}const R={defineNodes:[{name:"Task",block:true,style:o.list},{name:"TaskMarker",style:o.atom}],parseBlock:[{name:"TaskList",leaf(e,t){return/^\[[ xX]\][ \t]/.test(t.content)&&e.parentType().name=="ListItem"?new TaskParser:null},after:"SetextHeading"}]};const O=/(www\.)|(https?:\/\/)|([\w.+-]+@)|(mailto:|xmpp:)/gy;const z=/[\w-]+(\.[\w-]+)+(\/[^\s<]*)?/gy;const D=/[\w-]+\.[\w-]+($|\/)/;const X=/[\w.+-]+@[\w-]+(\.[\w.-]+)+/gy;const j=/\/[a-zA-Z\d@.]+/gy;function count(e,t,s,n){let r=0;for(let i=t;i-1)return-1;let n=t+s[0].length;for(;;){let s,r=e[n-1];if(/[?!.,:*_~]/.test(r)||r==")"&&count(e,t,n,")")>count(e,t,n,"("))n--;else{if(r!=";"||!(s=/&(?:#\d+|#x[a-f\d]+|\w+);$/.exec(e.slice(t,n))))break;n=t+s.index}}return n}function autolinkEmailEnd(e,t){X.lastIndex=t;let s=X.exec(e);if(!s)return-1;let n=s[0][s[0].length-1];return n=="_"||n=="-"?-1:t+s[0].length-(n=="."?1:0)}const F={parseInline:[{name:"Autolink",parse(e,t,s){let n=s-e.offset;O.lastIndex=n;let r=O.exec(e.text),i=-1;if(!r)return-1;if(r[1]||r[2])i=autolinkURLEnd(e.text,n+r[0].length);else if(r[3])i=autolinkEmailEnd(e.text,n);else{i=autolinkEmailEnd(e.text,n+r[0].length);if(i>-1&&r[0]=="xmpp:"){j.lastIndex=i;r=j.exec(e.text);r&&(i=r.index+r[0].length)}}if(i<0)return-1;e.addElement(e.elt("URL",s,i+e.offset));return i+e.offset}}]};const U=[N,R,P,F];function parseSubSuper(e,t,s){return(n,r,i)=>{if(r!=e||n.char(i+1)==e)return-1;let l=[n.elt(s,i,i+1)];for(let r=i+1;r