l&&" "!==n[l])&&new RegExp(`^\\s{${l},}`).test(n)){p.push(""),a++;continue}}break}if(i){if(new RegExp(`^(\\s{0,${r}})\\d+\\.\\s`).test(t))break}else if(new RegExp(`^(\\s{0,${r}})[-*+]\\s`).test(t))break;if(F.test(t)||H.test(t)||j.test(t)||V.test(t))break;const n=t.match(new RegExp(`^(\\s{${l},})([-*+])\\s`)),o=t.match(new RegExp(`^(\\s{${l},})(\\d+)\\.\\s`));if(n||o){const t=!!o,r=t?o[2]:n[2],i=t?o[1].length:n[1].length,s=t?parseInt(o[2],10):void 0,l=ae(e,a,r,i,t);m.push({type:t?"ol":"ul",items:l.items,start:s}),a=l.endIdx;continue}t.length>l&&""===t.slice(0,l).trim()?p.push(t.slice(l)):p.push(t),a++}p.length&&(h=h?h+"\n"+p.join("\n"):p.join("\n")),s.push({type:"listItem",text:h,checked:d,task:u,subTokens:m.length?m:void 0})}return{items:s,endIdx:a}},le=e=>N.test(e)&&/-/.test(e),ce=e=>e.replace(/^\s*\|?\s*|\s*\|?\s*$/g,"").split(/\s*\|\s*/).map(e=>/^:/.test(e)&&/:$/.test(e)?"center":/:$/.test(e)?"right":(/^:/.test(e),"left")),he=(e,n,r)=>{switch(e.type){case"heading":{const t=X(e.text);return`${G(e.text,n)}`}case"paragraph":return`${G(e.text,n)}
`;case"hr":return"
";case"quote":return`${se(e.content,n)}
`;case"code":return ue(e.content,e.lang,n,e.attrs);case"ul":return`${e.items.map(e=>de(e,n)).join("")}
`;case"ol":{const t=e.start||1;return`1?` start="${t}"`:""}>${e.items.map((e,r)=>de(e,n,t>1?r+t:void 0)).join("")}
`}case"table":return pe(e,n);case"defList":{let t="";return t+=`- ${G(e.term,n)}
`,e.defs.forEach(e=>{t+=`- ${G(e,n)}
`}),t+"
"}case"mathBlock":return`${t(e.content)}
`;default:return`${e.content?"string"==typeof e.content?G(e.content,n):t(String(e.content)):""}
`}},de=(e,t,n)=>{if(e.task){let n=` ${G(e.text,t)}`;return e.subTokens&&(n+="\n"+e.subTokens.map(e=>he(e,t)).join("\n")),n+""}let r=null!=n?``:"";return r+=G(e.text,t),e.subTokens&&(r+="\n"+e.subTokens.map(e=>he(e,t)).join("\n")),r+""},ue=(e,r,i,o)=>{if("mermaid"===r)return``;const s=r?` class="language-${n(r)}"`:"",a=o?.title?`${n(o.title)}
`:"";if(i.highlight&&"function"==typeof i.highlight&&r)try{const t=i.highlight(e,r);if("string"==typeof t)return`${a}${t}
`}catch(e){}return`${a}${t(e)}
`},pe=(e,t)=>{const n=e=>{const t=[];let n="",r=!1;for(let i=0;i0&&""===t[0]&&t.shift(),t.length>0&&""===t[t.length-1]&&t.pop(),t},r=n(e.header),i=e.align||[],o=e=>i[e]&&"left"!==i[e]?` style="text-align:${i[e]}"`:"";let s='';return s+=r.map((e,n)=>`| ${G(e,t)} | `).join(""),s+="
",s+=e.rows.map(e=>`${n(e).map((e,n)=>`| ${G(e,t)} | `).join("")}
`).join(""),s+"
"},me=(e,n)=>{if(!e)return"";const r=[];let i=ge(e,r);const o=[];return i=i.replace(/&(?:[a-zA-Z][a-zA-Z0-9]{1,31}|#\d{1,7}|#x[0-9a-fA-F]{1,6});/g,e=>(o.push(e),`${o.length-1}`)),i=i.replace(//g,e=>(o.push(e),`${o.length-1}`)),i=i.replace(/<\/?u\s*>/gi,e=>(o.push(e),`${o.length-1}`)),i=t(i),i=i.replace(/\u0005(\d+)\u0005/g,(e,t)=>o[+t]||e),i=be(i,r,n),i=i.replace(/\u0000(\d+)\u0000/g,(e,n)=>{const i=r[+n];return i?`${t(i.content)}`:e}),i=i.replace(/:([\w+-]+):/g,(e,t)=>U[t]||e),i=i.replace(/ \n/g,"
\n"),i=i.replace(/\n/g,"
"),i},ge=(e,t)=>{let n="",r=0;for(;r{const i=new Map;return(e=(e=e.replace(/\u0000\d+\u0000/g,e=>{const t=`${i.size}`;return i.set(t,e),t})).replace(fe,e=>{const n=e;if(n.startsWith("***"))return`${n.slice(3,-3)}`;if(n.startsWith("___"))return`${n.slice(3,-3)}`;if(n.startsWith("){const e=n.match(/!\[([^\]]*)\]\(([^)\s]+)(?:\s+['"](.+?)['"])?\s*\)/);if(!e)return n;const r=Q(e[2]);return r?`
`:t(n)}if(n.startsWith("![")&&n.includes("][")){const e=n.match(/!\[([^\]]*)\]\[([^\]]*)\]/);if(!e)return n;const i=(e[2]||e[1]).toLowerCase();if(r.refs&&r.refs[i])try{const o=JSON.parse(r.refs[i]),s=Q(o.url);return s?`
`:t(n)}catch(t){return`
`}return`
`}if(n.startsWith("[")&&n.includes("](")){const e=n.match(/\[([^\]]+)\]\(([^)\s]+)(?:\s+['"](.+?)['"])?\s*\)/);if(!e)return n;const t=Q(e[2]);if(!t)return n;const i=me(e[1],r);return`${i}`}if(n.startsWith("[")&&n.includes("][")){const e=n.match(/\[([^\]]+)\]\[([^\]]*)\]/);if(!e)return n;const i=(e[2]||e[1]).toLowerCase();if(r.refs&&r.refs[i])try{const o=JSON.parse(r.refs[i]),s=Q(o.url);return s?`${e[1]}`:t(n)}catch(e){return t(n)}return t(n)}if(n.startsWith("<http")){const e=n.slice(4,-4),t=Q(e);return`${e}`}if(n.startsWith("**"))return`${n.slice(2,-2)}`;if(n.startsWith("__"))return`${n.slice(2,-2)}`;if(n.startsWith("~~"))return`${n.slice(2,-2)}`;if(n.startsWith("=="))return`${n.slice(2,-2)}`;if(n.startsWith("*")&&!n.startsWith("**")&&!n.startsWith("***"))return`${n.slice(1,-1)}`;if(n.startsWith("_")&&!n.startsWith("__")&&!n.startsWith("___"))return`${n.slice(1,-1)}`;if(n.startsWith("^")&&!n.startsWith("^^"))return`${n.slice(1,-1)}`;if(n.startsWith("~")&&!n.startsWith("~~"))return`${n.slice(1,-1)}`;if(n.startsWith("$")&&!n.startsWith("$$"))return`${t(n.slice(1,-1))}`;if(/^\[\^/.test(n)){const e=n.slice(2,-1),t=J(e);return``}if(n.startsWith("\\")&&2===n.length){const e=n[1];return/[!"#$%&'()*+,\-./:;<=>?@\[\\\]^_`{|}~]/.test(e)?e:n}return n})).replace(/\u0003(\d+)\u0003/g,e=>i.get(e)||e)},ve=Symbol("me-plugin:autoSave"),ye=Symbol("me-plugin:searchReplace"),xe=Symbol("me-plugin:imagePaste"),we=Symbol("me-plugin:shortcutHelp"),_e=Symbol("me-plugin:fileSystem"),ke=(e,t)=>e[t],Ce=(e,t,n)=>{e[t]=n},Ee=(e,t)=>{delete e[t]};class Se{constructor(){this.plugins=new Map}register(e,t){return this.plugins.has(e)?this:t&&"object"==typeof t&&(t.name||e)?(this.plugins.set(e,{version:t.version||"0.0.0",description:t.description||"",depends:t.depends||[],priority:t.priority||0,...t,name:e,enabled:!0}),this):this}unregister(e){return this.plugins.delete(e),this}get(e){return this.plugins.get(e)||null}has(e){return this.plugins.has(e)}getAll(){return Array.from(this.plugins.values())}getNames(){return Array.from(this.plugins.keys())}enable(e){const t=this.plugins.get(e);return t&&(t.enabled=!0),this}disable(e){const t=this.plugins.get(e);return t&&(t.enabled=!1),this}isEnabled(e){const t=this.plugins.get(e);return!!t&&t.enabled}destroy(){this.plugins.clear()}}const Le=new Se,Te=e=>{const t=new Map;e.forEach(e=>t.set(e.name,e));const n=new Map,r=new Map;e.forEach(e=>{n.set(e.name,0),r.set(e.name,[])}),e.forEach(e=>{(e.depends||[]).forEach(i=>{t.has(i)&&(r.get(i).push(e.name),n.set(e.name,(n.get(e.name)||0)+1))})});const i=[];n.forEach((e,t)=>{0===e&&i.push(t)});const o=[];for(;i.length;){i.sort((e,n)=>(t.get(n).priority||0)-(t.get(e).priority||0));const e=i.shift();o.push(t.get(e)),(r.get(e)||[]).forEach(e=>{n.set(e,n.get(e)-1),0===n.get(e)&&i.push(e)})}return o.length!==e.length?e:o},Me=(e={},t={})=>{const n=[],r={...t};for(const[i,o]of Object.entries(e)){const e=t[i];if(o.required&&null==e)n.push(`"${i}" is required`);else if(void 0!==e||void 0===o.default){if(void 0!==e&&o.type){const t=Array.isArray(e)?"array":typeof e;t!==o.type&&n.push(`"${i}" expected ${o.type}, got ${t}`)}if(void 0!==e&&o.enum&&!o.enum.includes(e)&&n.push(`"${i}" must be one of [${o.enum.join(", ")}]`),void 0!==e&&"function"==typeof o.validator)try{const t=o.validator(e);!0!==t&&n.push(`"${i}": ${t}`)}catch(e){n.push(`"${i}": ${e.message}`)}}else r[i]=o.default}return{valid:0===n.length,errors:n,patched:r}},$e={autoSave:{name:"autoSave",version:"0.1.1",description:"Auto-save to localStorage",priority:100,install(e,t){if(!e||"function"!=typeof e.getValue)return;const n=t||this,r=n.key||"me-draft-"+(e.id||""),i=n.delay||1e3,o={_timer:null},s=()=>{o._timer&&(clearTimeout(o._timer),o._timer=null);try{localStorage.setItem(r,e.getValue()),"function"==typeof e._emit&&e._emit("autosave",{key:r,value:e.getValue()})}catch(e){}},a=()=>{o._timer&&clearTimeout(o._timer),o._timer=setTimeout(s,i)},l=s,c=s;"function"==typeof e.on&&(e.on("change",a),e.on("blur",l),e.on("save",c)),e.restoreDraft=()=>{try{const t=localStorage.getItem(r);return null!=t&&"function"==typeof e.setValue&&e.setValue(t),t}catch(e){return null}},e.clearDraft=()=>{try{localStorage.removeItem(r)}catch(e){}return e},e.getDraftKey=()=>r,Ce(e,ve,{state:o,_onInput:a,_onBlur:l,_onSave:c,save:s})},destroy(e){const t=ke(e,ve);t&&(t.state._timer&&(clearTimeout(t.state._timer),t.state._timer=null),e&&"function"==typeof e.off&&(t._onInput&&e.off("change",t._onInput),t._onBlur&&e.off("blur",t._onBlur),t._onSave&&e.off("save",t._onSave)),Ee(e,ve))}},exportTool:{name:"exportTool",version:"0.1.0",description:"Export Markdown/HTML",priority:50,install(e){if(!e||"function"!=typeof e.getValue)return;const t=(e,t,n)=>{if("undefined"==typeof document)return;const r=new Blob([t],{type:n+";charset=utf-8"}),i=URL.createObjectURL(r),o=document.createElement("a");o.href=i,o.download=e,document.body.appendChild(o),o.click(),document.body.removeChild(o),setTimeout(()=>URL.revokeObjectURL(i),0)},n=()=>{const e=new Date,t=e=>String(e).padStart(2,"0");return`${e.getFullYear()}${t(e.getMonth()+1)}${t(e.getDate())}-${t(e.getHours())}${t(e.getMinutes())}`},r=e,i=(t={})=>{const n=t.title||"Document",r=t.css||"",i="function"==typeof e.getHTML?e.getHTML():"",o=!1!==t.embedCSS?'':"";return`\n\n\n\n\n${n}\n${r?``:""}${o}\n\n\n${i}\n\n`};r.exportMarkdown=r=>(t(r||`metona-${n()}.md`,e.getValue(),"text/markdown"),e),r.exportHTML=(r,o={})=>(t(r||`metona-${n()}.html`,i(o),"text/html"),e),r.exportPDF=(t={})=>{if("undefined"==typeof document||!e.el)return e;const n=document.createElement("iframe");n.style.position="fixed",n.style.right="0",n.style.bottom="0",n.style.width="0",n.style.height="0",n.style.border="0",document.body.appendChild(n);const r=n.contentDocument;return r?(r.open(),r.write(i(t)),r.close(),setTimeout(()=>{try{n.contentWindow?.print()}catch(e){}setTimeout(()=>{n.parentNode&&n.parentNode.removeChild(n)},1e3)},50),e):(n.remove(),e)}},destroy(e){e&&(delete e.exportMarkdown,delete e.exportHTML,delete e.exportPDF)}},searchReplace:{name:"searchReplace",version:"0.2.1",description:"Search & Replace (Ctrl+F/H)",priority:80,install(e){if(!e||!e.textarea||"undefined"==typeof document)return;if(!document.getElementById("me-search-style")){const e=document.createElement("style");e.id="me-search-style",e.textContent=".me-search{position:absolute;top:10px;right:16px;z-index:20;display:flex;flex-direction:column;gap:8px;padding:12px;background:var(--md-bg,#1c2029);border:1px solid var(--md-border,rgba(255,255,255,0.12));border-radius:10px;box-shadow:0 12px 40px -8px rgba(0,0,0,0.4),0 0 0 1px rgba(255,255,255,0.05);font-size:13px;min-width:300px;backdrop-filter:blur(16px);-webkit-backdrop-filter:blur(16px);animation:meSearchIn .18s ease-out}@keyframes meSearchIn{from{opacity:0;transform:translateY(-8px)}to{opacity:1;transform:translateY(0)}}.me-search-row{display:flex;gap:6px;align-items:center}.me-search-row+.me-search-row{margin-top:2px}.me-search input{flex:1;min-width:0;padding:7px 10px;border:1px solid var(--md-border,rgba(255,255,255,0.12));border-radius:6px;background:var(--md-textarea-bg,rgba(255,255,255,0.04));color:var(--md-text,#e6e8eb);font-size:13px;outline:none;transition:border-color .15s,box-shadow .15s}.me-search input::placeholder{color:var(--md-muted,#6b7280)}.me-search input:focus{border-color:var(--md-accent,#3b82f6);box-shadow:0 0 0 3px rgba(59,130,246,0.15)}.me-search .me-search-btn{display:inline-flex;align-items:center;justify-content:center;width:28px;height:28px;padding:0;border:1px solid rgba(255,255,255,0.08);border-radius:5px;background:rgba(255,255,255,0.04);color:var(--md-muted,#9ca3af);cursor:pointer;transition:all .12s;flex-shrink:0}.me-search .me-search-btn:hover{background:var(--md-accent,#3b82f6);color:#fff;border-color:var(--md-accent,#3b82f6)}.me-search .me-search-btn:active{transform:scale(.92)}.me-search .me-search-btn.me-active{background:var(--md-accent,#3b82f6);color:#fff;border-color:var(--md-accent,#3b82f6)}.me-search .me-search-btn svg{width:14px;height:14px}.me-search .me-search-count{color:var(--md-muted,#9ca3af);font-size:11.5px;min-width:28px;text-align:center;font-variant-numeric:tabular-nums}.me-search .me-search-close{margin-left:2px;font-size:16px;line-height:1}.me-search .me-search-close:hover{background:#ef4444;border-color:#ef4444}.me-search .me-search-replace-one,.me-search .me-search-replace-all{width:auto;padding:5px 10px;font-size:12px;font-weight:500;letter-spacing:.01em}.me-search .me-search-replace-all{background:rgba(59,130,246,0.12);border-color:rgba(59,130,246,0.3);color:var(--md-accent,#60a5fa)}.me-search .me-search-replace-all:hover{background:var(--md-accent,#3b82f6);color:#fff}",document.head.appendChild(e)}const t={_panel:null,_regexMode:!1,_matchCase:!0,_wholeWord:!1,_cleanup:null},n="[\\w\\u4e00-\\u9fff]",r=e=>{if(!e)return null;let r=t._regexMode?e:e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");t._wholeWord&&(r=`(?{const r=e=>!!e&&/[\w\u4e00-\u9fff]/.test(e);return!r(e[t-1])&&!r(e[t+n])},o=()=>{if(!t._panel)return;const e="1"===t._panel.dataset.replace,n=t._panel.querySelector(".me-search-replace-row");n&&(n.style.display=e?"flex":"none")},s=()=>{t._cleanup&&(t._cleanup(),t._cleanup=null),t._panel=null,e&&e.textarea&&e.textarea.focus()},a=n=>{if(t._panel){t._panel.dataset.replace=n?"1":"0",o();const e=t._panel.querySelector(".me-search-find");return void(e&&(e.focus(),e.select()))}const a=e.textarea.value.substring(e.textarea.selectionStart,e.textarea.selectionEnd),l=document.createElement("div");var c;l.className="me-search",l.dataset.replace=n?"1":"0",l.innerHTML=``,e.el.appendChild(l),t._panel=l,o(),t._regexMode=!1,t._matchCase=!0,t._wholeWord=!1;const h=l.querySelector(".me-search-find"),d=l.querySelector(".me-search-replace"),u=l.querySelector(".me-search-count"),p=l.querySelector(".me-search-regex"),m=l.querySelector(".me-search-case"),g=l.querySelector(".me-search-word");p.addEventListener("click",()=>{t._regexMode=!t._regexMode,p.classList.toggle("me-active",t._regexMode),b=f()}),m.addEventListener("click",()=>{t._matchCase=!t._matchCase,m.classList.toggle("me-active",t._matchCase),b=f()}),g.addEventListener("click",()=>{t._wholeWord=!t._wholeWord,g.classList.toggle("me-active",t._wholeWord),b=f()});const f=()=>{const t=h.value;if(!t)return u.textContent="",[];const n=[],i=r(t);if(!i)return u.textContent="err",[];let o;for(;null!==(o=i.exec(e.textarea.value));)n.push(o.index),0===o[0].length&&i.lastIndex++;return u.textContent=n.length?`${n.length}`:"0",n};let b=[];const v=(t,n)=>{e.textarea.focus(),e.textarea.setSelectionRange(t,t+(n||h.value.length))},x=n=>{const o=e.textarea.value,s=h.value;if(!s)return 0;if(!t._regexMode)return t._matchCase&&o.slice(n,n+s.length)!==s?0:t._matchCase||o.slice(n,n+s.length).toLowerCase()===s.toLowerCase()?t._wholeWord&&!i(o,n,s.length)?0:s.length:0;const a=r(s);if(!a)return 0;a.lastIndex=n;const l=a.exec(o);return l&&l.index===n?l[0].length:0},w=()=>{if(b=f(),!b.length)return;const t=e.textarea.selectionEnd;let n=b.find(e=>e>=t);null==n&&(n=b[0]),v(n,x(n)||h.value.length)},_=()=>{if(b=f(),!b.length)return;const t=e.textarea.selectionStart;let n=-1;for(let e=b.length-1;e>=0;e--)if(b[e]{const n=h.value,o=d.value;if(!n)return;const s=e.textarea,a=s.selectionStart,l=s.selectionEnd,c=s.value.substring(a,l);let u=!1;if(t._regexMode){const e=r(n);if(e){e.lastIndex=a;const t=e.exec(s.value);u=!(!t||t.index!==a)}}else u=(t._matchCase?c===n:c.toLowerCase()===n.toLowerCase())&&(!t._wholeWord||i(s.value,a,n.length));u&&(s.value=s.value.substring(0,a)+o+s.value.substring(l),s.setSelectionRange(a,a+o.length)),e._value=s.value,"function"==typeof e._pushHistory&&e._pushHistory(),"function"==typeof e._render&&e._render(),"function"==typeof e._updateWordCount&&e._updateWordCount(),"function"==typeof e._emit&&e._emit("change",e._value),w()};h.addEventListener("input",()=>{b=f()}),h.addEventListener("keydown",e=>{"Enter"===e.key&&(e.preventDefault(),e.shiftKey?_():w()),"Escape"===e.key&&(e.preventDefault(),s())}),d.addEventListener("keydown",e=>{"Enter"===e.key&&(e.preventDefault(),k()),"Escape"===e.key&&(e.preventDefault(),s())}),l.querySelector(".me-search-next").addEventListener("click",w),l.querySelector(".me-search-prev").addEventListener("click",_),l.querySelector(".me-search-close").addEventListener("click",()=>s()),l.querySelector(".me-search-replace-one").addEventListener("click",k),l.querySelector(".me-search-replace-all").addEventListener("click",()=>{const t=h.value,n=d.value;if(!t)return;const i=e.textarea,o=r(t);o&&(i.value=i.value.replace(o,()=>n),i.setSelectionRange(0,0),e._value=i.value,"function"==typeof e._pushHistory&&e._pushHistory(),"function"==typeof e._render&&e._render(),"function"==typeof e._updateWordCount&&e._updateWordCount(),"function"==typeof e._emit&&e._emit("change",e._value),f())}),h.focus(),h.select(),t._cleanup=()=>{l.parentNode&&l.parentNode.removeChild(l)}},l=e=>{if(!e.ctrlKey&&!e.metaKey)return;const n=e.key.toLowerCase();"f"===n?(e.preventDefault(),a(!1)):"h"===n?(e.preventDefault(),a(!0)):"escape"===n&&t._panel&&s()};e.textarea.addEventListener("keydown",l),Ce(e,ye,{state:t,_open:a,_close:s,_onKeydown:l})},destroy(e){const t=ke(e,ye);if(t){const n=t.state;n&&(n._cleanup&&(n._cleanup(),n._cleanup=null),n._panel=null),t._onKeydown&&e&&e.textarea&&e.textarea.removeEventListener("keydown",t._onKeydown),Ee(e,ye)}}},imagePaste:{name:"imagePaste",version:"0.2.1",description:"Paste image as base64",priority:60,install(e,t={}){if(!e||!e.textarea||"undefined"==typeof document)return;const n=t.maxSizeKB||500,r=t=>{const r=t.clipboardData?.items;if(r)for(const i of r)if(i.type?.startsWith("image/")){const r=i.getAsFile();if(!r)continue;const o=r.size/1024;if(n>0&&o>n){t.preventDefault(),"function"==typeof e.toast&&e.toast(y("imageTooLarge",{size:o.toFixed(0),max:n})||`Image too large (${o.toFixed(0)}KB > ${n}KB)`,{type:"warning"});break}t.preventDefault();const s=new FileReader;s.onload=()=>{"function"==typeof e.insert&&e.insert(`\n`)},s.readAsDataURL(r);break}};e.textarea.addEventListener("paste",r),Ce(e,xe,r)},destroy(e){const t=ke(e,xe);t&&e?.textarea&&e.textarea.removeEventListener("paste",t),Ee(e,xe)}},shortcutHelp:{name:"shortcutHelp",version:"0.2.0",description:"Press ? to show shortcuts",priority:200,install(e){if(!e||!e.textarea||"undefined"==typeof document)return;if(!document.getElementById("me-shortcut-style")){const e=document.createElement("style");e.id="me-shortcut-style",e.textContent=".me-shortcut-overlay{position:fixed;top:0;left:0;right:0;bottom:0;z-index:50;background:rgba(0,0,0,0.5);display:flex;align-items:center;justify-content:center}.me-shortcut-panel{background:var(--md-bg,#fff);border-radius:12px;padding:24px;max-width:560px;width:90%;max-height:80vh;overflow-y:auto;box-shadow:0 12px 40px rgba(0,0,0,0.3)}.me-shortcut-panel h3{font-size:16px;margin:0 0 16px;color:var(--md-text)}.me-shortcut-panel table{width:100%;border-collapse:collapse;font-size:13px}.me-shortcut-panel td{padding:6px 10px;border-bottom:1px solid var(--md-border)}.me-shortcut-panel td:first-child{font-family:var(--md-mono);font-size:12px;color:var(--md-accent);white-space:nowrap;width:40%}.me-shortcut-panel .me-shortcut-close{position:absolute;top:16px;right:20px;background:none;border:none;font-size:20px;cursor:pointer;color:var(--md-muted)}",document.head.appendChild(e)}let t=null;const n=()=>{t&&(t.remove(),t=null)},r=()=>{if(t)return void n();const e=[["Ctrl+B",y("bold")||"Bold"],["Ctrl+I",y("italic")||"Italic"],["Ctrl+U",y("underline")||"Underline"],["Ctrl+K",y("link")||"Link"],["Ctrl+E",y("code")||"Code"],["Ctrl+1/2/3",y("h1")||"Heading"],["Ctrl+Q",y("quote")||"Quote"],["Ctrl+Z",y("undo")||"Undo"],["Ctrl+Y",y("redo")||"Redo"],["Ctrl+S",y("save")||"Save"],["Ctrl+F",y("search")||"Search"],["Ctrl+H",y("replace")||"Replace"],["Tab",y("indent")||"Indent"],["Shift+Tab",y("outdent")||"Outdent"],["?",y("shortcuts")||"Shortcuts"]];let r="";e.forEach(([e,t])=>{r+=`| ${e} | ${t} |
`});const i=document.createElement("div");i.className="me-shortcut-overlay",i.innerHTML=`⌨️ ${y("shortcuts")||"Shortcuts"}
`,i.addEventListener("click",e=>{(e.target===i||e.target.classList.contains("me-shortcut-close"))&&n()}),document.body.appendChild(i),t=i},i=e=>{"?"!==e.key||e.ctrlKey||e.metaKey||e.altKey||(e.preventDefault(),r()),"Escape"===e.key&&t&&n()};e.textarea.addEventListener("keydown",i),Ce(e,we,{_panel:t,_open:r,_close:n,_onKeydown:i})},destroy(e){const t=ke(e,we);t&&t._panel&&t._panel.remove();const n=t?._onKeydown;n&&e?.textarea&&e.textarea.removeEventListener("keydown",n),Ee(e,we)}},fileSystem:{name:"fileSystem",version:"0.2.0",description:"File System Access API",priority:90,install(e){if(!e||"function"!=typeof e.getValue)return;const t="undefined"!=typeof window&&"function"==typeof window.showOpenFilePicker;let n=null;e.openFile=async(r={})=>{if(!t)return"function"==typeof e.toast&&e.toast?.("File System Access API not supported",{type:"warning"}),null;try{const[t]=await window.showOpenFilePicker({types:[{accept:{"text/markdown":[".md",".txt",".markdown"]}}],...r});n=t;const i=await t.getFile(),o=await i.text();return"function"==typeof e.setValue&&e.setValue(o),e._emit?.("fileOpened",{name:i.name,handle:t}),{name:i.name,content:o,handle:t}}catch(e){return e.name,null}},e.saveFile=async(r={})=>{let i=n;if(!i||r.saveAs){if(!t)return"function"==typeof e.toast&&e.toast?.("File System Access API not supported",{type:"warning"}),!1;try{i=await window.showSaveFilePicker({types:[{accept:{"text/markdown":[".md"]}}],suggestedName:r.name||"document.md"}),n=i}catch(e){return e.name,!1}}try{const t=await i.createWritable();return await t.write(e.getValue()),await t.close(),e._emit?.("fileSaved",{handle:i}),!0}catch(t){return n=null,!r.saveAs&&e.saveFile({...r,saveAs:!0})}},e.saveFileAs=t=>e.saveFile({saveAs:!0,name:t}),e.getFileHandle=()=>n,Ce(e,_e,{getHandle:()=>n})},destroy(e){e&&(delete e.openFile,delete e.saveFile,delete e.saveFileAs,delete e.getFileHandle),Ee(e,_e)}}},ze={createManager:()=>new Se,manager:Le,register:(e,t)=>Le.register(e,t),unregister:e=>Le.unregister(e),get:e=>Le.get(e),has:e=>Le.has(e),getAll:()=>Le.getAll(),getNames:()=>Le.getNames(),enable:e=>Le.enable(e),disable:e=>Le.disable(e),isEnabled:e=>Le.isEnabled(e),getPreset:e=>$e[e]?{...$e[e]}:null,getAllPresets:()=>Object.keys($e).reduce((e,t)=>(e[t]={...$e[t]},e),{}),createPlugin:(e={})=>{const t={name:e.name||"custom",version:e.version||"0.0.0",description:e.description||"",depends:e.depends||[],priority:e.priority||0,install:()=>{},destroy:()=>{},...e};return"function"!=typeof t.install&&(t.install=()=>{}),"function"!=typeof t.destroy&&(t.destroy=()=>{}),t},validatePlugin:e=>{const t=[];return e&&"object"==typeof e||t.push("plugin must be an object"),e&&!e.name&&t.push("plugin must have a name"),e&&e.install&&"function"!=typeof e.install&&t.push("install must be a function"),{valid:0===t.length,errors:t}},topologicalSort:Te,validateConfig:Me};let Ae="auto",Be=new Set,He=null;const Ie=()=>"undefined"==typeof window?"light":"undefined"!=typeof window&&window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light",Pe=()=>{"undefined"!=typeof window&&window.matchMedia&&(He&&(He(),He=null),He=(()=>{if("undefined"==typeof window||!window.matchMedia)return()=>{};const e=window.matchMedia("(prefers-color-scheme: dark)"),t=e=>(e.matches,void("auto"===Ae&&Oe("auto")));return e.addEventListener("change",t),()=>e.removeEventListener("change",t)})())},Re=e=>e&&"auto"!==e?e:Ie(),je=e=>{const t=Re(e),n=a[t];return n&&"object"==typeof n?n:a.light},Fe=(e,t)=>{if("undefined"==typeof document||!e||"object"!=typeof e)return;const n=t||document.documentElement,r={"--md-bg":e.bg,"--md-text":e.text,"--md-border":e.border,"--md-shadow":e.shadow,"--md-hover-shadow":e.hoverShadow,"--md-toolbar-bg":e.toolbarBg||e.bg,"--md-textarea-bg":e.textareaBg||e.bg,"--md-preview-bg":e.previewBg||e.bg,"--md-code-bg":e.codeBg||"rgba(127,127,127,0.1)","--md-code-text":e.codeText||e.text,"--md-accent":e.accent||"#3b82f6","--md-muted":e.muted||"#9ca3af"};for(const[e,t]of Object.entries(r))null!=t&&n.style.setProperty(e,String(t));e.progressBg&&n.style.setProperty("--md-progress-bg",e.progressBg),e.closeHoverBg&&n.style.setProperty("--md-close-hover-bg",e.closeHoverBg)},We=e=>{const t=e||document.documentElement;if("undefined"==typeof document)return{};const n=getComputedStyle(t),r={};return["--md-bg","--md-text","--md-border","--md-shadow","--md-hover-shadow","--md-toolbar-bg","--md-textarea-bg","--md-preview-bg","--md-code-bg","--md-code-text","--md-accent","--md-muted"].forEach(e=>{const t=n.getPropertyValue(e).trim();t&&(r[e]=t)}),r},De=(e,t)=>{const n=t||document.documentElement;if("undefined"==typeof document)return"";const r=e.startsWith("--")?e:`--md-${e}`;return getComputedStyle(n).getPropertyValue(r).trim()},Oe=e=>{Ae=e;const t=Re(e);"undefined"!=typeof document&&(document.documentElement.setAttribute("data-md-theme",t),document.documentElement.classList.remove("md-theme-light","md-theme-dark","md-theme-auto","md-theme-warm"),document.documentElement.classList.add(`md-theme-${t}`),Fe(je(e))),Ue(e,t)},Ne=e=>{Oe(e),Ke(e)},qe=()=>Re(Ae),Ke=e=>{if("undefined"!=typeof localStorage)try{localStorage.setItem("metona-editor-theme",e)}catch(e){}},Ve=()=>{if("undefined"!=typeof localStorage)try{return localStorage.getItem("metona-editor-theme")||"auto"}catch(e){}return"auto"},Ue=(e,t)=>{Be.forEach(n=>{try{n(e,t)}catch(e){}})},Ze=(e,t)=>{if("undefined"==typeof document||"undefined"==typeof MutationObserver)return()=>{};const{element:n=document.documentElement,attr:r="data-theme",classMap:i=null,callback:o=null}=e,s=()=>{if("function"==typeof o)return o(n);const e=n.getAttribute(r);if(e)return e;if(i)for(const[e,t]of Object.entries(i))if(n.classList.contains(e))return t;return n.getAttribute("data-md-theme")||null};let a=s();a&&t(a);const l=new MutationObserver(()=>{const e=s();e&&e!==a&&(a=e,t(e))});return l.observe(n,{attributes:!0,attributeFilter:[r,"class","data-md-theme"]}),()=>l.disconnect()},Ge=(e,t)=>{if("undefined"==typeof document||!e)return()=>{};const n=()=>{let t=e.parentElement;for(;t;){const e=t.getAttribute("data-md-theme");if(e)return e;if(t.classList.contains("theme-dark")||t.classList.contains("dark"))return"dark";if(t.classList.contains("theme-light")||t.classList.contains("light"))return"light";t=t.parentElement}if(e.parentElement){const t=getComputedStyle(e.parentElement).getPropertyValue("--md-bg").trim()||getComputedStyle(e.parentElement).backgroundColor;if(t){const e=t.match(/\d+/g);if(e&&e.length>=3)return(299*parseInt(e[0])+587*parseInt(e[1])+114*parseInt(e[2]))/1e3<128?"dark":"light"}}return null},r=n();r&&t(r);const i=new MutationObserver(()=>{const e=n();e&&t(e)});let o=e.parentElement;for(;o&&(i.observe(o,{attributes:!0,attributeFilter:["class","data-md-theme","data-theme"]}),o=o.parentElement,o!==document.documentElement););return()=>i.disconnect()},Ye=(e,t)=>{if("function"==typeof e){const n=()=>{try{const n=e();n&&t(n)}catch(e){}};n();const r=setInterval(n,500);return()=>clearInterval(r)}if("string"==typeof e&&"undefined"!=typeof document){const n=document.querySelector(e);return n?Ze({element:n,attr:"data-theme"},t):()=>{}}return()=>{}},Qe=e=>{let t=e.config?.theme||Ae||"auto";const n=[],r=n=>{t=n;const r=Re(n),i=je(n);return e.el&&(["me-theme-light","me-theme-dark","me-theme-warm"].forEach(t=>e.el.classList.remove(t)),"auto"!==r&&e.el.classList.add(`me-theme-${r}`),e.el.setAttribute("data-md-theme",r),Fe(i,e.el)),"function"==typeof e._emit&&e._emit("themeChange",{theme:n,resolved:r,config:i}),"function"==typeof e.refresh&&e.refresh(),t};return r(t),{apply:r,get:()=>t,set:e=>r(e),toggle:()=>{const e=Re(t);return r("dark"===e?"light":"dark")},getResolved:()=>Re(t),getConfig:()=>je(t),getVars:()=>e.el?We(e.el):{},syncWithElement:(e,t={})=>{const i=Ze({element:e,attr:t.attr||"data-theme",classMap:t.classMap,callback:t.callback},e=>r(e));return n.push(i),i},adopt:()=>{const t=e.container?Ge(e.container,e=>r(e)):()=>{};return n.push(t),t},watch:e=>{const t=Ye(e,e=>r(e));return n.push(t),t},dispose:()=>{n.forEach(e=>{try{e()}catch(e){}}),n.length=0}}},Xe={light:{name:"浅色",description:"明亮清晰",config:a.light},dark:{name:"深色",description:"护眼暗色",config:a.dark},auto:{name:"自动",description:"跟随系统",config:"auto"},warm:{name:"暖色",description:"温馨暖色",config:a.warm}};for(const[e,t]of Object.entries(Xe))"auto"!==e&&"auto"!==t.config&&(a[e]=t.config);const Je={getSystemTheme:Ie,resolveTheme:Re,getThemeConfig:je,watchSystemTheme:Pe,unwatchSystemTheme:()=>{He&&(He(),He=null)},applyTheme:Oe,getCurrentTheme:()=>Ae,getResolvedTheme:qe,switchTheme:Ne,toggleTheme:()=>{const e=qe();Ne("dark"===e?"light":"dark")},resetToAuto:()=>{Ne("auto")},initTheme:()=>{Oe(Ve()),Pe()},saveTheme:Ke,loadTheme:Ve,addThemeListener:e=>(Be.add(e),()=>{Be.delete(e)}),removeThemeListener:e=>{Be.delete(e)},clearThemeListeners:()=>{Be.clear()},registerTheme:(e,t={})=>{const n=t.extends||"light",r=a[n]||a.light;a[e]={bg:t.bg||r.bg,text:t.text||r.text,border:t.border||r.border,shadow:t.shadow||r.shadow,hoverShadow:t.hoverShadow||r.hoverShadow,toolbarBg:t.toolbarBg||r.toolbarBg||r.bg,textareaBg:t.textareaBg||r.textareaBg||r.bg,previewBg:t.previewBg||r.previewBg||r.bg,codeBg:t.codeBg||r.codeBg,codeText:t.codeText||r.codeText,accent:t.accent||r.accent,muted:t.muted||r.muted,progressBg:t.progressBg||r.progressBg,closeHoverBg:t.closeHoverBg||r.closeHoverBg}},unregisterTheme:e=>{"light"!==e&&"dark"!==e&&"auto"!==e&&"warm"!==e&&delete a[e]},getAllThemes:()=>({...a}),getThemeNames:()=>Object.keys(a),hasTheme:e=>e in a,setThemeVariables:Fe,exportCSSVars:We,getCSSVariable:De,applyThemeToElement:(e,t)=>{if(!t||"undefined"==typeof document)return;const n=Re(e);t.setAttribute("data-md-theme",n),t.classList.remove("md-theme-light","md-theme-dark","md-theme-auto","md-theme-warm"),"auto"!==n&&t.classList.add(`md-theme-${n}`),Fe(je(e),t)},followExternalTheme:Ze,adoptFromParent:Ge,watch:Ye,createInstanceTheme:Qe};function et(e){const t=this.textarea,n=t.selectionStart,r=t.value.slice(0,n),i=(r&&!r.endsWith("\n")?"\n":"")+e;t.value=t.value.slice(0,n)+i+t.value.slice(t.selectionEnd),t.focus();const o=n+i.length;t.selectionStart=t.selectionEnd=o,this._value=t.value,this._pushHistory(),this._render(),this._updateWordCount(),this._emit("change",this._value)}function tt(){if("undefined"==typeof document)return;if(!document.getElementById("me-float-style")){const e=document.createElement("style");e.id="me-float-style",e.textContent=".me-float-toolbar{position:absolute;z-index:25;display:flex;gap:4px;padding:4px 6px;background:var(--md-toolbar-bg,#f8f9fa);border:1px solid var(--md-border,rgba(0,0,0,0.1));border-radius:8px;box-shadow:0 8px 24px -8px rgba(0,0,0,0.2);opacity:0;transform:translateY(4px);transition:opacity .15s,transform .15s;pointer-events:none}.me-float-toolbar.me-visible{opacity:1;transform:translateY(0);pointer-events:auto}.me-float-toolbar .me-btn{width:28px;height:28px}",document.head.appendChild(e)}if(!this._floatingEnabled)return;const e=this.textarea,t=()=>{if(this._destroyed||this.config.readOnly||!this._floatingEnabled)return;const t=e.selectionStart,n=e.selectionEnd;if(t===n)return void this._hideFloatingToolbar();this._floatingToolbar&&this._floatingToolbar.parentNode||this._buildFloatingToolbar();const r=e.getBoundingClientRect(),i=this.editorPane.getBoundingClientRect(),o=getComputedStyle(e),s=parseFloat(o.lineHeight)||22,a=parseFloat(o.paddingTop)||16,l=parseFloat(o.paddingLeft)||18,c=.6*(parseFloat(o.fontSize)||13.5),h=e.value.substring(0,t),d=h.split("\n").length-1,u=h.length-h.lastIndexOf("\n")-1+Math.floor((n-t)/2*("backward"===e.selectionDirection?-1:1)),p=r.top+a+d*s-e.scrollTop-i.top;let m=p-36-4;m<4&&(m=p+s+4);let g=r.left+l+u*c-e.scrollLeft-i.left-80;g=Math.max(4,Math.min(g,i.width-200)),this._floatingToolbar.style.top=m+"px",this._floatingToolbar.style.left=g+"px",this._floatingToolbar.classList.add("me-visible")},n=()=>{this._hideFloatingToolbar()};e.addEventListener("mouseup",()=>setTimeout(t,0)),e.addEventListener("keyup",()=>{e.selectionStart!==e.selectionEnd?setTimeout(t,0):setTimeout(n,0)}),e.addEventListener("blur",()=>setTimeout(n,300)),e.addEventListener("click",()=>{e.selectionStart===e.selectionEnd&&n()})}function nt(){return this._floatingEnabled=!this._floatingEnabled,this._floatingEnabled||this._floatingToolbar&&(this._floatingToolbar.parentNode&&this._floatingToolbar.parentNode.removeChild(this._floatingToolbar),this._floatingToolbar=null),this}function rt(){return this._floatingEnabled}function it(){const e=document.createElement("div");e.className="me-float-toolbar",["bold","italic","code","link","strikethrough"].forEach(t=>{const n=this._createBtn(t);n.addEventListener("mousedown",e=>{e.preventDefault(),e.stopPropagation(),this.exec(t),setTimeout(()=>this.textarea.focus(),0)}),e.appendChild(n)}),this.editorPane.appendChild(e),this._floatingToolbar=e}function ot(){this._floatingToolbar&&this._floatingToolbar.classList.remove("me-visible")}function st(e=[]){return this._contextMenuItems=e,this}function at(){if(!this.el)return;const e=e=>{const t=document.querySelector(".me-context-menu");t&&t.remove(),this._showContextMenu(e)};this.el.addEventListener("contextmenu",e),this._cleanups.push(()=>this.el.removeEventListener("contextmenu",e))}function lt(e){e.preventDefault();const t=this.textarea,n=t&&t.selectionStart!==t.selectionEnd,r=[{label:this.t("undo")||"Undo",action:"undo",shortcut:"Ctrl+Z",disabled:!this.canUndo()},{label:this.t("redo")||"Redo",action:"redo",shortcut:"Ctrl+Y",disabled:!this.canRedo()},{sep:!0},{label:this.t("cut")||"Cut",action:"cut",shortcut:"Ctrl+X",disabled:!n},{label:this.t("copy")||"Copy",action:"copy",shortcut:"Ctrl+C",disabled:!n},{label:this.t("paste")||"Paste",action:"paste",shortcut:"Ctrl+V",disabled:!!this.config.readOnly},{label:this.t("selectAll")||"Select All",action:"selectAll",shortcut:"Ctrl+A"}];this._contextMenuItems.length&&(r.push({sep:!0}),this._contextMenuItems.forEach(e=>r.push(e)));const i=document.createElement("div");i.className="me-context-menu",i.style.left=e.clientX+"px",i.style.top=e.clientY+"px",requestAnimationFrame(()=>{const t=i.getBoundingClientRect();t.right>window.innerWidth&&(i.style.left=e.clientX-t.width+"px"),t.bottom>window.innerHeight&&(i.style.top=e.clientY-t.height+"px")}),r.forEach(e=>{if(e.sep){const e=document.createElement("div");return e.className="me-context-menu-sep",void i.appendChild(e)}const t=document.createElement("div");t.className="me-context-menu-item",e.disabled&&t.classList.add("me-disabled"),t.innerHTML=`${e.label}${e.shortcut?``:""}`,t.addEventListener("click",t=>{t.stopPropagation(),e.disabled||(e.onClick?e.onClick():e.action&&this._execContextAction(e.action),this._hideContextMenu())}),i.appendChild(t)}),document.body.appendChild(i),document.addEventListener("click",e=>{i.contains(e.target)||this._hideContextMenu()},{once:!0}),document.addEventListener("keydown",e=>{"Escape"===e.key&&this._hideContextMenu()},{once:!0})}function ct(){const e=document.querySelector(".me-context-menu");e&&e.remove()}function ht(e){const t=this.textarea;if(t)switch(e){case"undo":this.undo();break;case"redo":this.redo();break;case"cut":document.execCommand("cut");break;case"copy":document.execCommand("copy");break;case"paste":document.execCommand("paste");break;case"selectAll":t.focus(),t.select();break;default:this.exec(e)}}function dt(){if(!this.config.outline||!this.previewEl)return;const e=this.el.querySelector(".me-outline");e&&e.remove();const n=[],r=/]*>(.+?)<\/h\1>/gi;let i;for(;null!==(i=r.exec(this.previewEl.innerHTML));)n.push({level:parseInt(i[1],10),id:i[2],text:i[3].replace(/<[^>]+>/g,"")});if(!n.length)return;const o=document.createElement("div");o.className="me-outline",o.innerHTML=`${this.t("outline")||"大纲"}
`,o.innerHTML+=(e=>{let n="";const r=[],i=e=>{for(;r.length&&r[r.length-1]>=e;)n+="",r.pop()};for(const o of e)o.level<1||(i(o.level),n+="