PHP and Javascript implementations of a simple markdown parser
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

markdown.min.js 42KB

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 Equal=new MDTokenType("Equal");static Caret=new MDTokenType("Caret");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,s=null,r=null){this.original=e,this.type=t,n instanceof MDTagModifier?(this.content=null,this.modifier=n):(this.content=n,this.modifier=null),this.extra=s,this.tag=r}toString(){return`(${this.constructor.name} type=${this.type.toString()} content=${this.content})`}static findFirstTokens(e,t,n=0){for(var s=[],r=n;r<e.length;r++){var i=!0;s=[];for(var o=0,a=0;a<t.length;a++){var l=r+a+o;if(l>=e.length)return null;let n=e[l],c=t[a];if(c==MDTokenType.META_OptionalWhitespace)n instanceof MDToken&&n.type==MDTokenType.Whitespace?s.push(n):o--;else if(c==MDTokenType.META_AnyNonWhitespace){if(n instanceof MDToken&&n.type==MDTokenType.Whitespace){i=!1;break}s.push(n)}else{if(!(n instanceof MDToken)||n.type!=c){i=!1;break}s.push(n)}}if(i)return{tokens:s,index:r}}return null}static findPairedTokens(e,t,n,s=null,r=0){for(var i=r;i<e.length;i++){var o=this.findFirstTokens(e,t,i);if(null===o)return null;for(var a=o.index+o.tokens.length;a<e.length;){var l=this.findFirstTokens(e,n,a);if(null===l)break;var c=e.slice(o.index+o.tokens.length,l.index);if(c.length>0&&(null===s||s(c)))return{startTokens:o.tokens,contentTokens:c,endTokens:l.tokens,startIndex:o.index,contentIndex:o.index+o.tokens.length,endIndex:l.index,totalLength:l.index+l.tokens.length-o.index};a=l.index+1}i=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 unescapeHTML(e,t=!1){t&&(e=e.replace(/<br[\/]?>\n?/g,"\n"));return(new DOMParser).parseFromString(e,"text/html").documentElement.textContent}static escapeObfuscated(e){if(e.startsWith("mailto:"))return"mailto:"+this.escapeObfuscated(e.substring(7));for(var t="",n=0;n<e.length;n++){t+=`&#${e.codePointAt(n)};`}return t}static scrubAttributeName(e){return e.replace(/[\t\n\f \/>"'=]+/,"")}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,s=1;s<e.length;s++){let r=e.substring(s,s+1);if("\\"==r)s++;else if("("==r)t++;else if(")"==r){if(--t<0)return null}else if("["==r)n++;else if("]"==r){if(!(n>0))return[e.substring(0,s+1),e.substring(1,s)];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#s=new RegExp("^\\(\\s*("+MDUtils.baseEmailRegex.source+")\\s*\\)","i");static tokenizeEmail(e){var t;return(t=this.#n.exec(e))?t:(t=this.#s.exec(e))?[...t,null]:null}static typename(e){return null===e?"null":e instanceof Object?e.constructor.name:typeof e}static#r(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#i(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.#r(e,t);if(e instanceof Object&&t instanceof Object)return this.#i(e,t);if("number"==typeof e&&"number"==typeof t){if(e===t)return!0;const s=(t-e)/e;return Math.abs(s)<=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 s=e.length;for(var r=0;r<s;r++){const s=e.substring(r,r+1);0==r&&/[a-zA-Z0-9]/.exec(s)?n+=t(s):"^$\\.*+?()[]{}|/".indexOf(s)>=0?n+=`\\${s}`:",-=<>#&!%:;@~'`\"".indexOf(s)>=0?n+=t(s):n+="\f"==s?"\\f":"\n"==s?"\\n":"\r"==s?"\\r":"\t"==s?"\\t":"\v"==s?"\\v":s}return n}static replaceNodes(e,t,n){for(var s=0;s<t.length;s++){var r=t[s];const i=n(r);null!==i?t.splice(s,1,i):this.replaceNodes(e,r.children,n)}}}class MDState{get root(){return this.#o?this.#o.root:this}get lines(){return this.#a}get currentLine(){return this.p<this.#a.length?this.#a[this.p]:null}p=0;#a=[];#o=null;config;#l=[];#c=[];#d=[];#u={};#h={};tagFilter;static#M=/^(\s*)(?:(\S|\S.*\S)(\s*?))?$/;constructor(e,t=null,n=null,s=null,r=null,i=null){this.#a=e,this.config=t,this.#l=n,this.#c=s,this.#d=r,this.tagFilter=i}copy(e){let t=new MDState(e);return t.#o=this,t.config=this.config,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.#T();if(!t)break;e.push(t)}return e}#f(){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,this.inlineMarkdownToNode(e.join("\n")))}#T(){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.#l){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.#f()}#p(e){if(this.#o)return this.#o.#p(e);var t=[],n="",s=!1;const r=function(){if(0==n.length)return;const e=MDState.#M.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 i=0;i<e.length;i++){const a=e.substring(i,i+1),l=e.substring(i);if(s)n+=a,s=!1;else if("\\"!=a){var o=!1;for(const e of this.root.#c){const n=e.readToken(this,l);if(null!==n){if(void 0===n&&console.warn(`${e.constructor.name}.readToken returned undefined instead of null`),r(),t.push(n),null==n.original||0==n.original.length)throw new Error(`${e.constructor.name} returned a token with an empty .original. This would cause an infinite loop.`);i+=n.original.length-1,o=!0;break}}o||(n+=a)}else s=!0}return r(),t}inlineMarkdownToNode(e){let t=this.inlineMarkdownToNodes(e);return 1==t.length?t[0]:new MDInlineNode(t)}inlineMarkdownToNodes(e){var t=this.#p(e instanceof Array?e.join("\n"):e);return this.tokensToNodes(t)}tokensToNodes(e){var t=e.slice(),n=!1;do{n=!1;for(const e of this.root.#d){const s=e[0];if(e[1].substituteTokens(this,s,t)){n=!0;break}}}while(n);var s=null;const r=this;return t=t.map((function(e){if(e instanceof MDToken){const t=e;return t.type==MDTokenType.Modifier&&s?(r.root.tagFilter.scrubModifier(t.modifier),t.modifier.applyTo(s),s=null,new MDTextNode("")):(s=null,new MDTextNode(t.original))}if(e instanceof MDNode)return s=e instanceof MDTextNode?null:e,e;throw new Error(`Unexpected node type ${e.constructor.name}`)}))}defineURL(e,t,n=null){this.root.#u[e.toLowerCase()]=t,null!==n&&(this.root.#h[e.toLowerCase()]=n)}urlForReference(e){return this.root.#u[e.toLowerCase()]??null}urlTitleForReference(e){return this.root.#h[e.toLowerCase()]??null}}class MDReader{preProcess(e){}readBlock(e){return null}readToken(e,t){return null}substituteTokens(e,t,n){return!1}postProcess(e,t){}compareBlockOrdering(e){return 0}compareTokenizeOrdering(e){return 0}compareSubstituteOrdering(e,t){return 0}get substitutionPassCount(){return 1}static#g(e,t,n){const s={},r={},i={};for(const t of e){const e=n(t);s[e]=[],r[e]=0,i[e]=t}for(let i=0;i<e.length;i++){const o=e[i],a=n(o);for(let l=0;l<e.length;l++){if(i===l)continue;const c=e[l],d=n(c),u=t(o,c);u<0?(s[a].push(d),r[d]++):u>0&&(s[d].push(a),r[a]++)}}const o=[];for(const e in r)0===r[e]&&o.push(e);const a=[];for(;o.length>0;){const e=o.shift();a.push(i[e]),delete i[e];for(const t of s[e])r[t]--,0===r[t]&&o.push(t)}for(const e in i)a.push(i[e]);return a}static sortReaderForBlocks(e){const t=e.slice();return MDReader.#g(t,((e,t)=>e.compareBlockOrdering(t)),(e=>e.constructor.name))}static sortReadersForTokenizing(e){const t=e.slice();return MDReader.#g(t,((e,t)=>e.compareTokenizeOrdering(t)),(e=>e.constructor.name))}static sortReadersForSubstitution(e){var t=[],n=1;for(const r of e){const e=r.substitutionPassCount;for(var s=1;s<=e;s++)t.push([s,r]);n=Math.max(n,s)}var r=[];for(s=1;s<=n;s++){var i=t.filter((e=>e[0]==s));const e=MDReader.#g(i,((e,t)=>{const n=e[1],r=t[1];return n.compareSubstituteOrdering(r,s)}),(e=>`${e[1].constructor.name}:${e[0]}`));r=r.concat(e)}return r}}class MDUnderlinedHeadingReader extends MDReader{readBlock(e){var t,n=e.p;if(!e.hasLines(2))return null;let s=e.lines[n++].trim();[s,t]=MDTagModifier.fromLine(s);let r=e.lines[n++].trim();if(""==s)return null;if(/^=+$/.exec(r)){e.p=n;let r=new MDHeadingNode(1,e.inlineMarkdownToNodes(s));return t&&t.applyTo(r),r}if(/^\-+$/.exec(r)){e.p=n;let r=new MDHeadingNode(2,e.inlineMarkdownToNodes(s));return t&&t.applyTo(r),r}return null}}class MDHashHeadingReader extends MDReader{static#D=/^(#{1,6})\s*([^#].*?)\s*\#*\s*$/;readBlock(e){var t=e.p;let n=e.lines[t++];var s;[n,s]=MDTagModifier.fromLine(n);var r=MDHashHeadingReader.#D.exec(n);if(null===r)return null;e.p=t;const i=r[1].length,o=r[2];let a=new MDHeadingNode(i,e.inlineMarkdownToNodes(o));return s&&s.applyTo(a),a}}class MDSubtextReader extends MDReader{static#k=/^\-#\s*(.*?)\s*$/;readBlock(e){var t=e.p;let n=e.lines[t++];var s;[n,s]=MDTagModifier.fromLine(n);var r=MDSubtextReader.#k.exec(n);if(null===r)return null;e.p=t;const i=r[1];let o=new MDSubtextNode(e.inlineMarkdownToNodes(i));return s&&s.applyTo(o),o}compareBlockOrdering(e){return e instanceof MDUnorderedListReader?-1:0}}class MDBlockQuoteReader extends MDReader{readBlock(e){for(var t=[],n=e.p;n<e.lines.length;){let s=e.lines[n++];if(!s.startsWith(">"))break;t.push(s)}if(t.length>0){let s=t.map((function(e){return e.substring(1).replace(/^ {0,3}\t?/,"")})),r=e.copy(s).readBlocks();return e.p=n,new MDBlockquoteNode(r)}return null}}class _MDListReader extends MDReader{#b(e,t){for(var n=e.p,s=[],r=!1;e.hasLines(1,n);){const o=n==e.p;var i=e.lines[n++];if(o&&(i=i.substring(t)),/^(?:\*|\+|\-|\d+\.)\s+/.exec(i)){0;break}const a=0==i.trim().length,l=null!==/^\s+\S/.exec(i);if(a)r=!0;else if(!l&&r)break;s.push(i)}return s=MDUtils.withoutTrailingBlankLines(s),MDUtils.stripIndent(s)}_readListItemContent(e,t){const n=this.#b(e,t);if(e.p+=Math.max(n.length,1),1==n.length)return e.inlineMarkdownToNode(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 MDNode(t)}for(var s=1;s<n.length;s++){const t=n[s];if(/^(?:\*|\-|\+|\d+\.)\s+/.exec(t)){const t=e.inlineMarkdownToNode(n.slice(0,s).join("\n")),r=e.copy(n.slice(s)).readBlocks();return new MDNode([t,...r])}}{const t=e.copy(n).readBlocks();return 1==t.length?t[0]:new MDNode(t)}}readBlock(e){throw new Error(`Abstract readBlock must be overridden in ${this.constructor.name}`)}}class MDUnorderedListReader extends _MDListReader{static#m=/^([\*\+\-]\s+)(.*)$/;#x(e){var t=e.p;let n=e.lines[t],s=MDUnorderedListReader.#m.exec(n);if(null===s)return null;const r=s[1].length;return new MDListItemNode(this._readListItemContent(e,r))}readBlock(e){var t=[],n=null;do{(n=this.#x(e))&&t.push(n)}while(n);return 0==t.length?null:new MDUnorderedListNode(t)}}class MDOrderedListReader extends _MDListReader{static#R=/^(\d+)(\.\s+)(.*)$/;#y(e){var t=e.p;let n=e.lines[t],s=MDOrderedListReader.#R.exec(n);if(null===s)return null;const r=parseInt(s[1]),i=s[1].length+s[2].length;return new MDListItemNode(this._readListItemContent(e,i),r)}readBlock(e){var t=[],n=null;do{(n=this.#y(e))&&t.push(n)}while(n);return 0==t.length?null:new MDOrderedListNode(t,t[0].ordinal)}}class MDFencedCodeBlockReader extends MDReader{readBlock(e){if(!e.hasLines(2))return null;var t=e.p;let n=e.lines[t++];var s;if([n,s]=MDTagModifier.fromLine(n),"```"!=n.trim())return null;for(var r=[];e.hasLines(1,t);){let n=e.lines[t++];if("```"==n.trim()){e.p=t;let n=new MDCodeBlockNode(r.join("\n"));return s&&s.applyTo(n),n}r.push(n)}return null}}class MDIndentedCodeBlockReader extends MDReader{readBlock(e){for(var t=e.p,n=[];e.hasLines(1,t);){let s=e.lines[t++];if(MDUtils.countIndents(s,!0)<1){t--;break}n.push(MDUtils.stripIndent(s))}return 0==n.length?null:(e.p=t,new MDCodeBlockNode(n.join("\n")))}}class MDHorizontalRuleReader extends MDReader{static#w=/^\s*(?:\-(?:\s*\-){2,}|\*(?:\s*\*){2,})\s*$/;readBlock(e){var t=e.p;let n=e.lines[t++];var s;if([n,s]=MDTagModifier.fromLine(n),MDHorizontalRuleReader.#w.exec(n)){e.p=t;let n=new MDHorizontalRuleNode;return s&&s.applyTo(n),n}return null}compareBlockOrdering(e){return e instanceof MDUnorderedListReader?-1:0}}class MDTableReader extends MDReader{#L(e,t){if(!e.hasLines(1))return null;var n=e.p;let s=MDTagModifier.strip(e.lines[n++].trim());if(null===/.*\|.*/.exec(s))return null;s.startsWith("|")&&(s=s.substring(1)),s.endsWith("|")&&(s=s.substring(0,s.length-1));let r=s.split("|").map((function(n){let s=e.inlineMarkdownToNode(n.trim());return t?new MDTableHeaderCellNode(s):new MDTableCellNode(s)}));return e.p=n,new MDTableRowNode(r)}#N(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(":")?"center":"left":e.endsWith(":")?"right":null}))}static#v=/^\s*[|]?\s*(?:[:]?-+[:]?)(?:\s*\|\s*[:]?-+[:]?)*\s*[|]?\s*$/;readBlock(e){if(!e.hasLines(2))return null;let t=e.p,n=e.lines[t];var s=MDTagModifier.fromLine(n)[1];let r=this.#L(e,!0);if(null===r)return e.p=t,null;let i=e.lines[e.p++];if(null===MDTableReader.#v.exec(i))return e.p=t,null;let o=this.#N(i);for(var a=[];e.hasLines(1);){let t=this.#L(e,!1);if(null===t)break;a.push(t)}let l=new MDTableNode(r,a);return l.columnAlignments=o,s&&s.applyTo(l),l}}class MDDefinitionListReader extends MDReader{readBlock(e){for(var t,n=e.p,s=0,r=0,i=[];e.hasLines(1,n);){let t=e.lines[n++];if(0==t.trim().length)break;if(/^\s+/.exec(t)){if(0==i.length)return null;i[i.length-1]+="\n"+t}else/^:\s+/.exec(t)?(i.push(t),r++):(i.push(t),s++)}if(0==s||0==r)return null;let o=i.map((function(n){return(t=/^:\s+(.*?)$/s.exec(n))?new MDDefinitionListDefinitionNode(e.inlineMarkdownToNodes(t[1])):new MDDefinitionListTermNode(e.inlineMarkdownToNodes(n))}));return e.p=n,new MDDefinitionListNode(o)}}class MDFootnoteReader extends MDReader{static#S=/^\[\^([^\]]+?)\s+"(.*?)"\]/;static#H=/^\[\^([^\]]+?)\]/;#B(e,t,n){var s=e.root.footnotes??{};s[t]=n,e.root.footnotes=s}#$(e,t,n){var s=e.root.footnoteInstances,r=s[t]??[];r.push(n),s[t]=r}#I(e,t){var n=e.root.footnoteIds;const s=n[t];if(s)return s;var r=e.root.nextFootnoteId;const i=r++;return n[t]=i,e.root.nextFootnoteId=r,i}preProcess(e){e.root.footnoteInstances={},e.root.footnotes={},e.root.footnoteIds={},e.root.nextFootnoteId=1}readBlock(e){var t=e.p;let n=/^\s*\[\^\s*([^\]]+)\s*\]:\s+(.*)\s*$/.exec(e.lines[t++]);if(null===n)return null;let s=n[1],r=n[2];for(;e.hasLines(1,t);){let n=e.lines[t++];if(!/^\s+/.exec(n)){t--;break}r+="\n"+n}let i=e.inlineMarkdownToNodes(r);return this.#B(e,s,i),e.p=t,new MDNode}readToken(e,t){var n;return(n=MDFootnoteReader.#S.exec(t))?new MDToken(n[0],MDTokenType.Footnote,n[1],n[2]):(n=MDFootnoteReader.#H.exec(t))?new MDToken(n[0],MDTokenType.Footnote,n[1]):null}substituteTokens(e,t,n){var s;if(s=MDToken.findFirstTokens(n,[MDTokenType.Footnote])){let e=s.tokens[0].content;return n.splice(s.index,1,new MDFootnoteNode(e)),!0}return!1}postProcess(e,t){var n=1;for(const s of t){const t=this;s.visitChildren(function(s){s instanceof MDFootnoteNode&&(s.footnoteId=t.#I(e,s.symbol),s.occurrenceId=n++,s.displaySymbol=`${s.footnoteId}`,t.#$(e,s.symbol,s.occurrenceId))}.bind(this))}0!=Object.keys(e.footnotes).length&&t.push(new MDFootnoteListNode)}compareBlockOrdering(e){return e instanceof MDLinkReader||e instanceof MDImageReader?-1:0}compareTokenizeOrdering(e){return e instanceof MDLinkReader||e instanceof MDImageReader?-1:0}compareSubstituteOrdering(e,t){return e instanceof MDLinkReader||e instanceof MDImageReader?-1:0}}class MDAbbreviationReader extends MDReader{#P(e,t,n){e.abbreviations[t]=n;const s=new RegExp("\\b("+MDUtils.escapeRegex(t)+")\\b","ig");e.abbreviationRegexes[t]=s}preProcess(e){e.root.abbreviations={},e.root.abbreviationRegexes={}}readBlock(e){var t=e.p;let n=e.lines[t++],s=/^\s*\*\[([^\]]+?)\]:\s+(.*?)\s*$/.exec(n);if(null===s)return null;let r=s[1],i=s[2];return this.#P(e,r,i),e.p=t,new MDNode}postProcess(e,t){const n=e.root.abbreviations,s=e.root.abbreviationRegexes;MDUtils.replaceNodes(e,t,(e=>{if(!(e instanceof MDTextNode))return null;for(var t=!1,r=[e.text],i=0;i<r.length;i++){var o=r[i];if("string"==typeof o)for(const e in n){const a=s[e].exec(o);if(null===a)continue;const l=n[e],c=o.substring(0,a.index),d=o.substring(a.index+a[0].length);r.splice(i,1,c,new MDAbbreviationNode(a[0],l),d),i=-1,t=!0;break}}if(!t)return null;const a=r.map((e=>"string"==typeof e?new MDTextNode(e):e));return new MDNode(a)}))}}class MDParagraphReader extends MDReader{readBlock(e){for(var t=[],n=e.p;n<e.lines.length;){let s=e.lines[n++];if(0==s.trim().length)break;t.push(s)}if(0==e.p&&n>=e.lines.length)return null;if(t.length>0){e.p=n;let s=t.join("\n");return new MDParagraphNode(e.inlineMarkdownToNodes(s))}return null}compareBlockOrdering(e){return 1}}class MDSimplePairInlineReader extends MDReader{get substitutionPassCount(){return 4}attemptPair(e,t,n,s,r,i=1,o=!1){if(1==i&&2!=t&&4!=t)return;if(2==i&&1!=t&&3!=t)return;let a=Array(i).fill(r);const l=this.substitutionPassCount>1&&1==t;let c=MDToken.findPairedTokens(n,a,a,(function(e){const t=e[0]instanceof MDToken?e[0].type:null,n=e[e.length-1]instanceof MDToken?e[e.length-1].type:null;if(t==MDTokenType.Whitespace)return!1;if(n==MDTokenType.Whitespace)return!1;for(const t of e)if(t.constructor==s)return!1;if(l){var i=0;for(let t of e)t instanceof MDToken&&t.type==r&&i++;if(i%2!=0)return!1}return!0}));if(null===c)return!1;let d=o?c.contentTokens.map((e=>e.original)).join(""):e.tokensToNodes(c.contentTokens);return n.splice(c.startIndex,c.totalLength,new s(d)),!0}}class MDEmphasisReader extends MDSimplePairInlineReader{readToken(e,t){return t.startsWith("*")?new MDToken("*",MDTokenType.Asterisk):t.startsWith("_")?new MDToken("_",MDTokenType.Underscore):null}substituteTokens(e,t,n){return!!this.attemptPair(e,t,n,MDEmphasisNode,MDTokenType.Asterisk)||!!this.attemptPair(e,t,n,MDEmphasisNode,MDTokenType.Underscore)}compareSubstituteOrdering(e,t){return e instanceof MDStrongReader?1:0}}class MDStrongReader extends MDSimplePairInlineReader{readToken(e,t){return t.startsWith("*")?new MDToken("*",MDTokenType.Asterisk):t.startsWith("_")?new MDToken("_",MDTokenType.Underscore):null}substituteTokens(e,t,n){return!!this.attemptPair(e,t,n,MDStrongNode,MDTokenType.Asterisk,2)||!!this.attemptPair(e,t,n,MDStrongNode,MDTokenType.Underscore,2)}compareSubstituteOrdering(e,t){return e instanceof MDEmphasisReader?-1:0}}class MDStrikethroughReader extends MDSimplePairInlineReader{readToken(e,t){return t.startsWith("~")?new MDToken("~",MDTokenType.Tilde):null}substituteTokens(e,t,n){return!!this.attemptPair(e,t,n,MDStrikethroughNode,MDTokenType.Tilde,2)||!(!e.config.strikethroughSingleTildeEnabled||!this.attemptPair(e,t,n,MDStrikethroughNode,MDTokenType.Tilde))}}class MDUnderlineReader extends MDSimplePairInlineReader{readToken(e,t){return t.startsWith("_")?new MDToken("_",MDTokenType.Underscore):null}substituteTokens(e,t,n){return this.attemptPair(e,t,n,MDUnderlineNode,MDTokenType.Underscore,2)}compareSubstituteOrdering(e,t){return e instanceof MDStrongReader?-1:0}}class MDHighlightReader extends MDSimplePairInlineReader{readToken(e,t){return t.startsWith("=")?new MDToken("=",MDTokenType.Equal):null}substituteTokens(e,t,n){return this.attemptPair(e,t,n,MDHighlightNode,MDTokenType.Equal,2)}}class MDLinkReader extends MDReader{static#A=new RegExp("^<("+MDUtils.baseEmailRegex.source+")>","i");static#C=new RegExp("^<("+MDUtils.baseURLRegex.source+")>","i");readToken(e,t){var n;return(n=MDUtils.tokenizeLabel(t))?new MDToken(n[0],MDTokenType.Label,n[1]):(n=MDUtils.tokenizeEmail(t))?new MDToken(n[0],MDTokenType.Email,n[1],n[2]):(n=MDUtils.tokenizeURL(t))?new MDToken(n[0],MDTokenType.URL,n[1],n[2]):(n=MDLinkReader.#A.exec(t))?new MDToken(n[0],MDTokenType.SimpleEmail,n[1]):(n=MDLinkReader.#C.exec(t))?new MDToken(n[0],MDTokenType.SimpleLink,n[1]):null}substituteTokens(e,t,n){var s;if(s=MDToken.findFirstTokens(n,[MDTokenType.Label,MDTokenType.META_OptionalWhitespace,MDTokenType.URL])){let t=s.tokens[0].content,r=s.tokens[s.tokens.length-1].content,i=s.tokens[s.tokens.length-1].extra;return n.splice(s.index,s.tokens.length,new MDLinkNode(r,e.inlineMarkdownToNode(t),i)),!0}if(s=MDToken.findFirstTokens(n,[MDTokenType.Label,MDTokenType.META_OptionalWhitespace,MDTokenType.Email])){let t=s.tokens[0].content,r=`mailto:${s.tokens[s.tokens.length-1].content}`,i=s.tokens[s.tokens.length-1].extra;return n.splice(s.index,s.tokens.length,new MDLinkNode(r,e.inlineMarkdownToNodes(t),i)),!0}if(s=MDToken.findFirstTokens(n,[MDTokenType.SimpleEmail])){const e=s.tokens[0],t=`mailto:${e.content}`,r=new MDLinkNode(t,new MDObfuscatedTextNode(e.content));return n.splice(s.index,1,r),!0}if(s=MDToken.findFirstTokens(n,[MDTokenType.SimpleLink])){const e=s.tokens[0].content,t=new MDLinkNode(e,new MDTextNode(e));return n.splice(s.index,1,t),!0}return!1}}class MDReferencedLinkReader extends MDLinkReader{readBlock(e){var t=e.p;let n=e.lines[t++];var s,r,i=null;let o=/^\s*\[(.+?)]:\s*(\S+)\s+"(.*?)"\s*$/.exec(n);if(o)s=o[1],r=o[2],i=o[3];else{if(o=/^\s*\[(.+?)]:\s*(\S+)\s*$/.exec(n),!o)return null;s=o[1],r=o[2]}return e.defineURL(s,r,i),e.p=t,new MDNode([])}substituteTokens(e,t,n){var s;if(s=MDToken.findFirstTokens(n,[MDTokenType.Label,MDTokenType.META_OptionalWhitespace,MDTokenType.Label])){let t=s.tokens[0].content,r=s.tokens[s.tokens.length-1].content;return n.splice(s.index,s.tokens.length,new MDReferencedLinkNode(r,e.inlineMarkdownToNodes(t))),!0}return!1}}class MDImageReader extends MDLinkReader{readToken(e,t){const n=super.readToken(e,t);return n||(t.startsWith("!")?new MDToken("!",MDTokenType.Bang):null)}substituteTokens(e,t,n){var s;if(s=MDToken.findFirstTokens(n,[MDTokenType.Bang,MDTokenType.Label,MDTokenType.META_OptionalWhitespace,MDTokenType.URL])){let e=s.tokens[1].content,t=s.tokens[s.tokens.length-1].content,r=s.tokens[s.tokens.length-1].extra;const i=new MDImageNode(t,e);return null!==r&&(i.attributes.title=r),n.splice(s.index,s.tokens.length,i),!0}if(s=MDToken.findFirstTokens(n,[MDTokenType.Bang,MDTokenType.Label,MDTokenType.META_OptionalWhitespace,MDTokenType.Label])){let e=s.tokens[1].content,t=s.tokens[s.tokens.length-1].content;return n.splice(s.index,s.tokens.length,new MDReferencedImageNode(t,e)),!0}return!1}compareSubstituteOrdering(e,t){return e instanceof MDLinkReader?-1:0}}class MDReferencedImageReader extends MDReferencedLinkReader{readBlock(e){return null}compareSubstituteOrdering(e,t){return e instanceof MDLinkReader?-1:0}}class MDCodeSpanReader extends MDSimplePairInlineReader{readToken(e,t){return t.startsWith("`")?new MDToken("`",MDTokenType.Backtick):null}substituteTokens(e,t,n){return!!this.attemptPair(e,t,n,MDCodeNode,MDTokenType.Backtick,2,!0)||(!!this.attemptPair(e,t,n,MDCodeNode,MDTokenType.Backtick,1,!0)||void 0)}}class MDSubscriptReader extends MDSimplePairInlineReader{readToken(e,t){return t.startsWith("~")?new MDToken("~",MDTokenType.Tilde):null}preProcess(e){e.config.strikethroughSingleTildeEnabled=!1}substituteTokens(e,t,n){return this.attemptPair(e,t,n,MDSubscriptNode,MDTokenType.Tilde)}compareSubstituteOrdering(e,t){return e instanceof MDStrikethroughReader?-1:0}}class MDSuperscriptReader extends MDSimplePairInlineReader{readToken(e,t){return t.startsWith("^")?new MDToken("^",MDTokenType.Caret):null}substituteTokens(e,t,n){return this.attemptPair(e,t,n,MDSuperscriptNode,MDTokenType.Caret)}}class MDHTMLTagReader extends MDReader{readToken(e,t){const n=MDHTMLTag.fromLineStart(t);return null===n?null:e.root.tagFilter.isValidTagName(n.tagName)?(e.root.tagFilter.scrubTag(n),new MDToken(n.original,MDTokenType.HTMLTag,null,null,n)):null}substituteTokens(e,t,n){var s;if(s=MDToken.findFirstTokens(n,[MDTokenType.HTMLTag])){const e=s.tokens[0].tag;return n.splice(s.index,s.tokens.length,new MDHTMLTagNode(e)),!0}return!1}}class MDModifierReader extends MDReader{readToken(e,t){var n=MDTagModifier.fromStart(t);return n?new MDToken(n.original,MDTokenType.Modifier,n):null}substituteTokens(e,t,n){return!1}}class MDNode{cssClasses=[];cssId=null;cssStyles={};attributes={};children;constructor(e=[]){if(e instanceof Array){for(const t of e)if(!(t instanceof MDNode))throw new Error(`${this.constructor.name} expects children of type MDNode[] or MDNode, got array with ${MDUtils.typename(t)} element`);this.children=e}else{if(!(e instanceof MDNode))throw new Error(`${this.constructor.name} expects children of type MDNode[] or MDNode, got ${MDUtils.typename(e)}`);this.children=[e]}}toHTML(e){return MDNode.toHTML(this.children,e)}toPlaintext(e){return MDNode.toPlaintext(this.children,e)}_htmlAttributes(){var e="";this.cssClasses.length>0&&(e+=` class="${this.cssClasses.join(" ")}"`),null!==this.cssId&&this.cssId.length>0&&(e+=` id="${this.cssId}"`);var t=[];for(const e in this.cssStyles)t.push(`${e}: ${this.cssStyles[e]};`);t.length>0&&(e+=` style="${MDUtils.escapeHTML(t.join(" "))}"`);for(const t in this.attributes){if("class"==t||"id"==t||"style"==t)continue;const n=`${this.attributes[t]}`,s=MDUtils.scrubAttributeName(t);if(0==s.length)continue;e+=` ${s}="${MDUtils.escapeHTML(n)}"`}return e}_childHTML(e){return this.children.map((t=>t.toHTML(e))).join("")}_simplePairedTagHTML(e,t,n=!1){const s=this.children[0]instanceof MDBlockNode?"\n":"",r=this.children[this.children.length-1]instanceof MDBlockNode?"\n":"",i=this instanceof MDBlockNode?"\n":"";return`<${t}${this._htmlAttributes()}>${s}${this._childHTML(e)}${r}</${t}>${i}`}visitChildren(e){if(void 0!==this.children&&Array.isArray(this.children))for(const t of this.children)e(t),t.visitChildren(e)}static toHTML(e,t){return e.map((e=>e.toHTML(t)+(e instanceof MDBlockNode?"\n":""))).join("")}static toPlaintext(e,t){return e.map((e=>e.toPlaintext(t))).join("")}}class MDBlockNode extends MDNode{}class MDParagraphNode extends MDBlockNode{toHTML(e){return this._simplePairedTagHTML(e,"p")}}class MDHeadingNode extends MDBlockNode{level;constructor(e,t){if(super(t),"number"!=typeof e||e<1||e>6)throw new Error(`${this.constructor.name} requires heading level 1 to 6`);this.level=e}toHTML(e){return this._simplePairedTagHTML(e,`h${this.level}`)}}class MDSubtextNode extends MDBlockNode{toHTML(e){return this.cssClasses.indexOf("subtext")<0&&this.cssClasses.push("subtext"),this._simplePairedTagHTML(e,"div")}}class MDHorizontalRuleNode extends MDBlockNode{toHTML(e){return`<hr${this._htmlAttributes()}>`}}class MDBlockquoteNode extends MDBlockNode{toHTML(e){return this._simplePairedTagHTML(e,"blockquote",!0)}}class MDUnorderedListNode extends MDBlockNode{constructor(e){super(e)}toHTML(e){return this._simplePairedTagHTML(e,"ul",!0)}}class MDOrderedListNode extends MDBlockNode{startOrdinal;constructor(e,t=null){super(e),this.startOrdinal=t}toHTML(e){return null!==this.startOrdinal&&1!=this.startOrdinal&&(this.attributes.start=this.startOrdinal),this._simplePairedTagHTML(e,"ol",!0)}}class MDListItemNode extends MDBlockNode{ordinal;constructor(e,t=null){super(e),this.ordinal=t}toHTML(e){return this._simplePairedTagHTML(e,"li")}}class MDCodeBlockNode extends MDBlockNode{text;constructor(e){super([]),this.text=e}toHTML(e){return`<pre${this._htmlAttributes()}><code>${MDUtils.escapeHTML(this.text)}</code></pre>\n`}}class MDTableNode extends MDBlockNode{get headerRow(){return this.#U}set headerRow(e){this.#U=e,this.#F()}#U;get bodyRows(){return this.#_}set bodyRows(e){this.#_=e,this.#F()}#_;columnAlignments=[];constructor(e,t){super([e,...t]),this.#U=e,this.#_=t}#F(){this.children=[this.#U,...this.#_]}#O(){this.children.forEach((e=>this.#E(e)))}#E(e){for(const[t,n]of e.children.entries()){const e=t<this.columnAlignments.length?this.columnAlignments[t]:null;this.#W(n,e)}}#W(e,t){t?e.cssStyles["text-align"]=t:delete e.cssStyles["text-align"]}toHTML(e){this.#O();var t="";return t+=`<table${this._htmlAttributes()}>\n`,t+="<thead>\n",t+=this.headerRow.toHTML(e)+"\n",t+="</thead>\n",t+="<tbody>\n",t+=MDNode.toHTML(this.bodyRows,e)+"\n",t+="</tbody>\n",t+="</table>\n"}}class MDTableRowNode extends MDBlockNode{toHTML(e){return this._simplePairedTagHTML(e,"tr",!0)}}class MDTableCellNode extends MDBlockNode{toHTML(e){return this._simplePairedTagHTML(e,"td")}}class MDTableHeaderCellNode extends MDBlockNode{toHTML(e){return this._simplePairedTagHTML(e,"th")}}class MDDefinitionListNode extends MDBlockNode{toHTML(e){return this._simplePairedTagHTML(e,"dl",!0)}}class MDDefinitionListTermNode extends MDBlockNode{toHTML(e){return this._simplePairedTagHTML(e,"dt")}}class MDDefinitionListDefinitionNode extends MDBlockNode{toHTML(e){return this._simplePairedTagHTML(e,"dd")}}class MDFootnoteListNode extends MDBlockNode{toHTML(e){const t=e.footnotes;var n=Object.keys(t);if(0==Object.keys(t).length)return"";const s=e.root.footnoteInstances;var r="";r+='<div class="footnotes"><hr/>',r+="<ol>";for(const i of n){let n=t[i];if(!n)continue;r+=`<li value="${i}" id="footnote_${i}">${MDNode.toHTML(n,e)}`;const o=s[i];if(o)for(const e of o)r+=` <a href="#footnoteref_${e}" class="footnote-backref">↩︎</a>`;r+="</li>\n"}return r+="</ol>",r+="</div>"}toPlaintext(e){const t=e.footnotes;var n=Object.keys(t);if(0==Object.keys(t).length)return"";var s="";for(const r of n){let n=t[r];n&&(s+=`${r}. ${n.toPlaintext(e)}\n`)}return s.trim()}}class MDInlineNode extends MDNode{}class MDTextNode extends MDInlineNode{text;constructor(e){super([]),this.text=e}toHTML(e){return MDUtils.escapeHTML(this.text)}toPlaintext(e){return this.text}}class MDObfuscatedTextNode extends MDTextNode{toHTML(e){return MDUtils.escapeObfuscated(this.text)}}class MDEmphasisNode extends MDInlineNode{toHTML(e){return this._simplePairedTagHTML(e,"em")}}class MDStrongNode extends MDInlineNode{toHTML(e){return this._simplePairedTagHTML(e,"strong")}}class MDStrikethroughNode extends MDInlineNode{toHTML(e){return this._simplePairedTagHTML(e,"s")}}class MDUnderlineNode extends MDInlineNode{toHTML(e){return this._simplePairedTagHTML(e,"u")}}class MDHighlightNode extends MDInlineNode{toHTML(e){return this._simplePairedTagHTML(e,"mark")}}class MDSuperscriptNode extends MDInlineNode{toHTML(e){return this._simplePairedTagHTML(e,"sup")}}class MDSubscriptNode extends MDInlineNode{toHTML(e){return this._simplePairedTagHTML(e,"sub")}}class MDCodeNode extends MDInlineNode{text;constructor(e){super([]),this.text=e}toHTML(e){return`<code${this._htmlAttributes()}>${MDUtils.escapeHTML(this.text)}</code>`}}class MDFootnoteNode extends MDInlineNode{symbol;displaySymbol=null;footnoteId=null;occurrenceId=null;constructor(e,t=null){super([]),this.symbol=e,t&&(this.attributes.title=t)}toHTML(e){return null!==this.differentiator?`<sup id="footnoteref_${this.occurrenceId}"${this._htmlAttributes()}><a href="#footnote_${this.footnoteId}">${MDUtils.escapeHTML(this.displaySymbol??this.symbol)}</a></sup>`:`\x3c!--FNREF:{${this.symbol}}--\x3e`}}class MDLinkNode extends MDInlineNode{href;constructor(e,t,n=null){super(t),this.href=e,null!==n&&(this.attributes.title=n)}toHTML(e){return`<a href="${this.href.startsWith("mailto:")?MDUtils.escapeObfuscated(this.href):MDUtils.escapeHTML(this.href)}"${this._htmlAttributes()}>${this._childHTML(e)}</a>`}}class MDReferencedLinkNode extends MDLinkNode{reference;constructor(e,t){super("",t),this.reference=e}toHTML(e){if(""===this.href){this.href=e.urlForReference(this.reference);const t=e.urlTitleForReference(this.reference);t&&(this.attributes.title=t)}return super.toHTML(e)}}class MDImageNode extends MDInlineNode{src;alt;constructor(e,t){super([]),this.src=e,this.alt=t}toHTML(e){var t=`<img src="${MDUtils.escapeHTML(this.src)}"`;return this.alt&&(t+=` alt="${MDUtils.escapeHTML(this.alt)}"`),t+=`${this._htmlAttributes()}>`}}class MDReferencedImageNode extends MDImageNode{reference;constructor(e,t=""){super("",t,[]),this.reference=e}toHTML(e){return""===this.src&&(this.src=e.urlForReference(this.reference),this.attributes.title=e.urlTitleForReference(this.reference)),super.toHTML(e)}}class MDAbbreviationNode extends MDInlineNode{abbreviation;get definition(){return this.attributes.title??null}set definition(e){this.attributes.title=e}constructor(e,t){super([]),this.abbreviation=e,this.attributes.title=t}toHTML(e){return`<abbr${this._htmlAttributes()}>${MDUtils.escapeHTML(this.abbreviation)}</abbr>`}}class MDLineBreakNode extends MDInlineNode{toHTML(e){return"<br>"}toPlaintext(e){return"\n"}}class MDHTMLTagNode extends MDInlineNode{tag;constructor(e){super([]),this.tag=e}toHTML(e){return this.tag.toString()}}class MDHTMLFilter{allowableTags={address:{cite:"{url}"},h1:{},h2:{},h3:{},h4:{},h5:{},h6:{},blockquote:{},dl:{},dt:{},dd:{},div:{},hr:{},ul:{},ol:{start:"{int}",type:"a|A|i|I|1"},li:{value:"{int}"},p:{},pre:{},table:{},thead:{},tbody:{},tfoot:{},tr:{},td:{},th:{},a:{href:"{url}",target:"*"},abbr:{},b:{},br:{},cite:{},code:{},data:{value:"*"},dfn:{},em:{},i:{},kbd:{},mark:{},q:{cite:"{url}"},s:{},samp:{},small:{},span:{},strong:{},sub:{},sup:{},time:{datetime:"*"},u:{},var:{},wbr:{},img:{alt:"*",href:"{url}"},figure:{},figcaption:{},del:{},ins:{},details:{},summary:{}};allowableGlobalAttributes={class:"{classlist}","data-*":"*",dir:"ltr|rtl|auto",id:"*",lang:"*",style:"{style}",title:"*",translate:"yes|no|{none}"};allowableStyleKeys={"background-color":"{color}",color:"{color}"};scrubTag(e){for(const t of Object.keys(e.attributes))this.isValidAttributeName(e.tagName,t)||delete e.attributes[t],this.isValidAttributeValue(e.tagName,t,e.attributes[t])||delete e.attributes[t]}scrubModifier(e,t){if(e.cssClasses.length>0){const n=e.cssClasses.join(" ");this.isValidAttributeValue(t,"class",n)||(e.cssClasses=[])}if(null!==e.cssId&&(this.isValidAttributeValue(t,"id",e.cssId)||(e.cssId=null)),this.isValidAttributeName(t,"style"))for(const t of Object.keys(e.cssStyles)){const n=e.cssStyles[t];this.isValidStyleValue(t,n)||delete e.cssStyles[t]}else e.cssStyles={};for(const n of Object.keys(e.attributes)){const s=e.attributes[n];this.isValidAttributeValue(t,n,s)||delete e.attributes[n]}}isValidTagName(e){return void 0!==this.allowableTags[e.toLowerCase()]}isValidAttributeName(e,t){const n=t.toLowerCase();if(void 0!==this.allowableGlobalAttributes[n])return!0;for(const e in this.allowableGlobalAttributes)if(e.endsWith("*")&&n.startsWith(e.substring(0,e.length-1)))return!0;if(null===e)return!1;const s=e.toLowerCase(),r=this.allowableTags[s];return!!r&&void 0!==r[n]}isValidAttributeValue(e,t,n){const s=t.toLowerCase(),r=this.allowableGlobalAttributes[t.toLowerCase()];if(void 0!==r)return this.#z(n,r);for(const e in this.allowableGlobalAttributes)if(e.endsWith("*")&&s.startsWith(e.substring(0,e.length-1)))return this.#z(n,this.allowableGlobalAttributes[e]);if(null===e)return!1;const i=e.toLowerCase(),o=this.allowableTags[i];if(void 0===o)return!1;const a=o[s];return void 0!==a&&this.#z(n,a)}static#q=/^\S+$/;static#j=/^[\-]?\d+$/;static#V=/^-?[_a-zA-Z]+[_a-zA-Z0-9-]*(?:\s+-?[_a-zA-Z]+[_a-zA-Z0-9-]*)*$/;#z(e,t){const n=t.split("|");for(const t of n)switch(t){case"*":return!0;case"{classlist}":if(MDHTMLFilter.#V.exec(e))return!0;break;case"{int}":if(MDHTMLFilter.#j.exec(e))return!0;break;case"{none}":if(!0===e)return!0;break;case"{style}":if(this.isValidStyleDeclaration(e))return!0;break;case"{url}":if(MDHTMLFilter.#q.exec(e))return!0;break;default:if(e===t)return!0}return!1}isValidStyleDeclaration(e){const t=e.split(";");for(const e of t){if(0==e.trim().length)continue;const t=e.split(":");if(2!=t.length)return!1;const n=t[0].trim();if(!this.isValidStyleKey(n))return!1;const s=t[1].trim();if(!this.isValidStyleValue(n,s))return!1}return!0}isValidStyleKey(e){return void 0!==this.allowableStyleKeys[e]}isValidStyleValue(e,t){const n=this.allowableStyleKeys[e];if(void 0===n)return!1;const s=n.split("|");for(const e of s){if("{color}"===e&&this.#G(t))return!0;if(t===e)return!0}return!1}static#Z=/^#[0-9a-f]{3}(?:[0-9a-f]{3})?$|^[a-zA-Z]+$/i;#G(e){return null!==MDHTMLFilter.#Z.exec(e)}}class MDHTMLTag{original;tagName;isCloser;attributes;constructor(e,t,n,s){this.original=e,this.tagName=t,this.isCloser=n,this.attributes=s}toString(){var e="<";this.isCloser&&(e+="/"),e+=this.tagName;for(const t in this.attributes){const n=MDUtils.scrubAttributeName(t),s=this.attributes[t];if(!0===s)e+=` ${n}`;else{e+=` ${n}="${MDUtils.escapeHTML(s)}"`}}return e+=">"}equals(e){return e instanceof MDHTMLTag&&(e.tagName==this.tagName&&(e.isCloser==this.isCloser&&MDUtils.equal(e.attributes,this.attributes)))}static#K=/[a-z]/i;static#Q=/[a-z0-9]/i;static#J=/[a-z]/i;static#X=/[a-z0-9-]/i;static#Y=/\s/;static fromLineStart(e){var t=!1,n="",s="",r="",i=null,o={},a=null;let l=function(e=!1){s.length>0&&(r.length>0||i?o[s]=e?MDUtils.unescapeHTML(r):r:o[s]=!0),s="",r="",i=null};for(var c=0,d=0;d<e.length&&null===a;d++){let o=e.substring(d,d+1),u=null!==this.#Y.exec(o);switch(c){case 0:if("<"!=o)return null;c=1;break;case 1:"/"==o?t=!0:d--,c=2;break;case 2:if(0==n.length){if(null===this.#K.exec(o))return null;n+=o}else this.#Q.exec(o)?n+=o:(d--,c=t?6:3);break;case 3:if(0==s.length)if(u);else if("/"==o)c=6;else{if(">"==o){a=e.substring(0,d+1);break}if(!this.#J.exec(o))return null;s+=o}else if(u)c=4;else if("/"==o)l(),c=6;else{if(">"==o){l(),a=e.substring(0,d+1);break}if("="==o)c=5;else{if(!this.#X.exec(o))return null;s+=o}}break;case 4:if("="==o)c=5;else if(u);else if("/"==o)c=6;else{if(">"==o){a=e.substring(0,d+1);break}this.#J.exec(o)&&(l(),c=3,d--)}break;case 5:if(0==r.length)if(null===i)u||('"'==o||"'"==o?i=o:(i="",d--));else if(o===i)l(""!=i),c=3;else{if(""===i&&("/"==o||">"==o))return null;r+=o}else o===i||""===i&&u?(l(),c=3):r+=o;break;case 6:if(u);else if(">"==o){a=e.substring(0,d+1);break}}}return null===a?null:(l(),new MDHTMLTag(a,n,t,o))}}class MDTagModifier{original;cssClasses=[];cssId=null;cssStyles={};attributes={};static#ee=/\.([a-z_\-][a-z0-9_\-]*?)/i;static#te=/#([a-z_\-][a-z0-9_\-]*?)/i;static#ne=/([a-z0-9]+?)=([^\s\}]+?)/i;static#se=/\{([^}]+?)}/i;static#re=new RegExp("^"+this.#se.source,"i");static#ie=new RegExp("^(.*?)\\s*"+this.#se.source+"\\s*$","i");static#oe=new RegExp("^"+this.#ee.source+"$","i");static#ae=new RegExp("^"+this.#te.source+"$","i");static#le=new RegExp("^"+this.#ne.source+"$","i");applyTo(e){if(e instanceof MDNode){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];for(const t in this.cssStyles)e.cssStyles[t]=this.cssStyles[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#ce(e){const t=e.split(";");var n={};for(const e of t){const t=e.split(":");2==t.length&&(n[t[0]]=t[1])}return n}static#de(e){let t=e.split(/\s+/),n=new MDTagModifier;var s;n.original=`{${e}}`;for(const e of t)if(""!=e.trim())if(s=this.#oe.exec(e))n.cssClasses.push(s[1]);else if(s=this.#ae.exec(e))n.cssId=s[1];else{if(!(s=this.#le.exec(e)))return null;"style"==s[1]?n.cssStyles=this.#ce(s[2]):n.attributes[s[1]]=s[2]}return n}static fromLine(e){let t=this.#ie.exec(e);return null===t?[e,null]:[t[1],this.#de(t[2])]}static fromStart(e){let t=this.#re.exec(e);return null===t?null:this.#de(t[1])}static strip(e){let t=this.#ie.exec(e);return null===t?e:t[1]}}class MDConfig{strikethroughSingleTildeEnabled=!0}class Markdown{static standardReaders=[new MDUnderlinedHeadingReader,new MDHashHeadingReader,new MDBlockQuoteReader,new MDHorizontalRuleReader,new MDUnorderedListReader,new MDOrderedListReader,new MDFencedCodeBlockReader,new MDIndentedCodeBlockReader,new MDParagraphReader,new MDStrongReader,new MDEmphasisReader,new MDCodeSpanReader,new MDImageReader,new MDLinkReader,new MDHTMLTagReader];static allReaders=[...this.standardReaders,new MDSubtextReader,new MDTableReader,new MDDefinitionListReader,new MDFootnoteReader,new MDAbbreviationReader,new MDUnderlineReader,new MDSubscriptReader,new MDStrikethroughReader,new MDHighlightReader,new MDSuperscriptReader,new MDReferencedImageReader,new MDReferencedLinkReader,new MDModifierReader];static standardParser=new Markdown(this.standardReaders);static completeParser=new Markdown(this.allReaders);config;tagFilter=new MDHTMLFilter;#ue;#l;#c;#d;constructor(e=Markdown.allReaders,t=new MDConfig){this.#ue=e,this.config=t,this.#l=MDReader.sortReaderForBlocks(e),this.#c=MDReader.sortReadersForTokenizing(e),this.#d=MDReader.sortReadersForSubstitution(e)}toHTML(e){const t=e.split(/(?:\n|\r|\r\n)/),n=new MDState(t,this.config,this.#l,this.#c,this.#d,this.tagFilter);for(const e of this.#ue)e.preProcess(n);const s=n.readBlocks();for(const e of this.#ue)e.postProcess(n,s);return MDNode.toHTML(s,n)}}