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.
7 lines • 105 kB
JavaScript
/**
* Quikdown Editor - Drop-in Markdown Parser
* @version 1.2.21
* @license BSD-2-Clause
* @copyright DeftIO 2025
*/
function e(e){if(e.length<3)return!1;let t="";for(let n=0;n<e.length;n++){const o=e[n];" "!==o&&"\t"!==o&&(t+=o)}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}function t(e){if(e.length<3)return null;const t=e[0];if("`"!==t&&"~"!==t)return null;let n=1;for(;n<e.length&&e[n]===t;)n++;if(n<3)return null;return{char:t,len:n,lang:e.slice(n).trim()}}function n(e,t,n){if(e.length<n)return!1;let o=0;for(;o<e.length&&e[o]===t;)o++;if(o<n)return!1;for(let t=o;t<e.length;t++)if(" "!==e[t]&&"\t"!==e[t])return!1;return!0}function o(t){return/^#{1,6}\s/.test(t)?"heading":e(t)?"hr":/^\d+\.\s/.test(t)?"list-ol":/^[-*+]\s/.test(t)?"list-ul":/^>/.test(t)?"blockquote":/^\|/.test(t)?"table":"paragraph"}function r(e){return e.includes("|")}const i="quikdown-",a="§CB",s="§HT",l="§BE",c={href:1,src:1,action:1,formaction:1},d={"&":"&","<":"<",">":">",'"':""","'":"'"},h={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 u(e){if(null==e||""===e)return{url:"",title:null};const t=e.match(/^(.*)\s+(?:"([^"]*)"|"([^&]*?)")\s*$/);if(t)return{url:t[1].replace(/\s+$/,""),title:t[2]??t[3]};const n=e.match(/^(.*)\s+(?:'([^']*)'|'([^&]*?)')\s*$/);return n?{url:n[1].replace(/\s+$/,""),title:n[2]??n[3]}:e.startsWith("<")&&e.endsWith(">")?{url:e.slice(4,-4),title:null}:{url:e,title:null}}function p(e){return e.replace(/[*_`~]/g,"").trim().toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")||"section"}function g(e,t){const n=t.get(e)||0;return t.set(e,n+1),0===n?e:`${e}-${n}`}function m(e){let t=0,n=0;for(;n<e.length&&e.startsWith(">",n);)n+=4,t++," "===e[n]&&n++;return{depth:t,content:e.slice(n)}}function f(t){const n=t.trim();return""===n||(!!/^#{1,6}\s/.test(n)||(!!e(n)||(!!/^>/.test(n)||(!!/^[-*+]\s/.test(n)||(!!/^\d+\.\s/.test(n)||(!!n.startsWith("|")||!!n.startsWith(a)))))))}const w=[[/\*\*(.+?)\*\*/g,"strong"],[/__(.+?)__/g,"strong"],[/(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)/g,"em"],[/(?<![A-Za-z0-9_])_(?![_\s])(.+?)(?<![\s_])_(?![A-Za-z0-9_])/g,"em"],[/~~(.+?)~~/g,"del"],[/`([^`\n]+)`/g,"code"]],b={NOTE:"Note",TIP:"Tip",IMPORTANT:"Important",WARNING:"Warning",CAUTION:"Caution"};function y(e,t,n){const o=e.length>0&&1===e[0].depth?e[0].content.trim().match(/^\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]\s*$/i):null;let r="";const a=[],s=t("blockquote").includes("style=");for(let l=0;l<e.length;l++){const{depth:c,content:d}=e[l];if(!(c<=0)){if(o&&0===l){const e=o[1].toUpperCase(),t=e.toLowerCase();if(s){const e=h.alert,o=h["alert-"+t];r+=`<div style="${o?`${e};${o}`:e}"${n(">")}>`}else r+=`<div class="${i}alert ${i}alert-${t}"${n(">")}>`;const l=b[e];r+=s?`<p style="${h["alert-title"]}">${l}</p>`:`<p class="${i}alert-title">${l}</p>`,a.push("alert");continue}for(;a.length>c;){r+="alert"===a.pop()?"</div>":"</blockquote>"}for(;a.length<c;)0===a.length&&o?a.push("alert"):(r+=`<blockquote${t("blockquote")}${n(">")}>`,a.push("blockquote"));r+=d,l<e.length-1&&(r+="\n")}}for(;a.length>0;){r+="alert"===a.pop()?"</div>":"</blockquote>"}return r.trimEnd()}function x(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 v(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}function q(e){return e.trim().replace(/^\|/,"").replace(/\|$/,"").split("|")}function $(t,n={}){if(!t||"string"!=typeof t)return"";const{fence_plugin:o,inline_styles:r=!1,bidirectional:b=!1,lazy_linefeeds:q=!1,allow_unsafe_html:$=!1,heading_ids:k=!1,reference_links:A=!1,footnotes:M=!1}=n,L=function(e,t){return function(n,o=""){if(e){let e=t[n];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="${i}${n}"`;return o?`${e} style="${o}"`:e}}}(r,h),_=new Map;function C(e){return e.replace(/[&<>"']/g,e=>d[e])}const T=b?e=>` data-qd="${C(e)}"`:()=>"";function E(e,t=!1){if(!e)return"";if(t)return e;const n=e.trim(),o=n.toLowerCase(),r=["javascript:","vbscript:","data:"];for(const e of r)if(o.startsWith(e))return"data:"===e&&o.startsWith("data:image/")?n:"#";return n}let H=t;const j=[],F=[];H=H.replace(/^(```|~~~)([^\n]*)\n([\s\S]*?)^\1$/gm,(e,t,n,r)=>{const i=`${a}${j.length}§`,s=n?n.trim():"";return o&&o.render&&"function"==typeof o.render?j.push({lang:s,code:r.trimEnd(),custom:!0,fence:t,hasReverse:!!o.reverse}):j.push({lang:s,code:C(r.trimEnd()),custom:!1,fence:t}),i});const N=[];H=H.replace(/\\`/g,()=>{const e=`${l}${N.length}§`;return N.push("`"),e}),H=H.replace(/`([^`\n]+)`/g,(e,t)=>{const n=`§IC${F.length}§`;return F.push(C(t)),n}),H=H.replace(/\\([\\*_{}[\]()#+\-.!~|<>])/g,(e,t)=>{const n=`${l}${N.length}§`;return N.push(C(t)),n});const z=new Map,B=new Map,R=[];if(A||M){const O=H.split("\n"),W=[];let D=0;for(;D<O.length;){const V=O[D];if(V.includes(a))W.push(V),D++;else{if(M){const G=V.match(/^\[\^([^\]]+)\]:\s+([\s\S]*)$/);if(G){const Z=G[1];let X=G[2];for(;D+1<O.length;){const Y=O[D+1];if(!/^[ \t]+\S/.test(Y)||Y.includes(a))break;X+=" "+Y.trim(),D++}const Q=Z;B.has(Q)||B.set(Q,X),D++;continue}}if(A){const K=V.match(/^\[([^\]]+)\]:\s+<?([^\s>]+)>?(?:\s+(?:"([^"]*)"|'([^']*)'|\(([^)]*)\)))?\s*$/);if(K){const ee=K[1].toLowerCase(),te=K[2],ne=void 0!==K[3]?K[3]:void 0!==K[4]?K[4]:void 0!==K[5]?K[5]:null;z.has(ee)||z.set(ee,{url:te,title:ne}),D++;continue}}W.push(V),D++}}H=W.join("\n")}const I=[],P=Array.isArray($)?Object.fromEntries($.map(e=>[e,1])):$&&"object"==typeof $?$:null;if(P&&(H=H.replace(/<!--[\s\S]*?-->/g,e=>{const t=I.length;return I.push(e),`${s}${t}§`}),H=H.replace(/<\/?([a-zA-Z][a-zA-Z0-9]*)\b[^>]*\/?>/g,(e,t)=>{if(t.toLowerCase()in P){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,o,r]=t,i=/([a-zA-Z_][\w\-.:]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+)))?/g,a=[];let s;for(;null!==(s=i.exec(o));){const e=s[1],t=void 0!==s[2]?s[2]:void 0!==s[3]?s[3]:s[4];if(!/^on/i.test(e))if(void 0===t)a.push(e);else{let n=t;e.toLowerCase()in c&&(n=E(t)),a.push(`${e}="${n}"`)}}return n+(a.length?" "+a.join(" "):"")+r}(e),n=I.length;return I.push(t),`${s}${n}§`}return e})),!0!==$&&(H=C(H)),P&&I.forEach((e,t)=>{H=H.replace(`${s}${t}§`,e)}),H=function(e,t,n){const o=e.split("\n"),r=[];let i=0;for(;i<o.length;){if(!x(o[i])){r.push(o[i]),i++;continue}const e=[];for(;i<o.length;){const t=o[i];if(""===t){if(i+1<o.length&&x(o[i+1])){e.push(""),i++;continue}break}if(!x(t))break;e.push(v(t)),i++}const a=e.join("\n");r.push(`<pre${t("pre")}${n(" ")}><code${t("code")}>${a}</code></pre>`)}return r.join("\n")}(H,L,T),H=function(e,t,n){const o=e.split("\n"),r=[];let i=!1,a=[];for(let e=0;e<o.length;e++){const s=o[e].trim();if(s.includes("|")&&(s.startsWith("|")||/[^\\|]/.test(s)))i||(i=!0,a=[]),a.push(s);else{if(i){const e=S(a,t,n);e?r.push(e):r.push(...a),i=!1,a=[]}r.push(o[e])}}if(i&&a.length>0){const e=S(a,t,n);e?r.push(e):r.push(...a)}return r.join("\n")}(H,L,b),H=function(t,n,o,r,i,a){const s=t.split("\n"),l=[];let c=0;for(;c<s.length;){const t=s[c];if(/^\[\/\/\]: #/.test(t)){c++;continue}let d=0;for(;d<t.length&&d<7&&"#"===t[d];)d++;if(d>=1&&d<=6&&" "===t[d]){const e=t.slice(d+1).replace(/\s*#+\s*$/,""),s="h"+d;let h="";if(r){h=` id="${a(g(p(e),i))}"`}l.push(`<${s}${n(s)}${h}${o("#".repeat(d))}>${e}</${s}>`),c++;continue}if(e(t))l.push(`<hr${n("hr")}${o(t.trim())}>`),c++;else{if(m(t).depth>0){const e=[];let t=0;for(;c<s.length;){const n=m(s[c]);if(n.depth>0)e.push(n),t=n.depth,c++;else{if(!(t>0)||f(s[c]))break;e.push({depth:t,content:s[c]}),c++}}l.push(y(e,n,o));continue}l.push(t),c++}}return l.join("\n")}(H,L,T,k,_,C),H=function(e,t,n,o){const r=e.split("\n"),a=[],s=[],l=e=>e.replace(/[&<>"']/g,e=>({"&":"&","<":"<",">":">",'"':""","'":"'"}[e])),c=o?e=>` data-qd="${l(e)}"`:()=>"";for(let e=0;e<r.length;e++){const o=r[e],l=o.match(/^(\s*)([*\-+]|\d+\.)\s+(.+)$/);if(l){const[,e,o,r]=l,d=Math.floor(e.length/2),h=/^\d+\./.test(o),u=h?"ol":"ul";let p=r,g="";const m=r.match(/^\[([x ])\]\s+(.*)$/i);if(m&&!h){const[,e,t]=m,o="x"===e.toLowerCase();p=`<input type="checkbox"${n?' style="margin-right:.5em"':` class="${i}task-checkbox"`}${o?" checked":""} disabled> ${t}`,g=n?' style="list-style:none"':` class="${i}task-item"`}for(;s.length>d+1;){const e=s.pop();a.push(`</${e.type}>`)}if(s.length===d)s.push({type:u,level:d}),a.push(`<${u}${t(u)}>`);else if(s.length===d+1){const e=s[s.length-1];e.type!==u&&(a.push(`</${e.type}>`),s.pop(),s.push({type:u,level:d}),a.push(`<${u}${t(u)}>`))}const f=g||t("li");a.push(`<li${f}${c(o)}>${p}</li>`)}else{for(;s.length>0;){const e=s.pop();a.push(`</${e.type}>`)}a.push(o)}}for(;s.length>0;){const e=s.pop();a.push(`</${e.type}>`)}return a.join("\n")}(H,L,r,b),H=H.replace(/!\[([^\]]*)\]\(([^)]+)\)/g,(e,t,o)=>{const{url:r,title:i}=u(o),a=E(r,n.allow_unsafe_urls),s=i?` title="${C(i)}"`:"",l=b&&t?` data-qd-alt="${C(t)}"`:"",c=b?` data-qd-src="${C(r)}"`:"",d=b&&i?` data-qd-title="${C(i)}"`:"";return`<img${L("img")} src="${a}" alt="${t}"${s}${l}${c}${d}${T("!")}>`}),H=H.replace(/\[([^\]]+)\]\(([^)]+)\)/g,(e,t,o)=>{const{url:r,title:i}=u(o),a=E(r,n.allow_unsafe_urls),s=/^https?:\/\//i.test(a)?' rel="noopener noreferrer"':"",l=i?` title="${C(i)}"`:"",c=b?` data-qd-text="${C(t)}"`:"",d=b&&i?` data-qd-title="${C(i)}"`:"";return`<a${L("a")} href="${a}"${s}${l}${c}${d}${T("[")}>${t}</a>`}),H=H.replace(/(^|\s)(https?:\/\/[^\s<]+)/g,(e,t,o)=>{const{url:r,trailing:i}=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}}(o),a=E(r,n.allow_unsafe_urls);return`${t}<a${L("a")} href="${a}" rel="noopener noreferrer">${r}</a>${i}`}),A&&z.size>0){function oe(e,t,o){const r=E(e.url,n.allow_unsafe_urls),i=/^https?:\/\//i.test(r)?' rel="noopener noreferrer"':"",a=e.title?` title="${C(e.title)}"`:"",s=b?` data-qd-ref="${C(t)}"`:"";return`<a${L("a")} href="${r}"${i}${a}${s}${T("[ref")}>${o}</a>`}H=H.replace(/\[([^\]]+)\]\[([^\]]*)\]/g,(e,t,n)=>{const o=z.get((""===n?t:n).toLowerCase());return o?oe(o,n,t):e}),H=H.replace(/(?<!\])\[([^\]^[\n]+)\](?!\(|\[)/g,(e,t)=>{const n=z.get(t.toLowerCase());return n?oe(n,t,t):e})}M&&B.size>0&&(H=H.replace(/\[\^([^\]]+)\]/g,(e,t)=>{if(!B.has(t))return e;let n=R.indexOf(t);-1===n?(R.push(t),n=R.length):n+=1;const o=b?` data-qd-fn="${C(t)}"`:"";return`<sup${L("sup")}${o}${T("[^")}><a href="#fn-${C(t)}" id="fnref-${C(t)}">${n}</a></sup>`}));const U=[];H=H.replace(/<[^>]+>/g,e=>(U.push(e),`%%T${U.length-1}%%`));const J=["**","__","*","_","~~"];for(let re=0;re<5;re++){const[ie,ae]=w[re];H=H.replace(ie,`<${ae}${L(ae)}${T(J[re])}>$1</${ae}>`)}if(H=H.replace(/%%T(\d+)%%/g,(e,t)=>U[t]),H=H.replace(/\\\n/g,`<br${L("br")}>`),q){const se=[];let le=0;H=H.replace(/<(table|[uo]l)[^>]*>[\s\S]*?<\/\1>/g,e=>(se[le]=e,`§B${le++}§`)),H=H.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>"),se.forEach((e,t)=>H=H.replace(`§B${t}§`,e)),H="<p>"+H+"</p>"}else H=H.replace(/ {2}$/gm,`<br${L("br")}>`),H=H.replace(/\n\n+/g,(e,t)=>H.substring(0,t).match(/<\/(h[1-6]|blockquote|ul|ol|table|pre|hr)>$/)?"<p>":"</p><p>"),H="<p>"+H+"</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>(${a}\\d+§)</p>`,"g"),"$1"]].forEach(([e,t])=>{H=H.replace(e,t)}),H=H.replace(/(<\/(?:h[1-6]|blockquote|div|section|ul|ol|table|pre|hr)>)\n([^<])/g,"$1\n<p>$2"),M&&R.length>0){const ce=b?' data-qd="[^section"':"";let de=`<section${L("footnotes")}${ce}>`;de+=`<hr${L("hr")}>`,de+=`<ol${L("ol")}>`;for(const he of R){const ue=B.get(he)||"";let pe=!0===$?ue:C(ue);for(const[me,fe]of w)pe=pe.replace(me,`<${fe}${L(fe)}>$1</${fe}>`);const ge=b?` data-qd-fn-id="${C(he)}"`:"";de+=`<li${L("li")} id="fn-${C(he)}"${ge}>${pe} <a href="#fnref-${C(he)}"${L("footnote-backref")}>↩</a></li>`}de+="</ol></section>",H+=de}return j.forEach((e,t)=>{let n;if(e.custom&&o&&o.render)if(n=o.render(e.code,e.lang),void 0===n){const t=!r&&e.lang?` class="language-${C(e.lang)}"`:"",o=r?L("code"):t,i=b&&e.lang?` data-qd-lang="${C(e.lang)}"`:"",a=b?` data-qd-fence="${C(e.fence)}"`:"";n=`<pre${L("pre")}${a}${i}><code${o}>${C(e.code)}</code></pre>`}else b&&(n=n.replace(/^<(\w+)/,`<$1 data-qd-fence="${C(e.fence)}" data-qd-lang="${C(e.lang)}" data-qd-source="${C(e.code)}"`));else{const t=!r&&e.lang?` class="language-${C(e.lang)}"`:"",o=r?L("code"):t,i=b&&e.lang?` data-qd-lang="${C(e.lang)}"`:"",a=b?` data-qd-fence="${C(e.fence)}"`:"";n=`<pre${L("pre")}${a}${i}><code${o}>${e.code}</code></pre>`}const i=`${a}${t}§`;H=H.replace(i,n)}),F.forEach((e,t)=>{const n=`§IC${t}§`;H=H.replace(n,`<code${L("code")}${T("`")}>${e}</code>`)}),N.forEach((e,t)=>{H=H.replace(`${l}${t}§`,e)}),H.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,o])=>{e=e.replace(n,`<${o}${t(o)}>$1</${o}>`)}),e}function S(e,t,n){if(e.length<2)return null;let o,r,i,a=-1;for(let t=1;t<e.length;t++)if(/^\|?[\s\-:|]+\|?$/.test(e[t])&&e[t].includes("-")){a=t;break}if(-1===a)o=[e[0]],r=e.slice(1),i=q(e[0]).map(()=>"left");else{o=e.slice(0,a),r=e.slice(a+1);const t=q(e[a]);i=t.map(e=>{const t=e.trim();return t.startsWith(":")&&t.endsWith(":")?"center":t.endsWith(":")?"right":"left"})}const s=i.length,l=n?` data-qd-align="${i.join(",")}"`:"";let c=`<table${t("table")}${l}>\n`;return c+=`<thead${t("thead")}>\n`,o.forEach(e=>{c+=`<tr${t("tr")}>\n`;const n=q(e);for(let e=0;e<s;e++){const o=e<n.length?n[e]:"",r=i[e]&&"left"!==i[e]?`text-align:${i[e]}`:"",a=k(o.trim(),t);c+=`<th${t("th",r)}>${a}</th>\n`}c+="</tr>\n"}),c+="</thead>\n",r.length>0&&(c+=`<tbody${t("tbody")}>\n`,r.forEach(e=>{c+=`<tr${t("tr")}>\n`;const n=q(e);for(let e=0;e<s;e++){const o=e<n.length?n[e]:"",r=i[e]&&"left"!==i[e]?`text-align:${i[e]}`:"",a=k(o.trim(),t);c+=`<td${t("td",r)}>${a}</td>\n`}c+="</tr>\n"}),c+="</tbody>\n"),c+="</table>",c}function A(e,t={}){return $(e,{...t,bidirectional:!0})}$.emitStyles=function(e="quikdown-",t="light"){const n=h,o={"#f4f4f4":"#2a2a2a","#f0f0f0":"#2a2a2a","#f2f2f2":"#2a2a2a","#ddd":"#3a3a3a","#06c":"#6db3f2","#ddf4ff":"#162d50","#dafbe1":"#16351d","#fbefff":"#2d1a42","#fff8c5":"#342a10","#ffebe9":"#3d1418",_textColor:"#e0e0e0"},r={_textColor:"#333"};let i="";for(const[a,s]of Object.entries(n)){let n=s;if("dark"===t&&o){for(const[e,t]of Object.entries(o))e.startsWith("_")||(n=n.replaceAll(e,t));["h1","h2","h3","h4","h5","h6","td","li","blockquote","alert","alert-title"].includes(a)&&(n+=`;color:${o._textColor}`)}else if("light"===t&&r){["h1","h2","h3","h4","h5","h6","td","li","blockquote","alert","alert-title"].includes(a)&&(n+=`;color:${r._textColor}`)}i+=`.${e}${a} { ${n} }\n`}return i},$.configure=function(e){return function(t){return $(t,e)}},$.version="1.2.21","undefined"!=typeof module&&module.exports&&(module.exports=$),"undefined"!=typeof window&&(window.quikdown=$),Object.keys($).forEach(e=>{"configure"!==e&&(A[e]=$[e])}),A.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 o(e,n={}){if(e.nodeType===Node.TEXT_NODE)return e.textContent;if(e.nodeType!==Node.ELEMENT_NODE)return"";const i=e.tagName.toLowerCase(),a=e.getAttribute("data-qd");let s="";for(const t of e.childNodes)s+=o(t,{parentTag:i,...n});switch(i){case"h1":case"h2":case"h3":case"h4":case"h5":case"h6":const n=parseInt(i[1]);return`${a||"#".repeat(n)} ${s.trim()}\n\n`;case"strong":case"b":if(!s)return"";const o=a||"**";return`${o}${s}${o}`;case"em":case"i":if(!s)return"";const l=a||"*";return`${l}${s}${l}`;case"del":case"s":case"strike":if(!s)return"";const c=a||"~~";return`${c}${s}${c}`;case"code":if(!s)return"";const d=a||"`";return`${d}${s}${d}`;case"pre":const h=e.getAttribute("data-qd-fence")||a||"```",u=e.getAttribute("data-qd-lang")||"";if(t.fence_plugin&&t.fence_plugin.reverse&&u)try{const n=t.fence_plugin.reverse(e);if(n&&n.content){const e=n.fence||h;return`${e}${n.lang||u}\n${n.content}\n${e}\n\n`}}catch(e){console.warn("Fence reverse handler error:",e)}const p=e.getAttribute("data-qd-source");if(p)return`${h}${u}\n${p}\n${h}\n\n`;const g=e.querySelector("code");return`${h}${u}\n${(g?g.textContent:s).trimEnd()}\n${h}\n\n`;case"blockquote":const m=a||">";return s.trim().split("\n").map(e=>`${m} ${e}`).join("\n")+"\n\n";case"hr":return`${a||"---"}\n\n`;case"br":return`${a||" "}\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")||s.trim();return""===n?`[${t}][]`:n.toLowerCase()===t.toLowerCase()?`[${n}]`:`[${t}][${n}]`}const o=e.getAttribute("data-qd-text")||s.trim();return o!==t||a?`[${o}](${t})`:`<${t}>`}case"sup":{const t=e.getAttribute("data-qd-fn");return t?`[^${t}]`:s}case"section":if("[^section"===a){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 o="";for(const t of e.childNodes)if(t.nodeType===Node.TEXT_NODE)o+=t.textContent;else if(t.nodeType===Node.ELEMENT_NODE){if("A"===t.tagName&&(t.getAttribute("href")||"").startsWith("#fnref-"))continue;o+=t.textContent}t+=`[^${n}]: ${o.trim()}\n`}return t?"\n"+t:""}return s;case"img":return`${a||"!"}[${e.getAttribute("data-qd-alt")||e.getAttribute("alt")||""}](${e.getAttribute("data-qd-src")||e.getAttribute("src")||""})`;case"ul":case"ol":return r(e,"ol"===i)+"\n";case"li":case"span":default:return s;case"table":return function(e){let t="";const n=e.getAttribute("data-qd-align"),o=n?n.split(","):[],r=e.querySelector("thead");if(r){const e=r.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=o[t]||"left";return"center"===n?":---:":"right"===n?"---:":"---"}).join(" | ")+" |\n"}}const i=e.querySelector("tbody");if(i)for(const e of i.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(s.trim()){const e=s.split("\n");let t=s.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 f=e.getAttribute("data-qd-lang"),w=e.getAttribute("data-qd-fence");if(f&&t.fence_plugin&&t.fence_plugin.reverse)try{const n=t.fence_plugin.reverse(e);if(n&&n.content){const e=n.fence||w||"```";return`${e}${n.lang||f}\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&&w)return`${w}${f||""}\n${b}\n${w}\n\n`;if(e.classList&&e.classList.contains("mermaid-container")){const t=e.getAttribute("data-qd-fence")||"```",n=e.getAttribute("data-qd-lang")||"mermaid",o=e.getAttribute("data-qd-source");if(o){const e=document.createElement("textarea");e.innerHTML=o;return`${t}${n}\n${e.value}\n${t}\n\n`}const r=e.querySelector("pre.mermaid");if(r){const e=r.getAttribute("data-qd-source");if(e){const o=document.createElement("textarea");o.innerHTML=e;return`${t}${n}\n${o.value}\n${t}\n\n`}}const i=e.querySelector(".mermaid-source");if(i){const e=document.createElement("div");e.innerHTML=i.innerHTML;return`${t}${n}\n${e.textContent}\n${t}\n\n`}const a=e.querySelector(".mermaid");if(a&&a.textContent.includes("graph"))return`${t}${n}\n${a.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 s}}function r(e,t,n=0){let i="",a=1;const s=" ".repeat(n);for(const l of e.children){if("LI"!==l.tagName)continue;let e=l.getAttribute("data-qd")||(t?`${a}.`:"-");const c=l.querySelector('input[type="checkbox"]');if(c){const t=c.checked?"x":" ";e="-";let n="";for(const e of l.childNodes)e.nodeType===Node.TEXT_NODE?n+=e.textContent:e.tagName&&"INPUT"!==e.tagName&&(n+=o(e));i+=`${s}${e} [${t}] ${n.trim()}\n`}else{let t="";for(const e of l.childNodes)"UL"===e.tagName||"OL"===e.tagName?t+=r(e,"OL"===e.tagName,n+1):t+=o(e);i+=`${s}${e} ${t.trim()}\n`}a++}return i}let i=o(n);return i=i.replace(/\n{3,}/g,"\n\n"),i=i.trim(),i},A.configure=function(e){const t=$.configure({...e,bidirectional:!0});return function(e){return t(e)}},"undefined"!=typeof module&&module.exports&&(module.exports=A),"undefined"!=typeof window&&(window.quikdown_bd=A);const M=["default","stripped","quikdown"],_={h1:{fontSize:"24pt",marginTop:"0.6em",marginBottom:"0.35em"},h2:{fontSize:"18pt",marginTop:"0.55em",marginBottom:"0.3em"},h3:{fontSize:"15pt",marginTop:"0.5em",marginBottom:"0.25em"},h4:{fontSize:"13pt",marginTop:"0.45em",marginBottom:"0.25em"},h5:{fontSize:"11pt",marginTop:"0.4em",marginBottom:"0.2em"},h6:{fontSize:"10pt",marginTop:"0.35em",marginBottom:"0.2em"}};async function C(e,t=!1){return new Promise((n,o)=>{const r=(new XMLSerializer).serializeToString(e),i=document.createElement("canvas"),a=i.getContext("2d"),s=new Image,l=e.closest(".mermaid")||e.classList.contains("mermaid"),c=e.getAttribute("width")&&e.getAttribute("height");let d,h;l||!c?(d=e.clientWidth||e.viewBox&&e.viewBox.baseVal.width||parseFloat(e.getAttribute("width"))||400,h=e.clientHeight||e.viewBox&&e.viewBox.baseVal.height||parseFloat(e.getAttribute("height"))||300):(d=parseFloat(e.getAttribute("width"))||e.viewBox&&e.viewBox.baseVal.width||e.clientWidth||400,h=parseFloat(e.getAttribute("height"))||e.viewBox&&e.viewBox.baseVal.height||e.clientHeight||300);let u=r;if(d&&h){const e=document.createElement("div");e.innerHTML=r;const t=e.querySelector("svg");t&&(t.setAttribute("width",d.toString()),t.setAttribute("height",h.toString()),u=(new XMLSerializer).serializeToString(t))}i.width=2*d,i.height=2*h,a.scale(2,2),s.onload=()=>{try{t&&(a.fillStyle="white",a.fillRect(0,0,i.width,i.height)),a.drawImage(s,0,0,d,h),i.toBlob(e=>{n(e)},"image/png",1)}catch(e){o(e)}},s.onerror=o;const p=`data:image/svg+xml;charset=utf-8,${encodeURIComponent(u)}`;s.src=p})}async function T(e){try{if(!e._map)return console.warn("No map found on container"),null;const t=e.getBoundingClientRect(),n=Math.round(t.width),o=Math.round(t.height);if(0===n||0===o)return console.warn("Map container has zero dimensions"),null;const r=document.createElement("canvas"),i=window.devicePixelRatio||1;r.width=n*i,r.height=o*i,r.style.width=n+"px",r.style.height=o+"px";const a=r.getContext("2d");a.scale(i,i),a.fillStyle="#FFFFFF",a.fillRect(0,0,n,o);const s=e.querySelectorAll(".leaflet-tile"),l=[];for(const e of s)l.push(new Promise(n=>{const o=new Image;o.crossOrigin="anonymous",o.onload=()=>{try{const n=e.getBoundingClientRect(),r=n.left-t.left,i=n.top-t.top;a.drawImage(o,r,i,n.width,n.height)}catch(e){console.warn("Failed to draw tile:",e)}n()},o.onerror=()=>{console.warn("Failed to load tile:",e.src),n()},o.src=e.src}));await Promise.all(l);const c=e.querySelectorAll("svg:not(.leaflet-attribution-flag)");for(const e of c)if(!e.closest(".leaflet-control"))try{const n=e.getBoundingClientRect(),o=n.left-t.left,r=n.top-t.top,i=(new XMLSerializer).serializeToString(e),s=new Blob([i],{type:"image/svg+xml;charset=utf-8"}),l=URL.createObjectURL(s);await new Promise((e,t)=>{const i=new Image;i.onload=()=>{a.drawImage(i,o,r,n.width,n.height),URL.revokeObjectURL(l),e()},i.onerror=()=>{URL.revokeObjectURL(l),t(new Error("Failed to load SVG overlay"))},i.src=l})}catch(e){console.warn("Failed to draw SVG overlay:",e)}const d=e.querySelectorAll(".leaflet-marker-icon");for(const e of d)try{const n=new Image;n.crossOrigin="anonymous",await new Promise(o=>{n.onload=()=>{const r=e.getBoundingClientRect(),i=r.left-t.left,s=r.top-t.top;a.drawImage(n,i,s,r.width,r.height),o()},n.onerror=o,n.src=e.src})}catch(e){console.warn("Failed to draw marker icon:",e)}return r.toDataURL("image/png",1)}catch(e){return console.error("Failed to rasterize GeoJSON map:",e),null}}async function E(e,t={}){const n=t.output||"default";if(!M.includes(n))throw new Error(`Invalid output profile "${n}". Must be one of: ${M.join(", ")}`);if(!e)throw new Error("No preview panel available");const o=e.querySelectorAll(".math-display");if(o.length>0){if(Array.from(o).some(e=>!e.querySelector("mjx-container"))&&window.MathJax&&window.MathJax.typesetPromise)try{await window.MathJax.typesetPromise(Array.from(o))}catch(e){console.warn("MathJax typesetting failed:",e)}}const r=e.cloneNode(!0);try{"stripped"!==n&&(r.querySelectorAll("strong, b").forEach(e=>{e.style.fontWeight="bold"}),r.querySelectorAll("em, i").forEach(e=>{e.style.fontStyle="italic"}),r.querySelectorAll("del, s, strike").forEach(e=>{e.style.textDecoration="line-through"}),r.querySelectorAll("u").forEach(e=>{e.style.textDecoration="underline"}),r.querySelectorAll("code:not(pre code)").forEach(e=>{e.style.backgroundColor="#f4f4f4",e.style.padding="2px 4px",e.style.borderRadius="3px",e.style.fontFamily="monospace",e.style.fontSize="0.9em"}),Object.entries(_).forEach(([e,t])=>{r.querySelectorAll(e).forEach(e=>{e.style.fontSize=t.fontSize,"quikdown"===n&&(e.style.fontWeight="bold"),e.style.lineHeight="1.25",e.style.marginTop=t.marginTop,e.style.marginBottom=t.marginBottom})}),r.querySelectorAll("blockquote").forEach(e=>{e.style.borderLeft="4px solid #ddd",e.style.marginLeft="0",e.style.paddingLeft="1em",e.style.color="#666"}),r.querySelectorAll("hr").forEach(e=>{e.style.border="none",e.style.borderTop="1px solid #ccc",e.style.margin="1em 0"}),r.querySelectorAll("table").forEach(e=>{e.style.borderCollapse="collapse",e.style.width="100%",e.style.marginBottom="1em"}),r.querySelectorAll("th").forEach(e=>{e.style.border="1px solid #ccc",e.style.padding="8px",e.style.textAlign="left",e.style.backgroundColor="#f0f0f0",e.style.fontWeight="bold"}),r.querySelectorAll("td").forEach(e=>{e.style.border="1px solid #ccc",e.style.padding="8px",e.style.textAlign="left"}),r.querySelectorAll("a").forEach(e=>{e.style.color="#0066cc",e.style.textDecoration="underline"}),r.querySelectorAll("pre code").forEach(e=>{const t=e.parentElement;e.classList.contains("hljs")&&(e.querySelectorAll(".hljs-keyword").forEach(e=>{e.style.color="#d73a49",e.style.fontWeight="bold"}),e.querySelectorAll(".hljs-string").forEach(e=>{e.style.color="#032f62"}),e.querySelectorAll(".hljs-number").forEach(e=>{e.style.color="#005cc5"}),e.querySelectorAll(".hljs-comment").forEach(e=>{e.style.color="#6a737d",e.style.fontStyle="italic"}),e.querySelectorAll(".hljs-function").forEach(e=>{e.style.color="#6f42c1"}),e.querySelectorAll(".hljs-class").forEach(e=>{e.style.color="#6f42c1"}),e.querySelectorAll(".hljs-title").forEach(e=>{e.style.color="#6f42c1"}),e.querySelectorAll(".hljs-built_in").forEach(e=>{e.style.color="#005cc5"}),e.querySelectorAll(".hljs-literal").forEach(e=>{e.style.color="#005cc5"}),e.querySelectorAll(".hljs-meta").forEach(e=>{e.style.color="#005cc5"}),e.querySelectorAll(".hljs-attr").forEach(e=>{e.style.color="#22863a"}),e.querySelectorAll(".hljs-variable").forEach(e=>{e.style.color="#e36209"}),e.querySelectorAll(".hljs-regexp").forEach(e=>{e.style.color="#032f62"}),e.querySelectorAll(".hljs-selector-class").forEach(e=>{e.style.color="#22863a"}),e.querySelectorAll(".hljs-selector-id").forEach(e=>{e.style.color="#6f42c1"}),e.querySelectorAll(".hljs-selector-tag").forEach(e=>{e.style.color="#22863a"}),e.querySelectorAll(".hljs-tag").forEach(e=>{e.style.color="#22863a"}),e.querySelectorAll(".hljs-name").forEach(e=>{e.style.color="#22863a"}),e.querySelectorAll(".hljs-attribute").forEach(e=>{e.style.color="#6f42c1"}));const n=document.createElement("table");n.style.width="100%",n.style.borderCollapse="collapse",n.style.border="none",n.style.marginBottom="1em";const o=document.createElement("tr"),r=document.createElement("td");r.style.backgroundColor="#f7f7f7",r.style.padding="12px",r.style.fontFamily='Consolas, Monaco, "Courier New", monospace',r.style.fontSize="14px",r.style.lineHeight="1.4",r.style.whiteSpace="pre",r.style.overflowX="auto",r.style.border="1px solid #ddd",r.style.borderRadius="4px",r.innerHTML=e.innerHTML,o.appendChild(r),n.appendChild(o),t.parentNode.replaceChild(n,t)}));const t=r.querySelectorAll("img");for(const e of t){!e.width&&e.naturalWidth&&(e.width=e.naturalWidth),!e.height&&e.naturalHeight&&(e.height=e.naturalHeight);const t=800,n=600;if(e.width>t||e.height>n){const o=Math.min(t/e.width,n/e.height);e.width=Math.round(e.width*o),e.height=Math.round(e.height*o)}if(e.width&&(e.setAttribute("width",e.width.toString()),e.style.width=e.width+"px"),e.height&&(e.setAttribute("height",e.height.toString()),e.style.height=e.height+"px"),e.getAttribute("v:shapes")||e.setAttribute("v:shapes","image"+Math.random().toString(36).substr(2,9)),e.src&&!e.src.startsWith("data:"))try{const t=await fetch(e.src),n=await t.blob(),o=2097152;if(n.size>o){console.warn("Image too large for inline data URL:",e.src,"Size:",n.size);continue}const r=await new Promise(e=>{const t=new FileReader;t.onloadend=()=>e(t.result),t.readAsDataURL(n)});e.src=r}catch(t){console.warn("Failed to convert image to data URL:",e.src,t)}}const o=r.querySelectorAll(".qde-stl-container");for(const t of o){try{const n=t.dataset.stlId,o=e.querySelector(`.qde-stl-container[data-stl-id="${n}"]`);if(o){const e=o.querySelector("canvas");if(e&&e.width>0&&e.height>0)try{const n=o._threeRenderer,r=o._threeScene,i=o._threeCamera;n&&r&&i&&n.render(r,i);const a=e.toDataURL("image/png",1),s=document.createElement("img");s.src=a;const l=e.width/2,c=e.height/2;s.width=l,s.height=c,s.setAttribute("width",l.toString()),s.setAttribute("height",c.toString()),s.style.width=l+"px",s.style.height=c+"px",s.style.maxWidth="none",s.style.maxHeight="none",s.style.border="1px solid #ddd",s.style.borderRadius="4px",s.style.margin="0.5em 0",s.setAttribute("v:shapes","image"+Math.random().toString(36).substr(2,9)),s.alt="STL 3D Model",t.parentNode.replaceChild(s,t);continue}catch(e){console.warn("Failed to convert STL canvas to image (likely WebGL context issue):",e)}else console.warn("No valid canvas found in STL container")}else console.warn("Could not find original STL container")}catch(e){console.error("Error processing STL container for copy:",e)}const n=document.createElement("div");n.style.cssText="padding: 12px; background-color: #f0f0f0; border: 1px solid #ccc; text-align: center; margin: 0.5em 0; border-radius: 4px;",n.textContent="[STL 3D Model - Interactive content not available in copy]",t.parentNode.replaceChild(n,t)}const i=r.querySelectorAll(".mermaid");for(const e of i){const t=e.querySelector("svg");if(t)try{const n=await C(t),o=await new Promise(e=>{const t=new FileReader;t.onloadend=()=>e(t.result),t.readAsDataURL(n)}),r=document.createElement("img");r.src=o;const i=t.closest(".mermaid")||t.classList.contains("mermaid"),a=t.getAttribute("width")&&t.getAttribute("height");let s,l;i||!a?(s=t.clientWidth||t.viewBox&&t.viewBox.baseVal.width||parseFloat(t.getAttribute("width"))||400,l=t.clientHeight||t.viewBox&&t.viewBox.baseVal.height||parseFloat(t.getAttribute("height"))||300):(s=parseFloat(t.getAttribute("width"))||t.viewBox&&t.viewBox.baseVal.width||t.clientWidth||400,l=parseFloat(t.getAttribute("height"))||t.viewBox&&t.viewBox.baseVal.height||t.clientHeight||300),r.width=s,r.height=l,r.setAttribute("width",s.toString()),r.setAttribute("height",l.toString()),r.style.width=s+"px",r.style.height=l+"px",r.style.maxWidth="none",r.style.maxHeight="none",r.setAttribute("v:shapes","image"+Math.random().toString(36).substr(2,9)),r.alt="Mermaid Diagram",e.parentNode.replaceChild(r,e)}catch(e){console.warn("Failed to convert Mermaid diagram:",e)}}const a=r.querySelectorAll(".qde-abc-container");for(const e of a){const t=e.querySelector("svg");if(t)try{const n=await C(t),o=await new Promise(e=>{const t=new FileReader;t.onloadend=()=>e(t.result),t.readAsDataURL(n)}),r=document.createElement("img");r.src=o;const i=t.viewBox&&t.viewBox.baseVal;let a,s;if(i&&i.width&&i.height){const e=i.width/i.height;a=t.clientWidth||i.width,s=Math.round(a/e)}else a=t.clientWidth||parseFloat(t.getAttribute("width"))||400,s=t.clientHeight||parseFloat(t.getAttribute("height"))||200;r.width=a,r.height=s,r.setAttribute("width",a.toString()),r.setAttribute("height",s.toString()),r.style.width=a+"px",r.style.height=s+"px",r.style.maxWidth="none",r.style.maxHeight="none",r.setAttribute("v:shapes","image"+Math.random().toString(36).substr(2,9)),r.alt="Music Notation",e.parentNode.replaceChild(r,e)}catch(e){console.warn("Failed to convert ABC notation:",e)}else{const t=document.createElement("div");t.style.cssText="padding: 12px; background-color: #f0f0f0; border: 1px solid #ccc; text-align: center; margin: 0.5em 0; border-radius: 4px;",t.textContent="[Music Notation - Interactive content not available in copy]",e.parentNode.replaceChild(t,e)}}const s=r.querySelectorAll(".qde-vega-container");for(const e of s){const t=e.querySelector("svg");if(t)try{const n=await C(t),o=await new Promise(e=>{const t=new FileReader;t.onloadend=()=>e(t.result),t.readAsDataURL(n)}),r=document.createElement("img");r.src=o;const i=t.clientWidth||parseFloat(t.getAttribute("width"))||400,a=t.clientHeight||parseFloat(t.getAttribute("height"))||300;r.width=i,r.height=a,r.setAttribute("width",i.toString()),r.setAttribute("height",a.toString()),r.style.width=i+"px",r.style.height=a+"px",r.style.maxWidth="none",r.style.maxHeight="none",r.setAttribute("v:shapes","image"+Math.random().toString(36).substr(2,9)),r.alt="Data Visualization",e.parentNode.replaceChild(r,e)}catch(e){console.warn("Failed to convert Vega chart:",e)}else{const t=document.createElement("div");t.style.cssText="padding: 12px; background-color: #f0f0f0; border: 1px solid #ccc; text-align: center; margin: 0.5em 0; border-radius: 4px;",t.textContent="[Vega Chart - Interactive content not available in copy]",e.parentNode.replaceChild(t,e)}}const l=r.querySelectorAll(".qde-chart-container");for(const t of l){try{const n=t.dataset.chartId,o=e.querySelector(`.qde-chart-container[data-chart-id="${n}"]`);if(o){const e=o.querySelector("canvas");if(e&&e.width>0&&e.height>0)try{const n=e.toDataURL("image/png",1),o=document.createElement("img");o.src=n;const r=e.width,i=e.height;o.width=r,o.height=i,o.setAttribute("width",r.toString()),o.setAttribute("height",i.toString()),o.style.width=r+"px",o.style.height=i+"px",o.style.maxWidth="none",o.style.maxHeight="none",o.style.margin="0.5em 0",o.setAttribute("v:shapes","image"+Math.random().toString(36).substr(2,9)),o.alt="Chart",t.parentNode.replaceChild(o,t);continue}catch(e){console.warn("Failed to convert chart canvas to image:",e)}}}catch(e){console.warn("Error processing chart container:",e)}const n=document.createElement("div");n.style.cssText="padding: 12px; background-color: #f0f0f0; border: 1px solid #ccc; text-align: center; margin: 0.5em 0; border-radius: 4px;",n.textContent="[Chart - Interactive content not available in copy]",t.parentNode.replaceChild(n,t)}const c=r.querySelectorAll(".qde-svg-container svg");for(const e of c)try{const t=await C(e),n=await new Promise(e=>{const n=new FileReader;n.onloadend=()=>e(n.result),n.readAsDataURL(t)}),o=document.createElement("img");o.src=n;let r,i;e.getAttribute("width")&&e.getAttribute("height")?(r=parseFloat(e.getAttribute("width"))||e.viewBox&&e.viewBox.baseVal.width||e.clientWidth||400,i=parseFloat(e.getAttribute("height"))||e.viewBox&&e.viewBox.baseVal.height||e.clientHeight||300):(r=e.clientWidth||e.viewBox&&e.viewBox.baseVal.width||parseFloat(e.getAttribute("width"))||400,i=e.clientHeight||e.viewBox&&e.viewBox.baseVal.height||parseFloat(e.getAttribute("height"))||300),o.width=r,o.height=i,o.setAttribute("width",r.toString()),o.setAttribute("height",i.toString()),o.style.width=r+"px",o.style.height=i+"px",o.style.maxWidth="none",o.style.maxHeight="none",o.setAttribute("v:shapes","image"+Math.random().toString(36).substr(2,9)),o.alt="SVG Image",e.parentNode.replaceChild(o,e)}catch(e){console.warn("Failed to convert SVG to image:",e)}const d=Array.from(r.querySelectorAll(".math-display"));if(d.length>0)for(const e of d)try{const t=e.querySelector("svg");if(!t){console.warn("No SVG found in math element, skipping");continue}const n=(new XMLSerializer).serializeToString(t),o=new Blob([n],{type:"image/svg+xml;charset=utf-8"}),r=URL.createObjectURL(o),i=new Image,a=await new Promise((e,n)=>{i.onload=function(){const n=document.createElement("canvas");let o,a;try{o=t.width.baseVal.value,a=t.height.baseVal.value}catch(e){t.viewBox&&t.viewBox.baseVal?(o=t.viewBox.baseVal.width,a=t.viewBox.baseVal.height):(o=i.naturalWidth||i.width||200,a=i.naturalHeight||i.height||50)}let s=.04;const l=o*s,c=a*s;if(l>150||c>45){const e=150/l,t=45/c;s*=Math.min(e,t)}o*=s,a*=s;n.width=2*o,n.height=2*a,n.style.width=o+"px",n.style.height=a+"px";const d=n.getContext("2d");d.scale(2,2),d.fillStyle="#FFFFFF",d.fillRect(0,0,o,a),d.drawImage(i,0,0,o,a),URL.revokeObjectURL(r),e(n.toDataURL("image/png"))},i.onerror=()=>{URL.revokeObjectURL(r),n(new Error("Failed to load SVG image"))},i.src=r}),s=document.createElement("img");s.src=a;const l=new Image;l.src=a,await new Promise(e=>{l.onload=e,l.onerror=e,setTimeout(e,100)});const c=l.naturalWidth/2,d=l.naturalHeight/2;s.width=c,s.height=d,s.style.cssText=`display:inline-block;margin:0.5em 0;width:${c}px;height:${d}px;vertical-align:middle;`,s.alt="Math equation",e.parentNode.replaceChild(s,e)}catch(e){console.error("Failed to convert math element to image:",e)}const h=r.querySelectorAll(".geojson-container");if(h.length>0)for(const t of h)try{const n=t.getAttribute("data-original-source");if(!n){console.warn("No original source found for GeoJSON container");continue}let o=null;const r=e.querySelectorAll(".geojson-container");for(const e of r)if(e.getAttribute("data-original-source")===n){o=e;break}if(!o){console.warn("Could not find live GeoJSON container");const e=document.createElement("div");e.style.cssText="padding: 12px; background-color: #f0f0f0; border: 1px solid #ccc; text-align: center; margin: 0.5em 0; border-radius: 4px;",e.textContent="[GeoJSON Map - Interactive content not available in copy]",t.parentNode.replaceChild(e,t);continue}if(!o._map){console.warn("Map not initialized yet");const e=document.createElement("div");e.style.cssText="padding: 12px; background-color: #f0f0f0; border: 1px solid #ccc; text-align: center; margin: 0.5em 0; border-radius: 4px;",e.textContent="[GeoJSON Map - Still loading]",t.parentNode.replaceChild(e,t);continue}const i=await T(o);if(i){const e=document.createElement("img");e.src=i,e.style.cssText="width: 100%; height: 300px; border: 1px solid #ddd; border-radius: 4px; margin: 0.5em 0;",e.alt="GeoJSON Map",t.parentNode.replaceChild(e,t)}else{const e=document.createElement("div");e.style.cssText="padding: 12px; background-color: #f0f0f0; border: 1px solid #ccc; text-align: center; margin: 0.5em 0; border-radius: 4px;",e.textContent="[GeoJSON Map - Interactive content not available in copy]",t.parentNode.replaceChild(e,t)}}catch(e){console.error("Failed to process GeoJSON container:",e);const n=document.createElement("div");n.style.cssText="padding: 12px; background-color: #f0f0f0; border: 1px solid #ccc; text-align: center; margin: 0.5em 0; border-radius: 4px;",n.textContent="[GeoJSON Map - Interactive content not available in copy]",t.parentNode.replaceChild(n,t)}const u=r.querySelectorAll('[data-qd-lang="geojson"]');for(const t of u)try{const n=t.id,o=n?e.querySelector(`#${n}`):null;if(!o)continue;const r=o.querySelector(".leaflet-container");if(!r)continue;const i=Math.max(1,window.devicePixelRatio||1),a=r.clientWidth||600,s=r.clientHeight||400,l=document.createElement("canvas");l.width=Math.round(a*i),l.height=Math.round(s*i);const c=l.getContext("2d");c.scale(i,i),c.fillStyle="#FFFFFF",c.fillRect(0,0,a,s);const d=r.getBoundingClientRect(),h=Array.from(r.querySelectorAll("img.leaflet-tile"));for(const e of h)try{const t=e.getBoundingClientRect(),n=Math.round(t.left-d.left),o=Math.round(t.top-d.top),r=Math.round(t.width),i=Math.round(t.height),a=!(t.right<=d.left||t.left>=d.right||t.bottom<=d.top||t.top>=d.bottom),s=window.getComputedStyle(e);r>0&&i>0&&a&&"none"!==s.display&&"hidden"!==s.visibility&&c.drawImage(e,n,o,r+1,i+1)}catch(e){console.warn("Failed to draw tile:",e)}const u=o.querySelectorAll(".leaflet-overlay-pane svg");for(const e of u)try{const t=(new XMLSerializer).serializeToString(e),n="data:image/svg+xml;charset=utf-8,"+encodeURIComponent(t),o=new Image;await new Promise(e=>{o.onload=e,o.onerror=e,o.src=n});const r=e.getBoundingClientRect(),i=Math.round(r.left-d.left),a=Math.round(r.top-d.top),s=Math.round(r.width),l=Math.round(r.height),h=!(r.right<=d.left||r.left>=d.right||r.bottom<=d.top||r.top>=d.bottom);s>0&&l>0&&h&&c.drawImage(o,i,a,s,l)}catch(e){console.warn("Failed to draw overlay SVG:",e)}const p=o.querySelectorAll(".leaflet-marker-pane img.leaflet-marker-icon");for(const e of p)try{const t=e.getBoundingClientRect(),n=Math.round(t.left-d.left),o=Math.round(t.top-d.top),r=Math.round(t.width),i=Math.round(t.height),a=!(t.right<=d.left||t.left>=d.right||t.bottom<=d.top||t.top>=d.bottom),s=window.getComputedStyle(e);r>0&&i>0&&a&&"none"!==s.display&&"hidden"!==s.visibility&&c.drawImage(e,n,o,r,i)}catch(e){console.warn("Failed to draw marker icon:",e)}let g="";try{g=l.toDataURL("image/png",1)}catch(e){console.warn("Map canvas tainted; falling back to placeholder")}const m=document.createElement("img");g?(m.src=g,m.width=a,m.height=s,m.setAttribute("width",String(a)),m.setAttribute("height",String(s)),m.style.width=a+"px",m.style.height=s+"px",m.style.display="block",m.style.border="1px solid #ddd",m.setAttribute("v:shapes","image"+Math.random().toString(36).substr(2,9)),m.alt="Map"):(m.alt="Map",m.style.width=a+"px",m.style.height=s+"px",m.style.border="1px solid #ddd",m.style.backgroundColor="#f0f0f0"),t.parentNode.replaceChild(m,t)}catch(e){console.warn("Failed to process map container:",e)}const p=r.querySelectorAll(".qde-html-container");for(const e of p)try{const t=e.getAttribute("data-qd-source"),n=e.querySelector("pre");if(t){const n=document.createElement("div");n.innerHTML=t,n.querySelectorAll("script").forEach(e=>e.remove()),n.querySelectorAll("*").forEach(e=>{for(const t of Array.from(e.attributes))/^on/i.test(t.name)&&e.removeAttribute(t.name)});const o=n.querySelectorAll("img");for(const e of o){const t=e.getAttribute("width"),n=e.getAttribute("height");if(t&&(e.width=parseInt(t),e.style.width=t.includes("%")?t:`${e.width}px`),n&&(e.height=parseInt(n),e.style.height=n.includes("%")?n:`${e.height}px`),e.src&&!e.src.startsWith("data:"))try{const o=document.createElement("canvas"),r=o.getContext("2d"),i=new Image;i.crossOrigin="anonymous",await new Promise((a,s)=>{if(i.onload=function(){let s=0,l=0;if(t&&!t.includes("%")&&(s=parseInt(t)),n&&!n.includes("%")&&(l=parseInt(n)),s>0&&0===l){if(i.naturalWidth>0){const e=i.naturalHeight/i.naturalWidth;l=Math.round(s*e)}}else if(l>0&&0===s){if(i.naturalHeight>0){const e=i.naturalWidth/i.naturalHeight;s=Math.round(l*e)}}else 0===s&&0===l&&(s=i.naturalWidth||250,l=i.naturalHeight||200);o.width=s,o.height=l,r.drawImage(i,0,0,s,l);const c=o.toDataURL("image/png",1);e.src=c,e.width=s,e.height=l,e.setAttribute("width",s.toString()),e.setAttribute("height",l.toString()),e.style.width=s+"px",e.style.height=l+"px",a()},i.onerror=function(){console.warn("Failed to load HTML fence image:",e.src),s(new Error("Image load failed"))},e.src.startsWith("http")||e.src.startsWith("//"))i.src=e.src;else{const t=new Image;t.src=e.src,i.src=t.src}})}catch(t){console.warn("Failed to convert HTML fence image:",e.src,t)}e.setAttribute("v:shapes","image"+Math.random().toString(36).substr(2,9))}e.innerHTML=n.innerHTML}else if(!n){const t=e.querySelectorAll("img");for(const e of t){const t=e.getAttribute("width"),n=e.getAttribute("height");if(t&&(e.width=parseInt(t),e.style.width=t.includes("%")?t:`${e.width}px`),n&&(e.height=parseInt(n),e.style.height=n.includes("%")?n:`${e.height}px`),e.src&&!e.src.startsWith("data:"))try{const t=document.createElement("canvas"),n=t.getContext("2d"),o=new Image;o.crossOrigin="anonymous",await new Promise((r,i)=>{if(o.onload=function(){let i=e.width||0,a=e.height||0;if(i&&!a){const e=o.naturalHeight/o.naturalWidth;a=Math.round(i*e)}else if(a&&!i){const e=o.naturalWidth/o.naturalHeight;i=Math.round(a*e)}else i||a||(i=o.naturalWidth||250,a=o.naturalHeight||Math.round(o.naturalHeight/o.naturalWidth*250));t.width=i,t.height=a,n.drawImage(o,0,0,i,a);const s=t.toDataURL("image/png",1);e.src=s,e.width=i,e.height=a,e.setAttribute("width",i.toString()),e.setAttribute("height",a.toString()),e.style.width=i+"px",e.style.height=a+"px",r()},o.onerror=function(){console.warn("Failed to load HTML fence image:",e.src),i(new Error("Image load failed"))},e.src.startsWith("http")||e.src.startsWith("//"))o.src=e.src;else{const t=new Image;t.src=e.src,o.src=t.src}})}catch(t){console.warn("Failed to convert HTML fence image:",e.src,t)}e.setAttribute("v:shapes","image"+Math.random().toString(36).substr(2,9))}}}catch(e){console.warn("Failed to process HTML container:",e)}const g=r.innerHTML,m=function(e){return"stripped"===e?"":Object.entries(_).map(([t,n])=>{const o="quikdown"===e?" font-weight:bold;":"";return`${t} { font-size:${n.fontSize};${o} line-height:1.25; margin:${n.marginTop} 0 ${n.marginBottom}; }`}).join("\n ")}(n),f=`\n <!DOCTYPE html>\n <html xmlns:v="urn:schemas-microsoft-com:vml"\n xmlns:o="urn:schemas-microsoft-com:office:office"\n xmlns:w="urn:schemas-microsoft-com:office:word">\n <head>\n <meta charset="utf-8">\n <style>\n ${"stripped"===n?"/* Minimal styles */\n img { max-width: 100%; height: auto; }":`${m}\n\n /* Table styling */\n table { border-collapse: collapse; width: 100%; margin-bottom: 1em; }\n th, td { border: 1px solid #ccc; padding: 8px; text-align: left; }\n th { background-color: #f0f0f0; font-weight: bold; }\n\n /* Code block styling */\n pre { background-color: #f4f4f4; padding: 1em; border-radius: 4px; overflow-x: auto; }\n code { font-family: monospace; background-color: #f4f4f4; padding: 0.2em 0.4em; border-radius: 3px; }\n\n /* Image handling */\n img { display: block; max-width: 100%; height: auto; margin: 0.5em 0; }\n\n /* Blockquote */\n blockquote { border-left: 4px solid #ddd; margin-left: 0; padding-left: 1em; color: #666; }`}\n \n /* Math equations centered like squibview */\n .math-display { text-align: center; margin: 1em 0; }\n .math-display img { display: inline-block; margin: 0 auto; }\n </style>\n </head>\n <body>\x3c!--StartFragment--\x3e${g}\x3c!--EndFr