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.2.11-beta to 2.2.12-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 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()}}};
"use strict";const e=e=>/^[ \t]{2}\n$/m.test(e),t=e=>/^[ \t\n]{1}$/.test(e),n=["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((t=>new RegExp(t).test(e))),shouldClose:()=>!0,canClose:()=>!1},i=(e,t)=>n=>{const s=o.get(e);if(!s)throw new Error(`No sequence parsers found for markdown element ${e}`);const r=s[t];return r instanceof RegExp?r.test(n):r(n)},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,t,n)=>((e,t,n)=>t in e?a(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,"symbol"!=typeof t?t+"":t,n);class d{constructor(e,t="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"===t?n:"none"===t?[]:t,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((t=>i(t,"canOpen")(e))),this.__potentialNestedMarkdownElements=this.__possibleNestedMarkdownElements.filter((t=>i(t,"canOpen")(e))),this.__shouldCloseCurrentMarkdown=i(this.__markdownElement,"shouldClose")(e),this.__markdownThatShouldBeRenderedAtParentLevel=this.__possibleYieldingMarkdownElements.find((t=>i(t,"shouldOpen")(e))),this.__markdownThatShouldBeNested=this.__possibleNestedMarkdownElements.find((t=>i(t,"shouldOpen")(e)))}}var c=Object.defineProperty,h=(e,t,n)=>((e,t,n)=>t in e?c(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,"symbol"!=typeof t?t+"":t,n);class u{constructor(e,t,n,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=t,this.__parent=e??void 0,this.__options=r||{},this.__openingSequence=n??void 0,this.__initialContent="Root"!==t?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}get htmlSanitizer(){return this.__options.htmlSanitizer??(e=>e)}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 t=this.__initialContent[e];this.processCharacter(t)}}parsingChildYielded(e,t,n,s){this.__parsingChild===e&&(this.__parsingChild=void 0),t&&this.createAndAppendMarkdown(t,n),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,t){if(this.yielded)return;const n=this.__sequenceParser&&!this.__sequenceParser?.shouldCloseCurrentMarkdown?this.__sequenceParser?.sequence:void 0;if(this.__yielded=!0,this.__sequenceParser=void 0,this.__parsingChild&&(this.__parsingChild.yield(),this.__parsingChild=void 0),this.__element){const e=this.htmlSanitizer,t=this.__element.innerHTML.trim();this.__element.innerHTML=e?e(t):t,this.removeWhenEmpty&&""===this.__element.innerHTML&&this.__element.remove(),this.__element=void 0}this.__parent&&(this.__parent.parsingChildYielded(this,e,n,t),this.__parent=void 0)}}class p extends u{constructor(e,t,n,s,r){super(e,t,n,s,r)}createAndAppendMarkdown(e,t){A(e,this,t,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}processCharacter(t){if(this.preProcessCharacter(t),this.parsingChild)return void this.parsingChild.processCharacter(t);const{sequenceDiscarded:n,discardedSequence:s,yielded:r}=this.processSequence(t);if(!r)if(e(this.last3Characters)&&(this.nestedMarkdownElements.includes("LineBreak")||"all"===this.nestedMarkdownElements)){if(this.domElement?.innerHTML){const e=this.markdownProcessorOptions?.htmlSanitizer,t=this.domElement.innerHTML.trim();this.domElement.innerHTML=e?e(t):t}this.createAndAppendMarkdown("LineBreak"),this.sequenceParser.reset()}else if(n){this.resetSequenceParser();this.processSequence(t).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 t=this.sequenceParser.nestedElementToCreate;if(t)return A(t,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions),this.sequenceParser.reset(),{sequenceDiscarded:!1};if(this.sequenceParser.canLeadToClosingOrNewMarkdown)return{sequenceDiscarded:!1};const n=this.sequenceParser.sequence;return this.sequenceParser.reset(),{sequenceDiscarded:!0,discardedSequence:n}}}class m extends p{constructor(e,t,n,s){super(e,"BoldAsterisk",t??null,n??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 p{constructor(e,t,n,s){super(e,"BoldUnderscore",t??null,n??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,t,n,s){super(e,"Code",t??null,n??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,t,n)=>((e,t,n)=>t in e?C(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,"symbol"!=typeof t?t+"":t,n);class w extends p{constructor(e,n,s,r={}){super(e,"CodeBlock",n??null,!s||t(s)?null:s,r),k(this,"codeContainer",null),k(this,"currentLineContainer",null),k(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:t,discardedSequence:n,yielded:s}=this.processSequence(e);if(s)return;if(!t)return;if(n&&n.length>1&&this.codeContainer)for(let e=0;e<n.length-1;e++)this.appendCharacterToCodeBlock(n[e]);this.resetSequenceParser();this.processSequence(e).sequenceDiscarded&&(this.sequenceParser.reset(),this.appendCharacterToCodeBlock(e))}yield(e,t){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 t="Copy code block to clipboard",n=document.createElement("button");n.classList.add("nlux-cpy-btn"),n.setAttribute("aria-label",t),n.setAttribute("title",t);const s=document.createElement("span");s.classList.add("icon-copy"),n.appendChild(s),e.insertAdjacentElement("beforebegin",n)})),(e=>{e.querySelectorAll(".nlux-cpy-btn").forEach((e=>{if(!(e instanceof HTMLButtonElement))return;if("true"===e.dataset.clickListenerSet)return;let t=!1;const n=e.nextElementSibling;e.addEventListener("click",(()=>{if(t||!n)return;const s=n.innerText;navigator.clipboard.writeText(s??""),t=!0,e.classList.add("clicked"),setTimeout((()=>{t=!1,e.classList.remove("clicked")}),1e3)})),e.dataset.clickListenerSet="true"}))})(e))}super.yield(e,t),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.replaceChildren(" "),this.currentLineContainer=null;else if(this.codeContainer.innerHTML){const e=document.createElement("div");e.replaceChildren(" "),this.codeContainer.append(e)}}extractLanguageFromOpeningSequence(e){if(!e)return;const t=e.match(/```(\w+)/);return Array.isArray(t)&&t.length>1?t[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 t=this.codeContainer.dataset.language;if(t){const n=this.syntaxHighlighter.createHighlighter({language:t,colorMode:"dark"}),s=this.syntaxHighlighter.highlightingClass(t);this.codeContainer.classList.contains(s)||this.codeContainer.classList.add(s),this.currentLineContainer.innerHTML=n(e,t)}}}}var E=Object.defineProperty,q=(e,t,n)=>((e,t,n)=>t in e?E(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,t+"",n);class P extends p{constructor(e,t,n,s,r){super(t,L(e),n??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:t,discardedSequence:n,yielded:s}=this.processSequence(e);s||t&&n&&(/^\s+$/.test(n)||this.domElement?.append(n))}}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 f extends p{constructor(e,t,n,s){super(e,"ItalicAsterisk",t??null,n??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 v extends p{constructor(e,t,n,s){super(e,"ItalicUnderscore",t??null,n??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,t,n){super(e,"LineBreak",t??null,n??null,null)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return"none"}get yieldingMarkdownElements(){return"none"}createAndAppendMarkdown(e,t){A(e,this,t,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}createElement(){return document.createElement("br")}init(){super.init()}processCharacter(e){this.yield(void 0,e)}}var S=Object.defineProperty,y=(e,t,n)=>((e,t,n)=>t in e?S(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,t+"",n);class $ extends p{constructor(e,t,n,s={}){super(e,"Link",t??null,n??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 t=/\[(.*[^\]])\]\((.*[^\)])\)/.exec(e),n=document.createElement("a");return n.textContent="",t&&t.length>=3&&t[2]&&(n.href=t[2]),"self"!==this.markdownProcessorOptions.markdownLinkTarget&&(n.target="_blank"),n}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 t=this.sequenceParser.nestedElementToCreate;if(t)return A(t,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions),this.sequenceParser.reset(),{sequenceDiscarded:!1};if(this.sequenceParser.canLeadToClosingOrNewMarkdown)return{sequenceDiscarded:!1};const n=this.sequenceParser.sequence;return this.sequenceParser.reset(),{sequenceDiscarded:!0,discardedSequence:n}}}class O extends p{constructor(e,t,n,s){super(e,"Paragraph",t??null,n??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 A=(e,t,n,s,r)=>{if("Paragraph"===e){const e=new O(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("LineBreak"===e){const e=new M(t,n,s);return e.init(),void t.setParsingChild(e)}if("Link"===e){const e=/\[([^\]]+)\]\(([^\)]*)\)/.exec(n),s=e&&e.length>=3?e[1]:"",i=new $(t,n,s,r);return i.init(),void t.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,t,n,s,r);return o.init(),void t.setParsingChild(o)}if("Code"===e){const e=new g(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("CodeBlock"===e){const e=new w(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("BoldAsterisk"===e){const e=new m(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("BoldUnderscore"===e){const e=new _(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("ItalicAsterisk"===e){const e=new f(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("ItalicUnderscore"===e){const e=new v(t,n,s,r);return e.init(),void t.setParsingChild(e)}throw new Error("Unable to create child processor for markdown element "+e)};var B=Object.defineProperty,H=(e,t,n)=>((e,t,n)=>t in e?B(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,t+"",n);class b extends u{constructor(e,t,n){super(null,"Root",t??null,null,n??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,t){A(e,this,t,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(n){if(this.sequenceParser.shouldCloseCurrentMarkdown)return;const s=this.sequenceParser.nestedElementToCreate;if(s){A(s,this,this.sequenceParser.sequence,n,this.markdownProcessorOptions);const e=this.parsingChild;return e&&!e.canExistAtRootLevel&&(A("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"):n&&!t(n)&&(A("Paragraph",this,void 0,this.sequenceParser.sequence,this.markdownProcessorOptions),this.sequenceParser.reset()))}}const x=10;exports.createMarkdownStreamParser=(e,t)=>{const n=((e,t)=>{const{syntaxHighlighter:n,htmlSanitizer:s,streamingAnimationSpeed:r=x,markdownLinkTarget:i,showCodeBlockCopyButton:o,skipStreamingAnimation:a=!1,onComplete:l}=t||{},d=new b(e,void 0,{syntaxHighlighter:n,htmlSanitizer:s,markdownLinkTarget:i,showCodeBlockCopyButton:o}),c=[];let h=0,u=!1,p="idle";const m=a?0:Math.max(r,0),_=r>0?r:20,g=Math.ceil(2e3/_),C=()=>{if("closed"===p)return;if("idle"===p&&(p="processing"),0===c.length)return void(!1===u&&h<g?(p="waitingForMoreCharacters",h+=1,setTimeout(C,_)):(d.processCharacter("\n"),d.complete(),d.yield(),t?.htmlSanitizer&&(e.innerHTML=t.htmlSanitizer(e.innerHTML)),p="closed",l?.()));p="processing",h=0;const n=c.shift();n&&d.processCharacter(n),setTimeout(C,m)};return{next:e=>{if(e){for(let t=0;t<e.length;t++)c.push(e[t]);"idle"===p&&C()}},complete:()=>{u=!0},error:()=>{}}})(e,{syntaxHighlighter:t?.syntaxHighlighter,htmlSanitizer:t?.htmlSanitizer,markdownLinkTarget:t?.markdownLinkTarget,showCodeBlockCopyButton:t?.showCodeBlockCopyButton,skipStreamingAnimation:t?.skipStreamingAnimation,streamingAnimationSpeed:t?.streamingAnimationSpeed,onComplete:t?.onComplete});return{next(e){n.next(e)},complete(){n.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 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};
const e=e=>/^[ \t]{2}\n$/m.test(e),t=e=>/^[ \t\n]{1}$/.test(e),n=["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((t=>new RegExp(t).test(e))),shouldClose:()=>!0,canClose:()=>!1},i=(e,t)=>n=>{const s=o.get(e);if(!s)throw new Error(`No sequence parsers found for markdown element ${e}`);const r=s[t];return r instanceof RegExp?r.test(n):r(n)},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,t,n)=>((e,t,n)=>t in e?a(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,"symbol"!=typeof t?t+"":t,n);class d{constructor(e,t="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"===t?n:"none"===t?[]:t,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((t=>i(t,"canOpen")(e))),this.__potentialNestedMarkdownElements=this.__possibleNestedMarkdownElements.filter((t=>i(t,"canOpen")(e))),this.__shouldCloseCurrentMarkdown=i(this.__markdownElement,"shouldClose")(e),this.__markdownThatShouldBeRenderedAtParentLevel=this.__possibleYieldingMarkdownElements.find((t=>i(t,"shouldOpen")(e))),this.__markdownThatShouldBeNested=this.__possibleNestedMarkdownElements.find((t=>i(t,"shouldOpen")(e)))}}var c=Object.defineProperty,h=(e,t,n)=>((e,t,n)=>t in e?c(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,"symbol"!=typeof t?t+"":t,n);class u{constructor(e,t,n,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=t,this.__parent=e??void 0,this.__options=r||{},this.__openingSequence=n??void 0,this.__initialContent="Root"!==t?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}get htmlSanitizer(){return this.__options.htmlSanitizer??(e=>e)}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 t=this.__initialContent[e];this.processCharacter(t)}}parsingChildYielded(e,t,n,s){this.__parsingChild===e&&(this.__parsingChild=void 0),t&&this.createAndAppendMarkdown(t,n),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,t){if(this.yielded)return;const n=this.__sequenceParser&&!this.__sequenceParser?.shouldCloseCurrentMarkdown?this.__sequenceParser?.sequence:void 0;if(this.__yielded=!0,this.__sequenceParser=void 0,this.__parsingChild&&(this.__parsingChild.yield(),this.__parsingChild=void 0),this.__element){const e=this.htmlSanitizer,t=this.__element.innerHTML.trim();this.__element.innerHTML=e?e(t):t,this.removeWhenEmpty&&""===this.__element.innerHTML&&this.__element.remove(),this.__element=void 0}this.__parent&&(this.__parent.parsingChildYielded(this,e,n,t),this.__parent=void 0)}}class p extends u{constructor(e,t,n,s,r){super(e,t,n,s,r)}createAndAppendMarkdown(e,t){A(e,this,t,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}processCharacter(t){if(this.preProcessCharacter(t),this.parsingChild)return void this.parsingChild.processCharacter(t);const{sequenceDiscarded:n,discardedSequence:s,yielded:r}=this.processSequence(t);if(!r)if(e(this.last3Characters)&&(this.nestedMarkdownElements.includes("LineBreak")||"all"===this.nestedMarkdownElements)){if(this.domElement?.innerHTML){const e=this.markdownProcessorOptions?.htmlSanitizer,t=this.domElement.innerHTML.trim();this.domElement.innerHTML=e?e(t):t}this.createAndAppendMarkdown("LineBreak"),this.sequenceParser.reset()}else if(n){this.resetSequenceParser();this.processSequence(t).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 t=this.sequenceParser.nestedElementToCreate;if(t)return A(t,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions),this.sequenceParser.reset(),{sequenceDiscarded:!1};if(this.sequenceParser.canLeadToClosingOrNewMarkdown)return{sequenceDiscarded:!1};const n=this.sequenceParser.sequence;return this.sequenceParser.reset(),{sequenceDiscarded:!0,discardedSequence:n}}}class m extends p{constructor(e,t,n,s){super(e,"BoldAsterisk",t??null,n??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 p{constructor(e,t,n,s){super(e,"BoldUnderscore",t??null,n??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,t,n,s){super(e,"Code",t??null,n??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,t,n)=>((e,t,n)=>t in e?C(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,"symbol"!=typeof t?t+"":t,n);class w extends p{constructor(e,n,s,r={}){super(e,"CodeBlock",n??null,!s||t(s)?null:s,r),k(this,"codeContainer",null),k(this,"currentLineContainer",null),k(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:t,discardedSequence:n,yielded:s}=this.processSequence(e);if(s)return;if(!t)return;if(n&&n.length>1&&this.codeContainer)for(let e=0;e<n.length-1;e++)this.appendCharacterToCodeBlock(n[e]);this.resetSequenceParser();this.processSequence(e).sequenceDiscarded&&(this.sequenceParser.reset(),this.appendCharacterToCodeBlock(e))}yield(e,t){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 t="Copy code block to clipboard",n=document.createElement("button");n.classList.add("nlux-cpy-btn"),n.setAttribute("aria-label",t),n.setAttribute("title",t);const s=document.createElement("span");s.classList.add("icon-copy"),n.appendChild(s),e.insertAdjacentElement("beforebegin",n)})),(e=>{e.querySelectorAll(".nlux-cpy-btn").forEach((e=>{if(!(e instanceof HTMLButtonElement))return;if("true"===e.dataset.clickListenerSet)return;let t=!1;const n=e.nextElementSibling;e.addEventListener("click",(()=>{if(t||!n)return;const s=n.innerText;navigator.clipboard.writeText(s??""),t=!0,e.classList.add("clicked"),setTimeout((()=>{t=!1,e.classList.remove("clicked")}),1e3)})),e.dataset.clickListenerSet="true"}))})(e))}super.yield(e,t),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.replaceChildren(" "),this.currentLineContainer=null;else if(this.codeContainer.innerHTML){const e=document.createElement("div");e.replaceChildren(" "),this.codeContainer.append(e)}}extractLanguageFromOpeningSequence(e){if(!e)return;const t=e.match(/```(\w+)/);return Array.isArray(t)&&t.length>1?t[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 t=this.codeContainer.dataset.language;if(t){const n=this.syntaxHighlighter.createHighlighter({language:t,colorMode:"dark"}),s=this.syntaxHighlighter.highlightingClass(t);this.codeContainer.classList.contains(s)||this.codeContainer.classList.add(s),this.currentLineContainer.innerHTML=n(e,t)}}}}var E=Object.defineProperty,q=(e,t,n)=>((e,t,n)=>t in e?E(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,t+"",n);class P extends p{constructor(e,t,n,s,r){super(t,L(e),n??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:t,discardedSequence:n,yielded:s}=this.processSequence(e);s||t&&n&&(/^\s+$/.test(n)||this.domElement?.append(n))}}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 f extends p{constructor(e,t,n,s){super(e,"ItalicAsterisk",t??null,n??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 v extends p{constructor(e,t,n,s){super(e,"ItalicUnderscore",t??null,n??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,t,n){super(e,"LineBreak",t??null,n??null,null)}get canExistAtRootLevel(){return!0}get nestedMarkdownElements(){return"none"}get yieldingMarkdownElements(){return"none"}createAndAppendMarkdown(e,t){A(e,this,t,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}createElement(){return document.createElement("br")}init(){super.init()}processCharacter(e){this.yield(void 0,e)}}var y=Object.defineProperty,S=(e,t,n)=>((e,t,n)=>t in e?y(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,t+"",n);class $ extends p{constructor(e,t,n,s={}){super(e,"Link",t??null,n??null,s),S(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 t=/\[(.*[^\]])\]\((.*[^\)])\)/.exec(e),n=document.createElement("a");return n.textContent="",t&&t.length>=3&&t[2]&&(n.href=t[2]),"self"!==this.markdownProcessorOptions.markdownLinkTarget&&(n.target="_blank"),n}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 t=this.sequenceParser.nestedElementToCreate;if(t)return A(t,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions),this.sequenceParser.reset(),{sequenceDiscarded:!1};if(this.sequenceParser.canLeadToClosingOrNewMarkdown)return{sequenceDiscarded:!1};const n=this.sequenceParser.sequence;return this.sequenceParser.reset(),{sequenceDiscarded:!0,discardedSequence:n}}}class O extends p{constructor(e,t,n,s){super(e,"Paragraph",t??null,n??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 A=(e,t,n,s,r)=>{if("Paragraph"===e){const e=new O(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("LineBreak"===e){const e=new M(t,n,s);return e.init(),void t.setParsingChild(e)}if("Link"===e){const e=/\[([^\]]+)\]\(([^\)]*)\)/.exec(n),s=e&&e.length>=3?e[1]:"",i=new $(t,n,s,r);return i.init(),void t.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,t,n,s,r);return o.init(),void t.setParsingChild(o)}if("Code"===e){const e=new g(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("CodeBlock"===e){const e=new w(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("BoldAsterisk"===e){const e=new m(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("BoldUnderscore"===e){const e=new _(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("ItalicAsterisk"===e){const e=new f(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("ItalicUnderscore"===e){const e=new v(t,n,s,r);return e.init(),void t.setParsingChild(e)}throw new Error("Unable to create child processor for markdown element "+e)};var B=Object.defineProperty,H=(e,t,n)=>((e,t,n)=>t in e?B(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,t+"",n);class b extends u{constructor(e,t,n){super(null,"Root",t??null,null,n??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,t){A(e,this,t,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(n){if(this.sequenceParser.shouldCloseCurrentMarkdown)return;const s=this.sequenceParser.nestedElementToCreate;if(s){A(s,this,this.sequenceParser.sequence,n,this.markdownProcessorOptions);const e=this.parsingChild;return e&&!e.canExistAtRootLevel&&(A("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"):n&&!t(n)&&(A("Paragraph",this,void 0,this.sequenceParser.sequence,this.markdownProcessorOptions),this.sequenceParser.reset()))}}const x=10,T=(e,t)=>{const n=((e,t)=>{const{syntaxHighlighter:n,htmlSanitizer:s,streamingAnimationSpeed:r=x,markdownLinkTarget:i,showCodeBlockCopyButton:o,skipStreamingAnimation:a=!1,onComplete:l}=t||{},d=new b(e,void 0,{syntaxHighlighter:n,htmlSanitizer:s,markdownLinkTarget:i,showCodeBlockCopyButton:o}),c=[];let h=0,u=!1,p="idle";const m=a?0:Math.max(r,0),_=r>0?r:20,g=Math.ceil(2e3/_),C=()=>{if("closed"===p)return;if("idle"===p&&(p="processing"),0===c.length)return void(!1===u&&h<g?(p="waitingForMoreCharacters",h+=1,setTimeout(C,_)):(d.processCharacter("\n"),d.complete(),d.yield(),t?.htmlSanitizer&&(e.innerHTML=t.htmlSanitizer(e.innerHTML)),p="closed",l?.()));p="processing",h=0;const n=c.shift();n&&d.processCharacter(n),setTimeout(C,m)};return{next:e=>{if(e){for(let t=0;t<e.length;t++)c.push(e[t]);"idle"===p&&C()}},complete:()=>{u=!0},error:()=>{}}})(e,{syntaxHighlighter:t?.syntaxHighlighter,htmlSanitizer:t?.htmlSanitizer,markdownLinkTarget:t?.markdownLinkTarget,showCodeBlockCopyButton:t?.showCodeBlockCopyButton,skipStreamingAnimation:t?.skipStreamingAnimation,streamingAnimationSpeed:t?.streamingAnimationSpeed,onComplete:t?.onComplete});return{next(e){n.next(e)},complete(){n.complete()}}};export{T as createMarkdownStreamParser};

@@ -12,2 +12,4 @@ type Highlighter = (input: string, language: string) => string;

type SanitizerExtension = (html: string) => string;
type CallbackArgType = object | string | number | boolean | symbol | null | undefined | void;

@@ -23,2 +25,3 @@ type CallbackFunction = (...args: CallbackArgType[]) => CallbackArgType;

syntaxHighlighter?: HighlighterExtension;
htmlSanitizer?: SanitizerExtension;
skipStreamingAnimation?: boolean;

@@ -25,0 +28,0 @@ streamingAnimationSpeed?: number;

{
"name": "@nlux/markdown",
"version": "2.2.11-beta",
"version": "2.2.12-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 _ 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()}}}}));
!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}get htmlSanitizer(){return this.__options.htmlSanitizer??(e=>e)}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;if(this.__yielded=!0,this.__sequenceParser=void 0,this.__parsingChild&&(this.__parsingChild.yield(),this.__parsingChild=void 0),this.__element){const e=this.htmlSanitizer,n=this.__element.innerHTML.trim();this.__element.innerHTML=e?e(n):n,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 m extends p{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(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)){if(this.domElement?.innerHTML){const e=this.markdownProcessorOptions?.htmlSanitizer,n=this.domElement.innerHTML.trim();this.domElement.innerHTML=e?e(n):n}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 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 m{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 m{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 m{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 m{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.replaceChildren(" "),this.currentLineContainer=null;else if(this.codeContainer.innerHTML){const e=document.createElement("div");e.replaceChildren(" "),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 m{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 m{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 m{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){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 S=Object.defineProperty,$=(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 O extends m{constructor(e,n,t,s={}){super(e,"Link",n??null,t??null,s),$(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 m{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 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 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 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 _(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){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(e){if(this.sequenceParser.shouldCloseCurrentMarkdown)return;const s=this.sequenceParser.nestedElementToCreate;if(s){B(s,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions);const n=this.parsingChild;return n&&!n.canExistAtRootLevel&&(B("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)&&(B("Paragraph",this,void 0,this.sequenceParser.sequence,this.markdownProcessorOptions),this.sequenceParser.reset()))}}const T=10;e.createMarkdownStreamParser=(e,n)=>{const t=((e,n)=>{const{syntaxHighlighter:t,htmlSanitizer:s,streamingAnimationSpeed:r=T,markdownLinkTarget:i,showCodeBlockCopyButton:o,skipStreamingAnimation:a=!1,onComplete:l}=n||{},d=new x(e,void 0,{syntaxHighlighter:t,htmlSanitizer:s,markdownLinkTarget:i,showCodeBlockCopyButton:o}),c=[];let h=0,u=!1,p="idle";const m=a?0:Math.max(r,0),_=r>0?r:20,g=Math.ceil(2e3/_),C=()=>{if("closed"===p)return;if("idle"===p&&(p="processing"),0===c.length)return void(!1===u&&h<g?(p="waitingForMoreCharacters",h+=1,setTimeout(C,_)):(d.processCharacter("\n"),d.complete(),d.yield(),n?.htmlSanitizer&&(e.innerHTML=n.htmlSanitizer(e.innerHTML)),p="closed",l?.()));p="processing",h=0;const t=c.shift();t&&d.processCharacter(t),setTimeout(C,m)};return{next:e=>{if(e){for(let n=0;n<e.length;n++)c.push(e[n]);"idle"===p&&C()}},complete:()=>{u=!0},error:()=>{}}})(e,{syntaxHighlighter:n?.syntaxHighlighter,htmlSanitizer:n?.htmlSanitizer,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