"use strict";"require baseclass";const MD_MAX_DEPTH=2,MD_HEADING_OFFSET=3,MD_FENCE="```",MD_HEADING_RE=/^(#{1,3}) +(.*)$/,MD_RULE_RE=/^-{3,} *$/,MD_QUOTE_RE=/^> ?/,MD_BULLET_RE=/^[-*] +/,MD_NUMBER_RE=/^[0-9]{1,9}[.] +/,MD_EXTERNAL_RE=/^https:/;function mdClean(e){const t="string"==typeof e?e:"";let n="",l=0;for(let e=0;e{i&&n.push({type:"text",text:i}),i=""},o=(t,n)=>{if(l[t])return-1;const i=e.indexOf(t,n);return-1===i&&(l[t]=!0),i},c=e=>t<2?mdInline(e,t+1):[{type:"text",text:e}];for(;rr+1){s(),n.push({type:"code",text:e.slice(r+1,t)}),r=t+1;continue}}else if("*"===t){const t="*"===e[r+1]?"**":"*",l=r+t.length;if(ll&&" "!==e[i-1]){s(),n.push({type:"**"===t?"strong":"em",children:c(e.slice(l,i))}),r=i+t.length;continue}}if("**"===t){i+=t,r+=2;continue}}else if("["===t){let t=r+1;for(;tr+1&&"]"===e[t]&&"("===e[t+1]){let l=t+2;for(;lt+2&&")"===e[l]){s(),n.push({type:"link",text:e.slice(r+1,t),url:e.slice(t+2,l)}),r=l+1;continue}}}i+=t,r+=1}return s(),n}function mdBlocks(e){const t=mdClean(e).split("\n"),n=[];let l=null;const i=()=>{l=null},r=(e,t)=>{l&&l.type===e?l.lines.push(t):(l={type:e,lines:[t]},n.push(l))},s=(e,t)=>{l&&l.type===e?l.items.push(t):(l={type:e,items:[t]},n.push(l))};for(let e=0;ee.children?mdPlain(e.children):e.text).join("")}function renderMarkdown(e,t,n){const l=t.createDocumentFragment(),i=(e,l)=>(l.forEach(l=>{if("text"===l.type)e.appendChild(t.createTextNode(l.text));else if("code"===l.type){const n=t.createElement("code");n.appendChild(t.createTextNode(l.text)),e.appendChild(n)}else if("link"===l.type){const i="function"==typeof n?n(l.url):"";if("string"!=typeof i||""===i)return void e.appendChild(t.createTextNode(l.text));const r=t.createElement("a");r.setAttribute("href",i),MD_EXTERNAL_RE.test(i)&&(r.setAttribute("target","_blank"),r.setAttribute("rel","noreferrer")),r.appendChild(t.createTextNode(l.text)),e.appendChild(r)}else{const n=t.createElement("strong"===l.type?"strong":"em");i(n,l.children),e.appendChild(n)}}),e),r=(e,n)=>i(t.createElement(e),mdInline(n,0));return mdBlocks(e).forEach(e=>{if("code"===e.type){const n=t.createElement("pre"),i=t.createElement("code");i.appendChild(t.createTextNode(e.text)),n.appendChild(i),l.appendChild(n)}else if("rule"===e.type)l.appendChild(t.createElement("hr"));else if("heading"===e.type)l.appendChild(r("h"+(e.level+3),e.text));else if(e.items){const n=t.createElement("numbers"===e.type?"ol":"ul");e.items.forEach(e=>n.appendChild(r("li",e))),l.appendChild(n)}else l.appendChild(r("quote"===e.type?"blockquote":"p",e.lines.join(" ")))}),l}function markdownToText(e){const t=mdBlocks(e);for(let e=0;e""!==e.trim())[0]||"":"heading"===n.type?l=mdPlain(mdInline(n.text,0)):n.items?l=mdPlain(mdInline(n.items[0],0)):n.lines&&(l=mdPlain(mdInline(n.lines[0],0))),""!==l.trim())return l.trim()}return""}return baseclass.extend({render:renderMarkdown,toText:markdownToText});