Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nlux/markdown

Package Overview
Dependencies
Maintainers
1
Versions
119
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nlux/markdown - npm Package Compare versions

Comparing version 2.1.5-beta to 2.1.6-beta

2

cjs/markdown.js

@@ -1,1 +0,1 @@

"use strict";const e=e=>/^[ \t]{2}\n$/m.test(e),n=e=>/^[ \t\n]{1}$/.test(e),t=["Root","Paragraph","LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Blockquote","Heading1","Heading2","Heading3","Heading4","Heading5","Heading6","OrderedList","UnorderedList","Code","CodeBlock","HorizontalRule","Link","Image"],s=["^\\[$","^\\[[^\\]]+$","^\\[[^\\]]+\\]$","^\\[[^\\]]+\\]\\($","^\\[[^\\]]+\\]\\([^\\)]*$"],r={shouldOpen:/\[([^\]]+)\]\(([^\)]*)\)/,canOpen:e=>s.some((n=>new RegExp(n).test(e))),shouldClose:()=>!0,canClose:()=>!1},i=(e,n)=>t=>{const s=o.get(e);if(!s)throw new Error(`No sequence parsers found for markdown element ${e}`);const r=s[n];return r instanceof RegExp?r.test(t):r(t)},o=new Map;o.set("Code",{shouldOpen:/^`[^`]$/,canOpen:/^`$/,shouldClose:/^`[\s\S]+$/,canClose:/^`$/}),o.set("CodeBlock",{shouldOpen:/^```.*\n$/,canOpen:/^`{1,3}.*$/,shouldClose:/^\n?```[\s\S]+$/,canClose:/^\n$|^\n?`{1,3}$/}),o.set("Heading1",{shouldOpen:/^\n*#{1} $/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading2",{shouldOpen:/^\n*#{2} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading3",{shouldOpen:/^\n*#{3} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading4",{shouldOpen:/^\n*#{4} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading5",{shouldOpen:/^\n*#{5} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading6",{shouldOpen:/^\n*#{6} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("BoldAsterisk",{canOpen:/^\*{1,2}$/,shouldOpen:/^\*{2}[^(\s\n)]$/,canClose:/^\*{1,2}$/,shouldClose:/^\*{2}[\s\S]$/}),o.set("BoldUnderscore",{canOpen:/^_{1,2}$/,shouldOpen:/^_{2}[^(\s\n)]$/,canClose:/^_{1,2}$/,shouldClose:/^_{2}[\s\S]$/}),o.set("ItalicAsterisk",{canOpen:/^\*$/,shouldOpen:/^\*[^(\*\s\n)]$/,canClose:/^\*$/,shouldClose:/^\*[\s\S]$/}),o.set("ItalicUnderscore",{canOpen:/^_$/,shouldOpen:/^_[^(_\s\n)]$/,canClose:/^_$/,shouldClose:/^_[\s\S]$/}),o.set("Link",r),o.set("LineBreak",{shouldOpen:()=>!1,canOpen:()=>!1,shouldClose:()=>!1,canClose:()=>!1}),o.set("Root",{canOpen:()=>!1,shouldOpen:()=>!1,canClose:()=>!1,shouldClose:()=>!1}),o.set("Paragraph",{canOpen:/^\n+$/,shouldOpen:/^\n{2,}$/,canClose:/^\n$/,shouldClose:/^\n{2,}$/}),o.set("Blockquote",{shouldOpen:/^$/,canOpen:/^>$/,shouldClose:/^$/,canClose:/^$/}),o.set("OrderedList",{shouldOpen:/^$/,canOpen:/^\d+\. $/,shouldClose:/^\n$/,canClose:/^\n$/}),o.set("UnorderedList",{shouldOpen:/^$/,canOpen:/^\* $/,shouldClose:/^\n$/,canClose:/^\n$/}),o.set("HorizontalRule",{shouldOpen:/^$/,canOpen:/^---$/,shouldClose:/^$/,canClose:/^$/}),o.set("Image",{shouldOpen:/^$/,canOpen:/^!\[.*\]\(.*\)$/,shouldClose:/^$/,canClose:/^$/});var a=Object.defineProperty,l=(e,n,t)=>((e,n,t)=>n in e?a(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class d{constructor(e,n="none",s="none"){l(this,"__canLeadToClosingMarkdown",!1),l(this,"__latestSequenceProcessed",!1),l(this,"__markdownElement"),l(this,"__markdownThatShouldBeNested"),l(this,"__markdownThatShouldBeRenderedAtParentLevel"),l(this,"__possibleNestedMarkdownElements"),l(this,"__possibleYieldingMarkdownElements"),l(this,"__potentialNestedMarkdownElements",[]),l(this,"__potentialYieldingMarkdownElements",[]),l(this,"__sequence",""),l(this,"__shouldCloseCurrentMarkdown",!1),this.__markdownElement=e,this.__possibleNestedMarkdownElements="all"===n?t:"none"===n?[]:n,this.__possibleYieldingMarkdownElements="none"===s?[]:s}get canLeadToClosingOrNewMarkdown(){return this.__latestSequenceProcessed||this.processSequence(),this.__canLeadToClosingMarkdown||this.__potentialYieldingMarkdownElements.length>0||this.__potentialNestedMarkdownElements.length>0}get elementToCreateAtParentLevel(){return this.__latestSequenceProcessed||this.processSequence(),this.__markdownThatShouldBeRenderedAtParentLevel}get nestedElementToCreate(){return this.__latestSequenceProcessed||this.processSequence(),this.__markdownThatShouldBeNested}get sequence(){return this.__sequence}get shouldCloseCurrentMarkdown(){return this.__latestSequenceProcessed||this.processSequence(),this.__shouldCloseCurrentMarkdown}appendCharacter(e){this.__sequence+=e,this.__latestSequenceProcessed=!1}reset(){this.__sequence="",this.__latestSequenceProcessed=!1,this.__canLeadToClosingMarkdown=!1,this.__potentialYieldingMarkdownElements=[],this.__potentialNestedMarkdownElements=[],this.__shouldCloseCurrentMarkdown=!1,this.__markdownThatShouldBeRenderedAtParentLevel=void 0,this.__markdownThatShouldBeNested=void 0}processSequence(){if(this.__latestSequenceProcessed)return;const e=this.__sequence;this.__canLeadToClosingMarkdown=i(this.__markdownElement,"canClose")(e),this.__potentialYieldingMarkdownElements=this.__possibleYieldingMarkdownElements.filter((n=>i(n,"canOpen")(e))),this.__potentialNestedMarkdownElements=this.__possibleNestedMarkdownElements.filter((n=>i(n,"canOpen")(e))),this.__shouldCloseCurrentMarkdown=i(this.__markdownElement,"shouldClose")(e),this.__markdownThatShouldBeRenderedAtParentLevel=this.__possibleYieldingMarkdownElements.find((n=>i(n,"shouldOpen")(e))),this.__markdownThatShouldBeNested=this.__possibleNestedMarkdownElements.find((n=>i(n,"shouldOpen")(e)))}}var c=Object.defineProperty,h=(e,n,t)=>((e,n,t)=>n in e?c(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class u{constructor(e,n,t,s,r){h(this,"__parent"),h(this,"__element"),h(this,"__initialContent"),h(this,"__initialized",!1),h(this,"__last3Characters",""),h(this,"__markdownElementName"),h(this,"__openingSequence"),h(this,"__options",{}),h(this,"__parsingChild"),h(this,"__sequenceParser"),h(this,"__yielded",!1),this.__markdownElementName=n,this.__parent=e??void 0,this.__options=r||{},this.__openingSequence=t??void 0,this.__initialContent="Root"!==n?s??void 0:void 0}get domElement(){return this.__element}get markdownElementName(){return this.__markdownElementName}get parsingChild(){return this.__parsingChild}get removeWhenEmpty(){return!1}get sequenceParser(){if(this.yielded)throw new Error("Cannot access sequence parser of a yielded processor");return this.__sequenceParser||(this.__sequenceParser=new d(this.__markdownElementName,this.nestedMarkdownElements,this.yieldingMarkdownElements)),this.__sequenceParser}get yielded(){return this.__yielded}get initialContent(){return this.__initialContent}get last3Characters(){return this.__last3Characters}get markdownProcessorOptions(){return this.__options}get syntaxHighlighter(){return this.__options.syntaxHighlighter}init(){if(!this.__initialized&&(this.__initialized=!0,"Root"!==this.__markdownElementName?this.__element=this.createElement(this.__openingSequence):this.__element=void 0,this.__initialContent))for(let e=0;e<this.__initialContent.length;e++){const n=this.__initialContent[e];this.processCharacter(n)}}parsingChildYielded(e,n,t,s){this.__parsingChild===e&&(this.__parsingChild=void 0),n&&this.createAndAppendMarkdown(n,t),s&&this.processCharacter(s)}preProcessCharacter(e){this.__last3Characters.length<3?this.__last3Characters+=e:this.__last3Characters=this.__last3Characters.slice(1)+e}purgeSequence(){this.__parsingChild&&this.__parsingChild.purgeSequence(),this.__sequenceParser?.sequence&&(this.__element?.append(this.__sequenceParser.sequence),this.__sequenceParser.reset())}resetSequenceParser(){this.__sequenceParser?.reset(),this.__sequenceParser=new d(this.__markdownElementName,this.nestedMarkdownElements,this.yieldingMarkdownElements)}setParsingChild(e){if(this.__parsingChild&&"Root"!==this.__markdownElementName)throw new Error("Cannot set spawn child while parsing child");this.__parsingChild=e,e.domElement&&this.domElement?.append(e.domElement),e.__parent=this}yield(e,n){if(this.yielded)return;const t=this.__sequenceParser&&!this.__sequenceParser?.shouldCloseCurrentMarkdown?this.__sequenceParser?.sequence:void 0;this.__yielded=!0,this.__sequenceParser=void 0,this.__parsingChild&&(this.__parsingChild.yield(),this.__parsingChild=void 0),this.__element&&(this.__element.innerHTML=this.__element.innerHTML.trim(),this.removeWhenEmpty&&""===this.__element.innerHTML&&this.__element.remove(),this.__element=void 0),this.__parent&&(this.__parent.parsingChildYielded(this,e,t,n),this.__parent=void 0)}}class C extends u{constructor(e,n,t,s,r){super(e,n,t,s,r)}createAndAppendMarkdown(e,n){B(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}processCharacter(n){if(this.preProcessCharacter(n),this.parsingChild)return void this.parsingChild.processCharacter(n);const{sequenceDiscarded:t,discardedSequence:s,yielded:r}=this.processSequence(n);if(!r)if(e(this.last3Characters)&&(this.nestedMarkdownElements.includes("LineBreak")||"all"===this.nestedMarkdownElements))this.domElement?.innerHTML&&(this.domElement.innerHTML=this.domElement?.innerHTML.trim()),this.createAndAppendMarkdown("LineBreak"),this.sequenceParser.reset();else if(t){this.resetSequenceParser();this.processSequence(n).sequenceDiscarded?(this.sequenceParser.reset(),s&&this.domElement?.append(s)):s&&s.length>1&&this.domElement?.append(s.slice(0,-1))}}processSequence(e){if(this.sequenceParser.appendCharacter(e),this.sequenceParser.shouldCloseCurrentMarkdown)return this.yield(void 0,e),{sequenceDiscarded:!1,yielded:!0};if(this.sequenceParser.elementToCreateAtParentLevel)return this.yield(this.sequenceParser.elementToCreateAtParentLevel,e),{sequenceDiscarded:!1,yielded:!0};const n=this.sequenceParser.nestedElementToCreate;if(n)return B(n,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions),this.sequenceParser.reset(),{sequenceDiscarded:!1};if(this.sequenceParser.canLeadToClosingOrNewMarkdown)return{sequenceDiscarded:!1};const t=this.sequenceParser.sequence;return this.sequenceParser.reset(),{sequenceDiscarded:!0,discardedSequence:t}}}class p extends C{constructor(e,n,t,s){super(e,"BoldAsterisk",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("strong")}}class _ extends C{constructor(e,n,t,s){super(e,"BoldUnderscore",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("strong")}}class m extends C{constructor(e,n,t,s){super(e,"Code",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("code")}}var g=Object.defineProperty,k=(e,n,t)=>((e,n,t)=>n in e?g(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class w extends C{constructor(e,t,s,r={}){super(e,"CodeBlock",t??null,!s||n(s)?null:s,r),k(this,"codeContainer",null),k(this,"currentLineContainer",null),k(this,"language"),this.language=this.extractLanguageFromOpeningSequence(t)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return"none"}get yieldingMarkdownElements(){return"none"}createElement(){const e=document.createElement("div");return e.classList.add("code-block"),this.codeContainer=document.createElement("pre"),e.append(this.codeContainer),this.language&&(this.codeContainer.dataset.language=this.language),e}processCharacter(e){this.preProcessCharacter(e);const{sequenceDiscarded:n,discardedSequence:t,yielded:s}=this.processSequence(e);if(s)return;if(!n)return;if(t&&t.length>1&&this.codeContainer)for(let e=0;e<t.length-1;e++)this.appendCharacterToCodeBlock(t[e]);this.resetSequenceParser();this.processSequence(e).sequenceDiscarded&&(this.sequenceParser.reset(),this.appendCharacterToCodeBlock(e))}yield(e,n){if(this.highlightCurrentLine(),!1!==this.markdownProcessorOptions.showCodeBlockCopyButton){const e=this.__parent?.domElement;e&&(e.querySelectorAll(".code-block").forEach((e=>{if(!e.querySelector("pre"))return;if(e.previousElementSibling?.classList.contains("nlux-cpy-btn"))return;const n="Copy code block to clipboard",t=document.createElement("button");t.classList.add("nlux-cpy-btn"),t.setAttribute("aria-label",n),t.setAttribute("title",n),t.innerHTML='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M15 1.25H10.9436C9.10583 1.24998 7.65019 1.24997 6.51098 1.40314C5.33856 1.56076 4.38961 1.89288 3.64124 2.64124C2.89288 3.38961 2.56076 4.33856 2.40314 5.51098C2.24997 6.65019 2.24998 8.10582 2.25 9.94357V16C2.25 17.8722 3.62205 19.424 5.41551 19.7047C5.55348 20.4687 5.81753 21.1208 6.34835 21.6517C6.95027 22.2536 7.70814 22.5125 8.60825 22.6335C9.47522 22.75 10.5775 22.75 11.9451 22.75H15.0549C16.4225 22.75 17.5248 22.75 18.3918 22.6335C19.2919 22.5125 20.0497 22.2536 20.6517 21.6517C21.2536 21.0497 21.5125 20.2919 21.6335 19.3918C21.75 18.5248 21.75 17.4225 21.75 16.0549V10.9451C21.75 9.57754 21.75 8.47522 21.6335 7.60825C21.5125 6.70814 21.2536 5.95027 20.6517 5.34835C20.1208 4.81753 19.4687 4.55348 18.7047 4.41551C18.424 2.62205 16.8722 1.25 15 1.25ZM17.1293 4.27117C16.8265 3.38623 15.9876 2.75 15 2.75H11C9.09318 2.75 7.73851 2.75159 6.71085 2.88976C5.70476 3.02502 5.12511 3.27869 4.7019 3.7019C4.27869 4.12511 4.02502 4.70476 3.88976 5.71085C3.75159 6.73851 3.75 8.09318 3.75 10V16C3.75 16.9876 4.38624 17.8265 5.27117 18.1293C5.24998 17.5194 5.24999 16.8297 5.25 16.0549V10.9451C5.24998 9.57754 5.24996 8.47522 5.36652 7.60825C5.48754 6.70814 5.74643 5.95027 6.34835 5.34835C6.95027 4.74643 7.70814 4.48754 8.60825 4.36652C9.47522 4.24996 10.5775 4.24998 11.9451 4.25H15.0549C15.8297 4.24999 16.5194 4.24998 17.1293 4.27117ZM7.40901 6.40901C7.68577 6.13225 8.07435 5.9518 8.80812 5.85315C9.56347 5.75159 10.5646 5.75 12 5.75H15C16.4354 5.75 17.4365 5.75159 18.1919 5.85315C18.9257 5.9518 19.3142 6.13225 19.591 6.40901C19.8678 6.68577 20.0482 7.07435 20.1469 7.80812C20.2484 8.56347 20.25 9.56458 20.25 11V16C20.25 17.4354 20.2484 18.4365 20.1469 19.1919C20.0482 19.9257 19.8678 20.3142 19.591 20.591C19.3142 20.8678 18.9257 21.0482 18.1919 21.1469C17.4365 21.2484 16.4354 21.25 15 21.25H12C10.5646 21.25 9.56347 21.2484 8.80812 21.1469C8.07435 21.0482 7.68577 20.8678 7.40901 20.591C7.13225 20.3142 6.9518 19.9257 6.85315 19.1919C6.75159 18.4365 6.75 17.4354 6.75 16V11C6.75 9.56458 6.75159 8.56347 6.85315 7.80812C6.9518 7.07435 7.13225 6.68577 7.40901 6.40901Z" fill="currentColor"/></svg>',e.insertAdjacentElement("beforebegin",t)})),(e=>{e.querySelectorAll(".nlux-cpy-btn").forEach((e=>{if(!(e instanceof HTMLButtonElement))return;if("true"===e.dataset.clickListenerSet)return;let n=!1;const t=e.nextElementSibling;e.addEventListener("click",(()=>{if(n||!t)return;const s=t.innerText;navigator.clipboard.writeText(s??""),n=!0,e.classList.add("clicked"),setTimeout((()=>{n=!1,e.classList.remove("clicked")}),1e3)})),e.dataset.clickListenerSet="true"}))})(e))}super.yield(e,n),this.codeContainer=null,this.currentLineContainer=null}appendCharacterToCodeBlock(e){if(this.codeContainer)if("\n"!==e)this.currentLineContainer||(this.currentLineContainer=document.createElement("div"),this.codeContainer.append(this.currentLineContainer)),this.currentLineContainer.append(e);else if(this.currentLineContainer)this.currentLineContainer.innerHTML?this.highlightCurrentLine():this.currentLineContainer.innerHTML=" ",this.currentLineContainer=null;else if(this.codeContainer.innerHTML){const e=document.createElement("div");e.innerHTML=" ",this.codeContainer.append(e)}}extractLanguageFromOpeningSequence(e){if(!e)return;const n=e.match(/```(\w+)/);return Array.isArray(n)&&n.length>1?n[1]:void 0}highlightCurrentLine(){if(!this.domElement||!this.currentLineContainer)return;const e=this.currentLineContainer.innerText??this.currentLineContainer.innerHTML;if(this.syntaxHighlighter&&this.codeContainer&&e){const n=this.codeContainer.dataset.language;if(n){const t=this.syntaxHighlighter.createHighlighter({language:n,colorMode:"dark"}),s=this.syntaxHighlighter.highlightingClass(n);this.codeContainer.classList.contains(s)||this.codeContainer.classList.add(s),this.currentLineContainer.innerHTML=t(e,n)}}}}var E=Object.defineProperty,q=(e,n,t)=>((e,n,t)=>n in e?E(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class P extends C{constructor(e,n,t,s,r){super(n,L(e),t??null,s??null,r??null),q(this,"__headingLevel"),this.__headingLevel=e}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("h"+this.__headingLevel)}processCharacter(e){if(this.preProcessCharacter(e),this.parsingChild)return void this.parsingChild.processCharacter(e);const{sequenceDiscarded:n,discardedSequence:t,yielded:s}=this.processSequence(e);s||n&&t&&(/^\s+$/.test(t)||this.domElement?.append(t))}}const L=e=>{switch(e){case 1:return"Heading1";case 2:return"Heading2";case 3:return"Heading3";case 4:return"Heading4";case 5:return"Heading5";case 6:return"Heading6"}};class v extends C{constructor(e,n,t,s){super(e,"ItalicAsterisk",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("em")}}class M extends C{constructor(e,n,t,s){super(e,"ItalicUnderscore",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("em")}}class f extends u{constructor(e,n,t){super(e,"LineBreak",n??null,t??null,null)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return"none"}get yieldingMarkdownElements(){return"none"}createAndAppendMarkdown(e,n){B(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}createElement(){return document.createElement("br")}init(){super.init()}processCharacter(e){this.yield(void 0,e)}}var $=Object.defineProperty,y=(e,n,t)=>((e,n,t)=>n in e?$(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class O extends C{constructor(e,n,t,s={}){super(e,"Link",n??null,t??null,s),y(this,"linkContentProcessed","")}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code"]}get yieldingMarkdownElements(){return["Paragraph","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code"]}createElement(e){const n=/\[(.*[^\]])\]\((.*[^\)])\)/.exec(e),t=document.createElement("a");return t.textContent="",n&&n.length>=3&&n[2]&&(t.href=n[2]),"self"!==this.markdownProcessorOptions.markdownLinkTarget&&(t.target="_blank"),t}processCharacter(e){super.processCharacter(e),this.yielded||(this.linkContentProcessed+=e)}processSequence(e){if(!this.initialContent||this.initialContent===this.linkContentProcessed)return this.yield(void 0,e),{yielded:!0,sequenceDiscarded:!0,discardedSequence:e};this.sequenceParser.appendCharacter(e);const n=this.sequenceParser.nestedElementToCreate;if(n)return B(n,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions),this.sequenceParser.reset(),{sequenceDiscarded:!1};if(this.sequenceParser.canLeadToClosingOrNewMarkdown)return{sequenceDiscarded:!1};const t=this.sequenceParser.sequence;return this.sequenceParser.reset(),{sequenceDiscarded:!0,discardedSequence:t}}}class A extends C{constructor(e,n,t,s){super(e,"Paragraph",n??null,t??null,s??null)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return["LineBreak","Blockquote","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get removeWhenEmpty(){return!0}get yieldingMarkdownElements(){return["Heading1","Heading2","Heading3","Heading4","Heading5","Heading6","CodeBlock","UnorderedList","OrderedList","HorizontalRule","Image"]}createElement(){return document.createElement("p")}}const B=(e,n,t,s,r)=>{if("Paragraph"===e){const e=new A(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("LineBreak"===e){const e=new f(n,t,s);return e.init(),void n.setParsingChild(e)}if("Link"===e){const e=/\[([^\]]+)\]\(([^\)]*)\)/.exec(t),s=e&&e.length>=3?e[1]:"",i=new O(n,t,s,r);return i.init(),void n.setParsingChild(i)}if("Heading1"===e||"Heading2"===e||"Heading3"===e||"Heading4"===e||"Heading5"===e||"Heading6"===e){const i=parseInt(e[e.length-1]);if(Number.isNaN(i)||!Number.isFinite(i)||i<1||i>6)throw new Error("Invalid heading level");const o=new P(i,n,t,s,r);return o.init(),void n.setParsingChild(o)}if("Code"===e){const e=new m(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("CodeBlock"===e){const e=new w(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("BoldAsterisk"===e){const e=new p(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("BoldUnderscore"===e){const e=new _(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("ItalicAsterisk"===e){const e=new v(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("ItalicUnderscore"===e){const e=new M(n,t,s,r);return e.init(),void n.setParsingChild(e)}throw new Error("Unable to create child processor for markdown element "+e)};var S=Object.defineProperty,H=(e,n,t)=>((e,n,t)=>n in e?S(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class b extends u{constructor(e,n,t){super(null,"Root",n??null,null,t??null),H(this,"__rootDomElement"),this.__rootDomElement=e}get canExistAtRootLevel(){return!1}get domElement(){return this.__rootDomElement}get nestedMarkdownElements(){return"all"}get yieldingMarkdownElements(){return"none"}complete(){var e;this.parsingChild&&this.parsingChild.purgeSequence(),this.purgeSequence(),e=this.domElement?.lastChild,e instanceof HTMLElement&&"BR"===e.tagName&&this.domElement?.lastChild?.remove()}createAndAppendMarkdown(e,n){B(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}createElement(){throw new Error("Root cannot create an element")}processCharacter(e){this.preProcessCharacter(e),this.parsingChild?this.parsingChild.processCharacter(e):(this.sequenceParser.appendCharacter(e),this.processSequence(e))}yield(){super.yield(),this.__rootDomElement=void 0}processSequence(t){if(this.sequenceParser.shouldCloseCurrentMarkdown)return;const s=this.sequenceParser.nestedElementToCreate;if(s){B(s,this,this.sequenceParser.sequence,t,this.markdownProcessorOptions);const e=this.parsingChild;return e&&!e.canExistAtRootLevel&&(B("Paragraph",this,void 0,void 0,this.markdownProcessorOptions),this.parsingChild?.setParsingChild(e)),void this.sequenceParser.reset()}this.sequenceParser.canLeadToClosingOrNewMarkdown||(e(this.last3Characters)?this.createAndAppendMarkdown("LineBreak"):t&&!n(t)&&(B("Paragraph",this,void 0,this.sequenceParser.sequence,this.markdownProcessorOptions),this.sequenceParser.reset()))}}const x=10;exports.createMarkdownStreamParser=(e,n)=>{const t=((e,n,t)=>{const{streamingAnimationSpeed:s=x,markdownLinkTarget:r,showCodeBlockCopyButton:i,skipStreamingAnimation:o=!1,onComplete:a}=t||{},l=new b(e,void 0,{syntaxHighlighter:n,markdownLinkTarget:r,showCodeBlockCopyButton:i}),d=[];let c=0,h=!1,u="idle";const C=o?0:Math.max(s,0),p=s>0?s:20,_=Math.ceil(2e3/p),m=()=>{if("closed"===u)return;if("idle"===u&&(u="processing"),0===d.length)return void(!1===h&&c<_?(u="waitingForMoreCharacters",c+=1,setTimeout(m,p)):(l.processCharacter("\n"),l.complete(),l.yield(),u="closed",a?.()));u="processing",c=0;const e=d.shift();e&&l.processCharacter(e),setTimeout(m,C)};return{next:e=>{if(e){for(let n=0;n<e.length;n++)d.push(e[n]);"idle"===u&&m()}},complete:()=>{h=!0},error:()=>{}}})(e,n?.syntaxHighlighter,{markdownLinkTarget:n?.markdownLinkTarget,showCodeBlockCopyButton:n?.showCodeBlockCopyButton,skipStreamingAnimation:n?.skipStreamingAnimation,streamingAnimationSpeed:n?.streamingAnimationSpeed,onComplete:n?.onComplete});return{next(e){t.next(e)},complete(){t.complete()}}};
"use strict";const e=e=>/^[ \t]{2}\n$/m.test(e),n=e=>/^[ \t\n]{1}$/.test(e),t=["Root","Paragraph","LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Blockquote","Heading1","Heading2","Heading3","Heading4","Heading5","Heading6","OrderedList","UnorderedList","Code","CodeBlock","HorizontalRule","Link","Image"],s=["^\\[$","^\\[[^\\]]+$","^\\[[^\\]]+\\]$","^\\[[^\\]]+\\]\\($","^\\[[^\\]]+\\]\\([^\\)]*$"],r={shouldOpen:/\[([^\]]+)\]\(([^\)]*)\)/,canOpen:e=>s.some((n=>new RegExp(n).test(e))),shouldClose:()=>!0,canClose:()=>!1},i=(e,n)=>t=>{const s=o.get(e);if(!s)throw new Error(`No sequence parsers found for markdown element ${e}`);const r=s[n];return r instanceof RegExp?r.test(t):r(t)},o=new Map;o.set("Code",{shouldOpen:/^`[^`]$/,canOpen:/^`$/,shouldClose:/^`[\s\S]+$/,canClose:/^`$/}),o.set("CodeBlock",{shouldOpen:/^```.*\n$/,canOpen:/^`{1,3}.*$/,shouldClose:/^\n?```[\s\S]+$/,canClose:/^\n$|^\n?`{1,3}$/}),o.set("Heading1",{shouldOpen:/^\n*#{1} $/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading2",{shouldOpen:/^\n*#{2} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading3",{shouldOpen:/^\n*#{3} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading4",{shouldOpen:/^\n*#{4} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading5",{shouldOpen:/^\n*#{5} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading6",{shouldOpen:/^\n*#{6} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("BoldAsterisk",{canOpen:/^\*{1,2}$/,shouldOpen:/^\*{2}[^(\s\n)]$/,canClose:/^\*{1,2}$/,shouldClose:/^\*{2}[\s\S]$/}),o.set("BoldUnderscore",{canOpen:/^_{1,2}$/,shouldOpen:/^_{2}[^(\s\n)]$/,canClose:/^_{1,2}$/,shouldClose:/^_{2}[\s\S]$/}),o.set("ItalicAsterisk",{canOpen:/^\*$/,shouldOpen:/^\*[^(\*\s\n)]$/,canClose:/^\*$/,shouldClose:/^\*[\s\S]$/}),o.set("ItalicUnderscore",{canOpen:/^_$/,shouldOpen:/^_[^(_\s\n)]$/,canClose:/^_$/,shouldClose:/^_[\s\S]$/}),o.set("Link",r),o.set("LineBreak",{shouldOpen:()=>!1,canOpen:()=>!1,shouldClose:()=>!1,canClose:()=>!1}),o.set("Root",{canOpen:()=>!1,shouldOpen:()=>!1,canClose:()=>!1,shouldClose:()=>!1}),o.set("Paragraph",{canOpen:/^\n+$/,shouldOpen:/^\n{2,}$/,canClose:/^\n$/,shouldClose:/^\n{2,}$/}),o.set("Blockquote",{shouldOpen:/^$/,canOpen:/^>$/,shouldClose:/^$/,canClose:/^$/}),o.set("OrderedList",{shouldOpen:/^$/,canOpen:/^\d+\. $/,shouldClose:/^\n$/,canClose:/^\n$/}),o.set("UnorderedList",{shouldOpen:/^$/,canOpen:/^\* $/,shouldClose:/^\n$/,canClose:/^\n$/}),o.set("HorizontalRule",{shouldOpen:/^$/,canOpen:/^---$/,shouldClose:/^$/,canClose:/^$/}),o.set("Image",{shouldOpen:/^$/,canOpen:/^!\[.*\]\(.*\)$/,shouldClose:/^$/,canClose:/^$/});var a=Object.defineProperty,l=(e,n,t)=>((e,n,t)=>n in e?a(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class d{constructor(e,n="none",s="none"){l(this,"__canLeadToClosingMarkdown",!1),l(this,"__latestSequenceProcessed",!1),l(this,"__markdownElement"),l(this,"__markdownThatShouldBeNested"),l(this,"__markdownThatShouldBeRenderedAtParentLevel"),l(this,"__possibleNestedMarkdownElements"),l(this,"__possibleYieldingMarkdownElements"),l(this,"__potentialNestedMarkdownElements",[]),l(this,"__potentialYieldingMarkdownElements",[]),l(this,"__sequence",""),l(this,"__shouldCloseCurrentMarkdown",!1),this.__markdownElement=e,this.__possibleNestedMarkdownElements="all"===n?t:"none"===n?[]:n,this.__possibleYieldingMarkdownElements="none"===s?[]:s}get canLeadToClosingOrNewMarkdown(){return this.__latestSequenceProcessed||this.processSequence(),this.__canLeadToClosingMarkdown||this.__potentialYieldingMarkdownElements.length>0||this.__potentialNestedMarkdownElements.length>0}get elementToCreateAtParentLevel(){return this.__latestSequenceProcessed||this.processSequence(),this.__markdownThatShouldBeRenderedAtParentLevel}get nestedElementToCreate(){return this.__latestSequenceProcessed||this.processSequence(),this.__markdownThatShouldBeNested}get sequence(){return this.__sequence}get shouldCloseCurrentMarkdown(){return this.__latestSequenceProcessed||this.processSequence(),this.__shouldCloseCurrentMarkdown}appendCharacter(e){this.__sequence+=e,this.__latestSequenceProcessed=!1}reset(){this.__sequence="",this.__latestSequenceProcessed=!1,this.__canLeadToClosingMarkdown=!1,this.__potentialYieldingMarkdownElements=[],this.__potentialNestedMarkdownElements=[],this.__shouldCloseCurrentMarkdown=!1,this.__markdownThatShouldBeRenderedAtParentLevel=void 0,this.__markdownThatShouldBeNested=void 0}processSequence(){if(this.__latestSequenceProcessed)return;const e=this.__sequence;this.__canLeadToClosingMarkdown=i(this.__markdownElement,"canClose")(e),this.__potentialYieldingMarkdownElements=this.__possibleYieldingMarkdownElements.filter((n=>i(n,"canOpen")(e))),this.__potentialNestedMarkdownElements=this.__possibleNestedMarkdownElements.filter((n=>i(n,"canOpen")(e))),this.__shouldCloseCurrentMarkdown=i(this.__markdownElement,"shouldClose")(e),this.__markdownThatShouldBeRenderedAtParentLevel=this.__possibleYieldingMarkdownElements.find((n=>i(n,"shouldOpen")(e))),this.__markdownThatShouldBeNested=this.__possibleNestedMarkdownElements.find((n=>i(n,"shouldOpen")(e)))}}var c=Object.defineProperty,h=(e,n,t)=>((e,n,t)=>n in e?c(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class u{constructor(e,n,t,s,r){h(this,"__parent"),h(this,"__element"),h(this,"__initialContent"),h(this,"__initialized",!1),h(this,"__last3Characters",""),h(this,"__markdownElementName"),h(this,"__openingSequence"),h(this,"__options",{}),h(this,"__parsingChild"),h(this,"__sequenceParser"),h(this,"__yielded",!1),this.__markdownElementName=n,this.__parent=e??void 0,this.__options=r||{},this.__openingSequence=t??void 0,this.__initialContent="Root"!==n?s??void 0:void 0}get domElement(){return this.__element}get markdownElementName(){return this.__markdownElementName}get parsingChild(){return this.__parsingChild}get removeWhenEmpty(){return!1}get sequenceParser(){if(this.yielded)throw new Error("Cannot access sequence parser of a yielded processor");return this.__sequenceParser||(this.__sequenceParser=new d(this.__markdownElementName,this.nestedMarkdownElements,this.yieldingMarkdownElements)),this.__sequenceParser}get yielded(){return this.__yielded}get initialContent(){return this.__initialContent}get last3Characters(){return this.__last3Characters}get markdownProcessorOptions(){return this.__options}get syntaxHighlighter(){return this.__options.syntaxHighlighter}init(){if(!this.__initialized&&(this.__initialized=!0,"Root"!==this.__markdownElementName?this.__element=this.createElement(this.__openingSequence):this.__element=void 0,this.__initialContent))for(let e=0;e<this.__initialContent.length;e++){const n=this.__initialContent[e];this.processCharacter(n)}}parsingChildYielded(e,n,t,s){this.__parsingChild===e&&(this.__parsingChild=void 0),n&&this.createAndAppendMarkdown(n,t),s&&this.processCharacter(s)}preProcessCharacter(e){this.__last3Characters.length<3?this.__last3Characters+=e:this.__last3Characters=this.__last3Characters.slice(1)+e}purgeSequence(){this.__parsingChild&&this.__parsingChild.purgeSequence(),this.__sequenceParser?.sequence&&(this.__element?.append(this.__sequenceParser.sequence),this.__sequenceParser.reset())}resetSequenceParser(){this.__sequenceParser?.reset(),this.__sequenceParser=new d(this.__markdownElementName,this.nestedMarkdownElements,this.yieldingMarkdownElements)}setParsingChild(e){if(this.__parsingChild&&"Root"!==this.__markdownElementName)throw new Error("Cannot set spawn child while parsing child");this.__parsingChild=e,e.domElement&&this.domElement?.append(e.domElement),e.__parent=this}yield(e,n){if(this.yielded)return;const t=this.__sequenceParser&&!this.__sequenceParser?.shouldCloseCurrentMarkdown?this.__sequenceParser?.sequence:void 0;this.__yielded=!0,this.__sequenceParser=void 0,this.__parsingChild&&(this.__parsingChild.yield(),this.__parsingChild=void 0),this.__element&&(this.__element.innerHTML=this.__element.innerHTML.trim(),this.removeWhenEmpty&&""===this.__element.innerHTML&&this.__element.remove(),this.__element=void 0),this.__parent&&(this.__parent.parsingChildYielded(this,e,t,n),this.__parent=void 0)}}class p extends u{constructor(e,n,t,s,r){super(e,n,t,s,r)}createAndAppendMarkdown(e,n){B(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}processCharacter(n){if(this.preProcessCharacter(n),this.parsingChild)return void this.parsingChild.processCharacter(n);const{sequenceDiscarded:t,discardedSequence:s,yielded:r}=this.processSequence(n);if(!r)if(e(this.last3Characters)&&(this.nestedMarkdownElements.includes("LineBreak")||"all"===this.nestedMarkdownElements))this.domElement?.innerHTML&&(this.domElement.innerHTML=this.domElement?.innerHTML.trim()),this.createAndAppendMarkdown("LineBreak"),this.sequenceParser.reset();else if(t){this.resetSequenceParser();this.processSequence(n).sequenceDiscarded?(this.sequenceParser.reset(),s&&this.domElement?.append(s)):s&&s.length>1&&this.domElement?.append(s.slice(0,-1))}}processSequence(e){if(this.sequenceParser.appendCharacter(e),this.sequenceParser.shouldCloseCurrentMarkdown)return this.yield(void 0,e),{sequenceDiscarded:!1,yielded:!0};if(this.sequenceParser.elementToCreateAtParentLevel)return this.yield(this.sequenceParser.elementToCreateAtParentLevel,e),{sequenceDiscarded:!1,yielded:!0};const n=this.sequenceParser.nestedElementToCreate;if(n)return B(n,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions),this.sequenceParser.reset(),{sequenceDiscarded:!1};if(this.sequenceParser.canLeadToClosingOrNewMarkdown)return{sequenceDiscarded:!1};const t=this.sequenceParser.sequence;return this.sequenceParser.reset(),{sequenceDiscarded:!0,discardedSequence:t}}}class _ extends p{constructor(e,n,t,s){super(e,"BoldAsterisk",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("strong")}}class m extends p{constructor(e,n,t,s){super(e,"BoldUnderscore",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("strong")}}class g extends p{constructor(e,n,t,s){super(e,"Code",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("code")}}var C=Object.defineProperty,k=(e,n,t)=>((e,n,t)=>n in e?C(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class w extends p{constructor(e,t,s,r={}){super(e,"CodeBlock",t??null,!s||n(s)?null:s,r),k(this,"codeContainer",null),k(this,"currentLineContainer",null),k(this,"language"),this.language=this.extractLanguageFromOpeningSequence(t)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return"none"}get yieldingMarkdownElements(){return"none"}createElement(){const e=document.createElement("div");return e.classList.add("code-block"),this.codeContainer=document.createElement("pre"),e.append(this.codeContainer),this.language&&(this.codeContainer.dataset.language=this.language),e}processCharacter(e){this.preProcessCharacter(e);const{sequenceDiscarded:n,discardedSequence:t,yielded:s}=this.processSequence(e);if(s)return;if(!n)return;if(t&&t.length>1&&this.codeContainer)for(let e=0;e<t.length-1;e++)this.appendCharacterToCodeBlock(t[e]);this.resetSequenceParser();this.processSequence(e).sequenceDiscarded&&(this.sequenceParser.reset(),this.appendCharacterToCodeBlock(e))}yield(e,n){if(this.highlightCurrentLine(),!1!==this.markdownProcessorOptions.showCodeBlockCopyButton){const e=this.__parent?.domElement;e&&(e.querySelectorAll(".code-block").forEach((e=>{if(!e.querySelector("pre"))return;if(e.previousElementSibling?.classList.contains("nlux-cpy-btn"))return;const n="Copy code block to clipboard",t=document.createElement("button");t.classList.add("nlux-cpy-btn"),t.setAttribute("aria-label",n),t.setAttribute("title",n);const s=document.createElement("span");s.classList.add("icon-copy"),t.appendChild(s),e.insertAdjacentElement("beforebegin",t)})),(e=>{e.querySelectorAll(".nlux-cpy-btn").forEach((e=>{if(!(e instanceof HTMLButtonElement))return;if("true"===e.dataset.clickListenerSet)return;let n=!1;const t=e.nextElementSibling;e.addEventListener("click",(()=>{if(n||!t)return;const s=t.innerText;navigator.clipboard.writeText(s??""),n=!0,e.classList.add("clicked"),setTimeout((()=>{n=!1,e.classList.remove("clicked")}),1e3)})),e.dataset.clickListenerSet="true"}))})(e))}super.yield(e,n),this.codeContainer=null,this.currentLineContainer=null}appendCharacterToCodeBlock(e){if(this.codeContainer)if("\n"!==e)this.currentLineContainer||(this.currentLineContainer=document.createElement("div"),this.codeContainer.append(this.currentLineContainer)),this.currentLineContainer.append(e);else if(this.currentLineContainer)this.currentLineContainer.innerHTML?this.highlightCurrentLine():this.currentLineContainer.innerHTML=" ",this.currentLineContainer=null;else if(this.codeContainer.innerHTML){const e=document.createElement("div");e.innerHTML=" ",this.codeContainer.append(e)}}extractLanguageFromOpeningSequence(e){if(!e)return;const n=e.match(/```(\w+)/);return Array.isArray(n)&&n.length>1?n[1]:void 0}highlightCurrentLine(){if(!this.domElement||!this.currentLineContainer)return;const e=this.currentLineContainer.innerText??this.currentLineContainer.innerHTML;if(this.syntaxHighlighter&&this.codeContainer&&e){const n=this.codeContainer.dataset.language;if(n){const t=this.syntaxHighlighter.createHighlighter({language:n,colorMode:"dark"}),s=this.syntaxHighlighter.highlightingClass(n);this.codeContainer.classList.contains(s)||this.codeContainer.classList.add(s),this.currentLineContainer.innerHTML=t(e,n)}}}}var E=Object.defineProperty,q=(e,n,t)=>((e,n,t)=>n in e?E(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class P extends p{constructor(e,n,t,s,r){super(n,L(e),t??null,s??null,r??null),q(this,"__headingLevel"),this.__headingLevel=e}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("h"+this.__headingLevel)}processCharacter(e){if(this.preProcessCharacter(e),this.parsingChild)return void this.parsingChild.processCharacter(e);const{sequenceDiscarded:n,discardedSequence:t,yielded:s}=this.processSequence(e);s||n&&t&&(/^\s+$/.test(t)||this.domElement?.append(t))}}const L=e=>{switch(e){case 1:return"Heading1";case 2:return"Heading2";case 3:return"Heading3";case 4:return"Heading4";case 5:return"Heading5";case 6:return"Heading6"}};class v extends p{constructor(e,n,t,s){super(e,"ItalicAsterisk",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("em")}}class f extends p{constructor(e,n,t,s){super(e,"ItalicUnderscore",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("em")}}class M extends u{constructor(e,n,t){super(e,"LineBreak",n??null,t??null,null)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return"none"}get yieldingMarkdownElements(){return"none"}createAndAppendMarkdown(e,n){B(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}createElement(){return document.createElement("br")}init(){super.init()}processCharacter(e){this.yield(void 0,e)}}var $=Object.defineProperty,y=(e,n,t)=>((e,n,t)=>n in e?$(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class O extends p{constructor(e,n,t,s={}){super(e,"Link",n??null,t??null,s),y(this,"linkContentProcessed","")}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code"]}get yieldingMarkdownElements(){return["Paragraph","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code"]}createElement(e){const n=/\[(.*[^\]])\]\((.*[^\)])\)/.exec(e),t=document.createElement("a");return t.textContent="",n&&n.length>=3&&n[2]&&(t.href=n[2]),"self"!==this.markdownProcessorOptions.markdownLinkTarget&&(t.target="_blank"),t}processCharacter(e){super.processCharacter(e),this.yielded||(this.linkContentProcessed+=e)}processSequence(e){if(!this.initialContent||this.initialContent===this.linkContentProcessed)return this.yield(void 0,e),{yielded:!0,sequenceDiscarded:!0,discardedSequence:e};this.sequenceParser.appendCharacter(e);const n=this.sequenceParser.nestedElementToCreate;if(n)return B(n,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions),this.sequenceParser.reset(),{sequenceDiscarded:!1};if(this.sequenceParser.canLeadToClosingOrNewMarkdown)return{sequenceDiscarded:!1};const t=this.sequenceParser.sequence;return this.sequenceParser.reset(),{sequenceDiscarded:!0,discardedSequence:t}}}class A extends p{constructor(e,n,t,s){super(e,"Paragraph",n??null,t??null,s??null)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return["LineBreak","Blockquote","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get removeWhenEmpty(){return!0}get yieldingMarkdownElements(){return["Heading1","Heading2","Heading3","Heading4","Heading5","Heading6","CodeBlock","UnorderedList","OrderedList","HorizontalRule","Image"]}createElement(){return document.createElement("p")}}const B=(e,n,t,s,r)=>{if("Paragraph"===e){const e=new A(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("LineBreak"===e){const e=new M(n,t,s);return e.init(),void n.setParsingChild(e)}if("Link"===e){const e=/\[([^\]]+)\]\(([^\)]*)\)/.exec(t),s=e&&e.length>=3?e[1]:"",i=new O(n,t,s,r);return i.init(),void n.setParsingChild(i)}if("Heading1"===e||"Heading2"===e||"Heading3"===e||"Heading4"===e||"Heading5"===e||"Heading6"===e){const i=parseInt(e[e.length-1]);if(Number.isNaN(i)||!Number.isFinite(i)||i<1||i>6)throw new Error("Invalid heading level");const o=new P(i,n,t,s,r);return o.init(),void n.setParsingChild(o)}if("Code"===e){const e=new g(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("CodeBlock"===e){const e=new w(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("BoldAsterisk"===e){const e=new _(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("BoldUnderscore"===e){const e=new m(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("ItalicAsterisk"===e){const e=new v(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("ItalicUnderscore"===e){const e=new f(n,t,s,r);return e.init(),void n.setParsingChild(e)}throw new Error("Unable to create child processor for markdown element "+e)};var S=Object.defineProperty,b=(e,n,t)=>((e,n,t)=>n in e?S(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class H extends u{constructor(e,n,t){super(null,"Root",n??null,null,t??null),b(this,"__rootDomElement"),this.__rootDomElement=e}get canExistAtRootLevel(){return!1}get domElement(){return this.__rootDomElement}get nestedMarkdownElements(){return"all"}get yieldingMarkdownElements(){return"none"}complete(){var e;this.parsingChild&&this.parsingChild.purgeSequence(),this.purgeSequence(),e=this.domElement?.lastChild,e instanceof HTMLElement&&"BR"===e.tagName&&this.domElement?.lastChild?.remove()}createAndAppendMarkdown(e,n){B(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}createElement(){throw new Error("Root cannot create an element")}processCharacter(e){this.preProcessCharacter(e),this.parsingChild?this.parsingChild.processCharacter(e):(this.sequenceParser.appendCharacter(e),this.processSequence(e))}yield(){super.yield(),this.__rootDomElement=void 0}processSequence(t){if(this.sequenceParser.shouldCloseCurrentMarkdown)return;const s=this.sequenceParser.nestedElementToCreate;if(s){B(s,this,this.sequenceParser.sequence,t,this.markdownProcessorOptions);const e=this.parsingChild;return e&&!e.canExistAtRootLevel&&(B("Paragraph",this,void 0,void 0,this.markdownProcessorOptions),this.parsingChild?.setParsingChild(e)),void this.sequenceParser.reset()}this.sequenceParser.canLeadToClosingOrNewMarkdown||(e(this.last3Characters)?this.createAndAppendMarkdown("LineBreak"):t&&!n(t)&&(B("Paragraph",this,void 0,this.sequenceParser.sequence,this.markdownProcessorOptions),this.sequenceParser.reset()))}}const T=10;exports.createMarkdownStreamParser=(e,n)=>{const t=((e,n,t)=>{const{streamingAnimationSpeed:s=T,markdownLinkTarget:r,showCodeBlockCopyButton:i,skipStreamingAnimation:o=!1,onComplete:a}=t||{},l=new H(e,void 0,{syntaxHighlighter:n,markdownLinkTarget:r,showCodeBlockCopyButton:i}),d=[];let c=0,h=!1,u="idle";const p=o?0:Math.max(s,0),_=s>0?s:20,m=Math.ceil(2e3/_),g=()=>{if("closed"===u)return;if("idle"===u&&(u="processing"),0===d.length)return void(!1===h&&c<m?(u="waitingForMoreCharacters",c+=1,setTimeout(g,_)):(l.processCharacter("\n"),l.complete(),l.yield(),u="closed",a?.()));u="processing",c=0;const e=d.shift();e&&l.processCharacter(e),setTimeout(g,p)};return{next:e=>{if(e){for(let n=0;n<e.length;n++)d.push(e[n]);"idle"===u&&g()}},complete:()=>{h=!0},error:()=>{}}})(e,n?.syntaxHighlighter,{markdownLinkTarget:n?.markdownLinkTarget,showCodeBlockCopyButton:n?.showCodeBlockCopyButton,skipStreamingAnimation:n?.skipStreamingAnimation,streamingAnimationSpeed:n?.streamingAnimationSpeed,onComplete:n?.onComplete});return{next(e){t.next(e)},complete(){t.complete()}}};

@@ -1,1 +0,1 @@

const e=e=>/^[ \t]{2}\n$/m.test(e),n=e=>/^[ \t\n]{1}$/.test(e),t=["Root","Paragraph","LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Blockquote","Heading1","Heading2","Heading3","Heading4","Heading5","Heading6","OrderedList","UnorderedList","Code","CodeBlock","HorizontalRule","Link","Image"],s=["^\\[$","^\\[[^\\]]+$","^\\[[^\\]]+\\]$","^\\[[^\\]]+\\]\\($","^\\[[^\\]]+\\]\\([^\\)]*$"],r={shouldOpen:/\[([^\]]+)\]\(([^\)]*)\)/,canOpen:e=>s.some((n=>new RegExp(n).test(e))),shouldClose:()=>!0,canClose:()=>!1},i=(e,n)=>t=>{const s=o.get(e);if(!s)throw new Error(`No sequence parsers found for markdown element ${e}`);const r=s[n];return r instanceof RegExp?r.test(t):r(t)},o=new Map;o.set("Code",{shouldOpen:/^`[^`]$/,canOpen:/^`$/,shouldClose:/^`[\s\S]+$/,canClose:/^`$/}),o.set("CodeBlock",{shouldOpen:/^```.*\n$/,canOpen:/^`{1,3}.*$/,shouldClose:/^\n?```[\s\S]+$/,canClose:/^\n$|^\n?`{1,3}$/}),o.set("Heading1",{shouldOpen:/^\n*#{1} $/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading2",{shouldOpen:/^\n*#{2} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading3",{shouldOpen:/^\n*#{3} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading4",{shouldOpen:/^\n*#{4} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading5",{shouldOpen:/^\n*#{5} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading6",{shouldOpen:/^\n*#{6} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("BoldAsterisk",{canOpen:/^\*{1,2}$/,shouldOpen:/^\*{2}[^(\s\n)]$/,canClose:/^\*{1,2}$/,shouldClose:/^\*{2}[\s\S]$/}),o.set("BoldUnderscore",{canOpen:/^_{1,2}$/,shouldOpen:/^_{2}[^(\s\n)]$/,canClose:/^_{1,2}$/,shouldClose:/^_{2}[\s\S]$/}),o.set("ItalicAsterisk",{canOpen:/^\*$/,shouldOpen:/^\*[^(\*\s\n)]$/,canClose:/^\*$/,shouldClose:/^\*[\s\S]$/}),o.set("ItalicUnderscore",{canOpen:/^_$/,shouldOpen:/^_[^(_\s\n)]$/,canClose:/^_$/,shouldClose:/^_[\s\S]$/}),o.set("Link",r),o.set("LineBreak",{shouldOpen:()=>!1,canOpen:()=>!1,shouldClose:()=>!1,canClose:()=>!1}),o.set("Root",{canOpen:()=>!1,shouldOpen:()=>!1,canClose:()=>!1,shouldClose:()=>!1}),o.set("Paragraph",{canOpen:/^\n+$/,shouldOpen:/^\n{2,}$/,canClose:/^\n$/,shouldClose:/^\n{2,}$/}),o.set("Blockquote",{shouldOpen:/^$/,canOpen:/^>$/,shouldClose:/^$/,canClose:/^$/}),o.set("OrderedList",{shouldOpen:/^$/,canOpen:/^\d+\. $/,shouldClose:/^\n$/,canClose:/^\n$/}),o.set("UnorderedList",{shouldOpen:/^$/,canOpen:/^\* $/,shouldClose:/^\n$/,canClose:/^\n$/}),o.set("HorizontalRule",{shouldOpen:/^$/,canOpen:/^---$/,shouldClose:/^$/,canClose:/^$/}),o.set("Image",{shouldOpen:/^$/,canOpen:/^!\[.*\]\(.*\)$/,shouldClose:/^$/,canClose:/^$/});var a=Object.defineProperty,l=(e,n,t)=>((e,n,t)=>n in e?a(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class d{constructor(e,n="none",s="none"){l(this,"__canLeadToClosingMarkdown",!1),l(this,"__latestSequenceProcessed",!1),l(this,"__markdownElement"),l(this,"__markdownThatShouldBeNested"),l(this,"__markdownThatShouldBeRenderedAtParentLevel"),l(this,"__possibleNestedMarkdownElements"),l(this,"__possibleYieldingMarkdownElements"),l(this,"__potentialNestedMarkdownElements",[]),l(this,"__potentialYieldingMarkdownElements",[]),l(this,"__sequence",""),l(this,"__shouldCloseCurrentMarkdown",!1),this.__markdownElement=e,this.__possibleNestedMarkdownElements="all"===n?t:"none"===n?[]:n,this.__possibleYieldingMarkdownElements="none"===s?[]:s}get canLeadToClosingOrNewMarkdown(){return this.__latestSequenceProcessed||this.processSequence(),this.__canLeadToClosingMarkdown||this.__potentialYieldingMarkdownElements.length>0||this.__potentialNestedMarkdownElements.length>0}get elementToCreateAtParentLevel(){return this.__latestSequenceProcessed||this.processSequence(),this.__markdownThatShouldBeRenderedAtParentLevel}get nestedElementToCreate(){return this.__latestSequenceProcessed||this.processSequence(),this.__markdownThatShouldBeNested}get sequence(){return this.__sequence}get shouldCloseCurrentMarkdown(){return this.__latestSequenceProcessed||this.processSequence(),this.__shouldCloseCurrentMarkdown}appendCharacter(e){this.__sequence+=e,this.__latestSequenceProcessed=!1}reset(){this.__sequence="",this.__latestSequenceProcessed=!1,this.__canLeadToClosingMarkdown=!1,this.__potentialYieldingMarkdownElements=[],this.__potentialNestedMarkdownElements=[],this.__shouldCloseCurrentMarkdown=!1,this.__markdownThatShouldBeRenderedAtParentLevel=void 0,this.__markdownThatShouldBeNested=void 0}processSequence(){if(this.__latestSequenceProcessed)return;const e=this.__sequence;this.__canLeadToClosingMarkdown=i(this.__markdownElement,"canClose")(e),this.__potentialYieldingMarkdownElements=this.__possibleYieldingMarkdownElements.filter((n=>i(n,"canOpen")(e))),this.__potentialNestedMarkdownElements=this.__possibleNestedMarkdownElements.filter((n=>i(n,"canOpen")(e))),this.__shouldCloseCurrentMarkdown=i(this.__markdownElement,"shouldClose")(e),this.__markdownThatShouldBeRenderedAtParentLevel=this.__possibleYieldingMarkdownElements.find((n=>i(n,"shouldOpen")(e))),this.__markdownThatShouldBeNested=this.__possibleNestedMarkdownElements.find((n=>i(n,"shouldOpen")(e)))}}var c=Object.defineProperty,h=(e,n,t)=>((e,n,t)=>n in e?c(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class u{constructor(e,n,t,s,r){h(this,"__parent"),h(this,"__element"),h(this,"__initialContent"),h(this,"__initialized",!1),h(this,"__last3Characters",""),h(this,"__markdownElementName"),h(this,"__openingSequence"),h(this,"__options",{}),h(this,"__parsingChild"),h(this,"__sequenceParser"),h(this,"__yielded",!1),this.__markdownElementName=n,this.__parent=e??void 0,this.__options=r||{},this.__openingSequence=t??void 0,this.__initialContent="Root"!==n?s??void 0:void 0}get domElement(){return this.__element}get markdownElementName(){return this.__markdownElementName}get parsingChild(){return this.__parsingChild}get removeWhenEmpty(){return!1}get sequenceParser(){if(this.yielded)throw new Error("Cannot access sequence parser of a yielded processor");return this.__sequenceParser||(this.__sequenceParser=new d(this.__markdownElementName,this.nestedMarkdownElements,this.yieldingMarkdownElements)),this.__sequenceParser}get yielded(){return this.__yielded}get initialContent(){return this.__initialContent}get last3Characters(){return this.__last3Characters}get markdownProcessorOptions(){return this.__options}get syntaxHighlighter(){return this.__options.syntaxHighlighter}init(){if(!this.__initialized&&(this.__initialized=!0,"Root"!==this.__markdownElementName?this.__element=this.createElement(this.__openingSequence):this.__element=void 0,this.__initialContent))for(let e=0;e<this.__initialContent.length;e++){const n=this.__initialContent[e];this.processCharacter(n)}}parsingChildYielded(e,n,t,s){this.__parsingChild===e&&(this.__parsingChild=void 0),n&&this.createAndAppendMarkdown(n,t),s&&this.processCharacter(s)}preProcessCharacter(e){this.__last3Characters.length<3?this.__last3Characters+=e:this.__last3Characters=this.__last3Characters.slice(1)+e}purgeSequence(){this.__parsingChild&&this.__parsingChild.purgeSequence(),this.__sequenceParser?.sequence&&(this.__element?.append(this.__sequenceParser.sequence),this.__sequenceParser.reset())}resetSequenceParser(){this.__sequenceParser?.reset(),this.__sequenceParser=new d(this.__markdownElementName,this.nestedMarkdownElements,this.yieldingMarkdownElements)}setParsingChild(e){if(this.__parsingChild&&"Root"!==this.__markdownElementName)throw new Error("Cannot set spawn child while parsing child");this.__parsingChild=e,e.domElement&&this.domElement?.append(e.domElement),e.__parent=this}yield(e,n){if(this.yielded)return;const t=this.__sequenceParser&&!this.__sequenceParser?.shouldCloseCurrentMarkdown?this.__sequenceParser?.sequence:void 0;this.__yielded=!0,this.__sequenceParser=void 0,this.__parsingChild&&(this.__parsingChild.yield(),this.__parsingChild=void 0),this.__element&&(this.__element.innerHTML=this.__element.innerHTML.trim(),this.removeWhenEmpty&&""===this.__element.innerHTML&&this.__element.remove(),this.__element=void 0),this.__parent&&(this.__parent.parsingChildYielded(this,e,t,n),this.__parent=void 0)}}class C extends u{constructor(e,n,t,s,r){super(e,n,t,s,r)}createAndAppendMarkdown(e,n){B(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}processCharacter(n){if(this.preProcessCharacter(n),this.parsingChild)return void this.parsingChild.processCharacter(n);const{sequenceDiscarded:t,discardedSequence:s,yielded:r}=this.processSequence(n);if(!r)if(e(this.last3Characters)&&(this.nestedMarkdownElements.includes("LineBreak")||"all"===this.nestedMarkdownElements))this.domElement?.innerHTML&&(this.domElement.innerHTML=this.domElement?.innerHTML.trim()),this.createAndAppendMarkdown("LineBreak"),this.sequenceParser.reset();else if(t){this.resetSequenceParser();this.processSequence(n).sequenceDiscarded?(this.sequenceParser.reset(),s&&this.domElement?.append(s)):s&&s.length>1&&this.domElement?.append(s.slice(0,-1))}}processSequence(e){if(this.sequenceParser.appendCharacter(e),this.sequenceParser.shouldCloseCurrentMarkdown)return this.yield(void 0,e),{sequenceDiscarded:!1,yielded:!0};if(this.sequenceParser.elementToCreateAtParentLevel)return this.yield(this.sequenceParser.elementToCreateAtParentLevel,e),{sequenceDiscarded:!1,yielded:!0};const n=this.sequenceParser.nestedElementToCreate;if(n)return B(n,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions),this.sequenceParser.reset(),{sequenceDiscarded:!1};if(this.sequenceParser.canLeadToClosingOrNewMarkdown)return{sequenceDiscarded:!1};const t=this.sequenceParser.sequence;return this.sequenceParser.reset(),{sequenceDiscarded:!0,discardedSequence:t}}}class p extends C{constructor(e,n,t,s){super(e,"BoldAsterisk",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("strong")}}class _ extends C{constructor(e,n,t,s){super(e,"BoldUnderscore",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("strong")}}class m extends C{constructor(e,n,t,s){super(e,"Code",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("code")}}var g=Object.defineProperty,k=(e,n,t)=>((e,n,t)=>n in e?g(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class w extends C{constructor(e,t,s,r={}){super(e,"CodeBlock",t??null,!s||n(s)?null:s,r),k(this,"codeContainer",null),k(this,"currentLineContainer",null),k(this,"language"),this.language=this.extractLanguageFromOpeningSequence(t)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return"none"}get yieldingMarkdownElements(){return"none"}createElement(){const e=document.createElement("div");return e.classList.add("code-block"),this.codeContainer=document.createElement("pre"),e.append(this.codeContainer),this.language&&(this.codeContainer.dataset.language=this.language),e}processCharacter(e){this.preProcessCharacter(e);const{sequenceDiscarded:n,discardedSequence:t,yielded:s}=this.processSequence(e);if(s)return;if(!n)return;if(t&&t.length>1&&this.codeContainer)for(let e=0;e<t.length-1;e++)this.appendCharacterToCodeBlock(t[e]);this.resetSequenceParser();this.processSequence(e).sequenceDiscarded&&(this.sequenceParser.reset(),this.appendCharacterToCodeBlock(e))}yield(e,n){if(this.highlightCurrentLine(),!1!==this.markdownProcessorOptions.showCodeBlockCopyButton){const e=this.__parent?.domElement;e&&(e.querySelectorAll(".code-block").forEach((e=>{if(!e.querySelector("pre"))return;if(e.previousElementSibling?.classList.contains("nlux-cpy-btn"))return;const n="Copy code block to clipboard",t=document.createElement("button");t.classList.add("nlux-cpy-btn"),t.setAttribute("aria-label",n),t.setAttribute("title",n),t.innerHTML='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M15 1.25H10.9436C9.10583 1.24998 7.65019 1.24997 6.51098 1.40314C5.33856 1.56076 4.38961 1.89288 3.64124 2.64124C2.89288 3.38961 2.56076 4.33856 2.40314 5.51098C2.24997 6.65019 2.24998 8.10582 2.25 9.94357V16C2.25 17.8722 3.62205 19.424 5.41551 19.7047C5.55348 20.4687 5.81753 21.1208 6.34835 21.6517C6.95027 22.2536 7.70814 22.5125 8.60825 22.6335C9.47522 22.75 10.5775 22.75 11.9451 22.75H15.0549C16.4225 22.75 17.5248 22.75 18.3918 22.6335C19.2919 22.5125 20.0497 22.2536 20.6517 21.6517C21.2536 21.0497 21.5125 20.2919 21.6335 19.3918C21.75 18.5248 21.75 17.4225 21.75 16.0549V10.9451C21.75 9.57754 21.75 8.47522 21.6335 7.60825C21.5125 6.70814 21.2536 5.95027 20.6517 5.34835C20.1208 4.81753 19.4687 4.55348 18.7047 4.41551C18.424 2.62205 16.8722 1.25 15 1.25ZM17.1293 4.27117C16.8265 3.38623 15.9876 2.75 15 2.75H11C9.09318 2.75 7.73851 2.75159 6.71085 2.88976C5.70476 3.02502 5.12511 3.27869 4.7019 3.7019C4.27869 4.12511 4.02502 4.70476 3.88976 5.71085C3.75159 6.73851 3.75 8.09318 3.75 10V16C3.75 16.9876 4.38624 17.8265 5.27117 18.1293C5.24998 17.5194 5.24999 16.8297 5.25 16.0549V10.9451C5.24998 9.57754 5.24996 8.47522 5.36652 7.60825C5.48754 6.70814 5.74643 5.95027 6.34835 5.34835C6.95027 4.74643 7.70814 4.48754 8.60825 4.36652C9.47522 4.24996 10.5775 4.24998 11.9451 4.25H15.0549C15.8297 4.24999 16.5194 4.24998 17.1293 4.27117ZM7.40901 6.40901C7.68577 6.13225 8.07435 5.9518 8.80812 5.85315C9.56347 5.75159 10.5646 5.75 12 5.75H15C16.4354 5.75 17.4365 5.75159 18.1919 5.85315C18.9257 5.9518 19.3142 6.13225 19.591 6.40901C19.8678 6.68577 20.0482 7.07435 20.1469 7.80812C20.2484 8.56347 20.25 9.56458 20.25 11V16C20.25 17.4354 20.2484 18.4365 20.1469 19.1919C20.0482 19.9257 19.8678 20.3142 19.591 20.591C19.3142 20.8678 18.9257 21.0482 18.1919 21.1469C17.4365 21.2484 16.4354 21.25 15 21.25H12C10.5646 21.25 9.56347 21.2484 8.80812 21.1469C8.07435 21.0482 7.68577 20.8678 7.40901 20.591C7.13225 20.3142 6.9518 19.9257 6.85315 19.1919C6.75159 18.4365 6.75 17.4354 6.75 16V11C6.75 9.56458 6.75159 8.56347 6.85315 7.80812C6.9518 7.07435 7.13225 6.68577 7.40901 6.40901Z" fill="currentColor"/></svg>',e.insertAdjacentElement("beforebegin",t)})),(e=>{e.querySelectorAll(".nlux-cpy-btn").forEach((e=>{if(!(e instanceof HTMLButtonElement))return;if("true"===e.dataset.clickListenerSet)return;let n=!1;const t=e.nextElementSibling;e.addEventListener("click",(()=>{if(n||!t)return;const s=t.innerText;navigator.clipboard.writeText(s??""),n=!0,e.classList.add("clicked"),setTimeout((()=>{n=!1,e.classList.remove("clicked")}),1e3)})),e.dataset.clickListenerSet="true"}))})(e))}super.yield(e,n),this.codeContainer=null,this.currentLineContainer=null}appendCharacterToCodeBlock(e){if(this.codeContainer)if("\n"!==e)this.currentLineContainer||(this.currentLineContainer=document.createElement("div"),this.codeContainer.append(this.currentLineContainer)),this.currentLineContainer.append(e);else if(this.currentLineContainer)this.currentLineContainer.innerHTML?this.highlightCurrentLine():this.currentLineContainer.innerHTML=" ",this.currentLineContainer=null;else if(this.codeContainer.innerHTML){const e=document.createElement("div");e.innerHTML=" ",this.codeContainer.append(e)}}extractLanguageFromOpeningSequence(e){if(!e)return;const n=e.match(/```(\w+)/);return Array.isArray(n)&&n.length>1?n[1]:void 0}highlightCurrentLine(){if(!this.domElement||!this.currentLineContainer)return;const e=this.currentLineContainer.innerText??this.currentLineContainer.innerHTML;if(this.syntaxHighlighter&&this.codeContainer&&e){const n=this.codeContainer.dataset.language;if(n){const t=this.syntaxHighlighter.createHighlighter({language:n,colorMode:"dark"}),s=this.syntaxHighlighter.highlightingClass(n);this.codeContainer.classList.contains(s)||this.codeContainer.classList.add(s),this.currentLineContainer.innerHTML=t(e,n)}}}}var E=Object.defineProperty,q=(e,n,t)=>((e,n,t)=>n in e?E(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class L extends C{constructor(e,n,t,s,r){super(n,P(e),t??null,s??null,r??null),q(this,"__headingLevel"),this.__headingLevel=e}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("h"+this.__headingLevel)}processCharacter(e){if(this.preProcessCharacter(e),this.parsingChild)return void this.parsingChild.processCharacter(e);const{sequenceDiscarded:n,discardedSequence:t,yielded:s}=this.processSequence(e);s||n&&t&&(/^\s+$/.test(t)||this.domElement?.append(t))}}const P=e=>{switch(e){case 1:return"Heading1";case 2:return"Heading2";case 3:return"Heading3";case 4:return"Heading4";case 5:return"Heading5";case 6:return"Heading6"}};class v extends C{constructor(e,n,t,s){super(e,"ItalicAsterisk",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("em")}}class f extends C{constructor(e,n,t,s){super(e,"ItalicUnderscore",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("em")}}class M extends u{constructor(e,n,t){super(e,"LineBreak",n??null,t??null,null)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return"none"}get yieldingMarkdownElements(){return"none"}createAndAppendMarkdown(e,n){B(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}createElement(){return document.createElement("br")}init(){super.init()}processCharacter(e){this.yield(void 0,e)}}var $=Object.defineProperty,y=(e,n,t)=>((e,n,t)=>n in e?$(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class O extends C{constructor(e,n,t,s={}){super(e,"Link",n??null,t??null,s),y(this,"linkContentProcessed","")}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code"]}get yieldingMarkdownElements(){return["Paragraph","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code"]}createElement(e){const n=/\[(.*[^\]])\]\((.*[^\)])\)/.exec(e),t=document.createElement("a");return t.textContent="",n&&n.length>=3&&n[2]&&(t.href=n[2]),"self"!==this.markdownProcessorOptions.markdownLinkTarget&&(t.target="_blank"),t}processCharacter(e){super.processCharacter(e),this.yielded||(this.linkContentProcessed+=e)}processSequence(e){if(!this.initialContent||this.initialContent===this.linkContentProcessed)return this.yield(void 0,e),{yielded:!0,sequenceDiscarded:!0,discardedSequence:e};this.sequenceParser.appendCharacter(e);const n=this.sequenceParser.nestedElementToCreate;if(n)return B(n,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions),this.sequenceParser.reset(),{sequenceDiscarded:!1};if(this.sequenceParser.canLeadToClosingOrNewMarkdown)return{sequenceDiscarded:!1};const t=this.sequenceParser.sequence;return this.sequenceParser.reset(),{sequenceDiscarded:!0,discardedSequence:t}}}class A extends C{constructor(e,n,t,s){super(e,"Paragraph",n??null,t??null,s??null)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return["LineBreak","Blockquote","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get removeWhenEmpty(){return!0}get yieldingMarkdownElements(){return["Heading1","Heading2","Heading3","Heading4","Heading5","Heading6","CodeBlock","UnorderedList","OrderedList","HorizontalRule","Image"]}createElement(){return document.createElement("p")}}const B=(e,n,t,s,r)=>{if("Paragraph"===e){const e=new A(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("LineBreak"===e){const e=new M(n,t,s);return e.init(),void n.setParsingChild(e)}if("Link"===e){const e=/\[([^\]]+)\]\(([^\)]*)\)/.exec(t),s=e&&e.length>=3?e[1]:"",i=new O(n,t,s,r);return i.init(),void n.setParsingChild(i)}if("Heading1"===e||"Heading2"===e||"Heading3"===e||"Heading4"===e||"Heading5"===e||"Heading6"===e){const i=parseInt(e[e.length-1]);if(Number.isNaN(i)||!Number.isFinite(i)||i<1||i>6)throw new Error("Invalid heading level");const o=new L(i,n,t,s,r);return o.init(),void n.setParsingChild(o)}if("Code"===e){const e=new m(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("CodeBlock"===e){const e=new w(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("BoldAsterisk"===e){const e=new p(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("BoldUnderscore"===e){const e=new _(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("ItalicAsterisk"===e){const e=new v(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("ItalicUnderscore"===e){const e=new f(n,t,s,r);return e.init(),void n.setParsingChild(e)}throw new Error("Unable to create child processor for markdown element "+e)};var S=Object.defineProperty,H=(e,n,t)=>((e,n,t)=>n in e?S(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class b extends u{constructor(e,n,t){super(null,"Root",n??null,null,t??null),H(this,"__rootDomElement"),this.__rootDomElement=e}get canExistAtRootLevel(){return!1}get domElement(){return this.__rootDomElement}get nestedMarkdownElements(){return"all"}get yieldingMarkdownElements(){return"none"}complete(){var e;this.parsingChild&&this.parsingChild.purgeSequence(),this.purgeSequence(),e=this.domElement?.lastChild,e instanceof HTMLElement&&"BR"===e.tagName&&this.domElement?.lastChild?.remove()}createAndAppendMarkdown(e,n){B(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}createElement(){throw new Error("Root cannot create an element")}processCharacter(e){this.preProcessCharacter(e),this.parsingChild?this.parsingChild.processCharacter(e):(this.sequenceParser.appendCharacter(e),this.processSequence(e))}yield(){super.yield(),this.__rootDomElement=void 0}processSequence(t){if(this.sequenceParser.shouldCloseCurrentMarkdown)return;const s=this.sequenceParser.nestedElementToCreate;if(s){B(s,this,this.sequenceParser.sequence,t,this.markdownProcessorOptions);const e=this.parsingChild;return e&&!e.canExistAtRootLevel&&(B("Paragraph",this,void 0,void 0,this.markdownProcessorOptions),this.parsingChild?.setParsingChild(e)),void this.sequenceParser.reset()}this.sequenceParser.canLeadToClosingOrNewMarkdown||(e(this.last3Characters)?this.createAndAppendMarkdown("LineBreak"):t&&!n(t)&&(B("Paragraph",this,void 0,this.sequenceParser.sequence,this.markdownProcessorOptions),this.sequenceParser.reset()))}}const x=10,T=(e,n)=>{const t=((e,n,t)=>{const{streamingAnimationSpeed:s=x,markdownLinkTarget:r,showCodeBlockCopyButton:i,skipStreamingAnimation:o=!1,onComplete:a}=t||{},l=new b(e,void 0,{syntaxHighlighter:n,markdownLinkTarget:r,showCodeBlockCopyButton:i}),d=[];let c=0,h=!1,u="idle";const C=o?0:Math.max(s,0),p=s>0?s:20,_=Math.ceil(2e3/p),m=()=>{if("closed"===u)return;if("idle"===u&&(u="processing"),0===d.length)return void(!1===h&&c<_?(u="waitingForMoreCharacters",c+=1,setTimeout(m,p)):(l.processCharacter("\n"),l.complete(),l.yield(),u="closed",a?.()));u="processing",c=0;const e=d.shift();e&&l.processCharacter(e),setTimeout(m,C)};return{next:e=>{if(e){for(let n=0;n<e.length;n++)d.push(e[n]);"idle"===u&&m()}},complete:()=>{h=!0},error:()=>{}}})(e,n?.syntaxHighlighter,{markdownLinkTarget:n?.markdownLinkTarget,showCodeBlockCopyButton:n?.showCodeBlockCopyButton,skipStreamingAnimation:n?.skipStreamingAnimation,streamingAnimationSpeed:n?.streamingAnimationSpeed,onComplete:n?.onComplete});return{next(e){t.next(e)},complete(){t.complete()}}};export{T as createMarkdownStreamParser};
const e=e=>/^[ \t]{2}\n$/m.test(e),n=e=>/^[ \t\n]{1}$/.test(e),t=["Root","Paragraph","LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Blockquote","Heading1","Heading2","Heading3","Heading4","Heading5","Heading6","OrderedList","UnorderedList","Code","CodeBlock","HorizontalRule","Link","Image"],s=["^\\[$","^\\[[^\\]]+$","^\\[[^\\]]+\\]$","^\\[[^\\]]+\\]\\($","^\\[[^\\]]+\\]\\([^\\)]*$"],r={shouldOpen:/\[([^\]]+)\]\(([^\)]*)\)/,canOpen:e=>s.some((n=>new RegExp(n).test(e))),shouldClose:()=>!0,canClose:()=>!1},i=(e,n)=>t=>{const s=o.get(e);if(!s)throw new Error(`No sequence parsers found for markdown element ${e}`);const r=s[n];return r instanceof RegExp?r.test(t):r(t)},o=new Map;o.set("Code",{shouldOpen:/^`[^`]$/,canOpen:/^`$/,shouldClose:/^`[\s\S]+$/,canClose:/^`$/}),o.set("CodeBlock",{shouldOpen:/^```.*\n$/,canOpen:/^`{1,3}.*$/,shouldClose:/^\n?```[\s\S]+$/,canClose:/^\n$|^\n?`{1,3}$/}),o.set("Heading1",{shouldOpen:/^\n*#{1} $/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading2",{shouldOpen:/^\n*#{2} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading3",{shouldOpen:/^\n*#{3} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading4",{shouldOpen:/^\n*#{4} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading5",{shouldOpen:/^\n*#{5} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("Heading6",{shouldOpen:/^\n*#{6} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),o.set("BoldAsterisk",{canOpen:/^\*{1,2}$/,shouldOpen:/^\*{2}[^(\s\n)]$/,canClose:/^\*{1,2}$/,shouldClose:/^\*{2}[\s\S]$/}),o.set("BoldUnderscore",{canOpen:/^_{1,2}$/,shouldOpen:/^_{2}[^(\s\n)]$/,canClose:/^_{1,2}$/,shouldClose:/^_{2}[\s\S]$/}),o.set("ItalicAsterisk",{canOpen:/^\*$/,shouldOpen:/^\*[^(\*\s\n)]$/,canClose:/^\*$/,shouldClose:/^\*[\s\S]$/}),o.set("ItalicUnderscore",{canOpen:/^_$/,shouldOpen:/^_[^(_\s\n)]$/,canClose:/^_$/,shouldClose:/^_[\s\S]$/}),o.set("Link",r),o.set("LineBreak",{shouldOpen:()=>!1,canOpen:()=>!1,shouldClose:()=>!1,canClose:()=>!1}),o.set("Root",{canOpen:()=>!1,shouldOpen:()=>!1,canClose:()=>!1,shouldClose:()=>!1}),o.set("Paragraph",{canOpen:/^\n+$/,shouldOpen:/^\n{2,}$/,canClose:/^\n$/,shouldClose:/^\n{2,}$/}),o.set("Blockquote",{shouldOpen:/^$/,canOpen:/^>$/,shouldClose:/^$/,canClose:/^$/}),o.set("OrderedList",{shouldOpen:/^$/,canOpen:/^\d+\. $/,shouldClose:/^\n$/,canClose:/^\n$/}),o.set("UnorderedList",{shouldOpen:/^$/,canOpen:/^\* $/,shouldClose:/^\n$/,canClose:/^\n$/}),o.set("HorizontalRule",{shouldOpen:/^$/,canOpen:/^---$/,shouldClose:/^$/,canClose:/^$/}),o.set("Image",{shouldOpen:/^$/,canOpen:/^!\[.*\]\(.*\)$/,shouldClose:/^$/,canClose:/^$/});var a=Object.defineProperty,l=(e,n,t)=>((e,n,t)=>n in e?a(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class d{constructor(e,n="none",s="none"){l(this,"__canLeadToClosingMarkdown",!1),l(this,"__latestSequenceProcessed",!1),l(this,"__markdownElement"),l(this,"__markdownThatShouldBeNested"),l(this,"__markdownThatShouldBeRenderedAtParentLevel"),l(this,"__possibleNestedMarkdownElements"),l(this,"__possibleYieldingMarkdownElements"),l(this,"__potentialNestedMarkdownElements",[]),l(this,"__potentialYieldingMarkdownElements",[]),l(this,"__sequence",""),l(this,"__shouldCloseCurrentMarkdown",!1),this.__markdownElement=e,this.__possibleNestedMarkdownElements="all"===n?t:"none"===n?[]:n,this.__possibleYieldingMarkdownElements="none"===s?[]:s}get canLeadToClosingOrNewMarkdown(){return this.__latestSequenceProcessed||this.processSequence(),this.__canLeadToClosingMarkdown||this.__potentialYieldingMarkdownElements.length>0||this.__potentialNestedMarkdownElements.length>0}get elementToCreateAtParentLevel(){return this.__latestSequenceProcessed||this.processSequence(),this.__markdownThatShouldBeRenderedAtParentLevel}get nestedElementToCreate(){return this.__latestSequenceProcessed||this.processSequence(),this.__markdownThatShouldBeNested}get sequence(){return this.__sequence}get shouldCloseCurrentMarkdown(){return this.__latestSequenceProcessed||this.processSequence(),this.__shouldCloseCurrentMarkdown}appendCharacter(e){this.__sequence+=e,this.__latestSequenceProcessed=!1}reset(){this.__sequence="",this.__latestSequenceProcessed=!1,this.__canLeadToClosingMarkdown=!1,this.__potentialYieldingMarkdownElements=[],this.__potentialNestedMarkdownElements=[],this.__shouldCloseCurrentMarkdown=!1,this.__markdownThatShouldBeRenderedAtParentLevel=void 0,this.__markdownThatShouldBeNested=void 0}processSequence(){if(this.__latestSequenceProcessed)return;const e=this.__sequence;this.__canLeadToClosingMarkdown=i(this.__markdownElement,"canClose")(e),this.__potentialYieldingMarkdownElements=this.__possibleYieldingMarkdownElements.filter((n=>i(n,"canOpen")(e))),this.__potentialNestedMarkdownElements=this.__possibleNestedMarkdownElements.filter((n=>i(n,"canOpen")(e))),this.__shouldCloseCurrentMarkdown=i(this.__markdownElement,"shouldClose")(e),this.__markdownThatShouldBeRenderedAtParentLevel=this.__possibleYieldingMarkdownElements.find((n=>i(n,"shouldOpen")(e))),this.__markdownThatShouldBeNested=this.__possibleNestedMarkdownElements.find((n=>i(n,"shouldOpen")(e)))}}var c=Object.defineProperty,h=(e,n,t)=>((e,n,t)=>n in e?c(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class u{constructor(e,n,t,s,r){h(this,"__parent"),h(this,"__element"),h(this,"__initialContent"),h(this,"__initialized",!1),h(this,"__last3Characters",""),h(this,"__markdownElementName"),h(this,"__openingSequence"),h(this,"__options",{}),h(this,"__parsingChild"),h(this,"__sequenceParser"),h(this,"__yielded",!1),this.__markdownElementName=n,this.__parent=e??void 0,this.__options=r||{},this.__openingSequence=t??void 0,this.__initialContent="Root"!==n?s??void 0:void 0}get domElement(){return this.__element}get markdownElementName(){return this.__markdownElementName}get parsingChild(){return this.__parsingChild}get removeWhenEmpty(){return!1}get sequenceParser(){if(this.yielded)throw new Error("Cannot access sequence parser of a yielded processor");return this.__sequenceParser||(this.__sequenceParser=new d(this.__markdownElementName,this.nestedMarkdownElements,this.yieldingMarkdownElements)),this.__sequenceParser}get yielded(){return this.__yielded}get initialContent(){return this.__initialContent}get last3Characters(){return this.__last3Characters}get markdownProcessorOptions(){return this.__options}get syntaxHighlighter(){return this.__options.syntaxHighlighter}init(){if(!this.__initialized&&(this.__initialized=!0,"Root"!==this.__markdownElementName?this.__element=this.createElement(this.__openingSequence):this.__element=void 0,this.__initialContent))for(let e=0;e<this.__initialContent.length;e++){const n=this.__initialContent[e];this.processCharacter(n)}}parsingChildYielded(e,n,t,s){this.__parsingChild===e&&(this.__parsingChild=void 0),n&&this.createAndAppendMarkdown(n,t),s&&this.processCharacter(s)}preProcessCharacter(e){this.__last3Characters.length<3?this.__last3Characters+=e:this.__last3Characters=this.__last3Characters.slice(1)+e}purgeSequence(){this.__parsingChild&&this.__parsingChild.purgeSequence(),this.__sequenceParser?.sequence&&(this.__element?.append(this.__sequenceParser.sequence),this.__sequenceParser.reset())}resetSequenceParser(){this.__sequenceParser?.reset(),this.__sequenceParser=new d(this.__markdownElementName,this.nestedMarkdownElements,this.yieldingMarkdownElements)}setParsingChild(e){if(this.__parsingChild&&"Root"!==this.__markdownElementName)throw new Error("Cannot set spawn child while parsing child");this.__parsingChild=e,e.domElement&&this.domElement?.append(e.domElement),e.__parent=this}yield(e,n){if(this.yielded)return;const t=this.__sequenceParser&&!this.__sequenceParser?.shouldCloseCurrentMarkdown?this.__sequenceParser?.sequence:void 0;this.__yielded=!0,this.__sequenceParser=void 0,this.__parsingChild&&(this.__parsingChild.yield(),this.__parsingChild=void 0),this.__element&&(this.__element.innerHTML=this.__element.innerHTML.trim(),this.removeWhenEmpty&&""===this.__element.innerHTML&&this.__element.remove(),this.__element=void 0),this.__parent&&(this.__parent.parsingChildYielded(this,e,t,n),this.__parent=void 0)}}class p extends u{constructor(e,n,t,s,r){super(e,n,t,s,r)}createAndAppendMarkdown(e,n){B(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}processCharacter(n){if(this.preProcessCharacter(n),this.parsingChild)return void this.parsingChild.processCharacter(n);const{sequenceDiscarded:t,discardedSequence:s,yielded:r}=this.processSequence(n);if(!r)if(e(this.last3Characters)&&(this.nestedMarkdownElements.includes("LineBreak")||"all"===this.nestedMarkdownElements))this.domElement?.innerHTML&&(this.domElement.innerHTML=this.domElement?.innerHTML.trim()),this.createAndAppendMarkdown("LineBreak"),this.sequenceParser.reset();else if(t){this.resetSequenceParser();this.processSequence(n).sequenceDiscarded?(this.sequenceParser.reset(),s&&this.domElement?.append(s)):s&&s.length>1&&this.domElement?.append(s.slice(0,-1))}}processSequence(e){if(this.sequenceParser.appendCharacter(e),this.sequenceParser.shouldCloseCurrentMarkdown)return this.yield(void 0,e),{sequenceDiscarded:!1,yielded:!0};if(this.sequenceParser.elementToCreateAtParentLevel)return this.yield(this.sequenceParser.elementToCreateAtParentLevel,e),{sequenceDiscarded:!1,yielded:!0};const n=this.sequenceParser.nestedElementToCreate;if(n)return B(n,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions),this.sequenceParser.reset(),{sequenceDiscarded:!1};if(this.sequenceParser.canLeadToClosingOrNewMarkdown)return{sequenceDiscarded:!1};const t=this.sequenceParser.sequence;return this.sequenceParser.reset(),{sequenceDiscarded:!0,discardedSequence:t}}}class _ extends p{constructor(e,n,t,s){super(e,"BoldAsterisk",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("strong")}}class m extends p{constructor(e,n,t,s){super(e,"BoldUnderscore",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("strong")}}class g extends p{constructor(e,n,t,s){super(e,"Code",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("code")}}var C=Object.defineProperty,k=(e,n,t)=>((e,n,t)=>n in e?C(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class w extends p{constructor(e,t,s,r={}){super(e,"CodeBlock",t??null,!s||n(s)?null:s,r),k(this,"codeContainer",null),k(this,"currentLineContainer",null),k(this,"language"),this.language=this.extractLanguageFromOpeningSequence(t)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return"none"}get yieldingMarkdownElements(){return"none"}createElement(){const e=document.createElement("div");return e.classList.add("code-block"),this.codeContainer=document.createElement("pre"),e.append(this.codeContainer),this.language&&(this.codeContainer.dataset.language=this.language),e}processCharacter(e){this.preProcessCharacter(e);const{sequenceDiscarded:n,discardedSequence:t,yielded:s}=this.processSequence(e);if(s)return;if(!n)return;if(t&&t.length>1&&this.codeContainer)for(let e=0;e<t.length-1;e++)this.appendCharacterToCodeBlock(t[e]);this.resetSequenceParser();this.processSequence(e).sequenceDiscarded&&(this.sequenceParser.reset(),this.appendCharacterToCodeBlock(e))}yield(e,n){if(this.highlightCurrentLine(),!1!==this.markdownProcessorOptions.showCodeBlockCopyButton){const e=this.__parent?.domElement;e&&(e.querySelectorAll(".code-block").forEach((e=>{if(!e.querySelector("pre"))return;if(e.previousElementSibling?.classList.contains("nlux-cpy-btn"))return;const n="Copy code block to clipboard",t=document.createElement("button");t.classList.add("nlux-cpy-btn"),t.setAttribute("aria-label",n),t.setAttribute("title",n);const s=document.createElement("span");s.classList.add("icon-copy"),t.appendChild(s),e.insertAdjacentElement("beforebegin",t)})),(e=>{e.querySelectorAll(".nlux-cpy-btn").forEach((e=>{if(!(e instanceof HTMLButtonElement))return;if("true"===e.dataset.clickListenerSet)return;let n=!1;const t=e.nextElementSibling;e.addEventListener("click",(()=>{if(n||!t)return;const s=t.innerText;navigator.clipboard.writeText(s??""),n=!0,e.classList.add("clicked"),setTimeout((()=>{n=!1,e.classList.remove("clicked")}),1e3)})),e.dataset.clickListenerSet="true"}))})(e))}super.yield(e,n),this.codeContainer=null,this.currentLineContainer=null}appendCharacterToCodeBlock(e){if(this.codeContainer)if("\n"!==e)this.currentLineContainer||(this.currentLineContainer=document.createElement("div"),this.codeContainer.append(this.currentLineContainer)),this.currentLineContainer.append(e);else if(this.currentLineContainer)this.currentLineContainer.innerHTML?this.highlightCurrentLine():this.currentLineContainer.innerHTML=" ",this.currentLineContainer=null;else if(this.codeContainer.innerHTML){const e=document.createElement("div");e.innerHTML=" ",this.codeContainer.append(e)}}extractLanguageFromOpeningSequence(e){if(!e)return;const n=e.match(/```(\w+)/);return Array.isArray(n)&&n.length>1?n[1]:void 0}highlightCurrentLine(){if(!this.domElement||!this.currentLineContainer)return;const e=this.currentLineContainer.innerText??this.currentLineContainer.innerHTML;if(this.syntaxHighlighter&&this.codeContainer&&e){const n=this.codeContainer.dataset.language;if(n){const t=this.syntaxHighlighter.createHighlighter({language:n,colorMode:"dark"}),s=this.syntaxHighlighter.highlightingClass(n);this.codeContainer.classList.contains(s)||this.codeContainer.classList.add(s),this.currentLineContainer.innerHTML=t(e,n)}}}}var E=Object.defineProperty,q=(e,n,t)=>((e,n,t)=>n in e?E(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class L extends p{constructor(e,n,t,s,r){super(n,P(e),t??null,s??null,r??null),q(this,"__headingLevel"),this.__headingLevel=e}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("h"+this.__headingLevel)}processCharacter(e){if(this.preProcessCharacter(e),this.parsingChild)return void this.parsingChild.processCharacter(e);const{sequenceDiscarded:n,discardedSequence:t,yielded:s}=this.processSequence(e);s||n&&t&&(/^\s+$/.test(t)||this.domElement?.append(t))}}const P=e=>{switch(e){case 1:return"Heading1";case 2:return"Heading2";case 3:return"Heading3";case 4:return"Heading4";case 5:return"Heading5";case 6:return"Heading6"}};class v extends p{constructor(e,n,t,s){super(e,"ItalicAsterisk",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("em")}}class f extends p{constructor(e,n,t,s){super(e,"ItalicUnderscore",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("em")}}class M extends u{constructor(e,n,t){super(e,"LineBreak",n??null,t??null,null)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return"none"}get yieldingMarkdownElements(){return"none"}createAndAppendMarkdown(e,n){B(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}createElement(){return document.createElement("br")}init(){super.init()}processCharacter(e){this.yield(void 0,e)}}var $=Object.defineProperty,y=(e,n,t)=>((e,n,t)=>n in e?$(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class O extends p{constructor(e,n,t,s={}){super(e,"Link",n??null,t??null,s),y(this,"linkContentProcessed","")}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code"]}get yieldingMarkdownElements(){return["Paragraph","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code"]}createElement(e){const n=/\[(.*[^\]])\]\((.*[^\)])\)/.exec(e),t=document.createElement("a");return t.textContent="",n&&n.length>=3&&n[2]&&(t.href=n[2]),"self"!==this.markdownProcessorOptions.markdownLinkTarget&&(t.target="_blank"),t}processCharacter(e){super.processCharacter(e),this.yielded||(this.linkContentProcessed+=e)}processSequence(e){if(!this.initialContent||this.initialContent===this.linkContentProcessed)return this.yield(void 0,e),{yielded:!0,sequenceDiscarded:!0,discardedSequence:e};this.sequenceParser.appendCharacter(e);const n=this.sequenceParser.nestedElementToCreate;if(n)return B(n,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions),this.sequenceParser.reset(),{sequenceDiscarded:!1};if(this.sequenceParser.canLeadToClosingOrNewMarkdown)return{sequenceDiscarded:!1};const t=this.sequenceParser.sequence;return this.sequenceParser.reset(),{sequenceDiscarded:!0,discardedSequence:t}}}class A extends p{constructor(e,n,t,s){super(e,"Paragraph",n??null,t??null,s??null)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return["LineBreak","Blockquote","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get removeWhenEmpty(){return!0}get yieldingMarkdownElements(){return["Heading1","Heading2","Heading3","Heading4","Heading5","Heading6","CodeBlock","UnorderedList","OrderedList","HorizontalRule","Image"]}createElement(){return document.createElement("p")}}const B=(e,n,t,s,r)=>{if("Paragraph"===e){const e=new A(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("LineBreak"===e){const e=new M(n,t,s);return e.init(),void n.setParsingChild(e)}if("Link"===e){const e=/\[([^\]]+)\]\(([^\)]*)\)/.exec(t),s=e&&e.length>=3?e[1]:"",i=new O(n,t,s,r);return i.init(),void n.setParsingChild(i)}if("Heading1"===e||"Heading2"===e||"Heading3"===e||"Heading4"===e||"Heading5"===e||"Heading6"===e){const i=parseInt(e[e.length-1]);if(Number.isNaN(i)||!Number.isFinite(i)||i<1||i>6)throw new Error("Invalid heading level");const o=new L(i,n,t,s,r);return o.init(),void n.setParsingChild(o)}if("Code"===e){const e=new g(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("CodeBlock"===e){const e=new w(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("BoldAsterisk"===e){const e=new _(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("BoldUnderscore"===e){const e=new m(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("ItalicAsterisk"===e){const e=new v(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("ItalicUnderscore"===e){const e=new f(n,t,s,r);return e.init(),void n.setParsingChild(e)}throw new Error("Unable to create child processor for markdown element "+e)};var S=Object.defineProperty,b=(e,n,t)=>((e,n,t)=>n in e?S(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class H extends u{constructor(e,n,t){super(null,"Root",n??null,null,t??null),b(this,"__rootDomElement"),this.__rootDomElement=e}get canExistAtRootLevel(){return!1}get domElement(){return this.__rootDomElement}get nestedMarkdownElements(){return"all"}get yieldingMarkdownElements(){return"none"}complete(){var e;this.parsingChild&&this.parsingChild.purgeSequence(),this.purgeSequence(),e=this.domElement?.lastChild,e instanceof HTMLElement&&"BR"===e.tagName&&this.domElement?.lastChild?.remove()}createAndAppendMarkdown(e,n){B(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}createElement(){throw new Error("Root cannot create an element")}processCharacter(e){this.preProcessCharacter(e),this.parsingChild?this.parsingChild.processCharacter(e):(this.sequenceParser.appendCharacter(e),this.processSequence(e))}yield(){super.yield(),this.__rootDomElement=void 0}processSequence(t){if(this.sequenceParser.shouldCloseCurrentMarkdown)return;const s=this.sequenceParser.nestedElementToCreate;if(s){B(s,this,this.sequenceParser.sequence,t,this.markdownProcessorOptions);const e=this.parsingChild;return e&&!e.canExistAtRootLevel&&(B("Paragraph",this,void 0,void 0,this.markdownProcessorOptions),this.parsingChild?.setParsingChild(e)),void this.sequenceParser.reset()}this.sequenceParser.canLeadToClosingOrNewMarkdown||(e(this.last3Characters)?this.createAndAppendMarkdown("LineBreak"):t&&!n(t)&&(B("Paragraph",this,void 0,this.sequenceParser.sequence,this.markdownProcessorOptions),this.sequenceParser.reset()))}}const T=10,x=(e,n)=>{const t=((e,n,t)=>{const{streamingAnimationSpeed:s=T,markdownLinkTarget:r,showCodeBlockCopyButton:i,skipStreamingAnimation:o=!1,onComplete:a}=t||{},l=new H(e,void 0,{syntaxHighlighter:n,markdownLinkTarget:r,showCodeBlockCopyButton:i}),d=[];let c=0,h=!1,u="idle";const p=o?0:Math.max(s,0),_=s>0?s:20,m=Math.ceil(2e3/_),g=()=>{if("closed"===u)return;if("idle"===u&&(u="processing"),0===d.length)return void(!1===h&&c<m?(u="waitingForMoreCharacters",c+=1,setTimeout(g,_)):(l.processCharacter("\n"),l.complete(),l.yield(),u="closed",a?.()));u="processing",c=0;const e=d.shift();e&&l.processCharacter(e),setTimeout(g,p)};return{next:e=>{if(e){for(let n=0;n<e.length;n++)d.push(e[n]);"idle"===u&&g()}},complete:()=>{h=!0},error:()=>{}}})(e,n?.syntaxHighlighter,{markdownLinkTarget:n?.markdownLinkTarget,showCodeBlockCopyButton:n?.showCodeBlockCopyButton,skipStreamingAnimation:n?.skipStreamingAnimation,streamingAnimationSpeed:n?.streamingAnimationSpeed,onComplete:n?.onComplete});return{next(e){t.next(e)},complete(){t.complete()}}};export{x as createMarkdownStreamParser};
{
"name": "@nlux/markdown",
"version": "2.1.5-beta",
"version": "2.1.6-beta",
"description": "A lightweight and fast markdown parser used by nlux that can be used to parse markdown text streams into DOM.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -1,1 +0,1 @@

!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlux/markdown"]={})}(this,(function(e){"use strict";const n=e=>/^[ \t]{2}\n$/m.test(e),t=e=>/^[ \t\n]{1}$/.test(e),s=["Root","Paragraph","LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Blockquote","Heading1","Heading2","Heading3","Heading4","Heading5","Heading6","OrderedList","UnorderedList","Code","CodeBlock","HorizontalRule","Link","Image"],r=["^\\[$","^\\[[^\\]]+$","^\\[[^\\]]+\\]$","^\\[[^\\]]+\\]\\($","^\\[[^\\]]+\\]\\([^\\)]*$"],i={shouldOpen:/\[([^\]]+)\]\(([^\)]*)\)/,canOpen:e=>r.some((n=>new RegExp(n).test(e))),shouldClose:()=>!0,canClose:()=>!1},o=(e,n)=>t=>{const s=a.get(e);if(!s)throw new Error(`No sequence parsers found for markdown element ${e}`);const r=s[n];return r instanceof RegExp?r.test(t):r(t)},a=new Map;a.set("Code",{shouldOpen:/^`[^`]$/,canOpen:/^`$/,shouldClose:/^`[\s\S]+$/,canClose:/^`$/}),a.set("CodeBlock",{shouldOpen:/^```.*\n$/,canOpen:/^`{1,3}.*$/,shouldClose:/^\n?```[\s\S]+$/,canClose:/^\n$|^\n?`{1,3}$/}),a.set("Heading1",{shouldOpen:/^\n*#{1} $/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),a.set("Heading2",{shouldOpen:/^\n*#{2} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),a.set("Heading3",{shouldOpen:/^\n*#{3} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),a.set("Heading4",{shouldOpen:/^\n*#{4} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),a.set("Heading5",{shouldOpen:/^\n*#{5} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),a.set("Heading6",{shouldOpen:/^\n*#{6} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),a.set("BoldAsterisk",{canOpen:/^\*{1,2}$/,shouldOpen:/^\*{2}[^(\s\n)]$/,canClose:/^\*{1,2}$/,shouldClose:/^\*{2}[\s\S]$/}),a.set("BoldUnderscore",{canOpen:/^_{1,2}$/,shouldOpen:/^_{2}[^(\s\n)]$/,canClose:/^_{1,2}$/,shouldClose:/^_{2}[\s\S]$/}),a.set("ItalicAsterisk",{canOpen:/^\*$/,shouldOpen:/^\*[^(\*\s\n)]$/,canClose:/^\*$/,shouldClose:/^\*[\s\S]$/}),a.set("ItalicUnderscore",{canOpen:/^_$/,shouldOpen:/^_[^(_\s\n)]$/,canClose:/^_$/,shouldClose:/^_[\s\S]$/}),a.set("Link",i),a.set("LineBreak",{shouldOpen:()=>!1,canOpen:()=>!1,shouldClose:()=>!1,canClose:()=>!1}),a.set("Root",{canOpen:()=>!1,shouldOpen:()=>!1,canClose:()=>!1,shouldClose:()=>!1}),a.set("Paragraph",{canOpen:/^\n+$/,shouldOpen:/^\n{2,}$/,canClose:/^\n$/,shouldClose:/^\n{2,}$/}),a.set("Blockquote",{shouldOpen:/^$/,canOpen:/^>$/,shouldClose:/^$/,canClose:/^$/}),a.set("OrderedList",{shouldOpen:/^$/,canOpen:/^\d+\. $/,shouldClose:/^\n$/,canClose:/^\n$/}),a.set("UnorderedList",{shouldOpen:/^$/,canOpen:/^\* $/,shouldClose:/^\n$/,canClose:/^\n$/}),a.set("HorizontalRule",{shouldOpen:/^$/,canOpen:/^---$/,shouldClose:/^$/,canClose:/^$/}),a.set("Image",{shouldOpen:/^$/,canOpen:/^!\[.*\]\(.*\)$/,shouldClose:/^$/,canClose:/^$/});var l=Object.defineProperty,d=(e,n,t)=>((e,n,t)=>n in e?l(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class c{constructor(e,n="none",t="none"){d(this,"__canLeadToClosingMarkdown",!1),d(this,"__latestSequenceProcessed",!1),d(this,"__markdownElement"),d(this,"__markdownThatShouldBeNested"),d(this,"__markdownThatShouldBeRenderedAtParentLevel"),d(this,"__possibleNestedMarkdownElements"),d(this,"__possibleYieldingMarkdownElements"),d(this,"__potentialNestedMarkdownElements",[]),d(this,"__potentialYieldingMarkdownElements",[]),d(this,"__sequence",""),d(this,"__shouldCloseCurrentMarkdown",!1),this.__markdownElement=e,this.__possibleNestedMarkdownElements="all"===n?s:"none"===n?[]:n,this.__possibleYieldingMarkdownElements="none"===t?[]:t}get canLeadToClosingOrNewMarkdown(){return this.__latestSequenceProcessed||this.processSequence(),this.__canLeadToClosingMarkdown||this.__potentialYieldingMarkdownElements.length>0||this.__potentialNestedMarkdownElements.length>0}get elementToCreateAtParentLevel(){return this.__latestSequenceProcessed||this.processSequence(),this.__markdownThatShouldBeRenderedAtParentLevel}get nestedElementToCreate(){return this.__latestSequenceProcessed||this.processSequence(),this.__markdownThatShouldBeNested}get sequence(){return this.__sequence}get shouldCloseCurrentMarkdown(){return this.__latestSequenceProcessed||this.processSequence(),this.__shouldCloseCurrentMarkdown}appendCharacter(e){this.__sequence+=e,this.__latestSequenceProcessed=!1}reset(){this.__sequence="",this.__latestSequenceProcessed=!1,this.__canLeadToClosingMarkdown=!1,this.__potentialYieldingMarkdownElements=[],this.__potentialNestedMarkdownElements=[],this.__shouldCloseCurrentMarkdown=!1,this.__markdownThatShouldBeRenderedAtParentLevel=void 0,this.__markdownThatShouldBeNested=void 0}processSequence(){if(this.__latestSequenceProcessed)return;const e=this.__sequence;this.__canLeadToClosingMarkdown=o(this.__markdownElement,"canClose")(e),this.__potentialYieldingMarkdownElements=this.__possibleYieldingMarkdownElements.filter((n=>o(n,"canOpen")(e))),this.__potentialNestedMarkdownElements=this.__possibleNestedMarkdownElements.filter((n=>o(n,"canOpen")(e))),this.__shouldCloseCurrentMarkdown=o(this.__markdownElement,"shouldClose")(e),this.__markdownThatShouldBeRenderedAtParentLevel=this.__possibleYieldingMarkdownElements.find((n=>o(n,"shouldOpen")(e))),this.__markdownThatShouldBeNested=this.__possibleNestedMarkdownElements.find((n=>o(n,"shouldOpen")(e)))}}var h=Object.defineProperty,u=(e,n,t)=>((e,n,t)=>n in e?h(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class p{constructor(e,n,t,s,r){u(this,"__parent"),u(this,"__element"),u(this,"__initialContent"),u(this,"__initialized",!1),u(this,"__last3Characters",""),u(this,"__markdownElementName"),u(this,"__openingSequence"),u(this,"__options",{}),u(this,"__parsingChild"),u(this,"__sequenceParser"),u(this,"__yielded",!1),this.__markdownElementName=n,this.__parent=e??void 0,this.__options=r||{},this.__openingSequence=t??void 0,this.__initialContent="Root"!==n?s??void 0:void 0}get domElement(){return this.__element}get markdownElementName(){return this.__markdownElementName}get parsingChild(){return this.__parsingChild}get removeWhenEmpty(){return!1}get sequenceParser(){if(this.yielded)throw new Error("Cannot access sequence parser of a yielded processor");return this.__sequenceParser||(this.__sequenceParser=new c(this.__markdownElementName,this.nestedMarkdownElements,this.yieldingMarkdownElements)),this.__sequenceParser}get yielded(){return this.__yielded}get initialContent(){return this.__initialContent}get last3Characters(){return this.__last3Characters}get markdownProcessorOptions(){return this.__options}get syntaxHighlighter(){return this.__options.syntaxHighlighter}init(){if(!this.__initialized&&(this.__initialized=!0,"Root"!==this.__markdownElementName?this.__element=this.createElement(this.__openingSequence):this.__element=void 0,this.__initialContent))for(let e=0;e<this.__initialContent.length;e++){const n=this.__initialContent[e];this.processCharacter(n)}}parsingChildYielded(e,n,t,s){this.__parsingChild===e&&(this.__parsingChild=void 0),n&&this.createAndAppendMarkdown(n,t),s&&this.processCharacter(s)}preProcessCharacter(e){this.__last3Characters.length<3?this.__last3Characters+=e:this.__last3Characters=this.__last3Characters.slice(1)+e}purgeSequence(){this.__parsingChild&&this.__parsingChild.purgeSequence(),this.__sequenceParser?.sequence&&(this.__element?.append(this.__sequenceParser.sequence),this.__sequenceParser.reset())}resetSequenceParser(){this.__sequenceParser?.reset(),this.__sequenceParser=new c(this.__markdownElementName,this.nestedMarkdownElements,this.yieldingMarkdownElements)}setParsingChild(e){if(this.__parsingChild&&"Root"!==this.__markdownElementName)throw new Error("Cannot set spawn child while parsing child");this.__parsingChild=e,e.domElement&&this.domElement?.append(e.domElement),e.__parent=this}yield(e,n){if(this.yielded)return;const t=this.__sequenceParser&&!this.__sequenceParser?.shouldCloseCurrentMarkdown?this.__sequenceParser?.sequence:void 0;this.__yielded=!0,this.__sequenceParser=void 0,this.__parsingChild&&(this.__parsingChild.yield(),this.__parsingChild=void 0),this.__element&&(this.__element.innerHTML=this.__element.innerHTML.trim(),this.removeWhenEmpty&&""===this.__element.innerHTML&&this.__element.remove(),this.__element=void 0),this.__parent&&(this.__parent.parsingChildYielded(this,e,t,n),this.__parent=void 0)}}class C extends p{constructor(e,n,t,s,r){super(e,n,t,s,r)}createAndAppendMarkdown(e,n){S(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}processCharacter(e){if(this.preProcessCharacter(e),this.parsingChild)return void this.parsingChild.processCharacter(e);const{sequenceDiscarded:t,discardedSequence:s,yielded:r}=this.processSequence(e);if(!r)if(n(this.last3Characters)&&(this.nestedMarkdownElements.includes("LineBreak")||"all"===this.nestedMarkdownElements))this.domElement?.innerHTML&&(this.domElement.innerHTML=this.domElement?.innerHTML.trim()),this.createAndAppendMarkdown("LineBreak"),this.sequenceParser.reset();else if(t){this.resetSequenceParser();this.processSequence(e).sequenceDiscarded?(this.sequenceParser.reset(),s&&this.domElement?.append(s)):s&&s.length>1&&this.domElement?.append(s.slice(0,-1))}}processSequence(e){if(this.sequenceParser.appendCharacter(e),this.sequenceParser.shouldCloseCurrentMarkdown)return this.yield(void 0,e),{sequenceDiscarded:!1,yielded:!0};if(this.sequenceParser.elementToCreateAtParentLevel)return this.yield(this.sequenceParser.elementToCreateAtParentLevel,e),{sequenceDiscarded:!1,yielded:!0};const n=this.sequenceParser.nestedElementToCreate;if(n)return S(n,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions),this.sequenceParser.reset(),{sequenceDiscarded:!1};if(this.sequenceParser.canLeadToClosingOrNewMarkdown)return{sequenceDiscarded:!1};const t=this.sequenceParser.sequence;return this.sequenceParser.reset(),{sequenceDiscarded:!0,discardedSequence:t}}}class _ extends C{constructor(e,n,t,s){super(e,"BoldAsterisk",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("strong")}}class m extends C{constructor(e,n,t,s){super(e,"BoldUnderscore",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("strong")}}class g extends C{constructor(e,n,t,s){super(e,"Code",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("code")}}var k=Object.defineProperty,w=(e,n,t)=>((e,n,t)=>n in e?k(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class E extends C{constructor(e,n,s,r={}){super(e,"CodeBlock",n??null,!s||t(s)?null:s,r),w(this,"codeContainer",null),w(this,"currentLineContainer",null),w(this,"language"),this.language=this.extractLanguageFromOpeningSequence(n)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return"none"}get yieldingMarkdownElements(){return"none"}createElement(){const e=document.createElement("div");return e.classList.add("code-block"),this.codeContainer=document.createElement("pre"),e.append(this.codeContainer),this.language&&(this.codeContainer.dataset.language=this.language),e}processCharacter(e){this.preProcessCharacter(e);const{sequenceDiscarded:n,discardedSequence:t,yielded:s}=this.processSequence(e);if(s)return;if(!n)return;if(t&&t.length>1&&this.codeContainer)for(let e=0;e<t.length-1;e++)this.appendCharacterToCodeBlock(t[e]);this.resetSequenceParser();this.processSequence(e).sequenceDiscarded&&(this.sequenceParser.reset(),this.appendCharacterToCodeBlock(e))}yield(e,n){if(this.highlightCurrentLine(),!1!==this.markdownProcessorOptions.showCodeBlockCopyButton){const e=this.__parent?.domElement;e&&(e.querySelectorAll(".code-block").forEach((e=>{if(!e.querySelector("pre"))return;if(e.previousElementSibling?.classList.contains("nlux-cpy-btn"))return;const n="Copy code block to clipboard",t=document.createElement("button");t.classList.add("nlux-cpy-btn"),t.setAttribute("aria-label",n),t.setAttribute("title",n),t.innerHTML='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M15 1.25H10.9436C9.10583 1.24998 7.65019 1.24997 6.51098 1.40314C5.33856 1.56076 4.38961 1.89288 3.64124 2.64124C2.89288 3.38961 2.56076 4.33856 2.40314 5.51098C2.24997 6.65019 2.24998 8.10582 2.25 9.94357V16C2.25 17.8722 3.62205 19.424 5.41551 19.7047C5.55348 20.4687 5.81753 21.1208 6.34835 21.6517C6.95027 22.2536 7.70814 22.5125 8.60825 22.6335C9.47522 22.75 10.5775 22.75 11.9451 22.75H15.0549C16.4225 22.75 17.5248 22.75 18.3918 22.6335C19.2919 22.5125 20.0497 22.2536 20.6517 21.6517C21.2536 21.0497 21.5125 20.2919 21.6335 19.3918C21.75 18.5248 21.75 17.4225 21.75 16.0549V10.9451C21.75 9.57754 21.75 8.47522 21.6335 7.60825C21.5125 6.70814 21.2536 5.95027 20.6517 5.34835C20.1208 4.81753 19.4687 4.55348 18.7047 4.41551C18.424 2.62205 16.8722 1.25 15 1.25ZM17.1293 4.27117C16.8265 3.38623 15.9876 2.75 15 2.75H11C9.09318 2.75 7.73851 2.75159 6.71085 2.88976C5.70476 3.02502 5.12511 3.27869 4.7019 3.7019C4.27869 4.12511 4.02502 4.70476 3.88976 5.71085C3.75159 6.73851 3.75 8.09318 3.75 10V16C3.75 16.9876 4.38624 17.8265 5.27117 18.1293C5.24998 17.5194 5.24999 16.8297 5.25 16.0549V10.9451C5.24998 9.57754 5.24996 8.47522 5.36652 7.60825C5.48754 6.70814 5.74643 5.95027 6.34835 5.34835C6.95027 4.74643 7.70814 4.48754 8.60825 4.36652C9.47522 4.24996 10.5775 4.24998 11.9451 4.25H15.0549C15.8297 4.24999 16.5194 4.24998 17.1293 4.27117ZM7.40901 6.40901C7.68577 6.13225 8.07435 5.9518 8.80812 5.85315C9.56347 5.75159 10.5646 5.75 12 5.75H15C16.4354 5.75 17.4365 5.75159 18.1919 5.85315C18.9257 5.9518 19.3142 6.13225 19.591 6.40901C19.8678 6.68577 20.0482 7.07435 20.1469 7.80812C20.2484 8.56347 20.25 9.56458 20.25 11V16C20.25 17.4354 20.2484 18.4365 20.1469 19.1919C20.0482 19.9257 19.8678 20.3142 19.591 20.591C19.3142 20.8678 18.9257 21.0482 18.1919 21.1469C17.4365 21.2484 16.4354 21.25 15 21.25H12C10.5646 21.25 9.56347 21.2484 8.80812 21.1469C8.07435 21.0482 7.68577 20.8678 7.40901 20.591C7.13225 20.3142 6.9518 19.9257 6.85315 19.1919C6.75159 18.4365 6.75 17.4354 6.75 16V11C6.75 9.56458 6.75159 8.56347 6.85315 7.80812C6.9518 7.07435 7.13225 6.68577 7.40901 6.40901Z" fill="currentColor"/></svg>',e.insertAdjacentElement("beforebegin",t)})),(e=>{e.querySelectorAll(".nlux-cpy-btn").forEach((e=>{if(!(e instanceof HTMLButtonElement))return;if("true"===e.dataset.clickListenerSet)return;let n=!1;const t=e.nextElementSibling;e.addEventListener("click",(()=>{if(n||!t)return;const s=t.innerText;navigator.clipboard.writeText(s??""),n=!0,e.classList.add("clicked"),setTimeout((()=>{n=!1,e.classList.remove("clicked")}),1e3)})),e.dataset.clickListenerSet="true"}))})(e))}super.yield(e,n),this.codeContainer=null,this.currentLineContainer=null}appendCharacterToCodeBlock(e){if(this.codeContainer)if("\n"!==e)this.currentLineContainer||(this.currentLineContainer=document.createElement("div"),this.codeContainer.append(this.currentLineContainer)),this.currentLineContainer.append(e);else if(this.currentLineContainer)this.currentLineContainer.innerHTML?this.highlightCurrentLine():this.currentLineContainer.innerHTML=" ",this.currentLineContainer=null;else if(this.codeContainer.innerHTML){const e=document.createElement("div");e.innerHTML=" ",this.codeContainer.append(e)}}extractLanguageFromOpeningSequence(e){if(!e)return;const n=e.match(/```(\w+)/);return Array.isArray(n)&&n.length>1?n[1]:void 0}highlightCurrentLine(){if(!this.domElement||!this.currentLineContainer)return;const e=this.currentLineContainer.innerText??this.currentLineContainer.innerHTML;if(this.syntaxHighlighter&&this.codeContainer&&e){const n=this.codeContainer.dataset.language;if(n){const t=this.syntaxHighlighter.createHighlighter({language:n,colorMode:"dark"}),s=this.syntaxHighlighter.highlightingClass(n);this.codeContainer.classList.contains(s)||this.codeContainer.classList.add(s),this.currentLineContainer.innerHTML=t(e,n)}}}}var q=Object.defineProperty,P=(e,n,t)=>((e,n,t)=>n in e?q(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class L extends C{constructor(e,n,t,s,r){super(n,f(e),t??null,s??null,r??null),P(this,"__headingLevel"),this.__headingLevel=e}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("h"+this.__headingLevel)}processCharacter(e){if(this.preProcessCharacter(e),this.parsingChild)return void this.parsingChild.processCharacter(e);const{sequenceDiscarded:n,discardedSequence:t,yielded:s}=this.processSequence(e);s||n&&t&&(/^\s+$/.test(t)||this.domElement?.append(t))}}const f=e=>{switch(e){case 1:return"Heading1";case 2:return"Heading2";case 3:return"Heading3";case 4:return"Heading4";case 5:return"Heading5";case 6:return"Heading6"}};class v extends C{constructor(e,n,t,s){super(e,"ItalicAsterisk",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("em")}}class M extends C{constructor(e,n,t,s){super(e,"ItalicUnderscore",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("em")}}class y extends p{constructor(e,n,t){super(e,"LineBreak",n??null,t??null,null)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return"none"}get yieldingMarkdownElements(){return"none"}createAndAppendMarkdown(e,n){S(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}createElement(){return document.createElement("br")}init(){super.init()}processCharacter(e){this.yield(void 0,e)}}var $=Object.defineProperty,O=(e,n,t)=>((e,n,t)=>n in e?$(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class A extends C{constructor(e,n,t,s={}){super(e,"Link",n??null,t??null,s),O(this,"linkContentProcessed","")}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code"]}get yieldingMarkdownElements(){return["Paragraph","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code"]}createElement(e){const n=/\[(.*[^\]])\]\((.*[^\)])\)/.exec(e),t=document.createElement("a");return t.textContent="",n&&n.length>=3&&n[2]&&(t.href=n[2]),"self"!==this.markdownProcessorOptions.markdownLinkTarget&&(t.target="_blank"),t}processCharacter(e){super.processCharacter(e),this.yielded||(this.linkContentProcessed+=e)}processSequence(e){if(!this.initialContent||this.initialContent===this.linkContentProcessed)return this.yield(void 0,e),{yielded:!0,sequenceDiscarded:!0,discardedSequence:e};this.sequenceParser.appendCharacter(e);const n=this.sequenceParser.nestedElementToCreate;if(n)return S(n,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions),this.sequenceParser.reset(),{sequenceDiscarded:!1};if(this.sequenceParser.canLeadToClosingOrNewMarkdown)return{sequenceDiscarded:!1};const t=this.sequenceParser.sequence;return this.sequenceParser.reset(),{sequenceDiscarded:!0,discardedSequence:t}}}class B extends C{constructor(e,n,t,s){super(e,"Paragraph",n??null,t??null,s??null)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return["LineBreak","Blockquote","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get removeWhenEmpty(){return!0}get yieldingMarkdownElements(){return["Heading1","Heading2","Heading3","Heading4","Heading5","Heading6","CodeBlock","UnorderedList","OrderedList","HorizontalRule","Image"]}createElement(){return document.createElement("p")}}const S=(e,n,t,s,r)=>{if("Paragraph"===e){const e=new B(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("LineBreak"===e){const e=new y(n,t,s);return e.init(),void n.setParsingChild(e)}if("Link"===e){const e=/\[([^\]]+)\]\(([^\)]*)\)/.exec(t),s=e&&e.length>=3?e[1]:"",i=new A(n,t,s,r);return i.init(),void n.setParsingChild(i)}if("Heading1"===e||"Heading2"===e||"Heading3"===e||"Heading4"===e||"Heading5"===e||"Heading6"===e){const i=parseInt(e[e.length-1]);if(Number.isNaN(i)||!Number.isFinite(i)||i<1||i>6)throw new Error("Invalid heading level");const o=new L(i,n,t,s,r);return o.init(),void n.setParsingChild(o)}if("Code"===e){const e=new g(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("CodeBlock"===e){const e=new E(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("BoldAsterisk"===e){const e=new _(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("BoldUnderscore"===e){const e=new m(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("ItalicAsterisk"===e){const e=new v(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("ItalicUnderscore"===e){const e=new M(n,t,s,r);return e.init(),void n.setParsingChild(e)}throw new Error("Unable to create child processor for markdown element "+e)};var H=Object.defineProperty,b=(e,n,t)=>((e,n,t)=>n in e?H(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class x extends p{constructor(e,n,t){super(null,"Root",n??null,null,t??null),b(this,"__rootDomElement"),this.__rootDomElement=e}get canExistAtRootLevel(){return!1}get domElement(){return this.__rootDomElement}get nestedMarkdownElements(){return"all"}get yieldingMarkdownElements(){return"none"}complete(){var e;this.parsingChild&&this.parsingChild.purgeSequence(),this.purgeSequence(),e=this.domElement?.lastChild,e instanceof HTMLElement&&"BR"===e.tagName&&this.domElement?.lastChild?.remove()}createAndAppendMarkdown(e,n){S(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}createElement(){throw new Error("Root cannot create an element")}processCharacter(e){this.preProcessCharacter(e),this.parsingChild?this.parsingChild.processCharacter(e):(this.sequenceParser.appendCharacter(e),this.processSequence(e))}yield(){super.yield(),this.__rootDomElement=void 0}processSequence(e){if(this.sequenceParser.shouldCloseCurrentMarkdown)return;const s=this.sequenceParser.nestedElementToCreate;if(s){S(s,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions);const n=this.parsingChild;return n&&!n.canExistAtRootLevel&&(S("Paragraph",this,void 0,void 0,this.markdownProcessorOptions),this.parsingChild?.setParsingChild(n)),void this.sequenceParser.reset()}this.sequenceParser.canLeadToClosingOrNewMarkdown||(n(this.last3Characters)?this.createAndAppendMarkdown("LineBreak"):e&&!t(e)&&(S("Paragraph",this,void 0,this.sequenceParser.sequence,this.markdownProcessorOptions),this.sequenceParser.reset()))}}const T=10;e.createMarkdownStreamParser=(e,n)=>{const t=((e,n,t)=>{const{streamingAnimationSpeed:s=T,markdownLinkTarget:r,showCodeBlockCopyButton:i,skipStreamingAnimation:o=!1,onComplete:a}=t||{},l=new x(e,void 0,{syntaxHighlighter:n,markdownLinkTarget:r,showCodeBlockCopyButton:i}),d=[];let c=0,h=!1,u="idle";const p=o?0:Math.max(s,0),C=s>0?s:20,_=Math.ceil(2e3/C),m=()=>{if("closed"===u)return;if("idle"===u&&(u="processing"),0===d.length)return void(!1===h&&c<_?(u="waitingForMoreCharacters",c+=1,setTimeout(m,C)):(l.processCharacter("\n"),l.complete(),l.yield(),u="closed",a?.()));u="processing",c=0;const e=d.shift();e&&l.processCharacter(e),setTimeout(m,p)};return{next:e=>{if(e){for(let n=0;n<e.length;n++)d.push(e[n]);"idle"===u&&m()}},complete:()=>{h=!0},error:()=>{}}})(e,n?.syntaxHighlighter,{markdownLinkTarget:n?.markdownLinkTarget,showCodeBlockCopyButton:n?.showCodeBlockCopyButton,skipStreamingAnimation:n?.skipStreamingAnimation,streamingAnimationSpeed:n?.streamingAnimationSpeed,onComplete:n?.onComplete});return{next(e){t.next(e)},complete(){t.complete()}}}}));
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlux/markdown"]={})}(this,(function(e){"use strict";const n=e=>/^[ \t]{2}\n$/m.test(e),t=e=>/^[ \t\n]{1}$/.test(e),s=["Root","Paragraph","LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Blockquote","Heading1","Heading2","Heading3","Heading4","Heading5","Heading6","OrderedList","UnorderedList","Code","CodeBlock","HorizontalRule","Link","Image"],r=["^\\[$","^\\[[^\\]]+$","^\\[[^\\]]+\\]$","^\\[[^\\]]+\\]\\($","^\\[[^\\]]+\\]\\([^\\)]*$"],i={shouldOpen:/\[([^\]]+)\]\(([^\)]*)\)/,canOpen:e=>r.some((n=>new RegExp(n).test(e))),shouldClose:()=>!0,canClose:()=>!1},o=(e,n)=>t=>{const s=a.get(e);if(!s)throw new Error(`No sequence parsers found for markdown element ${e}`);const r=s[n];return r instanceof RegExp?r.test(t):r(t)},a=new Map;a.set("Code",{shouldOpen:/^`[^`]$/,canOpen:/^`$/,shouldClose:/^`[\s\S]+$/,canClose:/^`$/}),a.set("CodeBlock",{shouldOpen:/^```.*\n$/,canOpen:/^`{1,3}.*$/,shouldClose:/^\n?```[\s\S]+$/,canClose:/^\n$|^\n?`{1,3}$/}),a.set("Heading1",{shouldOpen:/^\n*#{1} $/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),a.set("Heading2",{shouldOpen:/^\n*#{2} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),a.set("Heading3",{shouldOpen:/^\n*#{3} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),a.set("Heading4",{shouldOpen:/^\n*#{4} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),a.set("Heading5",{shouldOpen:/^\n*#{5} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),a.set("Heading6",{shouldOpen:/^\n*#{6} +$/,canOpen:/^\n*#*$/,shouldClose:/^\n$/,canClose:()=>!1}),a.set("BoldAsterisk",{canOpen:/^\*{1,2}$/,shouldOpen:/^\*{2}[^(\s\n)]$/,canClose:/^\*{1,2}$/,shouldClose:/^\*{2}[\s\S]$/}),a.set("BoldUnderscore",{canOpen:/^_{1,2}$/,shouldOpen:/^_{2}[^(\s\n)]$/,canClose:/^_{1,2}$/,shouldClose:/^_{2}[\s\S]$/}),a.set("ItalicAsterisk",{canOpen:/^\*$/,shouldOpen:/^\*[^(\*\s\n)]$/,canClose:/^\*$/,shouldClose:/^\*[\s\S]$/}),a.set("ItalicUnderscore",{canOpen:/^_$/,shouldOpen:/^_[^(_\s\n)]$/,canClose:/^_$/,shouldClose:/^_[\s\S]$/}),a.set("Link",i),a.set("LineBreak",{shouldOpen:()=>!1,canOpen:()=>!1,shouldClose:()=>!1,canClose:()=>!1}),a.set("Root",{canOpen:()=>!1,shouldOpen:()=>!1,canClose:()=>!1,shouldClose:()=>!1}),a.set("Paragraph",{canOpen:/^\n+$/,shouldOpen:/^\n{2,}$/,canClose:/^\n$/,shouldClose:/^\n{2,}$/}),a.set("Blockquote",{shouldOpen:/^$/,canOpen:/^>$/,shouldClose:/^$/,canClose:/^$/}),a.set("OrderedList",{shouldOpen:/^$/,canOpen:/^\d+\. $/,shouldClose:/^\n$/,canClose:/^\n$/}),a.set("UnorderedList",{shouldOpen:/^$/,canOpen:/^\* $/,shouldClose:/^\n$/,canClose:/^\n$/}),a.set("HorizontalRule",{shouldOpen:/^$/,canOpen:/^---$/,shouldClose:/^$/,canClose:/^$/}),a.set("Image",{shouldOpen:/^$/,canOpen:/^!\[.*\]\(.*\)$/,shouldClose:/^$/,canClose:/^$/});var l=Object.defineProperty,d=(e,n,t)=>((e,n,t)=>n in e?l(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class c{constructor(e,n="none",t="none"){d(this,"__canLeadToClosingMarkdown",!1),d(this,"__latestSequenceProcessed",!1),d(this,"__markdownElement"),d(this,"__markdownThatShouldBeNested"),d(this,"__markdownThatShouldBeRenderedAtParentLevel"),d(this,"__possibleNestedMarkdownElements"),d(this,"__possibleYieldingMarkdownElements"),d(this,"__potentialNestedMarkdownElements",[]),d(this,"__potentialYieldingMarkdownElements",[]),d(this,"__sequence",""),d(this,"__shouldCloseCurrentMarkdown",!1),this.__markdownElement=e,this.__possibleNestedMarkdownElements="all"===n?s:"none"===n?[]:n,this.__possibleYieldingMarkdownElements="none"===t?[]:t}get canLeadToClosingOrNewMarkdown(){return this.__latestSequenceProcessed||this.processSequence(),this.__canLeadToClosingMarkdown||this.__potentialYieldingMarkdownElements.length>0||this.__potentialNestedMarkdownElements.length>0}get elementToCreateAtParentLevel(){return this.__latestSequenceProcessed||this.processSequence(),this.__markdownThatShouldBeRenderedAtParentLevel}get nestedElementToCreate(){return this.__latestSequenceProcessed||this.processSequence(),this.__markdownThatShouldBeNested}get sequence(){return this.__sequence}get shouldCloseCurrentMarkdown(){return this.__latestSequenceProcessed||this.processSequence(),this.__shouldCloseCurrentMarkdown}appendCharacter(e){this.__sequence+=e,this.__latestSequenceProcessed=!1}reset(){this.__sequence="",this.__latestSequenceProcessed=!1,this.__canLeadToClosingMarkdown=!1,this.__potentialYieldingMarkdownElements=[],this.__potentialNestedMarkdownElements=[],this.__shouldCloseCurrentMarkdown=!1,this.__markdownThatShouldBeRenderedAtParentLevel=void 0,this.__markdownThatShouldBeNested=void 0}processSequence(){if(this.__latestSequenceProcessed)return;const e=this.__sequence;this.__canLeadToClosingMarkdown=o(this.__markdownElement,"canClose")(e),this.__potentialYieldingMarkdownElements=this.__possibleYieldingMarkdownElements.filter((n=>o(n,"canOpen")(e))),this.__potentialNestedMarkdownElements=this.__possibleNestedMarkdownElements.filter((n=>o(n,"canOpen")(e))),this.__shouldCloseCurrentMarkdown=o(this.__markdownElement,"shouldClose")(e),this.__markdownThatShouldBeRenderedAtParentLevel=this.__possibleYieldingMarkdownElements.find((n=>o(n,"shouldOpen")(e))),this.__markdownThatShouldBeNested=this.__possibleNestedMarkdownElements.find((n=>o(n,"shouldOpen")(e)))}}var h=Object.defineProperty,u=(e,n,t)=>((e,n,t)=>n in e?h(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class p{constructor(e,n,t,s,r){u(this,"__parent"),u(this,"__element"),u(this,"__initialContent"),u(this,"__initialized",!1),u(this,"__last3Characters",""),u(this,"__markdownElementName"),u(this,"__openingSequence"),u(this,"__options",{}),u(this,"__parsingChild"),u(this,"__sequenceParser"),u(this,"__yielded",!1),this.__markdownElementName=n,this.__parent=e??void 0,this.__options=r||{},this.__openingSequence=t??void 0,this.__initialContent="Root"!==n?s??void 0:void 0}get domElement(){return this.__element}get markdownElementName(){return this.__markdownElementName}get parsingChild(){return this.__parsingChild}get removeWhenEmpty(){return!1}get sequenceParser(){if(this.yielded)throw new Error("Cannot access sequence parser of a yielded processor");return this.__sequenceParser||(this.__sequenceParser=new c(this.__markdownElementName,this.nestedMarkdownElements,this.yieldingMarkdownElements)),this.__sequenceParser}get yielded(){return this.__yielded}get initialContent(){return this.__initialContent}get last3Characters(){return this.__last3Characters}get markdownProcessorOptions(){return this.__options}get syntaxHighlighter(){return this.__options.syntaxHighlighter}init(){if(!this.__initialized&&(this.__initialized=!0,"Root"!==this.__markdownElementName?this.__element=this.createElement(this.__openingSequence):this.__element=void 0,this.__initialContent))for(let e=0;e<this.__initialContent.length;e++){const n=this.__initialContent[e];this.processCharacter(n)}}parsingChildYielded(e,n,t,s){this.__parsingChild===e&&(this.__parsingChild=void 0),n&&this.createAndAppendMarkdown(n,t),s&&this.processCharacter(s)}preProcessCharacter(e){this.__last3Characters.length<3?this.__last3Characters+=e:this.__last3Characters=this.__last3Characters.slice(1)+e}purgeSequence(){this.__parsingChild&&this.__parsingChild.purgeSequence(),this.__sequenceParser?.sequence&&(this.__element?.append(this.__sequenceParser.sequence),this.__sequenceParser.reset())}resetSequenceParser(){this.__sequenceParser?.reset(),this.__sequenceParser=new c(this.__markdownElementName,this.nestedMarkdownElements,this.yieldingMarkdownElements)}setParsingChild(e){if(this.__parsingChild&&"Root"!==this.__markdownElementName)throw new Error("Cannot set spawn child while parsing child");this.__parsingChild=e,e.domElement&&this.domElement?.append(e.domElement),e.__parent=this}yield(e,n){if(this.yielded)return;const t=this.__sequenceParser&&!this.__sequenceParser?.shouldCloseCurrentMarkdown?this.__sequenceParser?.sequence:void 0;this.__yielded=!0,this.__sequenceParser=void 0,this.__parsingChild&&(this.__parsingChild.yield(),this.__parsingChild=void 0),this.__element&&(this.__element.innerHTML=this.__element.innerHTML.trim(),this.removeWhenEmpty&&""===this.__element.innerHTML&&this.__element.remove(),this.__element=void 0),this.__parent&&(this.__parent.parsingChildYielded(this,e,t,n),this.__parent=void 0)}}class _ extends p{constructor(e,n,t,s,r){super(e,n,t,s,r)}createAndAppendMarkdown(e,n){S(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}processCharacter(e){if(this.preProcessCharacter(e),this.parsingChild)return void this.parsingChild.processCharacter(e);const{sequenceDiscarded:t,discardedSequence:s,yielded:r}=this.processSequence(e);if(!r)if(n(this.last3Characters)&&(this.nestedMarkdownElements.includes("LineBreak")||"all"===this.nestedMarkdownElements))this.domElement?.innerHTML&&(this.domElement.innerHTML=this.domElement?.innerHTML.trim()),this.createAndAppendMarkdown("LineBreak"),this.sequenceParser.reset();else if(t){this.resetSequenceParser();this.processSequence(e).sequenceDiscarded?(this.sequenceParser.reset(),s&&this.domElement?.append(s)):s&&s.length>1&&this.domElement?.append(s.slice(0,-1))}}processSequence(e){if(this.sequenceParser.appendCharacter(e),this.sequenceParser.shouldCloseCurrentMarkdown)return this.yield(void 0,e),{sequenceDiscarded:!1,yielded:!0};if(this.sequenceParser.elementToCreateAtParentLevel)return this.yield(this.sequenceParser.elementToCreateAtParentLevel,e),{sequenceDiscarded:!1,yielded:!0};const n=this.sequenceParser.nestedElementToCreate;if(n)return S(n,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions),this.sequenceParser.reset(),{sequenceDiscarded:!1};if(this.sequenceParser.canLeadToClosingOrNewMarkdown)return{sequenceDiscarded:!1};const t=this.sequenceParser.sequence;return this.sequenceParser.reset(),{sequenceDiscarded:!0,discardedSequence:t}}}class m extends _{constructor(e,n,t,s){super(e,"BoldAsterisk",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("strong")}}class g extends _{constructor(e,n,t,s){super(e,"BoldUnderscore",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("strong")}}class C extends _{constructor(e,n,t,s){super(e,"Code",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("code")}}var k=Object.defineProperty,w=(e,n,t)=>((e,n,t)=>n in e?k(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,"symbol"!=typeof n?n+"":n,t);class E extends _{constructor(e,n,s,r={}){super(e,"CodeBlock",n??null,!s||t(s)?null:s,r),w(this,"codeContainer",null),w(this,"currentLineContainer",null),w(this,"language"),this.language=this.extractLanguageFromOpeningSequence(n)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return"none"}get yieldingMarkdownElements(){return"none"}createElement(){const e=document.createElement("div");return e.classList.add("code-block"),this.codeContainer=document.createElement("pre"),e.append(this.codeContainer),this.language&&(this.codeContainer.dataset.language=this.language),e}processCharacter(e){this.preProcessCharacter(e);const{sequenceDiscarded:n,discardedSequence:t,yielded:s}=this.processSequence(e);if(s)return;if(!n)return;if(t&&t.length>1&&this.codeContainer)for(let e=0;e<t.length-1;e++)this.appendCharacterToCodeBlock(t[e]);this.resetSequenceParser();this.processSequence(e).sequenceDiscarded&&(this.sequenceParser.reset(),this.appendCharacterToCodeBlock(e))}yield(e,n){if(this.highlightCurrentLine(),!1!==this.markdownProcessorOptions.showCodeBlockCopyButton){const e=this.__parent?.domElement;e&&(e.querySelectorAll(".code-block").forEach((e=>{if(!e.querySelector("pre"))return;if(e.previousElementSibling?.classList.contains("nlux-cpy-btn"))return;const n="Copy code block to clipboard",t=document.createElement("button");t.classList.add("nlux-cpy-btn"),t.setAttribute("aria-label",n),t.setAttribute("title",n);const s=document.createElement("span");s.classList.add("icon-copy"),t.appendChild(s),e.insertAdjacentElement("beforebegin",t)})),(e=>{e.querySelectorAll(".nlux-cpy-btn").forEach((e=>{if(!(e instanceof HTMLButtonElement))return;if("true"===e.dataset.clickListenerSet)return;let n=!1;const t=e.nextElementSibling;e.addEventListener("click",(()=>{if(n||!t)return;const s=t.innerText;navigator.clipboard.writeText(s??""),n=!0,e.classList.add("clicked"),setTimeout((()=>{n=!1,e.classList.remove("clicked")}),1e3)})),e.dataset.clickListenerSet="true"}))})(e))}super.yield(e,n),this.codeContainer=null,this.currentLineContainer=null}appendCharacterToCodeBlock(e){if(this.codeContainer)if("\n"!==e)this.currentLineContainer||(this.currentLineContainer=document.createElement("div"),this.codeContainer.append(this.currentLineContainer)),this.currentLineContainer.append(e);else if(this.currentLineContainer)this.currentLineContainer.innerHTML?this.highlightCurrentLine():this.currentLineContainer.innerHTML=" ",this.currentLineContainer=null;else if(this.codeContainer.innerHTML){const e=document.createElement("div");e.innerHTML=" ",this.codeContainer.append(e)}}extractLanguageFromOpeningSequence(e){if(!e)return;const n=e.match(/```(\w+)/);return Array.isArray(n)&&n.length>1?n[1]:void 0}highlightCurrentLine(){if(!this.domElement||!this.currentLineContainer)return;const e=this.currentLineContainer.innerText??this.currentLineContainer.innerHTML;if(this.syntaxHighlighter&&this.codeContainer&&e){const n=this.codeContainer.dataset.language;if(n){const t=this.syntaxHighlighter.createHighlighter({language:n,colorMode:"dark"}),s=this.syntaxHighlighter.highlightingClass(n);this.codeContainer.classList.contains(s)||this.codeContainer.classList.add(s),this.currentLineContainer.innerHTML=t(e,n)}}}}var q=Object.defineProperty,P=(e,n,t)=>((e,n,t)=>n in e?q(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class L extends _{constructor(e,n,t,s,r){super(n,f(e),t??null,s??null,r??null),P(this,"__headingLevel"),this.__headingLevel=e}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("h"+this.__headingLevel)}processCharacter(e){if(this.preProcessCharacter(e),this.parsingChild)return void this.parsingChild.processCharacter(e);const{sequenceDiscarded:n,discardedSequence:t,yielded:s}=this.processSequence(e);s||n&&t&&(/^\s+$/.test(t)||this.domElement?.append(t))}}const f=e=>{switch(e){case 1:return"Heading1";case 2:return"Heading2";case 3:return"Heading3";case 4:return"Heading4";case 5:return"Heading5";case 6:return"Heading6"}};class v extends _{constructor(e,n,t,s){super(e,"ItalicAsterisk",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("em")}}class M extends _{constructor(e,n,t,s){super(e,"ItalicUnderscore",n??null,t??null,s??null)}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["LineBreak","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get yieldingMarkdownElements(){return"none"}createElement(){return document.createElement("em")}}class y extends p{constructor(e,n,t){super(e,"LineBreak",n??null,t??null,null)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return"none"}get yieldingMarkdownElements(){return"none"}createAndAppendMarkdown(e,n){S(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}createElement(){return document.createElement("br")}init(){super.init()}processCharacter(e){this.yield(void 0,e)}}var $=Object.defineProperty,O=(e,n,t)=>((e,n,t)=>n in e?$(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class A extends _{constructor(e,n,t,s={}){super(e,"Link",n??null,t??null,s),O(this,"linkContentProcessed","")}get canExistAtRootLevel(){return!1}get nestedMarkdownElements(){return["BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code"]}get yieldingMarkdownElements(){return["Paragraph","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code"]}createElement(e){const n=/\[(.*[^\]])\]\((.*[^\)])\)/.exec(e),t=document.createElement("a");return t.textContent="",n&&n.length>=3&&n[2]&&(t.href=n[2]),"self"!==this.markdownProcessorOptions.markdownLinkTarget&&(t.target="_blank"),t}processCharacter(e){super.processCharacter(e),this.yielded||(this.linkContentProcessed+=e)}processSequence(e){if(!this.initialContent||this.initialContent===this.linkContentProcessed)return this.yield(void 0,e),{yielded:!0,sequenceDiscarded:!0,discardedSequence:e};this.sequenceParser.appendCharacter(e);const n=this.sequenceParser.nestedElementToCreate;if(n)return S(n,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions),this.sequenceParser.reset(),{sequenceDiscarded:!1};if(this.sequenceParser.canLeadToClosingOrNewMarkdown)return{sequenceDiscarded:!1};const t=this.sequenceParser.sequence;return this.sequenceParser.reset(),{sequenceDiscarded:!0,discardedSequence:t}}}class B extends _{constructor(e,n,t,s){super(e,"Paragraph",n??null,t??null,s??null)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return["LineBreak","Blockquote","BoldAsterisk","ItalicAsterisk","BoldUnderscore","ItalicUnderscore","Code","Link"]}get removeWhenEmpty(){return!0}get yieldingMarkdownElements(){return["Heading1","Heading2","Heading3","Heading4","Heading5","Heading6","CodeBlock","UnorderedList","OrderedList","HorizontalRule","Image"]}createElement(){return document.createElement("p")}}const S=(e,n,t,s,r)=>{if("Paragraph"===e){const e=new B(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("LineBreak"===e){const e=new y(n,t,s);return e.init(),void n.setParsingChild(e)}if("Link"===e){const e=/\[([^\]]+)\]\(([^\)]*)\)/.exec(t),s=e&&e.length>=3?e[1]:"",i=new A(n,t,s,r);return i.init(),void n.setParsingChild(i)}if("Heading1"===e||"Heading2"===e||"Heading3"===e||"Heading4"===e||"Heading5"===e||"Heading6"===e){const i=parseInt(e[e.length-1]);if(Number.isNaN(i)||!Number.isFinite(i)||i<1||i>6)throw new Error("Invalid heading level");const o=new L(i,n,t,s,r);return o.init(),void n.setParsingChild(o)}if("Code"===e){const e=new C(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("CodeBlock"===e){const e=new E(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("BoldAsterisk"===e){const e=new m(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("BoldUnderscore"===e){const e=new g(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("ItalicAsterisk"===e){const e=new v(n,t,s,r);return e.init(),void n.setParsingChild(e)}if("ItalicUnderscore"===e){const e=new M(n,t,s,r);return e.init(),void n.setParsingChild(e)}throw new Error("Unable to create child processor for markdown element "+e)};var b=Object.defineProperty,H=(e,n,t)=>((e,n,t)=>n in e?b(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t)(e,n+"",t);class x extends p{constructor(e,n,t){super(null,"Root",n??null,null,t??null),H(this,"__rootDomElement"),this.__rootDomElement=e}get canExistAtRootLevel(){return!1}get domElement(){return this.__rootDomElement}get nestedMarkdownElements(){return"all"}get yieldingMarkdownElements(){return"none"}complete(){var e;this.parsingChild&&this.parsingChild.purgeSequence(),this.purgeSequence(),e=this.domElement?.lastChild,e instanceof HTMLElement&&"BR"===e.tagName&&this.domElement?.lastChild?.remove()}createAndAppendMarkdown(e,n){S(e,this,n,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}createElement(){throw new Error("Root cannot create an element")}processCharacter(e){this.preProcessCharacter(e),this.parsingChild?this.parsingChild.processCharacter(e):(this.sequenceParser.appendCharacter(e),this.processSequence(e))}yield(){super.yield(),this.__rootDomElement=void 0}processSequence(e){if(this.sequenceParser.shouldCloseCurrentMarkdown)return;const s=this.sequenceParser.nestedElementToCreate;if(s){S(s,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions);const n=this.parsingChild;return n&&!n.canExistAtRootLevel&&(S("Paragraph",this,void 0,void 0,this.markdownProcessorOptions),this.parsingChild?.setParsingChild(n)),void this.sequenceParser.reset()}this.sequenceParser.canLeadToClosingOrNewMarkdown||(n(this.last3Characters)?this.createAndAppendMarkdown("LineBreak"):e&&!t(e)&&(S("Paragraph",this,void 0,this.sequenceParser.sequence,this.markdownProcessorOptions),this.sequenceParser.reset()))}}const T=10;e.createMarkdownStreamParser=(e,n)=>{const t=((e,n,t)=>{const{streamingAnimationSpeed:s=T,markdownLinkTarget:r,showCodeBlockCopyButton:i,skipStreamingAnimation:o=!1,onComplete:a}=t||{},l=new x(e,void 0,{syntaxHighlighter:n,markdownLinkTarget:r,showCodeBlockCopyButton:i}),d=[];let c=0,h=!1,u="idle";const p=o?0:Math.max(s,0),_=s>0?s:20,m=Math.ceil(2e3/_),g=()=>{if("closed"===u)return;if("idle"===u&&(u="processing"),0===d.length)return void(!1===h&&c<m?(u="waitingForMoreCharacters",c+=1,setTimeout(g,_)):(l.processCharacter("\n"),l.complete(),l.yield(),u="closed",a?.()));u="processing",c=0;const e=d.shift();e&&l.processCharacter(e),setTimeout(g,p)};return{next:e=>{if(e){for(let n=0;n<e.length;n++)d.push(e[n]);"idle"===u&&g()}},complete:()=>{h=!0},error:()=>{}}})(e,n?.syntaxHighlighter,{markdownLinkTarget:n?.markdownLinkTarget,showCodeBlockCopyButton:n?.showCodeBlockCopyButton,skipStreamingAnimation:n?.skipStreamingAnimation,streamingAnimationSpeed:n?.streamingAnimationSpeed,onComplete:n?.onComplete});return{next(e){t.next(e)},complete(){t.complete()}}}}));
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc