UNPKG

quikdown

Version:

Small, safe, bidirectional Markdown parser and editor with streaming, undo/redo, rich fences, MCP tools, and a standalone airgapped build. Zero runtime deps. Browser and Node.js.

9 lines (8 loc) • 23.9 kB
/** * quikdown_bd - Bidirectional Markdown Parser * @version 1.2.22 * @license BSD-2-Clause * @copyright DeftIO 2025 */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).quikdown_bd=t()}(this,function(){"use strict";function e(e){if(e.length<3)return!1;let t="";for(let n=0;n<e.length;n++){const r=e[n];" "!==r&&"\t"!==r&&(t+=r)}if(t.length<3)return!1;const n=t[0];if("-"!==n&&"*"!==n&&"_"!==n)return!1;for(let e=1;e<t.length;e++)if(t[e]!==n)return!1;return!0}const t="quikdown-",n="§CB",r="§HT",o="§BE",l="§NL§",s={href:1,src:1,action:1,formaction:1},c={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"},i={h1:"font-size:2em;margin:.67em 0;text-align:left",h2:"font-size:1.5em;margin:.83em 0",h3:"font-size:1.25em;margin:1em 0",h4:"font-size:1em;margin:1.33em 0",h5:"font-size:.875em;margin:1.67em 0",h6:"font-size:.85em;margin:2em 0",pre:"background:#f4f4f4;padding:10px;border-radius:4px;overflow-x:auto;margin:1em 0",code:"background:#f0f0f0;padding:2px 4px;border-radius:3px;font-family:monospace",blockquote:"border-left:4px solid #ddd;margin-left:0;padding-left:1em",table:"border-collapse:collapse;width:100%;margin:1em 0",th:"border:1px solid #ddd;padding:8px;background-color:#f2f2f2;font-weight:bold;text-align:left",td:"border:1px solid #ddd;padding:8px;text-align:left",hr:"border:none;border-top:1px solid #ddd;margin:1em 0",img:"max-width:100%;height:auto",a:"color:#06c;text-decoration:underline",strong:"font-weight:bold",em:"font-style:italic",del:"text-decoration:line-through",ul:"margin:.5em 0;padding-left:2em",ol:"margin:.5em 0;padding-left:2em",li:"margin:.25em 0","task-item":"list-style:none","task-checkbox":"margin-right:.5em",alert:"padding:1em;margin:1em 0;border-left:4px solid #0969da;border-radius:4px;background:#ddf4ff","alert-title":"font-weight:600;margin:0 0 .4em","alert-note":"border-left-color:#0969da;background:#ddf4ff","alert-tip":"border-left-color:#1a7f37;background:#dafbe1","alert-important":"border-left-color:#8250df;background:#fbefff","alert-warning":"border-left-color:#9a6700;background:#fff8c5","alert-caution":"border-left-color:#cf222e;background:#ffebe9",sup:"font-size:.75em;vertical-align:super;line-height:0",footnotes:"margin-top:2em;font-size:.9em","footnote-backref":"text-decoration:none;margin-left:.25em"};function a(e){if(null==e||""===e)return{url:"",title:null};const t=e.match(/^(.*)\s+(?:"([^"]*)"|&quot;([^&]*?)&quot;)\s*$/);if(t)return{url:t[1].replace(/\s+$/,""),title:t[2]??t[3]};const n=e.match(/^(.*)\s+(?:'([^']*)'|&#39;([^&]*?)&#39;)\s*$/);return n?{url:n[1].replace(/\s+$/,""),title:n[2]??n[3]}:e.startsWith("&lt;")&&e.endsWith("&gt;")?{url:e.slice(4,-4),title:null}:{url:e,title:null}}function u(e){return e.replace(/[*_`~]/g,"").trim().toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")||"section"}function f(e,t){const n=t.get(e)||0;return t.set(e,n+1),0===n?e:`${e}-${n}`}function d(e){let t=0,n=0;for(;n<e.length&&e.startsWith("&gt;",n);)n+=4,t++," "===e[n]&&n++;return{depth:t,content:e.slice(n)}}function $(t){const r=t.trim();return""===r||(!!/^#{1,6}\s/.test(r)||(!!e(r)||(!!/^&gt;/.test(r)||(!!/^[-*+]\s/.test(r)||(!!/^\d+\.\s/.test(r)||(!!r.startsWith("|")||!!r.startsWith(n)))))))}const g=[[/\*\*(.+?)\*\*/g,"strong"],[/__(.+?)__/g,"strong"],[/(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)/g,"em"],[/(?<![A-Za-z0-9_])_(?![_\s])(.+?)(?<![\s_])_(?![A-Za-z0-9_])/g,"em"],[/~~(.+?)~~/g,"del"],[/`([^`\n]+)`/g,"code"]],p={NOTE:"Note",TIP:"Tip",IMPORTANT:"Important",WARNING:"Warning",CAUTION:"Caution"};function h(e,n,r){const o=e.length>0&&1===e[0].depth?e[0].content.trim().match(/^\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]\s*$/i):null;let l="";const s=[],c=n("blockquote").includes("style=");for(let a=0;a<e.length;a++){const{depth:u,content:f}=e[a];if(!(u<=0)){if(o&&0===a){const e=o[1].toUpperCase(),n=e.toLowerCase();if(c){const e=i.alert,t=i["alert-"+n];l+=`<div style="${t?`${e};${t}`:e}"${r(">")}>`}else l+=`<div class="${t}alert ${t}alert-${n}"${r(">")}>`;const a=p[e];l+=c?`<p style="${i["alert-title"]}">${a}</p>`:`<p class="${t}alert-title">${a}</p>`,s.push("alert");continue}for(;s.length>u;){l+="alert"===s.pop()?"</div>":"</blockquote>"}for(;s.length<u;)0===s.length&&o?s.push("alert"):(l+=`<blockquote${n("blockquote")}${r(">")}>`,s.push("blockquote"));l+=f,a<e.length-1&&(l+="\n")}}for(;s.length>0;){l+="alert"===s.pop()?"</div>":"</blockquote>"}return l.trimEnd()}function m(e){if(0===e.length)return!1;const t=e.match(/^([ \t]+)(.*)$/);if(!t)return!1;if(t[1].replace(/\t/g," ").length<4)return!1;const n=t[2];return!(/^[-*+]\s/.test(n)||/^\d+\.\s/.test(n)||/^>/.test(n)||/^#{1,6}\s/.test(n))}function b(e){if(e.startsWith(" "))return e.slice(4);if("\t"===e[0])return e.slice(1);const t=e.match(/^[ \t]+(.*)$/);return t?t[1]:e}const q=/^(\s*)([*\-+]|\d+\.)\s+(.+)$/;function x(e){const t=e.match(/^[ \t]+(\S.*)$/);if(!t)return!1;const n=t[1];return!/^(```|~~~|>|&gt;|\||#{1,6}(\s|$)|<)/.test(n)&&!/^([-*_])\1{2,}\s*$/.test(n)}function _(e){return e.trim().replace(/^\|/,"").replace(/\|$/,"").split("|")}function y(p,_={}){if(!p||"string"!=typeof p)return"";const{fence_plugin:y,inline_styles:k=!1,bidirectional:w=!1,lazy_linefeeds:N=!1,allow_unsafe_html:E=!1,heading_ids:T=!1,reference_links:C=!1,footnotes:v=!1}=_,L=function(e,n){return function(r,o=""){if(e){let e=n[r];return e||o?(o&&o.includes("text-align")&&e&&e.includes("text-align")&&(e=e.replace(/text-align:[^;]+;?/,"").trim(),e&&!e.endsWith(";")&&(e+=";")),` style="${o?e?`${e}${o}`:o:e}"`):""}{const e=` class="${t}${r}"`;return o?`${e} style="${o}"`:e}}}(k,i),z=new Map;function S(e){return e.replace(/[&<>"']/g,e=>c[e])}const j=w?e=>` data-qd="${S(e)}"`:()=>"";function O(e,t=!1){if(!e)return"";if(t)return e;const n=e.trim(),r=n.toLowerCase(),o=["javascript:","vbscript:","data:"];for(const e of o)if(r.startsWith(e))return"data:"===e&&r.startsWith("data:image/")?n:"#";return n}let W=p;const I=[],M=[];W=W.replace(/^(```|~~~)([^\n]*)\n([\s\S]*?)^\1$/gm,(e,t,r,o)=>{const l=`${n}${I.length}§`,s=r?r.trim():"";return y&&y.render&&"function"==typeof y.render?I.push({lang:s,code:o.trimEnd(),custom:!0,fence:t,hasReverse:!!y.reverse}):I.push({lang:s,code:S(o.trimEnd()),custom:!1,fence:t}),l});const Z=[];W=W.replace(/\\`/g,()=>{const e=`${o}${Z.length}§`;return Z.push("`"),e}),W=W.replace(/`([^`\n]+)`/g,(e,t)=>{const n=`§IC${M.length}§`;return M.push(S(t)),n}),W=W.replace(/\\([\\*_{}[\]()#+\-.!~|<>])/g,(e,t)=>{const n=`${o}${Z.length}§`;return Z.push(S(t)),n});const P=new Map,B=new Map,H=[];if(C||v){const F=W.split("\n"),G=[];let J=0;for(;J<F.length;){const K=F[J];if(K.includes(n))G.push(K),J++;else{if(v){const Q=K.match(/^\[\^([^\]]+)\]:\s+([\s\S]*)$/);if(Q){const V=Q[1];let Y=Q[2];for(;J+1<F.length;){const te=F[J+1];if(!/^[ \t]+\S/.test(te)||te.includes(n))break;Y+=" "+te.trim(),J++}const ee=V;B.has(ee)||B.set(ee,Y),J++;continue}}if(C){const ne=K.match(/^\[([^\]]+)\]:\s+<?([^\s>]+)>?(?:\s+(?:"([^"]*)"|'([^']*)'|\(([^)]*)\)))?\s*$/);if(ne){const re=ne[1].toLowerCase(),oe=ne[2],le=void 0!==ne[3]?ne[3]:void 0!==ne[4]?ne[4]:void 0!==ne[5]?ne[5]:null;P.has(re)||P.set(re,{url:oe,title:le}),J++;continue}}G.push(K),J++}}W=G.join("\n")}const R=[],U=Array.isArray(E)?Object.fromEntries(E.map(e=>[e,1])):E&&"object"==typeof E?E:null;if(U&&(W=W.replace(/<!--[\s\S]*?-->/g,e=>{const t=R.length;return R.push(e),`${r}${t}§`}),W=W.replace(/<\/?([a-zA-Z][a-zA-Z0-9]*)\b[^>]*\/?>/g,(e,t)=>{if(t.toLowerCase()in U){const t=function(e){if(!/\s/.test(e.replace(/<\/?[a-zA-Z][a-zA-Z0-9]*/,"").replace(/\/?>$/,"")))return e;const t=e.match(/^(<\/?[a-zA-Z][a-zA-Z0-9]*)([\s\S]*?)(\/?>)$/);if(!t)return e;const[,n,r,o]=t,l=/([a-zA-Z_][\w\-.:]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+)))?/g,c=[];let i;for(;null!==(i=l.exec(r));){const e=i[1],t=void 0!==i[2]?i[2]:void 0!==i[3]?i[3]:i[4];if(!/^on/i.test(e))if(void 0===t)c.push(e);else{let n=t;e.toLowerCase()in s&&(n=O(t)),c.push(`${e}="${n}"`)}}return n+(c.length?" "+c.join(" "):"")+o}(e),n=R.length;return R.push(t),`${r}${n}§`}return e})),!0!==E&&(W=S(W)),U&&R.forEach((e,t)=>{W=W.replace(`${r}${t}§`,e)}),W=function(e,t,n){const r=e.split("\n"),o=[];let s=0,c=!1;for(;s<r.length;){const e=r[s];if(q.test(e)){c=!0,o.push(e),s++;continue}if(c){if(x(e)){o.push(e),s++;continue}c=!1}if(!m(e)){o.push(e),s++;continue}const i=[];for(;s<r.length;){const e=r[s];if(""===e){if(s+1<r.length&&m(r[s+1])){i.push(""),s++;continue}break}if(!m(e))break;i.push(b(e)),s++}const a=i.join(l);o.push(`<pre${t("pre")}${n(" ")}><code${t("code")}>${a}</code></pre>`)}return o.join("\n")}(W,L,j),W=function(e,t,n){const r=e.split("\n"),o=[];let l=!1,s=[];for(let e=0;e<r.length;e++){const c=r[e].trim();if(c.includes("|")&&(c.startsWith("|")||/[^\\|]/.test(c)))l||(l=!0,s=[]),s.push(c);else{if(l){const e=A(s,t,n);e?o.push(e):o.push(...s),l=!1,s=[]}o.push(r[e])}}if(l&&s.length>0){const e=A(s,t,n);e?o.push(e):o.push(...s)}return o.join("\n")}(W,L,w),W=function(t,n,r,o,l,s){const c=t.split("\n"),i=[];let a=0;for(;a<c.length;){const t=c[a];if(/^\[\/\/\]: #/.test(t)){a++;continue}let g=0;for(;g<t.length&&g<7&&"#"===t[g];)g++;if(g>=1&&g<=6&&" "===t[g]){const e=t.slice(g+1).replace(/\s*#+\s*$/,""),c="h"+g;let d="";if(o){d=` id="${s(f(u(e),l))}"`}i.push(`<${c}${n(c)}${d}${r("#".repeat(g))}>${e}</${c}>`),a++;continue}if(e(t))i.push(`<hr${n("hr")}${r(t.trim())}>`),a++;else{if(d(t).depth>0){const e=[];let t=0;for(;a<c.length;){const n=d(c[a]);if(n.depth>0)e.push(n),t=n.depth,a++;else{if(!(t>0)||$(c[a]))break;e.push({depth:t,content:c[a]}),a++}}i.push(h(e,n,r));continue}i.push(t),a++}}return i.join("\n")}(W,L,j,T,z,S),W=function(e,n,r,o){const l=e.split("\n"),s=[],c=[];let i=0,a=!1;const u=e=>e.replace(/[&<>"']/g,e=>({"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"}[e])),f=o?e=>` data-qd="${u(e)}"`:()=>"";for(let e=0;e<l.length;e++){const o=l[e],u=o.match(q);if(u){const[,e,o,l]=u;0===c.length&&(i=e.length);const d=Math.max(0,Math.floor((e.length-i)/2)),$=/^\d+\./.test(o),g=$?"ol":"ul";let p=l,h="";const m=l.match(/^\[([x ])\]\s+(.*)$/i);if(m&&!$){const[,e,n]=m,o="x"===e.toLowerCase();p=`<input type="checkbox"${r?' style="margin-right:.5em"':` class="${t}task-checkbox"`}${o?" checked":""} disabled> ${n}`,h=r?' style="list-style:none"':` class="${t}task-item"`}for(;c.length>d+1;){const e=c.pop();s.push(`</${e.type}>`)}for(c.length===d+1&&c[d].type!==g&&s.push(`</${c.pop().type}>`);c.length<d+1;)c.push({type:g,level:c.length}),s.push(`<${g}${n(g)}>`);const b=h||n("li");s.push(`<li${b}${f(o)}>${p}</li>`),a=!0}else if(a&&x(o)){let e=s[s.length-1].slice(0,-5);/( {2}|\\)$/.test(e)?e=e.replace(/( {2}|\\)$/,`<br${n("br")}>`):e+=" ",s[s.length-1]=`${e}${o.trim()}</li>`}else{for(;c.length>0;){const e=c.pop();s.push(`</${e.type}>`)}s.push(o),a=!1}}for(;c.length>0;){const e=c.pop();s.push(`</${e.type}>`)}return s.join("\n")}(W,L,k,w),W=W.replace(/!\[([^\]]*)\]\(([^)]+)\)/g,(e,t,n)=>{const{url:r,title:o}=a(n),l=O(r,_.allow_unsafe_urls),s=o?` title="${S(o)}"`:"",c=w&&t?` data-qd-alt="${S(t)}"`:"",i=w?` data-qd-src="${S(r)}"`:"",u=w&&o?` data-qd-title="${S(o)}"`:"";return`<img${L("img")} src="${l}" alt="${t}"${s}${c}${i}${u}${j("!")}>`}),W=W.replace(/\[([^\]]+)\]\(([^)]+)\)/g,(e,t,n)=>{const{url:r,title:o}=a(n),l=O(r,_.allow_unsafe_urls),s=/^https?:\/\//i.test(l)?' rel="noopener noreferrer"':"",c=o?` title="${S(o)}"`:"",i=w?` data-qd-text="${S(t)}"`:"",u=w&&o?` data-qd-title="${S(o)}"`:"";return`<a${L("a")} href="${l}"${s}${c}${i}${u}${j("[")}>${t}</a>`}),W=W.replace(/(^|\s)(https?:\/\/[^\s<]+)/g,(e,t,n)=>{const{url:r,trailing:o}=function(e){let t="";const n=/[.,;:!?)]/;for(;e.length>0&&n.test(e[e.length-1]);){const n=e[e.length-1];if(")"===n&&(e.match(/\(/g)||[]).length>=(e.match(/\)/g)||[]).length)break;t=n+t,e=e.slice(0,-1)}return{url:e,trailing:t}}(n),l=O(r,_.allow_unsafe_urls);return`${t}<a${L("a")} href="${l}" rel="noopener noreferrer">${r}</a>${o}`}),C&&P.size>0){function se(e,t,n){const r=O(e.url,_.allow_unsafe_urls),o=/^https?:\/\//i.test(r)?' rel="noopener noreferrer"':"",l=e.title?` title="${S(e.title)}"`:"",s=w?` data-qd-ref="${S(t)}"`:"";return`<a${L("a")} href="${r}"${o}${l}${s}${j("[ref")}>${n}</a>`}W=W.replace(/\[([^\]]+)\]\[([^\]]*)\]/g,(e,t,n)=>{const r=P.get((""===n?t:n).toLowerCase());return r?se(r,n,t):e}),W=W.replace(/(?<!\])\[([^\]^[\n]+)\](?!\(|\[)/g,(e,t)=>{const n=P.get(t.toLowerCase());return n?se(n,t,t):e})}v&&B.size>0&&(W=W.replace(/\[\^([^\]]+)\]/g,(e,t)=>{if(!B.has(t))return e;let n=H.indexOf(t);-1===n?(H.push(t),n=H.length):n+=1;const r=w?` data-qd-fn="${S(t)}"`:"";return`<sup${L("sup")}${r}${j("[^")}><a href="#fn-${S(t)}" id="fnref-${S(t)}">${n}</a></sup>`}));const D=[];W=W.replace(/<[^>]+>/g,e=>(D.push(e),`%%T${D.length-1}%%`));const X=["**","__","*","_","~~"];for(let ce=0;ce<5;ce++){const[ie,ae]=g[ce];W=W.replace(ie,`<${ae}${L(ae)}${j(X[ce])}>$1</${ae}>`)}if(W=W.replace(/%%T(\d+)%%/g,(e,t)=>D[t]),W=W.replace(/\\\n/g,`<br${L("br")}>`),N){const ue=[];let fe=0;W=W.replace(/<(table|[uo]l)[^>]*>[\s\S]*?<\/\1>/g,e=>(ue[fe]=e,`§B${fe++}§`)),W=W.replace(/\n\n+/g,"§P§").replace(/(<\/(?:h[1-6]|blockquote|pre)>)\n/g,"$1§N§").replace(/(<(?:h[1-6]|blockquote|pre|hr)[^>]*>)\n/g,"$1§N§").replace(/\n(<(?:h[1-6]|blockquote|pre|hr)[^>]*>)/g,"§N§$1").replace(/\n(§B\d+§)/g,"§N§$1").replace(/(§B\d+§)\n/g,"$1§N§").replace(/\n/g,`<br${L("br")}>`).replace(/§N§/g,"\n").replace(/§P§/g,"</p><p>"),ue.forEach((e,t)=>W=W.replace(`§B${t}§`,e)),W="<p>"+W+"</p>"}else W=W.replace(/ {2}$/gm,`<br${L("br")}>`),W=W.replace(/\n\n+/g,(e,t)=>W.substring(0,t).match(/<\/(h[1-6]|blockquote|ul|ol|table|pre|hr)>$/)?"<p>":"</p><p>"),W="<p>"+W+"</p>";if([[/<p><\/p>/g,""],[/<p>(<h[1-6][^>]*>)/g,"$1"],[/(<\/h[1-6]>)<\/p>/g,"$1"],[/<p>(<blockquote[^>]*>)/g,"$1"],[/(<\/blockquote>)<\/p>/g,"$1"],[/<p>(<ul[^>]*>|<ol[^>]*>)/g,"$1"],[/(<\/ul>|<\/ol>)<\/p>/g,"$1"],[/<p>(<hr[^>]*>)<\/p>/g,"$1"],[/<p>(<table[^>]*>)/g,"$1"],[/(<\/table>)<\/p>/g,"$1"],[/<p>(<pre[^>]*>)/g,"$1"],[/(<\/pre>)<\/p>/g,"$1"],[/<p>(<div[^>]*>)/g,"$1"],[/(<\/div>)<\/p>/g,"$1"],[/<p>(<section[^>]*>)/g,"$1"],[/(<\/section>)<\/p>/g,"$1"],[new RegExp(`<p>(${n}\\d+§)</p>`,"g"),"$1"]].forEach(([e,t])=>{W=W.replace(e,t)}),W=W.replace(/(<\/(?:h[1-6]|blockquote|div|section|ul|ol|table|pre|hr)>)\n([^<])/g,"$1\n<p>$2"),v&&H.length>0){const de=w?' data-qd="[^section"':"";let $e=`<section${L("footnotes")}${de}>`;$e+=`<hr${L("hr")}>`,$e+=`<ol${L("ol")}>`;for(const ge of H){const pe=B.get(ge)||"";let he=!0===E?pe:S(pe);for(const[be,qe]of g)he=he.replace(be,`<${qe}${L(qe)}>$1</${qe}>`);const me=w?` data-qd-fn-id="${S(ge)}"`:"";$e+=`<li${L("li")} id="fn-${S(ge)}"${me}>${he} <a href="#fnref-${S(ge)}"${L("footnote-backref")}>↩</a></li>`}$e+="</ol></section>",W+=$e}return I.forEach((e,t)=>{let r;if(e.custom&&y&&y.render)if(r=y.render(e.code,e.lang),void 0===r){const t=!k&&e.lang?` class="language-${S(e.lang)}"`:"",n=k?L("code"):t,o=w&&e.lang?` data-qd-lang="${S(e.lang)}"`:"",l=w?` data-qd-fence="${S(e.fence)}"`:"";r=`<pre${L("pre")}${l}${o}><code${n}>${S(e.code)}</code></pre>`}else w&&(r=r.replace(/^<(\w+)/,`<$1 data-qd-fence="${S(e.fence)}" data-qd-lang="${S(e.lang)}" data-qd-source="${S(e.code)}"`));else{const t=!k&&e.lang?` class="language-${S(e.lang)}"`:"",n=k?L("code"):t,o=w&&e.lang?` data-qd-lang="${S(e.lang)}"`:"",l=w?` data-qd-fence="${S(e.fence)}"`:"";r=`<pre${L("pre")}${l}${o}><code${n}>${e.code}</code></pre>`}const o=`${n}${t}§`;W=W.replace(o,r)}),M.forEach((e,t)=>{const n=`§IC${t}§`;W=W.replace(n,`<code${L("code")}${j("`")}>${e}</code>`)}),Z.forEach((e,t)=>{W=W.replace(`${o}${t}§`,e)}),W=W.split(l).join("\n"),W.trim()}function k(e,t){return[[/\*\*(.+?)\*\*/g,"strong"],[/__(.+?)__/g,"strong"],[/(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)/g,"em"],[/(?<![A-Za-z0-9_])_(?![_\s])(.+?)(?<![\s_])_(?![A-Za-z0-9_])/g,"em"],[/~~(.+?)~~/g,"del"],[/`([^`\n]+)`/g,"code"]].forEach(([n,r])=>{e=e.replace(n,`<${r}${t(r)}>$1</${r}>`)}),e}function A(e,t,n){if(e.length<2)return null;let r,o,l,s=-1;for(let t=1;t<e.length;t++)if(/^\|?[\s\-:|]+\|?$/.test(e[t])&&e[t].includes("-")){s=t;break}if(-1===s)r=[e[0]],o=e.slice(1),l=_(e[0]).map(()=>"left");else{r=e.slice(0,s),o=e.slice(s+1);const t=_(e[s]);l=t.map(e=>{const t=e.trim();return t.startsWith(":")&&t.endsWith(":")?"center":t.endsWith(":")?"right":"left"})}const c=l.length,i=n?` data-qd-align="${l.join(",")}"`:"";let a=`<table${t("table")}${i}>\n`;return a+=`<thead${t("thead")}>\n`,r.forEach(e=>{a+=`<tr${t("tr")}>\n`;const n=_(e);for(let e=0;e<c;e++){const r=e<n.length?n[e]:"",o=l[e]&&"left"!==l[e]?`text-align:${l[e]}`:"",s=k(r.trim(),t);a+=`<th${t("th",o)}>${s}</th>\n`}a+="</tr>\n"}),a+="</thead>\n",o.length>0&&(a+=`<tbody${t("tbody")}>\n`,o.forEach(e=>{a+=`<tr${t("tr")}>\n`;const n=_(e);for(let e=0;e<c;e++){const r=e<n.length?n[e]:"",o=l[e]&&"left"!==l[e]?`text-align:${l[e]}`:"",s=k(r.trim(),t);a+=`<td${t("td",o)}>${s}</td>\n`}a+="</tr>\n"}),a+="</tbody>\n"),a+="</table>",a}function w(e,t={}){return y(e,{...t,bidirectional:!0})}return y.emitStyles=function(e="quikdown-",t="light"){const n=i,r={"#f4f4f4":"#2a2a2a","#f0f0f0":"#2a2a2a","#f2f2f2":"#2a2a2a","#ddd":"#3a3a3a","#06c":"#6db3f2","#ddf4ff":"#162d50","#dafbe1":"#16351d","#fbefff":"#2d1a42","#fff8c5":"#342a10","#ffebe9":"#3d1418",_textColor:"#e0e0e0"},o={_textColor:"#333"};let l="";for(const[s,c]of Object.entries(n)){let n=c;if("dark"===t&&r){for(const[e,t]of Object.entries(r))e.startsWith("_")||(n=n.replaceAll(e,t));["h1","h2","h3","h4","h5","h6","td","li","blockquote","alert","alert-title"].includes(s)&&(n+=`;color:${r._textColor}`)}else if("light"===t&&o){["h1","h2","h3","h4","h5","h6","td","li","blockquote","alert","alert-title"].includes(s)&&(n+=`;color:${o._textColor}`)}l+=`.${e}${s} { ${n} }\n`}return l},y.configure=function(e){return function(t){return y(t,e)}},y.version="1.2.22","undefined"!=typeof module&&module.exports&&(module.exports=y),"undefined"!=typeof window&&(window.quikdown=y),Object.keys(y).forEach(e=>{"configure"!==e&&(w[e]=y[e])}),w.toMarkdown=function(e,t={}){let n;if("string"==typeof e)n=document.createElement("div"),n.innerHTML=e;else{if(!(e instanceof Element))return"";n=e}function r(e,n={}){if(e.nodeType===Node.TEXT_NODE)return e.textContent;if(e.nodeType!==Node.ELEMENT_NODE)return"";const l=e.tagName.toLowerCase(),s=e.getAttribute("data-qd");let c="";for(const t of e.childNodes)c+=r(t,{parentTag:l,...n});switch(l){case"h1":case"h2":case"h3":case"h4":case"h5":case"h6":const n=parseInt(l[1]);return`${s||"#".repeat(n)} ${c.trim()}\n\n`;case"strong":case"b":if(!c)return"";const r=s||"**";return`${r}${c}${r}`;case"em":case"i":if(!c)return"";const i=s||"*";return`${i}${c}${i}`;case"del":case"s":case"strike":if(!c)return"";const a=s||"~~";return`${a}${c}${a}`;case"code":if(!c)return"";const u=s||"`";return`${u}${c}${u}`;case"pre":const f=e.getAttribute("data-qd-fence")||s||"```",d=e.getAttribute("data-qd-lang")||"";if(t.fence_plugin&&t.fence_plugin.reverse&&d)try{const n=t.fence_plugin.reverse(e);if(n&&n.content){const e=n.fence||f;return`${e}${n.lang||d}\n${n.content}\n${e}\n\n`}}catch(e){console.warn("Fence reverse handler error:",e)}const $=e.getAttribute("data-qd-source");if($)return`${f}${d}\n${$}\n${f}\n\n`;const g=e.querySelector("code");return`${f}${d}\n${(g?g.textContent:c).trimEnd()}\n${f}\n\n`;case"blockquote":const p=s||">";return c.trim().split("\n").map(e=>`${p} ${e}`).join("\n")+"\n\n";case"hr":return`${s||"---"}\n\n`;case"br":return`${s||" "}\n`;case"a":{const t=e.getAttribute("href")||"";if(t.startsWith("#fnref-"))return"";const n=e.getAttribute("data-qd-ref");if(null!==n){const t=e.getAttribute("data-qd-text")||c.trim();return""===n?`[${t}][]`:n.toLowerCase()===t.toLowerCase()?`[${n}]`:`[${t}][${n}]`}const r=e.getAttribute("data-qd-text")||c.trim();return r!==t||s?`[${r}](${t})`:`<${t}>`}case"sup":{const t=e.getAttribute("data-qd-fn");return t?`[^${t}]`:c}case"section":if("[^section"===s){let t="";const n=e.querySelector("ol");if(n)for(const e of n.children){if("LI"!==e.tagName)continue;const n=e.getAttribute("data-qd-fn-id");if(!n)continue;let r="";for(const t of e.childNodes)if(t.nodeType===Node.TEXT_NODE)r+=t.textContent;else if(t.nodeType===Node.ELEMENT_NODE){if("A"===t.tagName&&(t.getAttribute("href")||"").startsWith("#fnref-"))continue;r+=t.textContent}t+=`[^${n}]: ${r.trim()}\n`}return t?"\n"+t:""}return c;case"img":return`${s||"!"}[${e.getAttribute("data-qd-alt")||e.getAttribute("alt")||""}](${e.getAttribute("data-qd-src")||e.getAttribute("src")||""})`;case"ul":case"ol":return o(e,"ol"===l)+"\n";case"li":case"span":default:return c;case"table":return function(e){let t="";const n=e.getAttribute("data-qd-align"),r=n?n.split(","):[],o=e.querySelector("thead");if(o){const e=o.querySelector("tr");if(e){const n=[];for(const t of e.querySelectorAll("th"))n.push(t.textContent.trim());t+="| "+n.join(" | ")+" |\n";t+="| "+n.map((e,t)=>{const n=r[t]||"left";return"center"===n?":---:":"right"===n?"---:":"---"}).join(" | ")+" |\n"}}const l=e.querySelector("tbody");if(l)for(const e of l.querySelectorAll("tr")){const n=[];for(const t of e.querySelectorAll("td"))n.push(t.textContent.trim());n.length>0&&(t+="| "+n.join(" | ")+" |\n")}return t.trim()}(e)+"\n\n";case"p":if(c.trim()){const e=c.split("\n");let t=c.trim();if(e.length>1){let n=0;for(let t=e.length-1;t>=0&&""===e[t].trim();t--)n++;if(n>0)return t+="\n ",t+"\n"}return t+"\n\n"}return"";case"div":const h=e.getAttribute("data-qd-lang"),m=e.getAttribute("data-qd-fence");if(h&&t.fence_plugin&&t.fence_plugin.reverse)try{const n=t.fence_plugin.reverse(e);if(n&&n.content){const e=n.fence||m||"```";return`${e}${n.lang||h}\n${n.content}\n${e}\n\n`}}catch(e){console.warn("Fence reverse handler error:",e)}const b=e.getAttribute("data-qd-source");if(b&&m)return`${m}${h||""}\n${b}\n${m}\n\n`;if(e.classList&&e.classList.contains("mermaid-container")){const t=e.getAttribute("data-qd-fence")||"```",n=e.getAttribute("data-qd-lang")||"mermaid",r=e.getAttribute("data-qd-source");if(r){const e=document.createElement("textarea");e.innerHTML=r;return`${t}${n}\n${e.value}\n${t}\n\n`}const o=e.querySelector("pre.mermaid");if(o){const e=o.getAttribute("data-qd-source");if(e){const r=document.createElement("textarea");r.innerHTML=e;return`${t}${n}\n${r.value}\n${t}\n\n`}}const l=e.querySelector(".mermaid-source");if(l){const e=document.createElement("div");e.innerHTML=l.innerHTML;return`${t}${n}\n${e.textContent}\n${t}\n\n`}const s=e.querySelector(".mermaid");if(s&&s.textContent.includes("graph"))return`${t}${n}\n${s.textContent.trim()}\n${t}\n\n`}if(e.classList&&e.classList.contains("mermaid")){const t=e.getAttribute("data-qd-fence")||"```";return`${t}${e.getAttribute("data-qd-lang")||"mermaid"}\n${e.textContent.trim()}\n${t}\n\n`}return c}}function o(e,t,n=0){let l="",s=1;const c=" ".repeat(n);for(const i of e.children){if("UL"===i.tagName||"OL"===i.tagName){l+=o(i,"OL"===i.tagName,n+1);continue}if("LI"!==i.tagName)continue;let e=i.getAttribute("data-qd")||(t?`${s}.`:"-");const a=i.querySelector('input[type="checkbox"]');if(a){const t=a.checked?"x":" ";e="-";const n="\n"+c+" ".repeat(e.length+5);let o="";for(const e of i.childNodes)e.nodeType===Node.TEXT_NODE?o+=e.textContent:e.tagName&&"INPUT"!==e.tagName&&(o+=r(e));l+=`${c}${e} [${t}] ${o.trim().replace(/\n/g,n)}\n`}else{const t="\n"+c+" ".repeat(e.length+1);let s="",a="";for(const e of i.childNodes)"UL"===e.tagName||"OL"===e.tagName?a+=o(e,"OL"===e.tagName,n+1):s+=r(e);l+=`${c}${e} ${s.trim().replace(/\n/g,t)}\n${a}`}s++}return l}let l=r(n);return l=l.replace(/\n{3,}/g,"\n\n"),l=l.trim(),l},w.configure=function(e){const t=y.configure({...e,bidirectional:!0});return function(e){return t(e)}},"undefined"!=typeof module&&module.exports&&(module.exports=w),"undefined"!=typeof window&&(window.quikdown_bd=w),w}); //# sourceMappingURL=quikdown_bd.umd.min.js.map