import{NodeType,NodeProp,NodeSet,Tree,Parser,parseMixed}from"@lezer/common";import{styleTags,tags,Tag}from"@lezer/highlight";class CompositeBlock{constructor(type,value,from,hash,end,children,positions){this.type=type;this.value=value;this.from=from;this.hash=hash;this.end=end;this.children=children;this.positions=positions;this.hashProp=[[NodeProp.contextHash,hash]]}static create(type,value,from,parentHash,end){let hash=parentHash+(parentHash<<8)+type+(value<<4)|0;return new CompositeBlock(type,value,from,hash,end,[],[])}addChild(child,pos){if(child.prop(NodeProp.contextHash)!=this.hash)child=new Tree(child.type,child.children,child.positions,child.length,this.hashProp);this.children.push(child);this.positions.push(pos)}toTree(nodeSet,end=this.end){let last=this.children.length-1;if(last>=0)end=Math.max(end,this.positions[last]+this.children[last].length+this.from);let tree=new Tree(nodeSet.types[this.type],this.children,this.positions,end-this.from).balance({makeTree:(children,positions,length)=>new Tree(NodeType.none,children,positions,length,this.hashProp)});return tree}}var Type;(function(Type){Type[Type["Document"]=1]="Document";Type[Type["CodeBlock"]=2]="CodeBlock";Type[Type["FencedCode"]=3]="FencedCode";Type[Type["Blockquote"]=4]="Blockquote";Type[Type["HorizontalRule"]=5]="HorizontalRule";Type[Type["BulletList"]=6]="BulletList";Type[Type["OrderedList"]=7]="OrderedList";Type[Type["ListItem"]=8]="ListItem";Type[Type["ATXHeading1"]=9]="ATXHeading1";Type[Type["ATXHeading2"]=10]="ATXHeading2";Type[Type["ATXHeading3"]=11]="ATXHeading3";Type[Type["ATXHeading4"]=12]="ATXHeading4";Type[Type["ATXHeading5"]=13]="ATXHeading5";Type[Type["ATXHeading6"]=14]="ATXHeading6";Type[Type["SetextHeading1"]=15]="SetextHeading1";Type[Type["SetextHeading2"]=16]="SetextHeading2";Type[Type["HTMLBlock"]=17]="HTMLBlock";Type[Type["LinkReference"]=18]="LinkReference";Type[Type["Paragraph"]=19]="Paragraph";Type[Type["CommentBlock"]=20]="CommentBlock";Type[Type["ProcessingInstructionBlock"]=21]="ProcessingInstructionBlock";Type[Type["Escape"]=22]="Escape";Type[Type["Entity"]=23]="Entity";Type[Type["HardBreak"]=24]="HardBreak";Type[Type["Emphasis"]=25]="Emphasis";Type[Type["StrongEmphasis"]=26]="StrongEmphasis";Type[Type["Link"]=27]="Link";Type[Type["Image"]=28]="Image";Type[Type["InlineCode"]=29]="InlineCode";Type[Type["HTMLTag"]=30]="HTMLTag";Type[Type["Comment"]=31]="Comment";Type[Type["ProcessingInstruction"]=32]="ProcessingInstruction";Type[Type["URL"]=33]="URL";Type[Type["HeaderMark"]=34]="HeaderMark";Type[Type["QuoteMark"]=35]="QuoteMark";Type[Type["ListMark"]=36]="ListMark";Type[Type["LinkMark"]=37]="LinkMark";Type[Type["EmphasisMark"]=38]="EmphasisMark";Type[Type["CodeMark"]=39]="CodeMark";Type[Type["CodeText"]=40]="CodeText";Type[Type["CodeInfo"]=41]="CodeInfo";Type[Type["LinkTitle"]=42]="LinkTitle";Type[Type["LinkLabel"]=43]="LinkLabel"})(Type||(Type={}));class LeafBlock{constructor(start,content){this.start=start;this.content=content;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(){if(this.basePos>this.pos)this.forwardInner()}forwardInner(){let newPos=this.skipSpace(this.basePos);this.indent=this.countIndent(newPos,this.pos,this.indent);this.pos=newPos;this.next=newPos==this.text.length?-1:this.text.charCodeAt(newPos)}skipSpace(from){return skipSpace(this.text,from)}reset(text){this.text=text;this.baseIndent=this.basePos=this.pos=this.indent=0;this.forwardInner();this.depth=1;while(this.markers.length)this.markers.pop()}moveBase(to){this.basePos=to;this.baseIndent=this.countIndent(to,this.pos,this.indent)}moveBaseColumn(indent){this.baseIndent=indent;this.basePos=this.findColumn(indent)}addMarker(elt){this.markers.push(elt)}countIndent(to,from=0,indent=0){for(let i=from;i=cx.stack[line.depth+1].value+line.baseIndent)return true;if(line.indent>=line.baseIndent+4)return false;let size=(bl.type==Type.OrderedList?isOrderedList:isBulletList)(line,cx,false);return size>0&&(bl.type!=Type.BulletList||isHorizontalRule(line,cx,false)<0)&&line.text.charCodeAt(line.pos+size-1)==bl.value}const DefaultSkipMarkup={[Type.Blockquote](bl,cx,line){if(line.next!=62)return false;line.markers.push(elt(Type.QuoteMark,cx.lineStart+line.pos,cx.lineStart+line.pos+1));line.moveBase(line.pos+(space(line.text.charCodeAt(line.pos+1))?2:1));bl.end=cx.lineStart+line.text.length;return true},[Type.ListItem](bl,_cx,line){if(line.indent-1)return false;line.moveBaseColumn(line.baseIndent+bl.value);return true},[Type.OrderedList]:skipForList,[Type.BulletList]:skipForList,[Type.Document](){return true}};function space(ch){return ch==32||ch==9||ch==10||ch==13}function skipSpace(line,i=0){while(ito&&space(line.charCodeAt(i-1)))i--;return i}function isFencedCode(line){if(line.next!=96&&line.next!=126)return-1;let pos=line.pos+1;while(pos-1&&line.depth==cx.stack.length)return-1;return count<3?-1:1}function inList(cx,type){for(let i=cx.stack.length-1;i>=0;i--)if(cx.stack[i].type==type)return true;return false}function isBulletList(line,cx,breaking){return(line.next==45||line.next==43||line.next==42)&&(line.pos==line.text.length-1||space(line.text.charCodeAt(line.pos+1)))&&(!breaking||inList(cx,Type.BulletList)||line.skipSpace(line.pos+2)=48&&next<=57)pos++;else break;if(pos==line.text.length)return-1;next=line.text.charCodeAt(pos)}if(pos==line.pos||pos>line.pos+9||next!=46&&next!=41||posline.pos+1||line.next!=49))return-1;return pos+1-line.pos}function isAtxHeading(line){if(line.next!=35)return-1;let pos=line.pos+1;while(pos6?-1:size}function isSetextUnderline(line){if(line.next!=45&&line.next!=61||line.indent>=line.baseIndent+4)return-1;let pos=line.pos+1;while(pos/,ProcessingEnd=/\?>/;const HTMLBlockStyle=[[/^<(?:script|pre|style)(?:\s|>|$)/i,/<\/(?:script|pre|style)>/i],[/^\s*/i.exec(after);if(comment)return cx.append(elt(Type.Comment,start,start+1+comment[0].length));let procInst=/^\?[^]*?\?>/.exec(after);if(procInst)return cx.append(elt(Type.ProcessingInstruction,start,start+1+procInst[0].length));let m=/^(?:![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(after);if(!m)return-1;return cx.append(elt(Type.HTMLTag,start,start+1+m[0].length))},Emphasis(cx,next,start){if(next!=95&&next!=42)return-1;let pos=start+1;while(cx.char(pos)==next)pos++;let before=cx.slice(start-1,start),after=cx.slice(pos,pos+1);let pBefore=Punctuation.test(before),pAfter=Punctuation.test(after);let sBefore=/\s|^$/.test(before),sAfter=/\s|^$/.test(after);let leftFlanking=!sAfter&&(!pAfter||sBefore||pBefore);let rightFlanking=!sBefore&&(!pBefore||sAfter||pAfter);let canOpen=leftFlanking&&(next==42||!rightFlanking||pBefore);let canClose=rightFlanking&&(next==42||!leftFlanking||pAfter);return cx.append(new InlineDelimiter(next==95?EmphasisUnderscore:EmphasisAsterisk,start,pos,(canOpen?1:0)|(canClose?2:0)))},HardBreak(cx,next,start){if(next==92&&cx.char(start+1)==10)return cx.append(elt(Type.HardBreak,start,start+2));if(next==32){let pos=start+1;while(cx.char(pos)==32)pos++;if(cx.char(pos)==10&&pos>=start+2)return cx.append(elt(Type.HardBreak,start,pos+1))}return-1},Link(cx,next,start){return next==91?cx.append(new InlineDelimiter(LinkStart,start,start+1,1)):-1},Image(cx,next,start){return next==33&&cx.char(start+1)==91?cx.append(new InlineDelimiter(ImageStart,start,start+2,1)):-1},LinkEnd(cx,next,start){if(next!=93)return-1;for(let i=cx.parts.length-1;i>=0;i--){let part=cx.parts[i];if(part instanceof InlineDelimiter&&(part.type==LinkStart||part.type==ImageStart)){if(!part.side||cx.skipSpace(part.to)==start&&!/[(\[]/.test(cx.slice(start+1,start+2))){cx.parts[i]=null;return-1}let content=cx.takeContent(i);let link=cx.parts[i]=finishLink(cx,content,part.type==LinkStart?Type.Link:Type.Image,part.from,start+1);if(part.type==LinkStart)for(let j=0;jstart?elt(Type.URL,start+offset,pos+offset):pos==text.length?null:false}}function parseLinkTitle(text,start,offset){let next=text.charCodeAt(start);if(next!=39&&next!=34&&next!=40)return false;let end=next==40?41:next;for(let pos=start+1,escaped=false;pos=this.end?-1:this.text.charCodeAt(pos-this.offset)}get end(){return this.offset+this.text.length}slice(from,to){return this.text.slice(from-this.offset,to-this.offset)}append(elt){this.parts.push(elt);return elt.to}addDelimiter(type,from,to,open,close){return this.append(new InlineDelimiter(type,from,to,(open?1:0)|(close?2:0)))}addElement(elt){return this.append(elt)}resolveMarkers(from){for(let i=from;i=from;j--){let part=this.parts[j];if(part instanceof InlineDelimiter&&part.side&1&&part.type==close.type&&!(emp&&(close.side&1||part.side&2)&&(part.to-part.from+closeSize)%3==0&&((part.to-part.from)%3||closeSize%3))){open=part;break}}if(!open)continue;let type=close.type.resolve,content=[];let start=open.from,end=close.to;if(emp){let size=Math.min(2,open.to-open.from,closeSize);start=open.to-size;end=close.from+size;type=size==1?"Emphasis":"StrongEmphasis"}if(open.type.mark)content.push(this.elt(open.type.mark,start,open.to));for(let k=j+1;k=0;i--){let part=this.parts[i];if(part instanceof InlineDelimiter&&part.type==type)return i}return null}takeContent(startIndex){let content=this.resolveMarkers(startIndex);this.parts.length=startIndex;return content}skipSpace(from){return skipSpace(this.text,from-this.offset)+this.offset}elt(type,from,to,children){if(typeof type=="string")return elt(this.parser.getNodeType(type),from,to,children);return new TreeElement(type,from)}}function injectMarks(elements,marks){if(!marks.length)return elements;if(!elements.length)return marks;let elts=elements.slice(),eI=0;for(let mark of marks){while(eI(pos?pos-1:0))return false;if(this.fragmentEnd<0){let end=this.fragment.to;while(end>0&&this.input.read(end-1,end)!="\n")end--;this.fragmentEnd=end?end-1:0}let c=this.cursor;if(!c){c=this.cursor=this.fragment.tree.cursor();c.firstChild()}let rPos=pos+this.fragment.offset;while(c.to<=rPos)if(!c.parent())return false;for(;;){if(c.from>=rPos)return this.fragment.from<=lineStart;if(!c.childAfter(rPos))return false}}matches(hash){let tree=this.cursor.tree;return tree&&tree.prop(NodeProp.contextHash)==hash}takeNodes(cx){let cur=this.cursor,off=this.fragment.offset,fragEnd=this.fragmentEnd-(this.fragment.openEnd?1:0);let start=cx.absoluteLineStart,end=start,blockI=cx.block.children.length;let prevEnd=end,prevI=blockI;for(;;){if(cur.to-off>fragEnd){if(cur.type.isAnonymous&&cur.firstChild())continue;break}cx.dontInject.add(cur.tree);cx.addNode(cur.tree,cur.from-off);if(cur.type.is("Block")){if(NotLast.indexOf(cur.type.id)<0){end=cur.to-off;blockI=cx.block.children.length}else{end=prevEnd;blockI=prevI;prevEnd=cur.to-off;prevI=cx.block.children.length}}if(!cur.nextSibling())break}while(cx.block.children.length>blockI){cx.block.children.pop();cx.block.positions.pop()}return end-start}}const markdownHighlighting=styleTags({"Blockquote/...":tags.quote,HorizontalRule:tags.contentSeparator,"ATXHeading1/... SetextHeading1/...":tags.heading1,"ATXHeading2/... SetextHeading2/...":tags.heading2,"ATXHeading3/...":tags.heading3,"ATXHeading4/...":tags.heading4,"ATXHeading5/...":tags.heading5,"ATXHeading6/...":tags.heading6,"Comment CommentBlock":tags.comment,Escape:tags.escape,Entity:tags.character,"Emphasis/...":tags.emphasis,"StrongEmphasis/...":tags.strong,"Link/... Image/...":tags.link,"OrderedList/... BulletList/...":tags.list,"BlockQuote/...":tags.quote,"InlineCode CodeText":tags.monospace,URL:tags.url,"HeaderMark HardBreak QuoteMark ListMark LinkMark EmphasisMark CodeMark":tags.processingInstruction,"CodeInfo LinkLabel":tags.labelName,LinkTitle:tags.string,Paragraph:tags.content});const parser=new MarkdownParser(new NodeSet(nodeTypes).extend(markdownHighlighting),Object.keys(DefaultBlockParsers).map(n=>DefaultBlockParsers[n]),Object.keys(DefaultBlockParsers).map(n=>DefaultLeafBlocks[n]),Object.keys(DefaultBlockParsers),DefaultEndLeaf,DefaultSkipMarkup,Object.keys(DefaultInline).map(n=>DefaultInline[n]),Object.keys(DefaultInline),[]);function leftOverSpace(node,from,to){let ranges=[];for(let n=node.firstChild,pos=from;;n=n.nextSibling){let nextPos=n?n.from:to;if(nextPos>pos)ranges.push({from:pos,to:nextPos});if(!n)break;pos=n.to}return ranges}function parseCode(config){let{codeParser,htmlParser}=config;let wrap=parseMixed((node,input)=>{let id=node.type.id;if(codeParser&&(id==Type.CodeBlock||id==Type.FencedCode)){let info="";if(id==Type.FencedCode){let infoNode=node.node.getChild(Type.CodeInfo);if(infoNode)info=input.read(infoNode.from,infoNode.to)}let parser=codeParser(info);if(parser)return{parser:parser,overlay:node=>node.type.id==Type.CodeText}}else if(htmlParser&&(id==Type.HTMLBlock||id==Type.HTMLTag)){return{parser:htmlParser,overlay:leftOverSpace(node.node,node.from,node.to)}}return null});return{wrap:wrap}}const StrikethroughDelim={resolve:"Strikethrough",mark:"StrikethroughMark"};const Strikethrough={defineNodes:[{name:"Strikethrough",style:{"Strikethrough/...":tags.strikethrough}},{name:"StrikethroughMark",style:tags.processingInstruction}],parseInline:[{name:"Strikethrough",parse(cx,next,pos){if(next!=126||cx.char(pos+1)!=126||cx.char(pos+2)==126)return-1;let before=cx.slice(pos-1,pos),after=cx.slice(pos+2,pos+3);let sBefore=/\s|^$/.test(before),sAfter=/\s|^$/.test(after);let pBefore=Punctuation.test(before),pAfter=Punctuation.test(after);return cx.addDelimiter(StrikethroughDelim,pos,pos+2,!sAfter&&(!pAfter||sBefore||pBefore),!sBefore&&(!pBefore||sAfter||pAfter))},after:"Emphasis"}]};function parseRow(cx,line,startI=0,elts,offset=0){let count=0,first=true,cellStart=-1,cellEnd=-1,esc=false;let parseCell=()=>{elts.push(cx.elt("TableCell",offset+cellStart,offset+cellEnd,cx.parser.parseInline(line.slice(cellStart,cellEnd),offset+cellStart)))};for(let i=startI;i-1)count++;first=false;if(elts){if(cellStart>-1)parseCell();elts.push(cx.elt("TableDelimiter",i+offset,i+offset+1))}cellStart=cellEnd=-1}else if(esc||next!=32&&next!=9){if(cellStart<0)cellStart=i;cellEnd=i+1}esc=!esc&&next==92}if(cellStart>-1){count++;if(elts)parseCell()}return count}function hasPipe(str,start){for(let i=start;ip instanceof TableParser)||!hasPipe(line.text,line.basePos))return false;let next=cx.scanLine(cx.absoluteLineEnd+1).text;return delimiterLine.test(next)&&parseRow(cx,line.text,line.basePos)==parseRow(cx,next,line.basePos)},before:"SetextHeading"}]};class TaskParser{nextLine(){return false}finish(cx,leaf){cx.addLeafElement(leaf,cx.elt("Task",leaf.start,leaf.start+leaf.content.length,[cx.elt("TaskMarker",leaf.start,leaf.start+3),...cx.parser.parseInline(leaf.content.slice(3),leaf.start+3)]));return true}}const TaskList={defineNodes:[{name:"Task",block:true,style:tags.list},{name:"TaskMarker",style:tags.atom}],parseBlock:[{name:"TaskList",leaf(cx,leaf){return/^\[[ xX]\]/.test(leaf.content)&&cx.parentType().name=="ListItem"?new TaskParser:null},after:"SetextHeading"}]};const GFM=[Table,TaskList,Strikethrough];function parseSubSuper(ch,node,mark){return(cx,next,pos)=>{if(next!=ch||cx.char(pos+1)==ch)return-1;let elts=[cx.elt(mark,pos,pos+1)];for(let i=pos+1;i