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.3.6 to 2.3.7

2

cjs/markdown.js

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

"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);const r=n(e,t);this.currentLineContainer.innerHTML=this.htmlSanitizer?this.htmlSanitizer(r):r}}}}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()}}};
"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){B(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 B(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-comp-copyButton"))return;const t="Copy code block to clipboard",n=document.createElement("button");n.classList.add("nlux-comp-copyButton"),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-comp-copyButton").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);const r=n(e,t);this.currentLineContainer.innerHTML=this.htmlSanitizer?this.htmlSanitizer(r):r}}}}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){B(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 B(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 B=(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 A=Object.defineProperty,H=(e,t,n)=>((e,t,n)=>t in e?A(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){B(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){B(s,this,this.sequenceParser.sequence,n,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"):n&&!t(n)&&(B("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),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);const r=n(e,t);this.currentLineContainer.innerHTML=this.htmlSanitizer?this.htmlSanitizer(r):r}}}}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,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};
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){B(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 B(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-comp-copyButton"))return;const t="Copy code block to clipboard",n=document.createElement("button");n.classList.add("nlux-comp-copyButton"),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-comp-copyButton").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);const r=n(e,t);this.currentLineContainer.innerHTML=this.htmlSanitizer?this.htmlSanitizer(r):r}}}}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){B(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 B(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 B=(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 A=Object.defineProperty,H=(e,t,n)=>((e,t,n)=>t in e?A(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){B(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){B(s,this,this.sequenceParser.sequence,n,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"):n&&!t(n)&&(B("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};
{
"name": "@nlux/markdown",
"version": "2.3.6",
"version": "2.3.7",
"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,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlux/markdown"]={})}(this,(function(e){"use strict";const t=e=>/^[ \t]{2}\n$/m.test(e),n=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((t=>new RegExp(t).test(e))),shouldClose:()=>!0,canClose:()=>!1},o=(e,t)=>n=>{const s=a.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)},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,t,n)=>((e,t,n)=>t in e?l(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,"symbol"!=typeof t?t+"":t,n);class c{constructor(e,t="none",n="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"===t?s:"none"===t?[]:t,this.__possibleYieldingMarkdownElements="none"===n?[]:n}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((t=>o(t,"canOpen")(e))),this.__potentialNestedMarkdownElements=this.__possibleNestedMarkdownElements.filter((t=>o(t,"canOpen")(e))),this.__shouldCloseCurrentMarkdown=o(this.__markdownElement,"shouldClose")(e),this.__markdownThatShouldBeRenderedAtParentLevel=this.__possibleYieldingMarkdownElements.find((t=>o(t,"shouldOpen")(e))),this.__markdownThatShouldBeNested=this.__possibleNestedMarkdownElements.find((t=>o(t,"shouldOpen")(e)))}}var h=Object.defineProperty,u=(e,t,n)=>((e,t,n)=>t in e?h(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,"symbol"!=typeof t?t+"":t,n);class p{constructor(e,t,n,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=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 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 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 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,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 m extends p{constructor(e,t,n,s,r){super(e,t,n,s,r)}createAndAppendMarkdown(e,t){B(e,this,t,void 0,this.markdownProcessorOptions),this.sequenceParser.reset()}processCharacter(e){if(this.preProcessCharacter(e),this.parsingChild)return void this.parsingChild.processCharacter(e);const{sequenceDiscarded:n,discardedSequence:s,yielded:r}=this.processSequence(e);if(!r)if(t(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(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 t=this.sequenceParser.nestedElementToCreate;if(t)return B(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 _ extends m{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 g extends m{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 C extends m{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 k=Object.defineProperty,w=(e,t,n)=>((e,t,n)=>t in e?k(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,"symbol"!=typeof t?t+"":t,n);class E extends m{constructor(e,t,s,r={}){super(e,"CodeBlock",t??null,!s||n(s)?null:s,r),w(this,"codeContainer",null),w(this,"currentLineContainer",null),w(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: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);const r=n(e,t);this.currentLineContainer.innerHTML=this.htmlSanitizer?this.htmlSanitizer(r):r}}}}var q=Object.defineProperty,P=(e,t,n)=>((e,t,n)=>t in e?q(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,t+"",n);class L extends m{constructor(e,t,n,s,r){super(t,f(e),n??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:t,discardedSequence:n,yielded:s}=this.processSequence(e);s||t&&n&&(/^\s+$/.test(n)||this.domElement?.append(n))}}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,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 M extends m{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 y extends p{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){B(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,$=(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 O extends m{constructor(e,t,n,s={}){super(e,"Link",t??null,n??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 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 B(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 A extends m{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 B=(e,t,n,s,r)=>{if("Paragraph"===e){const e=new A(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("LineBreak"===e){const e=new y(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 O(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 L(i,t,n,s,r);return o.init(),void t.setParsingChild(o)}if("Code"===e){const e=new C(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("CodeBlock"===e){const e=new E(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("BoldAsterisk"===e){const e=new _(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("BoldUnderscore"===e){const e=new g(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("ItalicAsterisk"===e){const e=new v(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("ItalicUnderscore"===e){const e=new M(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 x extends p{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){B(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(e){if(this.sequenceParser.shouldCloseCurrentMarkdown)return;const s=this.sequenceParser.nestedElementToCreate;if(s){B(s,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions);const t=this.parsingChild;return t&&!t.canExistAtRootLevel&&(B("Paragraph",this,void 0,void 0,this.markdownProcessorOptions),this.parsingChild?.setParsingChild(t)),void this.sequenceParser.reset()}this.sequenceParser.canLeadToClosingOrNewMarkdown||(t(this.last3Characters)?this.createAndAppendMarkdown("LineBreak"):e&&!n(e)&&(B("Paragraph",this,void 0,this.sequenceParser.sequence,this.markdownProcessorOptions),this.sequenceParser.reset()))}}const T=10;e.createMarkdownStreamParser=(e,t)=>{const n=((e,t)=>{const{syntaxHighlighter:n,htmlSanitizer:s,streamingAnimationSpeed:r=T,markdownLinkTarget:i,showCodeBlockCopyButton:o,skipStreamingAnimation:a=!1,onComplete:l}=t||{},d=new x(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()}}}}));
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlux/markdown"]={})}(this,(function(e){"use strict";const t=e=>/^[ \t]{2}\n$/m.test(e),n=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((t=>new RegExp(t).test(e))),shouldClose:()=>!0,canClose:()=>!1},o=(e,t)=>n=>{const s=a.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)},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,t,n)=>((e,t,n)=>t in e?l(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,"symbol"!=typeof t?t+"":t,n);class c{constructor(e,t="none",n="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"===t?s:"none"===t?[]:t,this.__possibleYieldingMarkdownElements="none"===n?[]:n}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((t=>o(t,"canOpen")(e))),this.__potentialNestedMarkdownElements=this.__possibleNestedMarkdownElements.filter((t=>o(t,"canOpen")(e))),this.__shouldCloseCurrentMarkdown=o(this.__markdownElement,"shouldClose")(e),this.__markdownThatShouldBeRenderedAtParentLevel=this.__possibleYieldingMarkdownElements.find((t=>o(t,"shouldOpen")(e))),this.__markdownThatShouldBeNested=this.__possibleNestedMarkdownElements.find((t=>o(t,"shouldOpen")(e)))}}var h=Object.defineProperty,u=(e,t,n)=>((e,t,n)=>t in e?h(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,"symbol"!=typeof t?t+"":t,n);class p{constructor(e,t,n,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=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 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 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 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,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 m extends p{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(e){if(this.preProcessCharacter(e),this.parsingChild)return void this.parsingChild.processCharacter(e);const{sequenceDiscarded:n,discardedSequence:s,yielded:r}=this.processSequence(e);if(!r)if(t(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(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 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 _ extends m{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 g extends m{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 C extends m{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 k=Object.defineProperty,w=(e,t,n)=>((e,t,n)=>t in e?k(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,"symbol"!=typeof t?t+"":t,n);class E extends m{constructor(e,t,s,r={}){super(e,"CodeBlock",t??null,!s||n(s)?null:s,r),w(this,"codeContainer",null),w(this,"currentLineContainer",null),w(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: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-comp-copyButton"))return;const t="Copy code block to clipboard",n=document.createElement("button");n.classList.add("nlux-comp-copyButton"),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-comp-copyButton").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);const r=n(e,t);this.currentLineContainer.innerHTML=this.htmlSanitizer?this.htmlSanitizer(r):r}}}}var q=Object.defineProperty,P=(e,t,n)=>((e,t,n)=>t in e?q(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,t+"",n);class L extends m{constructor(e,t,n,s,r){super(t,f(e),n??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:t,discardedSequence:n,yielded:s}=this.processSequence(e);s||t&&n&&(/^\s+$/.test(n)||this.domElement?.append(n))}}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,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 M extends m{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 y extends p{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,$=(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 O extends m{constructor(e,t,n,s={}){super(e,"Link",t??null,n??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 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 B extends m{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 B(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("LineBreak"===e){const e=new y(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 O(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 L(i,t,n,s,r);return o.init(),void t.setParsingChild(o)}if("Code"===e){const e=new C(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("CodeBlock"===e){const e=new E(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("BoldAsterisk"===e){const e=new _(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("BoldUnderscore"===e){const e=new g(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("ItalicAsterisk"===e){const e=new v(t,n,s,r);return e.init(),void t.setParsingChild(e)}if("ItalicUnderscore"===e){const e=new M(t,n,s,r);return e.init(),void t.setParsingChild(e)}throw new Error("Unable to create child processor for markdown element "+e)};var H=Object.defineProperty,b=(e,t,n)=>((e,t,n)=>t in e?H(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n)(e,t+"",n);class x extends p{constructor(e,t,n){super(null,"Root",t??null,null,n??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,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(e){if(this.sequenceParser.shouldCloseCurrentMarkdown)return;const s=this.sequenceParser.nestedElementToCreate;if(s){A(s,this,this.sequenceParser.sequence,e,this.markdownProcessorOptions);const t=this.parsingChild;return t&&!t.canExistAtRootLevel&&(A("Paragraph",this,void 0,void 0,this.markdownProcessorOptions),this.parsingChild?.setParsingChild(t)),void this.sequenceParser.reset()}this.sequenceParser.canLeadToClosingOrNewMarkdown||(t(this.last3Characters)?this.createAndAppendMarkdown("LineBreak"):e&&!n(e)&&(A("Paragraph",this,void 0,this.sequenceParser.sequence,this.markdownProcessorOptions),this.sequenceParser.reset()))}}const T=10;e.createMarkdownStreamParser=(e,t)=>{const n=((e,t)=>{const{syntaxHighlighter:n,htmlSanitizer:s,streamingAnimationSpeed:r=T,markdownLinkTarget:i,showCodeBlockCopyButton:o,skipStreamingAnimation:a=!1,onComplete:l}=t||{},d=new x(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()}}}}));
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