class MDTokenType{static Text=new MDTokenType("Text");static Whitespace=new MDTokenType("Whitespace");static Underscore=new MDTokenType("Underscore");static Asterisk=new MDTokenType("Asterisk");static Slash=new MDTokenType("Slash");static Tilde=new MDTokenType("Tilde");static Bang=new MDTokenType("Bang");static Backtick=new MDTokenType("Backtick");static Label=new MDTokenType("Label");static URL=new MDTokenType("URL");static Email=new MDTokenType("Email");static SimpleLink=new MDTokenType("SimpleLink");static SimpleEmail=new MDTokenType("SimpleEmail");static Footnote=new MDTokenType("Footnote");static Modifier=new MDTokenType("Modifier");static HTMLTag=new MDTokenType("HTMLTag");static META_AnyNonWhitespace=new MDTokenType("METAAnyNonWhitespace");static META_OptionalWhitespace=new MDTokenType("METAOptionalWhitespace");name;constructor(e){this.name=e}toString(){return`${this.constructor.name}.${this.name}`}equals(e){return e instanceof MDTokenType&&e.name==this.name}}class MDToken{original;type;content;extra;tag;modifier;constructor(e,t,n=null,i=null,s=null){this.original=e,this.type=t,n instanceof MDTagModifier?(this.content=null,this.modifier=n):(this.content=n,this.modifier=null),this.extra=i,this.tag=s}toString(){return`(${this.constructor.name} type=${this.type.toString()} content=${this.content})`}static findFirstTokens(e,t,n=0){for(var i=[],s=n;s=e.length)return null;let n=e[a],c=t[l];if(c==MDTokenType.META_OptionalWhitespace)n instanceof MDToken&&n.type==MDTokenType.Whitespace?i.push(n):o--;else if(c==MDTokenType.META_AnyNonWhitespace){if(n instanceof MDToken&&n.type==MDTokenType.Whitespace){r=!1;break}i.push(n)}else{if(!(n instanceof MDToken)||n.type!=c){r=!1;break}i.push(n)}}if(r)return{tokens:i,index:s}}return null}static findPairedTokens(e,t,n,i=null,s=0){for(var r=s;r0&&(null===i||i(c)))return{startTokens:o.tokens,contentTokens:c,endTokens:a.tokens,startIndex:o.index,contentIndex:o.index+o.tokens.length,endIndex:a.index,totalLength:a.index+a.tokens.length-o.index};l=a.index+1}r=o.index}return null}equals(e){return e instanceof MDToken&&(e.original===this.original&&(!!e.type.equals(this.type)&&(e.content===this.content&&(e.extra===this.extra&&(!!MDUtils.equal(e.tag,this.tag)&&!!MDUtils.equals(e.modifier,this.modifier))))))}}class MDUtils{static baseURLRegex=/(?:(?:(?:[a-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[a-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[a-z0-9\.\-]+)(?:(?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/i;static baseEmailRegex=/(?:(?:[^<>()\[\]\\.,;:\s@"]+(?:\.[^<>()\[\]\\.,;:\s@"]+)*)|(?:".+"))@(?:(?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(?:(?:[a-z\-0-9]+\.)+[a-z]{2,}))/i;static escapeHTML(e,t=!1){if("string"!=typeof e)return"";var n=e.replace(/&/g,"&").replace(//g,">").replace(/"/g,""");return t&&(n=n.replace(/\n/g,"
\n")),n}static escapeObfuscated(e){for(var t="",n=0;ne.replace(n,""))):e.replace(n,"")}static withoutTrailingBlankLines(e){for(var t=e.slice();t.length>0&&0==t[t.length-1].trim().length;)t.pop();return t}static containsBlankLine(e){for(const t of e)if(0==t.trim().length)return!0;return!1}static countIndents(e,t=!1){return e.replace(t?/(?: {4}|\t)/g:/(?: {1,4}|\t)/g,"\t").replace(/^(\t*)(.*?)$/,"$1").length}static tokenizeLabel(e){if(!e.startsWith("["))return null;for(var t=0,n=0,i=1;i0))return[e.substring(0,i+1),e.substring(1,i)];n--}}return null}static#e=/^\((\S+?)\s+"(.*?)"\)/i;static#t=/^\((\S+?)\)/i;static tokenizeURL(e){var t;return(t=this.#e.exec(e))?this.tokenizeEmail(e)?null:t:(t=this.#t.exec(e))?this.tokenizeEmail(e)?null:[...t,null]:null}static#n=new RegExp("^\\(\\s*("+MDUtils.baseEmailRegex.source+')\\s+"(.*?)"\\s*\\)',"i");static#i=new RegExp("^\\(\\s*("+MDUtils.baseEmailRegex.source+")\\s*\\)","i");static tokenizeEmail(e){var t;return(t=this.#n.exec(e))?t:(t=this.#i.exec(e))?[...t,null]:null}static typename(e){return null===e?"null":e instanceof Object?e.constructor.name:typeof e}static#s(e,t){if(e===t)return!0;if(!(e instanceof Array&&t instanceof Array))return!1;if(null==e||null==t)return!1;if(e.length!=t.length)return!1;for(var n=0;n=0?n+=`\\${i}`:",-=<>#&!%:;@~'`\"".indexOf(i)>=0?n+=t(i):n+="\f"==i?"\\f":"\n"==i?"\\n":"\r"==i?"\\r":"\t"==i?"\\t":"\v"==i?"\\v":i}return n}}class MDState{get root(){return this.#o?this.#o.root:this}get lines(){return this.#l}get currentLine(){return this.p=this.lines.length)return null;const e=MDUtils.withoutTrailingBlankLines(this.lines.slice(this.p));return 0==e.length?null:(this.p=this.lines.length,new MDInlineBlock(this.inlineMarkdownToSpans(e.join("\n"))))}#M(){for(;this.hasLines(1)&&0==this.lines[this.p].trim().length;)this.p++;if(!this.hasLines(1))return null;for(const e of this.root.#a){const t=this.p,n=e.readBlock(this);if(n){if(this.p==t)throw new Error(`${e.constructor.name} returned an ${n.constructor.name} without incrementing MDState.p. This could lead to an infinite loop.`);return n}}return this.#d()}#p(e){if(this.#o)return this.#o.#p(e);var t=[],n="",i=!1;const s=function(){if(0==n.length)return;const e=MDState.#h.exec(n);null!==e?(e[1].length>0&&t.push(new MDToken(e[1],MDTokenType.Whitespace,e[1])),void 0!==e[2]&&e[2].length>0&&t.push(new MDToken(e[2],MDTokenType.Text,e[2])),void 0!==e[3]&&e[3].length>0&&t.push(new MDToken(e[3],MDTokenType.Whitespace,e[3]))):t.push(new MDToken(n,MDTokenType.Text,n)),n=""};for(var r=0;r"))break;t.push(i)}if(t.length>0){let i=t.map((function(e){return e.substring(1).replace(/^ {0,3}\t?/,"")})),s=e.copy(i).readBlocks();return e.p=n,new MDBlockquoteBlock(s)}return null}}class MDBaseListBlockReader extends MDBlockReader{constructor(e){super(e)}#D(e,t){for(var n=e.p,i=[],s=!1;e.hasLines(1,n);){const o=n==e.p;var r=e.lines[n++];if(o&&(r=r.substring(t)),/^(?:\*|\+|\-|\d+\.)\s+/.exec(r)){0;break}const l=0==r.trim().length,a=null!==/^\s+\S/.exec(r);if(l)s=!0;else if(!a&&s)break;i.push(r)}return i=MDUtils.withoutTrailingBlankLines(i),MDUtils.stripIndent(i)}readListItemContent(e,t){const n=this.#D(e,t);if(e.p+=Math.max(n.length,1),1==n.length)return new MDInlineBlock(e.inlineMarkdownToSpans(n[0]));if(n.filter((e=>0==e.trim().length)).length>0){const t=e.copy(n).readBlocks();return 1==t.length?t[0]:new MDMultiBlock(t)}for(var i=1;i=e.lines.length)return null;if(t.length>0){e.p=n;let i=t.join("\n");return new MDParagraphBlock(new MDInlineBlock(e.inlineMarkdownToSpans(i)))}return null}}class MDInlineReader{#w;#L;get tokenizePriority(){return this.#w}get substitutePriority(){return this.#L}constructor(e,t){this.#w=e,this.#L=t}readFirstToken(e,t,n){throw new Error(`Abstract readFirstToken must be overridden in ${this.constructor.name}`)}substituteTokens(e,t,n){throw new Error(`Abstract substituteTokens must be overridden in ${this.constructor.name}`)}preProcess(e){}postProcess(e,t){}}class MDSimplePairInlineReader extends MDInlineReader{constructor(e,t){super(e,t)}attemptPair(e,t,n,i,s,r=1,o=!1){let l=Array(r).fill(s),a=this.substitutePriority instanceof Array?this.substitutePriority[0]:null,c=MDToken.findPairedTokens(n,l,l,(function(e){const n=e[0]instanceof MDToken?e[0].type:null,i=e[e.length-1]instanceof MDToken?e[e.length-1].type:null;if(n==MDTokenType.Whitespace)return!1;if(i==MDTokenType.Whitespace)return!1;if(t==a){var r=0;for(let t of e)t instanceof MDToken&&t.type==s&&r++;if(r%2!=0)return!1}return!0}));if(null===c)return!1;let u=o?c.contentTokens.map((e=>e.original)).join(""):e.tokensToSpans(c.contentTokens);return n.splice(c.startIndex,c.totalLength,new i(u)),!0}}class MDStrongInlineReader extends MDSimplePairInlineReader{constructor(e=0,t=[0,50]){super(e,t)}readFirstToken(e,t,n){return n.startsWith("*")?new MDToken("*",MDTokenType.Asterisk):n.startsWith("_")?new MDToken("_",MDTokenType.Underscore):null}substituteTokens(e,t,n){return!!this.attemptPair(e,t,n,MDStrongSpan,MDTokenType.Asterisk,2)||!!this.attemptPair(e,t,n,MDStrongSpan,MDTokenType.Underscore,2)}}class MDEmphasisInlineReader extends MDSimplePairInlineReader{constructor(e=0,t=[0,50]){super(e,t)}readFirstToken(e,t,n){return n.startsWith("*")?new MDToken("*",MDTokenType.Asterisk):n.startsWith("_")?new MDToken("_",MDTokenType.Underscore):null}substituteTokens(e,t,n){return!!this.attemptPair(e,t,n,MDEmphasisSpan,MDTokenType.Asterisk)||!!this.attemptPair(e,t,n,MDEmphasisSpan,MDTokenType.Underscore)}}class MDCodeInlineReader extends MDSimplePairInlineReader{constructor(e=0,t=0){super(e,t)}readFirstToken(e,t,n){return n.startsWith("`")?new MDToken("`",MDTokenType.Backtick):null}substituteTokens(e,t,n){return!!this.attemptPair(e,-1,n,MDCodeSpan,MDTokenType.Backtick,2,!0)||!!this.attemptPair(e,-1,n,MDCodeSpan,MDTokenType.Backtick,1,!0)}}class MDStrikethroughInlineReader extends MDSimplePairInlineReader{constructor(e=0,t=[0,50]){super(e,t)}readFirstToken(e,t,n){return n.startsWith("~")?new MDToken("~",MDTokenType.Tilde):null}substituteTokens(e,t,n){return!!this.attemptPair(e,t,n,MDStrikethroughSpan,MDTokenType.Tilde,2)||!!this.attemptPair(e,t,n,MDStrikethroughSpan,MDTokenType.Tilde)}}class MDImageInlineReader extends MDInlineReader{constructor(e=0,t=0){super(e,t)}readFirstToken(e,t,n){return n.startsWith("!")?new MDToken("!",MDTokenType.Bang):(i=MDUtils.tokenizeLabel(n))?new MDToken(i[0],MDTokenType.Label,i[1]):(i=MDUtils.tokenizeURL(n))?new MDToken(i[0],MDTokenType.URL,i[1],i[2]):null;var i}substituteTokens(e,t,n){var i;if(i=MDToken.findFirstTokens(n,[MDTokenType.Bang,MDTokenType.Label,MDTokenType.META_OptionalWhitespace,MDTokenType.URL])){let e=i.tokens[1].content,t=i.tokens[i.tokens.length-1].content,s=i.tokens[i.tokens.length-1].extra;return n.splice(i.index,i.tokens.length,new MDImageSpan(t,e,s)),!0}if(i=MDToken.findFirstTokens(n,[MDTokenType.Bang,MDTokenType.Label,MDTokenType.META_OptionalWhitespace,MDTokenType.Label])){let e=i.tokens[1].content,t=i.tokens[i.tokens.length-1].content;return n.splice(i.index,i.tokens.length,new MDReferencedImageSpan(t,e)),!0}return!1}}class MDFootnoteInlineReader extends MDInlineReader{static#v=/^\[\^([^\]]+?)\s+"(.*?)"\]/;static#S=/^\[\^([^\]]+?)\]/;constructor(e=0,t=0){super(e,t)}readFirstToken(e,t,n){var i;return(i=MDFootnoteInlineReader.#v.exec(n))?new MDToken(i[0],MDTokenType.Footnote,i[1],i[2]):(i=MDFootnoteInlineReader.#S.exec(n))?new MDToken(i[0],MDTokenType.Footnote,i[1]):null}substituteTokens(e,t,n){var i;if(i=MDToken.findFirstTokens(n,[MDTokenType.Footnote])){let e=i.tokens[0];return n.splice(i.index,1,new MDFootnoteReferenceSpan(e.content)),!0}return!1}}class MDLinkInlineReader extends MDInlineReader{static{Object.defineProperties(MDState.prototype,{urlDefinitions:{get:function(){return void 0===this.root._urlDefinitions&&(this.root._urlDefinitions={}),this.root._urlDefinitions},set:function(e){this.root._urlDefinitions=e}},urlTitles:{get:function(){return void 0===this.root._urlTitles&&(this.root._urlTitles={}),this.root._urlTitles},set:function(e){this.root._urlTitles=e}}}),MDState.prototype.defineURL=function(e,t,n=null){this.urlDefinitions[e.toLowerCase()]=t,null!==n&&(this.urlTitles[e.toLowerCase()]=n)},MDState.prototype.urlForLinkRef=function(e){return this.urlDefinitions[e.toLowerCase()]},MDState.prototype.titleForLinkRef=function(e){return this.urlTitles[e.toLowerCase()]}}constructor(e=0,t=0){super(e,t)}readFirstToken(e,t,n){var i;return(i=MDUtils.tokenizeLabel(n))?new MDToken(i[0],MDTokenType.Label,i[1]):(i=MDUtils.tokenizeEmail(n))?new MDToken(i[0],MDTokenType.Email,i[1],i[2]):(i=MDUtils.tokenizeURL(n))?new MDToken(i[0],MDTokenType.URL,i[1],i[2]):null}substituteTokens(e,t,n){var i;if(i=MDToken.findFirstTokens(n,[MDTokenType.Label,MDTokenType.META_OptionalWhitespace,MDTokenType.URL])){let t=i.tokens[0].content,s=i.tokens[i.tokens.length-1].content,r=i.tokens[i.tokens.length-1].extra;return n.splice(i.index,i.tokens.length,new MDLinkSpan(s,e.inlineMarkdownToSpan(t),r)),!0}if(i=MDToken.findFirstTokens(n,[MDTokenType.Label,MDTokenType.META_OptionalWhitespace,MDTokenType.Email])){let t=i.tokens[0].content,s=`mailto:${i.tokens[i.tokens.length-1].content}`,r=i.tokens[i.tokens.length-1].extra;return n.splice(i.index,i.tokens.length,new MDLinkSpan(s,e.inlineMarkdownToSpan(t),r)),!0}if(i=MDToken.findFirstTokens(n,[MDTokenType.Label,MDTokenType.META_OptionalWhitespace,MDTokenType.Label])){let t=i.tokens[0].content,s=i.tokens[i.tokens.length-1].content;return n.splice(i.index,i.tokens.length,new MDReferencedLinkSpan(s,e.inlineMarkdownToSpan(t))),!0}return!1}}class MDSimpleLinkInlineReader extends MDInlineReader{static#$=new RegExp("^<("+MDUtils.baseEmailRegex.source+")>","i");static#P=new RegExp("^<("+MDUtils.baseURLRegex.source+")>","i");constructor(e=0,t=0){super(e,t)}readFirstToken(e,t,n){var i;return(i=MDSimpleLinkInlineReader.#$.exec(n))?new MDToken(i[0],MDTokenType.SimpleEmail,i[1]):(i=MDSimpleLinkInlineReader.#P.exec(n))?new MDToken(i[0],MDTokenType.SimpleLink,i[1]):null}#H(e,t){const n=MDToken.findFirstTokens(t,[MDTokenType.SimpleEmail]);if(null===n)return!1;const i=n.tokens[0],s=`mailto:${i.content}`,r=new MDLinkSpan(s,new MDObfuscatedTextSpan(i.content));return t.splice(n.index,1,r),!0}#C(e,t){const n=MDToken.findFirstTokens(t,[MDTokenType.SimpleLink]);if(null===n)return!1;const i=n.tokens[0].content,s=new MDLinkSpan(i,new MDTextSpan(i));return t.splice(n.index,1,s),!0}substituteTokens(e,t,n){return!!this.#H(e,n)||!!this.#C(e,n)}}class MDHTMLTagInlineReader extends MDInlineReader{constructor(e=0,t=95){super(e,t)}readFirstToken(e,t,n){var i=MDHTMLTag.fromLineStart(n);return i?new MDToken(i.fullTag,MDTokenType.HTMLTag,i.fullTag,null,i):null}substituteTokens(e,t,n){const i=MDToken.findFirstTokens(n,[MDTokenType.HTMLTag]);if(null===i)return!1;const s=i.tokens[0].tag,r=new MDHTMLSpan(s);return n.splice(i.index,1,r),!0}}class MDModifierInlineReader extends MDInlineReader{constructor(e=0,t=100){super(e,t)}readFirstToken(e,t,n){var i=MDTagModifier.fromStart(n);return i?new MDToken(i.original,MDTokenType.Modifier,i):null}substituteTokens(e,t,n){return!1}}class MDBlock{cssClasses=[];cssId=null;attributes={};toHTML(e){throw new Error(`Abstract ${this.constructor.name}.toHTML must be implemented`)}toPlaintext(e){throw new Error(`Abstract ${this.constructor.name}.toPlaintext must be implemented`)}htmlAttributes(){var e="";this.cssClasses.length>0&&(e+=` class="${this.cssClasses.join(" ")}"`),null!==this.cssId&&(e+=` id="${this.cssId}"`);for(const t in this.attributes){let n=this.attributes[t];e+=` ${t}="${MDUtils.escapeHTML(n)}"`}return e}static toHTML(e,t){return e.map((e=>e.toHTML(t))).join("\n")}static toPlaintext(e,t){return e.map((e=>e.toPlaintext(t))).join("\n")}visitChildren(e){}}class MDMultiBlock extends MDBlock{blocks;constructor(e){if(super(),!(e instanceof Array))throw new Error(`${MDUtils.typename(this)} expects MDBlock[], got ${MDUtils.typename(e)}`);this.blocks=e}toHTML(e){return MDBlock.toHTML(this.blocks,e)}toPlaintext(e){return MDBlock.toPlaintext(this.blocks,e)}visitChildren(e){for(const t of this.blocks)e(t),t.visitChildren(e)}}class MDParagraphBlock extends MDBlock{content;constructor(e){if(super(),e instanceof Array)this.content=e;else{if(!(e instanceof MDBlock))throw new Error(`${MDUtils.typename(this)} expects MDBlock[] or MDBlock, got ${MDUtils.typename(e)}`);this.content=[e]}}toHTML(e){const t=MDBlock.toHTML(this.content,e);return`${t}

\n`}toPlaintext(e){return MDBlock.toPlaintext(this.content,e)}visitChildren(e){for(const t of this.content)e(t),t.visitChildren(e)}}class MDHeaderBlock extends MDBlock{level;content;constructor(e,t){super(),this.level=e,this.content=t instanceof Array?t:[t]}toHTML(e){let t=MDBlock.toHTML(this.content,e);return`${t}\n`}toPlaintext(e){return MDBlock.toPlaintext(this.content,e)}visitChildren(e){for(const t of this.content)e(t),t.visitChildren(e)}}class MDBlockquoteBlock extends MDBlock{content;constructor(e){super(),this.content=e instanceof MDBlock?[e]:e}toHTML(e){let t=MDBlock.toHTML(this.content,e);return`\n${t}\n`}toPlaintext(e){return MDBlock.toPlaintext(this.content,e)}visitChildren(e){for(const t of this.content)e(t),t.visitChildren(e)}}class MDUnorderedListBlock extends MDBlock{items;constructor(e){super(),this.items=e}toHTML(e){let t=MDBlock.toHTML(this.items,e);return`\n${t}\n`}toPlaintext(e){return MDBlock.toPlaintext(this.items,e)}visitChildren(e){for(const t of this.items)e(t),t.visitChildren(e)}}class MDOrderedListBlock extends MDBlock{items;startOrdinal;constructor(e,t=null){super(),this.items=e,this.startOrdinal=t}htmlAttributes(){var e=super.htmlAttributes();return null!==this.startOrdinal&&1!=this.startOrdinal&&(e+=` start="${this.startOrdinal}"`),e}toHTML(e){let t=MDBlock.toHTML(this.items,e);return`\n${t}\n`}toPlaintext(e){return MDBlock.toPlaintext(this.items,e)}visitChildren(e){for(const t of this.items)e(t),t.visitChildren(e)}}class MDListItemBlock extends MDBlock{content;ordinal;constructor(e,t=null){super(),this.content=e instanceof Array?e:[e],this.ordinal=t}toHTML(e){let t=MDBlock.toHTML(this.content,e);return`${t}`}toPlaintext(e){return MDBlock.toPlaintext(this.content,e)}visitChildren(e){for(const t of this.content)e(t),t.visitChildren(e)}}class MDCodeBlock extends MDBlock{code;constructor(e){super(),this.code=e}toHTML(e){return`${MDUtils.escapeHTML(this.code)}`}toPlaintext(e){return this.code}}class MDHorizontalRuleBlock extends MDBlock{toHTML(e){return`\n`}toPlaintext(e){return""}}class MDTableCellBlock extends MDBlock{static AlignLeft="left";static AlignCenter="center";static AlignRight="right";content;align=null;constructor(e){super(),this.content=e}#I(){switch(this.align){case MDTableCellBlock.AlignLeft:return' align="left"';case MDTableCellBlock.AlignCenter:return' align="center"';case MDTableCellBlock.AlignRight:return' align="right"';default:return""}}htmlAttributes(){var e=super.htmlAttributes();return e+=this.#I()}toHTML(e){let t=this.content.toHTML(e);return`${t}`}toPlaintext(e){return this.content.toPlaintext(e)}visitChildren(e){e(this.content),this.content.visitChildren(e)}}class MDTableHeaderCellBlock extends MDTableCellBlock{toHTML(e){let t=super.toHTML(e);return`$/.exec(t)[1]}th>`}}class MDTableRowBlock extends MDBlock{cells;constructor(e){super(),this.cells=e}applyAlignments(e){for(var t=0;t\n${t}\n`}toPlaintext(e){return this.cells.map((t=>t.toPlaintext(e))).join(" ")}visitChildren(e){for(const t of this.cells)e(t),t.visitChildren(e)}}class MDTableBlock extends MDBlock{headerRow;bodyRows;constructor(e,t){super(),this.headerRow=e,this.bodyRows=t}toHTML(e){let t=this.headerRow.toHTML(e),n=MDBlock.toHTML(this.bodyRows,e);return`\n\n${t}\n\n\n${n}\n\n`}toPlaintext(e){return this.headerRow.toPlaintext(e)+"\n"+this.bodyRows.map((t=>t.toPlaintext(e))).join("\n")}visitChildren(e){e(this.headerRow),this.headerRow.visitChildren(e);for(const t of this.bodyRows)e(t),t.visitChildren(e)}}class MDDefinitionListBlock extends MDBlock{content;constructor(e){super(),this.content=e}toHTML(e){let t=MDBlock.toHTML(this.content,e);return`\n${t}\n`}toPlaintext(e){return MDBlock.toPlaintext(this.content,e)}visitChildren(e){for(const t of this.content)e(t),t.visitChildren(e)}}class MDDefinitionTermBlock extends MDBlock{content;constructor(e){if(super(),e instanceof Array)this.content=e;else{if(!(e instanceof MDBlock))throw new Error(`${this.constructor.name} expects MDBlock or MDBlock[], got ${typeof e}`);this.content=[e]}}toHTML(e){let t=MDBlock.toHTML(this.content,e);return`${t}`}toPlaintext(e){return MDBlock.toPlaintext(this.content,e)}visitChildren(e){for(const t of this.content)e(t),t.visitChildren(e)}}class MDDefinitionDefinitionBlock extends MDBlock{content;constructor(e){if(super(),e instanceof Array)this.content=e;else{if(!(e instanceof MDBlock))throw new Error(`${this.constructor.name} expects MDBlock or MDBlock[], got ${typeof e}`);this.content=[e]}}toHTML(e){let t=MDBlock.toHTML(this.content,e);return`${t}`}toPlaintext(e){return MDBlock.toPlaintext(this.content,e)}visitChildren(e){for(const t of this.content)e(t),t.visitChildren(e)}}class MDFootnoteListingBlock extends MDBlock{constructor(){super()}toHTML(e){const t=e.footnotes;var n=Object.keys(t);if(0==Object.keys(t).length)return"";const i=e.footnoteInstances;var s="";s+='

',s+="
    ";for(const r of n){let n=t[r];if(n){s+=`
  1. ${n.toHTML(e)}`;for(const e of i[r])s+=` ↩︎`;s+="
  2. \n"}}return s+="
",s+="
"}toPlaintext(e){const t=e.footnotes;var n=Object.keys(t);if(0==Object.keys(t).length)return"";var i="";for(const s of n){let n=t[s];n&&(i+=`${s}. ${n.toPlaintext(e)}\n`)}return i.trim()}}class MDInlineBlock extends MDBlock{content;constructor(e){super(),this.content=e instanceof Array?e:[e];for(const e of this.content)if(!(e instanceof MDSpan))throw new Error(`${this.constructor.name} expects MDSpan or MDSpan[], got ${MDUtils.typename(e)}`)}toHTML(e){return MDSpan.toHTML(this.content,e)}toPlaintext(e){return MDSpan.toPlaintext(this.content,e)}visitChildren(e){for(const t of this.content)e(t),t.visitChildren(e)}}class MDSpan{cssClasses=[];cssId=null;attributes={};toHTML(e){throw new Error(`Abstract ${this.constructor.name}.toHTML must be implemented`)}toPlaintext(e){throw new Error(`Abstract ${this.constructor.name}.toPlaintext must be implemented`)}htmlAttributes(){var e="";this.cssClasses.length>0&&(e+=` class="${this.cssClasses.join(" ")}"`),null!==this.cssId&&(e+=` id="${this.cssId}"`);for(const t in this.attributes){let n=this.attributes[t];e+=` ${t}="${MDUtils.escapeHTML(n)}"`}return e}static toHTML(e,t){return e.map((e=>e.toHTML(t))).join("")}static toPlaintext(e,t){return e.map((e=>e.toPlaintext(t))).join("")}visitChildren(e){}}class MDMultiSpan extends MDSpan{content;constructor(e){super(),this.content=e}toHTML(e){return MDSpan.toHTML(this.content,e)}toPlaintext(e){return MDSpan.toPlaintext(this.content,e)}visitChildren(e){for(const t of this.content)e(t),t.visitChildren(e)}}class MDTextSpan extends MDSpan{text;constructor(e){super(),this.text=e}toHTML(e){let t=MDUtils.escapeHTML(this.text),n=e.abbreviations,i=e.abbreviationRegexes;for(const e in n){let s=n[e],r=i[e],o=MDUtils.escapeHTML(s);t=t.replace(r,`$1`)}return t}toPlaintext(e){return this.text}}class MDHTMLSpan extends MDSpan{tag;constructor(e){super(),this.tag=e}toHTML(e){return this.tag.fullTag}toPlaintext(e){return""}}class MDObfuscatedTextSpan extends MDSpan{text;constructor(e){super(),this.text=e}toHTML(e){return MDUtils.escapeObfuscated(this.text)}toPlaintext(e){return this.text}}class MDLinkSpan extends MDSpan{link;target=null;content;title=null;constructor(e,t,n=null){super(),this.link=e,this.content=t,this.title=n}toHTML(e){var t=`"+this.content.toHTML(e)+""}toPlaintext(e){return this.content.toPlaintext(e)}visitChildren(e){e(this.content),this.content.visitChildren(e)}}class MDReferencedLinkSpan extends MDLinkSpan{ref;constructor(e,t){super(null,t,null),this.ref=e}toHTML(e){if(!this.link){let t=e.urlForLinkRef(this.ref),n=e.titleForLinkRef(this.ref);this.link=t,this.title=n||this.title}if(this.link)return super.toHTML(e);return`[${this.content.toHTML(e)}][${this.ref}]`}}class MDEmphasisSpan extends MDSpan{content;constructor(e){super(),this.content=e instanceof MDSpan?[e]:e}toHTML(e){let t=MDSpan.toHTML(this.content,e);return`${t}`}toPlaintext(e){return MDSpan.toPlaintext(this.content,e)}visitChildren(e){for(const t of this.content)e(t),t.visitChildren(e)}}class MDStrongSpan extends MDSpan{content;constructor(e){super(),this.content=e instanceof MDSpan?[e]:e}toHTML(e){let t=MDSpan.toHTML(this.content,e);return`${t}`}toPlaintext(e){return MDSpan.toPlaintext(this.content,e)}visitChildren(e){for(const t of this.content)e(t),t.visitChildren(e)}}class MDStrikethroughSpan extends MDSpan{content;constructor(e){super(),this.content=e instanceof MDSpan?[e]:e}toHTML(e){let t=MDSpan.toHTML(this.content,e);return`${t}`}toPlaintext(e){return MDSpan.toPlaintext(this.content,e)}visitChildren(e){for(const t of this.content)e(t),t.visitChildren(e)}}class MDCodeSpan extends MDSpan{content;constructor(e){if(super(),"string"!=typeof e)throw new Error(`${this.constructor.name} content must be String, got ${typeof e}`);this.content=e}toHTML(e){return`${MDUtils.escapeHTML(this.content)}`}toPlaintext(e){return this.content}}class MDImageSpan extends MDSpan{source;alt;title;constructor(e,t,n=null){super(),this.source=e,this.alt=t,this.title=n}toHTML(e){let t=`${MDUtils.escapeHTML(this.alt)}",t}toPlaintext(e){return this.alt||""}}class MDReferencedImageSpan extends MDImageSpan{ref;constructor(e,t){super(null,t),this.ref=e}toHTML(e){if(!this.source){let t=e.urlForLinkRef(this.ref),n=e.titleForLinkRef(this.ref);this.source=t,this.title=n||this.title}return this.source?super.toHTML(e):`![${MDUtils.escapeHTML(this.alt)}][${MDUtils.escapeHTML(this.ref)}]`}}class MDFootnoteReferenceSpan extends MDSpan{symbol;differentiator=null;constructor(e){super(),this.symbol=e}toHTML(e){return null!==this.differentiator?`${this.symbol}`:`\x3c!--FNREF:{${this.symbol}}--\x3e`}toPlaintext(e){return this.symbol}}class MDHTMLTag{fullTag;tagName;isCloser;attributes;constructor(e,t,n,i){this.fullTag=e,this.tagName=t,this.isCloser=n,this.attributes=i}toString(){return this.fullTag}equals(e){return e instanceof MDHTMLTag&&e.fullTag==this.fullTag}static#A=/[a-z]/i;static#U=/[a-z0-9]/i;static#E=/[a-z]/i;static#F=/[a-z0-9-]/i;static#_=/\s/;static fromLineStart(e){var t=!1,n="",i="",s="",r=null,o={},l=null;let a=function(){i.length>0&&(s.length>0||r?o[i]=s:o[i]=!0),i="",s="",r=null};for(var c=0,u=0;u"==o){l=e.substring(0,u+1);break}if(!this.#E.exec(o))return null;i+=o}else if(h)c=4;else if("/"==o)a(),c=6;else{if(">"==o){a(),l=e.substring(0,u+1);break}if("="==o)c=5;else{if(!this.#F.exec(o))return null;i+=o}}break;case 4:if("="==o)c=5;else if(h);else if("/"==o)c=6;else{if(">"==o){l=e.substring(0,u+1);break}this.#E.exec(o)&&(a(),c=3,u--)}break;case 5:if(0==s.length)if(null===r)h||('"'==o||"'"==o?r=o:(r="",u--));else if(o===r)a(),c=3;else{if(""===r&&("/"==o||">"==o))return null;s+=o}else o===r||""===r&&h?(a(),c=3):s+=o;break;case 6:if(h);else if(">"==o){l=e.substring(0,u+1);break}}}return null===l?null:(a(),new MDHTMLTag(l,n,t,o))}}class MDTagModifier{original;cssClasses=[];cssId=null;attributes={};static#O=/\.([a-z_\-][a-z0-9_\-]*?)/i;static#W=/#([a-z_\-][a-z0-9_\-]*?)/i;static#z=/([a-z0-9]+?)=([^\s\}]+?)/i;static#j=/\{([^}]+?)}/i;static#q=new RegExp("^"+this.#j.source,"i");static#N=new RegExp("^(.*?)\\s*"+this.#j.source+"\\s*$","i");static#Q=new RegExp("^"+this.#O.source+"$","i");static#Z=new RegExp("^"+this.#W.source+"$","i");static#G=new RegExp("^"+this.#z.source+"$","i");applyTo(e){if(e instanceof MDBlock||e instanceof MDSpan){e.cssClasses=e.cssClasses.concat(this.cssClasses),this.cssId&&(e.cssId=this.cssId);for(const t in this.attributes)e.attributes[t]=this.attributes[t]}}equals(e){return e instanceof MDTagModifier&&(!!MDUtils.equal(e.cssClasses,this.cssClasses)&&(e.cssId===this.cssId&&!!MDUtils.equal(e.attributes,this.attributes)))}toString(){return this.original}static#J(e){let t=e.split(/\s+/),n=new MDTagModifier;var i;n.original=`{${e}}`;for(const e of t)if(""!=e.trim())if(i=this.#Q.exec(e))n.cssClasses.push(i[1]);else if(i=this.#Z.exec(e))n.cssId=i[1];else{if(!(i=this.#G.exec(e)))return null;n.attributes[i[1]]=i[2]}return n}static fromLine(e){let t=this.#N.exec(e);return null===t?[e,null]:[t[1],this.#J(t[2])]}static fromStart(e){let t=this.#q.exec(e);return null===t?null:this.#J(t[1])}static strip(e){let t=this.#N.exec(e);return null===t?e:t[1]}}class MDConfig{}class Markdown{static standardBlockReaders=[new MDUnderlinedHeaderBlockReader(10),new MDHashHeaderBlockReader(15),new MDBlockQuoteBlockReader(20),new MDHorizontalRuleBlockReader(25),new MDFencedCodeBlockReader(30),new MDIndentedCodeBlockReader(40),new MDOrderedListBlockReader(45),new MDUnorderedListBlockReader(50),new MDURLDefinitionBlockReader(95),new MDParagraphBlockReader(100)];static allBlockReaders=[...this.standardBlockReaders,new MDTableBlockReader(55),new MDDefinitionListBlockReader(60),new MDAbbreviationDefinitionBlockReader(90),new MDFootnoteDefinitionBlockReader(91)];static standardInlineReaders=[new MDStrongInlineReader(10,[0,2]),new MDEmphasisInlineReader(15,[5,55]),new MDCodeInlineReader(20,[10,60]),new MDImageInlineReader(25,15),new MDLinkInlineReader(30,20),new MDSimpleLinkInlineReader(35,25),new MDHTMLTagInlineReader(80,30)];static allInlineReaders=[...this.standardInlineReaders,new MDStrikethroughInlineReader(21,[12,50]),new MDFootnoteInlineReader(5,40),new MDModifierInlineReader(90,45)];static standardParser=new Markdown(this.standardBlockReaders,this.standardInlineReaders);static completeParser=new Markdown(this.allBlockReaders,this.allInlineReaders);#K;#V;#a;#c;#u;constructor(e=Markdown.allBlockReaders,t=Markdown.allInlineReaders){this.#K=e,this.#V=t,this.#a=e.slice(),this.#a.sort(((e,t)=>e.priority-t.priority));const n=function(e){var n=[];for(const i of t){const t=e(i),s=t instanceof Array?t:[t];for(const e of s)n.push([e,i])}return n.sort(((e,t)=>e[0]-t[0])),n};this.#c=n((e=>e.tokenizePriority)),this.#u=n((e=>e.substitutePriority))}toHTML(e){const t=e.split(/(?:\n|\r|\r\n)/),n=new MDState(t,this.#a,this.#c,this.#u);for(const e of this.#K)e.preProcess(n);for(const e of this.#V)e.preProcess(n);const i=n.readBlocks();for(const e of this.#K)e.postProcess(n,i);for(const e of this.#V)e.postProcess(n,i);return MDBlock.toHTML(i,n)}}