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 0.10.10 to 0.10.11

2

cjs/markdown.js

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

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

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

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

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

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

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

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc