PHP and Javascript implementations of a simple markdown parser
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1
  1. 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;s++){var r=!0;i=[];for(var o=0,l=0;l<t.length;l++){var a=s+l+o;if(a>=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;r<e.length;r++){var o=this.findFirstTokens(e,t,r);if(null===o)return null;for(var l=o.index+o.tokens.length;l<e.length;){var a=this.findFirstTokens(e,n,l);if(null===a)break;var c=e.slice(o.index+o.tokens.length,a.index);if(c.length>0&&(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,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;");return t&&(n=n.replace(/\n/g,"<br>\n")),n}static escapeObfuscated(e){for(var t="",n=0;n<e.length;n++){t+=`&#${e.codePointAt(n)};`}return t}static stripIndent(e,t=1){const n=new RegExp(`^(?: {1,4}|\t){${t}}`);return e instanceof Array?e.map((e=>e.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;i<e.length;i++){let s=e.substring(i,i+1);if("\\"==s)i++;else if("("==s)t++;else if(")"==s){if(--t<0)return null}else if("["==s)n++;else if("]"==s){if(!(n>0))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<e.length;n++)if(!this.equal(e[n],t[n]))return!1;return!0}static#r(e,t){if(e===t)return!0;if(!(e instanceof Object&&t instanceof Object))return!1;if(null==e||null==t)return!1;if(void 0!==e.equals)return e.equals(t);for(const n of Object.keys(e))if(!this.equal(e[n],t[n]))return!1;for(const n of Object.keys(t))if(!this.equal(e[n],t[n]))return!1;return!0}static equal(e,t,n=0){if(e instanceof Array&&t instanceof Array)return this.#s(e,t);if(e instanceof Object&&t instanceof Object)return this.#r(e,t);if("number"==typeof e&&"number"==typeof t){if(e===t)return!0;const i=(t-e)/e;return Math.abs(i)<=n}return e==t}static escapeRegex(e){const t=function(e){const t="00"+e.codePointAt(0).toString(16);return`\\x${t.substring(t.length-2)}`};var n="";const i=e.length;for(var s=0;s<i;s++){const i=e.substring(s,s+1);0==s&&/[a-zA-Z0-9]/.exec(i)?n+=t(i):"^$\\.*+?()[]{}|/".indexOf(i)>=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.#l.length?this.#l[this.p]:null}p=0;#l=[];#o=null;#a=[];#c=[];#u=[];static#h=/^(\s*)(?:(\S|\S.*\S)(\s*?))?$/;constructor(e,t=null,n=null,i=null){this.#l=e,this.#a=t,this.#c=n,this.#u=i}copy(e){let t=new MDState(e);return t.#o=this,t}hasLines(e,t=null){return(null===t?this.p:t)+e<=this.lines.length}readBlocks(){for(var e=[];this.hasLines(1);){let t=this.#M();if(!t)break;e.push(t)}return e}#d(){if(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<e.length;r++){const l=e.substring(r,r+1),a=e.substring(r);if(i)n+=l,i=!1;else if("\\"!=l){var o=!1;for(const e of this.root.#c){const n=e[0],i=e[1],l=i.readFirstToken(this,n,a);if(null!==l){if(s(),t.push(l),null==l.original||0==l.original.length)throw new Error(`${i.constructor.name} returned a token with an empty .original. This would cause an infinite loop.`);r+=l.original.length-1,o=!0;break}}o||(n+=l)}else i=!0}return s(),t}inlineMarkdownToSpan(e){let t=this.inlineMarkdownToSpans(e);return 1==t.length?t[0]:new MDMultiSpan(t)}inlineMarkdownToSpans(e){var t=this.#p(e instanceof Array?e.join("\n"):e);return this.tokensToSpans(t)}tokensToSpans(e){var t=e.slice(),n=!1;do{n=!1;for(const e of this.root.#u){const i=e[0];if(e[1].substituteTokens(this,i,t)){n=!0;break}}}while(n);var i=null;return t=t.map((function(e){if(e instanceof MDToken)return e.type==MDTokenType.Modifier&&i?(e.modifier.applyTo(i),i=null,new MDTextSpan("")):(i=null,new MDTextSpan(e.original));if(e instanceof MDSpan)return i=e instanceof MDTextSpan?null:e,e;throw new Error(`Unexpected span type ${e.constructor.name}`)}))}}class MDBlockReader{#k;get priority(){return this.#k}constructor(e){this.#k=e}readBlock(e){throw Error(`Abstract readBlock must be overridden in ${this.constructor.name}`)}preProcess(e){}postProcess(e,t){}}class MDUnderlinedHeaderBlockReader extends MDBlockReader{constructor(e=0){super(e)}readBlock(e){var t,n=e.p;if(!e.hasLines(2))return null;let i=e.lines[n++].trim();[i,t]=MDTagModifier.fromLine(i);let s=e.lines[n++].trim();if(""==i)return null;if(/^=+$/.exec(s)){e.p=n;let s=new MDHeaderBlock(1,e.inlineMarkdownToSpan(i));return t&&t.applyTo(s),s}if(/^\-+$/.exec(s)){e.p=n;let s=new MDHeaderBlock(2,e.inlineMarkdownToSpan(i));return t&&t.applyTo(s),s}return null}}class MDHashHeaderBlockReader extends MDBlockReader{static#f=/^(#{1,6})\s*([^#].*?)\s*\#*\s*$/;constructor(e=5){super(e)}readBlock(e){var t=e.p;let n=e.lines[t++];var i;[n,i]=MDTagModifier.fromLine(n);var s=MDHashHeaderBlockReader.#f.exec(n);if(null===s)return null;e.p=t;const r=s[1].length,o=s[2];let l=new MDHeaderBlock(r,new MDInlineBlock(e.inlineMarkdownToSpan(o)));return i&&i.applyTo(l),l}}class MDBlockQuoteBlockReader extends MDBlockReader{constructor(e=10){super(e)}readBlock(e){for(var t=[],n=e.p;n<e.lines.length;){let i=e.lines[n++];if(!i.startsWith(">"))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<n.length;i++){const t=n[i];if(/^(?:\*|\-|\+|\d+\.)\s+/.exec(t)){const t=new MDInlineBlock(e.inlineMarkdownToSpans(n.slice(0,i).join("\n"))),s=e.copy(n.slice(i)).readBlocks();return new MDMultiBlock([t,...s])}}{const t=e.copy(n).readBlocks();return 1==t.length?t[0]:new MDMultiBlock(t)}}readBlock(e){throw new Error(`Abstract readBlock must be overridden in ${this.constructor.name}`)}}class MDUnorderedListBlockReader extends MDBaseListBlockReader{static#T=/^([\*\+\-]\s+)(.*)$/;constructor(e=15){super(e)}#g(e){var t=e.p;let n=e.lines[t],i=MDUnorderedListBlockReader.#T.exec(n);if(null===i)return null;const s=i[1].length;return new MDListItemBlock(this.readListItemContent(e,s))}readBlock(e){var t=[],n=null;do{(n=this.#g(e))&&t.push(n)}while(n);return 0==t.length?null:new MDUnorderedListBlock(t)}}class MDOrderedListBlockReader extends MDBaseListBlockReader{static#x=/^(\d+)(\.\s+)(.*)$/;constructor(e=16){super(e)}#b(e){var t=e.p;let n=e.lines[t],i=MDOrderedListBlockReader.#x.exec(n);if(null===i)return null;const s=parseInt(i[1]),r=i[1].length+i[2].length;return new MDListItemBlock(this.readListItemContent(e,r),s)}readBlock(e){var t=[],n=null;do{(n=this.#b(e))&&t.push(n)}while(n);return 0==t.length?null:new MDOrderedListBlock(t,t[0].ordinal)}}class MDFencedCodeBlockReader extends MDBlockReader{constructor(e=20){super(e)}readBlock(e){if(!e.hasLines(2))return null;var t=e.p;let n=e.lines[t++];var i;if([n,i]=MDTagModifier.fromLine(n),"```"!=n.trim())return null;for(var s=[];e.hasLines(1,t);){let n=e.lines[t++];if("```"==n.trim()){e.p=t;let n=new MDCodeBlock(s.join("\n"));return i&&i.applyTo(n),n}s.push(n)}return null}}class MDIndentedCodeBlockReader extends MDBlockReader{constructor(e=21){super(e)}readBlock(e){for(var t=e.p,n=[];e.hasLines(1,t);){let i=e.lines[t++];if(MDUtils.countIndents(i,!0)<1){t--;break}n.push(MDUtils.stripIndent(i))}return 0==n.length?null:(e.p=t,new MDCodeBlock(n.join("\n")))}}class MDHorizontalRuleBlockReader extends MDBlockReader{static#R=/^\s*(?:\-(?:\s*\-){2,}|\*(?:\s*\*){2,})\s*$/;constructor(e=25){super(e)}readBlock(e){var t=e.p;let n=e.lines[t++];var i;if([n,i]=MDTagModifier.fromLine(n),MDHorizontalRuleBlockReader.#R.exec(n)){e.p=t;let n=new MDHorizontalRuleBlock;return i&&i.applyTo(n),n}return null}}class MDTableBlockReader extends MDBlockReader{constructor(e=30){super(e)}#m(e,t){if(!e.hasLines(1))return null;var n=e.p;let i=MDTagModifier.strip(e.lines[n++].trim());if(null===/.*\|.*/.exec(i))return null;i.startsWith("|")&&(i=i.substring(1)),i.endsWith("|")&&(i=i.substring(0,i.length-1));let s=i.split("|").map((function(n){let i=e.inlineMarkdownToSpan(n.trim());return t?new MDTableHeaderCellBlock(i):new MDTableCellBlock(i)}));return e.p=n,new MDTableRowBlock(s)}#B(e){return(e=e.trim()).startsWith("|")&&(e=e.substring(1)),e.endsWith("|")&&(e=e.substring(0,e.length-1)),e.split(/\s*\|\s*/).map((function(e){return e.startsWith(":")?e.endsWith(":")?MDTableCellBlock.AlignCenter:MDTableCellBlock.AlignLeft:e.endsWith(":")?MDTableCellBlock.AlignRight:null}))}static#y=/^\s*[|]?\s*(?:[:]?-+[:]?)(?:\s*\|\s*[:]?-+[:]?)*\s*[|]?\s*$/;readBlock(e){if(!e.hasLines(2))return null;let t=e.p,n=e.lines[t];var i=MDTagModifier.fromLine(n)[1];let s=this.#m(e,!0);if(null===s)return e.p=t,null;let r=e.lines[e.p++];if(null===MDTableBlockReader.#y.exec(r))return e.p=t,null;let o=this.#B(r);s.applyAlignments(o);for(var l=[];e.hasLines(1);){let t=this.#m(e,!1);if(null===t)break;t.applyAlignments(o),l.push(t)}let a=new MDTableBlock(s,l);return i&&i.applyTo(a),a}}class MDDefinitionListBlockReader extends MDBlockReader{constructor(e=35){super(e)}readBlock(e){for(var t,n=e.p,i=0,s=0,r=[];e.hasLines(1,n);){let t=e.lines[n++];if(0==t.trim().length)break;if(/^\s+/.exec(t)){if(0==r.length)return null;r[r.length-1]+="\n"+t}else/^:\s+/.exec(t)?(r.push(t),s++):(r.push(t),i++)}if(0==i||0==s)return null;let o=r.map((function(n){return(t=/^:\s+(.*?)$/s.exec(n))?new MDDefinitionDefinitionBlock(e.inlineMarkdownToSpans(t[1])):new MDDefinitionTermBlock(e.inlineMarkdownToSpans(n))}));return e.p=n,new MDDefinitionListBlock(o)}}class MDFootnoteDefinitionBlockReader extends MDBlockReader{static{Object.defineProperties(MDState.prototype,{footnotes:{get:function(){return void 0===this.root._footnotes&&(this.root._footnotes={}),this.root._footnotes},set:function(e){this.root._footnotes=e}},footnoteInstances:{get:function(){return void 0===this.root._footnoteInstances&&(this.root._footnoteInstances={}),this.root._footnoteInstances},set:function(e){this.root._footnoteInstances=e}}}),MDState.prototype.defineFootnote=function(e,t){this.footnotes[e]=t},MDState.prototype.registerUniqueFootnote=function(e,t){var n=this.footnoteInstances[e]||[];n.push(t),this.footnoteInstances[e]=n}}constructor(e=40){super(e)}readBlock(e){var t=e.p;let n=/^\s*\[\^\s*([^\]]+)\s*\]:\s+(.*)\s*$/.exec(e.lines[t++]);if(null===n)return null;let i=n[1],s=n[2];for(;e.hasLines(1,t);){let n=e.lines[t++];if(!/^\s+/.exec(n)){t--;break}s+="\n"+n}e.p=t;let r=e.inlineMarkdownToSpan(s);return e.defineFootnote(i,r),e.p=t,new MDMultiBlock([])}postProcess(e,t){var n=1;for(const i of t)i.visitChildren((function(t){t instanceof MDFootnoteReferenceSpan&&(t.differentiator=n++,e.registerUniqueFootnote(t.symbol,t.differentiator))}));0!=Object.keys(e.footnotes).length&&t.push(new MDFootnoteListingBlock)}}class MDAbbreviationDefinitionBlockReader extends MDBlockReader{static{Object.defineProperties(MDState.prototype,{abbreviations:{get:function(){return void 0===this.root._abbreviations&&(this.root._abbreviations={}),this.root._abbreviations},set:function(e){this.root._abbreviations=e}},abbreviationRegexes:{get:function(){return void 0===this.root._abbreviationRegexes&&(this.root._abbreviationRegexes={}),this.root._abbreviationRegexes},set:function(e){this.root._abbreviationRegexes=e}}}),MDState.prototype.defineAbbreviation=function(e,t){this.abbreviations[e]=t;const n=new RegExp("\\b("+MDUtils.escapeRegex(e)+")\\b","ig");this.abbreviationRegexes[e]=n}}constructor(e=45){super(e)}readBlock(e){var t=e.p;let n=e.lines[t++],i=/^\s*\*\[([^\]]+?)\]:\s+(.*?)\s*$/.exec(n);if(null===i)return null;let s=i[1],r=i[2];return e.defineAbbreviation(s,r),e.p=t,new MDMultiBlock([])}}class MDURLDefinitionBlockReader extends MDBlockReader{constructor(e=50){super(e)}readBlock(e){var t=e.p;let n=e.lines[t++];var i,s,r=null;let o=/^\s*\[(.+?)]:\s*(\S+)\s+"(.*?)"\s*$/.exec(n);if(o)i=o[1],s=o[2],r=o[3];else{if(o=/^\s*\[(.+?)]:\s*(\S+)\s*$/.exec(n),!o)return null;i=o[1],s=o[2]}return e.defineURL(i,s,r),e.p=t,new MDInlineBlock([])}}class MDParagraphBlockReader extends MDBlockReader{constructor(e=100){super(e)}readBlock(e){for(var t=[],n=e.p;n<e.lines.length;){let i=e.lines[n++];if(0==i.trim().length){0;break}t.push(i)}if(0==e.p&&n>=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`<p${this.htmlAttributes()}>${t}</p>\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`<h${this.level}${this.htmlAttributes()}>${t}</h${this.level}>\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`<blockquote${this.htmlAttributes()}>\n${t}\n</blockquote>`}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`<ul${this.htmlAttributes()}>\n${t}\n</ul>`}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`<ol${this.htmlAttributes()}>\n${t}\n</ol>`}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`<li${this.htmlAttributes()}>${t}</li>`}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`<pre${this.htmlAttributes()}><code>${MDUtils.escapeHTML(this.code)}</code></pre>`}toPlaintext(e){return this.code}}class MDHorizontalRuleBlock extends MDBlock{toHTML(e){return`<hr${this.htmlAttributes()}>\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`<td${this.htmlAttributes()}>${t}</td>`}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`<th${/^<td(.*)td>$/.exec(t)[1]}th>`}}class MDTableRowBlock extends MDBlock{cells;constructor(e){super(),this.cells=e}applyAlignments(e){for(var t=0;t<this.cells.length;t++){let n=this.cells[t],i=t<e.length?e[t]:null;n.align=i}}toHTML(e){let t=MDBlock.toHTML(this.cells,e);return`<tr${this.htmlAttributes()}>\n${t}\n</tr>`}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`<table${this.htmlAttributes()}>\n<thead>\n${t}\n</thead>\n<tbody>\n${n}\n</tbody>\n</table>`}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`<dl${this.htmlAttributes()}>\n${t}\n</dl>`}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`<dt${this.htmlAttributes()}>${t}</dt>`}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`<dd${this.htmlAttributes()}>${t}</dd>`}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+='<div class="footnotes"><hr/>',s+="<ol>";for(const r of n){let n=t[r];if(n){s+=`<li value="${r}" id="footnote_${r}">${n.toHTML(e)}`;for(const e of i[r])s+=` <a href="#footnoteref_${e}" class="footnote-backref">↩︎</a>`;s+="</li>\n"}}return s+="</ol>",s+="</div>"}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,`<abbr title="${o}">$1</abbr>`)}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=`<a href="${this.link.startsWith("mailto:")?"mailto:"+MDUtils.escapeObfuscated(this.link.substring(7)):MDUtils.escapeHTML(this.link)}"`;return this.target&&(t+=` target="${MDUtils.escapeHTML(this.target)}"`),this.title&&(t+=` title="${MDUtils.escapeHTML(this.title)}"`),t+=this.htmlAttributes(),t+=">"+this.content.toHTML(e)+"</a>"}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`<em${this.htmlAttributes()}>${t}</em>`}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`<strong${this.htmlAttributes()}>${t}</strong>`}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`<strike${this.htmlAttributes()}>${t}</strike>`}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`<code${this.htmlAttributes()}>${MDUtils.escapeHTML(this.content)}</code>`}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=`<img src="${MDUtils.escapeHTML(this.source)}"`;return this.alt&&(t+=` alt="${MDUtils.escapeHTML(this.alt)}"`),this.title&&(t+=` title="${MDUtils.escapeHTML(this.title)}"`),t+=this.htmlAttributes(),t+=">",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?`<sup id="footnoteref_${this.differentiator}"><a href="#footnote_${this.symbol}">${this.symbol}</a></sup>`:`\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<e.length&&null===l;u++){let o=e.substring(u,u+1),h=null!==this.#_.exec(o);switch(c){case 0:if("<"!=o)return null;c=1;break;case 1:"/"==o?t=!0:u--,c=2;break;case 2:if(0==n.length){if(null===this.#A.exec(o))return null;n+=o}else this.#U.exec(o)?n+=o:(u--,c=t?6:3);break;case 3:if(0==i.length)if(h);else if("/"==o)c=6;else{if(">"==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)}}