diff --git a/main.js b/main.js index 375fe6c..f81ded2 100644 --- a/main.js +++ b/main.js @@ -36,7 +36,7 @@ end`},Nc=function(){for(var t=[],e=0;e1||o.length===1&&n)&&this.enableMultiselect();for(var s=new Array(o.length),f=0,l=o.length;f1||o.length===1&&n)&&this.enableMultiselect();for(var a=new Array(o.length),s=0,f=o.length;sn)throw new Eu(r.length,n,this.getName());this.markAsDirty(),this.disableRichFormatting(),r?this.acroField.setValue(H.fromText(r)):this.acroField.removeValue()},e.prototype.getAlignment=function(){var r=this.acroField.getQuadding();return r===0?De.Left:r===1?De.Center:r===2?De.Right:De.Left},e.prototype.setAlignment=function(r){yt(r,"alignment",De),this.markAsDirty(),this.acroField.setQuadding(r)},e.prototype.getMaxLength=function(){return this.acroField.getMaxLength()},e.prototype.setMaxLength=function(r){if(ot(r,"maxLength",0,Number.MAX_SAFE_INTEGER),this.markAsDirty(),r===void 0)this.acroField.removeMaxLength();else{var n=this.getText();if(n&&n.length>r)throw new Au(n.length,r,this.getName());this.acroField.setMaxLength(r)}},e.prototype.removeMaxLength=function(){this.markAsDirty(),this.acroField.removeMaxLength()},e.prototype.setImage=function(r){for(var n=this.getAlignment(),o=n===De.Center?Ft.Center:n===De.Right?Ft.Right:Ft.Left,i=this.acroField.getWidgets(),a=0,s=i.length;at/25.4*96;function Gv(t){return t.pageSize==="Custom"?{w:Math.max(1,Math.round(kn(t.customPageWidth))),h:Math.max(1,Math.round(kn(t.customPageHeight)))}:Vi[t.pageSize]??Vi.A4}function Ui(t){return t.replace(/&/g,"&").replace(//g,">").replace(/"/g,""").replace(/'/g,"'")}function Gu(t){return t.replace(/<\/style/gi,"<\\/style")}function Cn(t,e){let r=e.trim();if(!r)return"";if(/^(https?:|data:)/i.test(r))return r;let n=t.vault.getAbstractFileByPath(r);if(n instanceof N.TFile)return t.vault.getResourcePath(n);let o=t.metadataCache.getFirstLinkpathDest(r,"");return o instanceof N.TFile?t.vault.getResourcePath(o):(console.warn("[advanced-pdf-export] Could not resolve image path:",r),r)}function Ku(t){return{size:t==="fill"?"100% 100%":t==="tile"?"auto":t,repeat:t==="tile"?"repeat":"no-repeat"}}var Kv=/[\u0590-\u08FF\uFB1D-\uFDFD\uFE70-\uFEFC]/g,Xv=/[A-Za-z\u0590-\u08FF\uFB1D-\uFDFD\uFE70-\uFEFC]/g;function Zv(t){let e=(t.match(Kv)??[]).length,r=(t.match(Xv)??[]).length;return r>0&&e/r>.1}function Yv(t){return t.replace(/\r\n|\r/g,` -`)}function Jv(t){return t.replace(/^---[ \t]*\n[\s\S]*?\n---[ \t]*(\n|$)/,"")}function Qv(t){return t.split(/^\/\/\/\s*$/m).map(e=>e.trim()).filter(Boolean)}async function _v(t,e,r,n){let o=activeDocument.createElement("div");o.setCssStyles({position:"fixed",top:"0",left:"-99999px",visibility:"hidden",pointerEvents:"none"}),activeDocument.body.appendChild(o);try{await N.MarkdownRenderer.render(t,e,o,r,n);try{await(0,N.finishRenderMath)()}catch{}await nm(o),await om(o);let i=activeDocument.fonts;if(i)try{await i.ready}catch{}}finally{activeDocument.body.removeChild(o)}return rm(o),o}var $v=/[^\p{L}\p{N}\s-]/gu,em=/\s+/g,tm=/-+/g;function Xu(t){return t.toLowerCase().replace($v,"").trim().replace(em,"-").replace(tm,"-")}function rm(t){let e=new Map;t.querySelectorAll("h1,h2,h3,h4,h5,h6").forEach(r=>{let n=r.textContent||"",o=Xu(n);if(!o)return;let i=e.get(o)??0;e.set(o,i+1),r.id=i===0?o:`${o}-${i}`}),t.querySelectorAll("a").forEach(r=>{r.classList.remove("external-link");let n=r.getAttribute("data-href")??r.getAttribute("href");n?.startsWith("#")&&r.setAttribute("href","#"+Xu(n.slice(1)))}),t.querySelectorAll(".copy-code-button").forEach(r=>r.remove()),t.querySelectorAll(".callout").forEach(r=>{r.removeAttribute("data-callout-fold"),r.classList.remove("is-collapsed"),r.querySelectorAll(".callout-fold").forEach(n=>n.remove())}),t.querySelectorAll("style, script").forEach(r=>{r.closest("svg")||r.remove()})}async function nm(t){let e=Array.from(t.querySelectorAll(".mermaid"));if(e.length===0)return;let r=5e3;await Promise.all(e.map(n=>new Promise(o=>{if(n.querySelector("svg")){o();return}let i=window.setTimeout(()=>{a.disconnect(),o()},r),a=new MutationObserver(()=>{n.querySelector("svg")&&(window.clearTimeout(i),a.disconnect(),o())});a.observe(n,{childList:!0,subtree:!0})})))}async function om(t){let e=Array.from(t.querySelectorAll(".math"));if(e.length===0)return;let r=window.MathJax;if(r?.startup?.promise)try{await r.startup.promise}catch{}let n=8e3;await Promise.all(e.filter(o=>!o.querySelector("mjx-container")).map(o=>new Promise(i=>{let a=window.setTimeout(()=>{s.disconnect(),i()},n),s=new MutationObserver(()=>{o.querySelector("mjx-container")&&(window.clearTimeout(a),s.disconnect(),i())});s.observe(o,{childList:!0,subtree:!0})}))),await Ps()}async function Ps(){let n=Date.now(),o=qi().length,i=0;for(;i<2&&Date.now()-n<2500;){await new Promise(s=>window.setTimeout(s,60));let a=qi().length;a===o?i++:(i=0,o=a)}}var im={comment:["comment","prolog","doctype","cdata"],punctuation:["punctuation"],property:["property","tag","boolean","number","constant","symbol","deleted","attr-name"],selector:["selector","string","char","builtin","inserted","attr-value"],operator:["operator","entity","url"],keyword:["atrule","keyword","important"],function:["function","class-name"],regex:["regex","variable"]},Ds={none:{name:"None (plain)"},"github-light":{name:"GitHub Light",background:"#f6f8fa",text:"#24292e",tokens:{comment:"#6a737d",punctuation:"#24292e",property:"#005cc5",selector:"#032f62",operator:"#d73a49",keyword:"#d73a49",function:"#6f42c1",regex:"#032f62"}},"github-dark":{name:"GitHub Dark",background:"#0d1117",text:"#c9d1d9",tokens:{comment:"#8b949e",punctuation:"#c9d1d9",property:"#79c0ff",selector:"#a5d6ff",operator:"#ff7b72",keyword:"#ff7b72",function:"#d2a8ff",regex:"#a5d6ff"}},"atom-one-light":{name:"Atom One Light",background:"#fafafa",text:"#383a42",tokens:{comment:"#a0a1a7",punctuation:"#383a42",property:"#986801",selector:"#50a14f",operator:"#0184bc",keyword:"#a626a4",function:"#4078f2",regex:"#50a14f"}},"atom-one-dark":{name:"Atom One Dark",background:"#282c34",text:"#abb2bf",tokens:{comment:"#5c6370",punctuation:"#abb2bf",property:"#d19a66",selector:"#98c379",operator:"#56b6c2",keyword:"#c678dd",function:"#61afef",regex:"#98c379"}},monokai:{name:"Monokai",background:"#272822",text:"#f8f8f2",tokens:{comment:"#75715e",punctuation:"#f8f8f2",property:"#ae81ff",selector:"#e6db74",operator:"#f92672",keyword:"#f92672",function:"#a6e22e",regex:"#e6db74"}},dracula:{name:"Dracula",background:"#282a36",text:"#f8f8f2",tokens:{comment:"#6272a4",punctuation:"#f8f8f2",property:"#bd93f9",selector:"#f1fa8c",operator:"#ff79c6",keyword:"#ff79c6",function:"#50fa7b",regex:"#ff79c6"}},"tokyo-night":{name:"Tokyo Night",background:"#1a1b26",text:"#a9b1d6",tokens:{comment:"#565f89",punctuation:"#89ddff",property:"#ff9e64",selector:"#9ece6a",operator:"#89ddff",keyword:"#bb9af7",function:"#7aa2f7",regex:"#b4f9f8"}},"solarized-light":{name:"Solarized Light",background:"#fdf6e3",text:"#586e75",tokens:{comment:"#93a1a1",punctuation:"#586e75",property:"#cb4b16",selector:"#2aa198",operator:"#859900",keyword:"#859900",function:"#268bd2",regex:"#2aa198"}},"catppuccin-macchiato":{name:"Catppuccin Macchiato",background:"#24273a",text:"#cad3f5",tokens:{comment:"#939ab7",punctuation:"#b8c0e0",property:"#f5a97f",selector:"#a6da95",operator:"#91d7e3",keyword:"#c6a0f6",function:"#8aadf4",regex:"#f5bde6"}},"catppuccin-mocha":{name:"Catppuccin Mocha",background:"#1e1e2e",text:"#cdd6f4",tokens:{comment:"#9399b2",punctuation:"#bac2de",property:"#fab387",selector:"#a6e3a1",operator:"#89dceb",keyword:"#cba6f7",function:"#89b4fa",regex:"#f5c2e7"}}};function am(t){let e=Ds[t.codeTheme]??Ds.none,r=e.background??t.codeBackground,n=e.text??t.bodyColor,o=Object.entries(e.tokens??{}).map(([i,a])=>`${im[i].map(l=>`.mpdf-doc pre code .token.${l}`).join(", ")} { color: ${a};${i==="comment"?" font-style: italic;":""} }`).join(` +`)}function Jv(t){return t.replace(/^---[ \t]*\n[\s\S]*?\n---[ \t]*(\n|$)/,"")}function Qv(t){return t.split(/^\/\/\/\s*$/m).map(e=>e.trim()).filter(Boolean)}async function _v(t,e,r,n){let o=activeDocument.createElement("div");o.setCssStyles({position:"fixed",top:"0",left:"-99999px",visibility:"hidden",pointerEvents:"none"}),activeDocument.body.appendChild(o);try{await N.MarkdownRenderer.render(t,e,o,r,n);try{await(0,N.finishRenderMath)()}catch(a){console.warn("[advanced-pdf-export] finishRenderMath failed:",a)}await nm(o),await om(o);let i=activeDocument.fonts;if(i)try{await i.ready}catch{}}finally{activeDocument.body.removeChild(o)}return rm(o),o}var $v=/[^\p{L}\p{N}\s-]/gu,em=/\s+/g,tm=/-+/g;function Xu(t){return t.toLowerCase().replace($v,"").trim().replace(em,"-").replace(tm,"-")}function rm(t){let e=new Map;t.querySelectorAll("h1,h2,h3,h4,h5,h6").forEach(r=>{let n=r.textContent||"",o=Xu(n);if(!o)return;let i=e.get(o)??0;e.set(o,i+1),r.id=i===0?o:`${o}-${i}`}),t.querySelectorAll("a").forEach(r=>{r.classList.remove("external-link");let n=r.getAttribute("data-href")??r.getAttribute("href");n?.startsWith("#")&&r.setAttribute("href","#"+Xu(n.slice(1)))}),t.querySelectorAll(".copy-code-button").forEach(r=>r.remove()),t.querySelectorAll(".callout").forEach(r=>{r.removeAttribute("data-callout-fold"),r.classList.remove("is-collapsed"),r.querySelectorAll(".callout-fold").forEach(n=>n.remove())}),t.querySelectorAll("style, script").forEach(r=>{r.closest("svg")||r.remove()})}async function nm(t){let e=Array.from(t.querySelectorAll(".mermaid"));if(e.length===0)return;let r=5e3;await Promise.all(e.map(n=>new Promise(o=>{if(n.querySelector("svg")){o();return}let i=window.setTimeout(()=>{a.disconnect(),o()},r),a=new MutationObserver(()=>{n.querySelector("svg")&&(window.clearTimeout(i),a.disconnect(),o())});a.observe(n,{childList:!0,subtree:!0})})))}async function om(t){let e=Array.from(t.querySelectorAll(".math"));if(e.length===0)return;let r=window.MathJax;if(r?.startup?.promise)try{await r.startup.promise}catch{}let n=8e3;await Promise.all(e.filter(o=>!o.querySelector("mjx-container")).map(o=>new Promise(i=>{let a=window.setTimeout(()=>{s.disconnect(),i()},n),s=new MutationObserver(()=>{o.querySelector("mjx-container")&&(window.clearTimeout(a),s.disconnect(),i())});s.observe(o,{childList:!0,subtree:!0})}))),await Ps()}async function Ps(){let n=Date.now(),o=qi().length,i=0;for(;i<2&&Date.now()-n<2500;){await new Promise(s=>window.setTimeout(s,60));let a=qi().length;a===o?i++:(i=0,o=a)}}var im={comment:["comment","prolog","doctype","cdata"],punctuation:["punctuation"],property:["property","tag","boolean","number","constant","symbol","deleted","attr-name"],selector:["selector","string","char","builtin","inserted","attr-value"],operator:["operator","entity","url"],keyword:["atrule","keyword","important"],function:["function","class-name"],regex:["regex","variable"]},Ds={none:{name:"None (plain)"},"github-light":{name:"GitHub Light",background:"#f6f8fa",text:"#24292e",tokens:{comment:"#6a737d",punctuation:"#24292e",property:"#005cc5",selector:"#032f62",operator:"#d73a49",keyword:"#d73a49",function:"#6f42c1",regex:"#032f62"}},"github-dark":{name:"GitHub Dark",background:"#0d1117",text:"#c9d1d9",tokens:{comment:"#8b949e",punctuation:"#c9d1d9",property:"#79c0ff",selector:"#a5d6ff",operator:"#ff7b72",keyword:"#ff7b72",function:"#d2a8ff",regex:"#a5d6ff"}},"atom-one-light":{name:"Atom One Light",background:"#fafafa",text:"#383a42",tokens:{comment:"#a0a1a7",punctuation:"#383a42",property:"#986801",selector:"#50a14f",operator:"#0184bc",keyword:"#a626a4",function:"#4078f2",regex:"#50a14f"}},"atom-one-dark":{name:"Atom One Dark",background:"#282c34",text:"#abb2bf",tokens:{comment:"#5c6370",punctuation:"#abb2bf",property:"#d19a66",selector:"#98c379",operator:"#56b6c2",keyword:"#c678dd",function:"#61afef",regex:"#98c379"}},monokai:{name:"Monokai",background:"#272822",text:"#f8f8f2",tokens:{comment:"#75715e",punctuation:"#f8f8f2",property:"#ae81ff",selector:"#e6db74",operator:"#f92672",keyword:"#f92672",function:"#a6e22e",regex:"#e6db74"}},dracula:{name:"Dracula",background:"#282a36",text:"#f8f8f2",tokens:{comment:"#6272a4",punctuation:"#f8f8f2",property:"#bd93f9",selector:"#f1fa8c",operator:"#ff79c6",keyword:"#ff79c6",function:"#50fa7b",regex:"#ff79c6"}},"tokyo-night":{name:"Tokyo Night",background:"#1a1b26",text:"#a9b1d6",tokens:{comment:"#565f89",punctuation:"#89ddff",property:"#ff9e64",selector:"#9ece6a",operator:"#89ddff",keyword:"#bb9af7",function:"#7aa2f7",regex:"#b4f9f8"}},"solarized-light":{name:"Solarized Light",background:"#fdf6e3",text:"#586e75",tokens:{comment:"#93a1a1",punctuation:"#586e75",property:"#cb4b16",selector:"#2aa198",operator:"#859900",keyword:"#859900",function:"#268bd2",regex:"#2aa198"}},"catppuccin-macchiato":{name:"Catppuccin Macchiato",background:"#24273a",text:"#cad3f5",tokens:{comment:"#939ab7",punctuation:"#b8c0e0",property:"#f5a97f",selector:"#a6da95",operator:"#91d7e3",keyword:"#c6a0f6",function:"#8aadf4",regex:"#f5bde6"}},"catppuccin-mocha":{name:"Catppuccin Mocha",background:"#1e1e2e",text:"#cdd6f4",tokens:{comment:"#9399b2",punctuation:"#bac2de",property:"#fab387",selector:"#a6e3a1",operator:"#89dceb",keyword:"#cba6f7",function:"#89b4fa",regex:"#f5c2e7"}}};function am(t){let e=Ds[t.codeTheme]??Ds.none,r=e.background??t.codeBackground,n=e.text??t.bodyColor,o=Object.entries(e.tokens??{}).map(([i,a])=>`${im[i].map(l=>`.mpdf-doc pre code .token.${l}`).join(", ")} { color: ${a};${i==="comment"?" font-style: italic;":""} }`).join(` `);return` .mpdf-doc pre { background: ${r}; @@ -223,7 +223,7 @@ end`},Nc=function(){for(var t=[],e=0;e{try{return Array.from(o.cssRules).map(i=>i.cssText).join(` `)}catch{return""}}).filter(Boolean).join(` -`)}catch{}return r?`${e} +`)}catch(n){console.warn("[advanced-pdf-export] adoptedStyleSheets read failed:",n)}return r?`${e} ${r}`:e}function cm(t){return t.replace(/@font-face\s*\{[^{}]*(?:\{[^{}]*\}[^{}]*)?\}/g,"")}async function um(){let t=qi();if(!t)return"";let e=new Set;for(let[,i]of t.matchAll(Zu)){let a=i.trim();a.startsWith("data:")||e.add(a)}if(!e.size)return t;let r=async i=>{try{let a=await(0,N.requestUrl)(i);if(a.status!==200)return i;let s=new Uint8Array(a.arrayBuffer),f="";for(let d=0;d[i,await r(i)])),o=new Map(n);return t.replace(Zu,(i,a)=>{let s=o.get(a.trim());return s&&s!==a.trim()?`url('${s}')`:i})}var Zu=/url\(\s*["']?([^"')]+)["']?\s*\)/g,dm=new Set(["P","LI","BLOCKQUOTE","TD","TH"]),hm=new Set(["P","DIV","SECTION","ARTICLE","ASIDE","NAV","HEADER","FOOTER","UL","OL","LI","TABLE","THEAD","TBODY","TFOOT","TR","TD","TH","PRE","BLOCKQUOTE","HR","IMG","H1","H2","H3","H4","H5","H6"]),pm=new Set(["CODE","IMG","HR","H1","H2","H3","H4","H5","H6"]),vm=2;function mm(t,e){e.empty();for(let r of t)e.appendChild(r.cloneNode(!0));return e.getBoundingClientRect().height}function gm(t,e,r){return n=>mm([...t,n],e)<=r-vm}function ym(t){let e=activeDocument.createTreeWalker(t,NodeFilter.SHOW_TEXT);for(;e.nextNode();){let r=e.currentNode,n=(r.textContent??"").replace(/^\s+/,"");if(n!==r.textContent&&(r.textContent=n),n.length>0)break}}function Uo(t,e,r=!0){let n=activeDocument.createTreeWalker(t,NodeFilter.SHOW_TEXT),o=0,i=null,a=0;for(;n.nextNode();){let u=n.currentNode,d=u.textContent?.length??0;if(o+d>=e){i=u,a=e-o;break}o+=d}if(!i)return null;let s=activeDocument.createRange();s.selectNodeContents(t),s.setEnd(i,a);let f=activeDocument.createRange();f.selectNodeContents(t),f.setStart(i,a);let l=t.cloneNode(!1);l.appendChild(s.cloneContents());let c=t.cloneNode(!1);return c.appendChild(f.cloneContents()),r&&ym(c),[l,c]}function bm(t){let e=[],r=/\s+/g,n;for(;n=r.exec(t);)n.index>0&&n.index0){let f=0,l=o.length-1,c=-1;for(;f<=l;){let u=Math.floor((f+l)/2),d=Uo(t,o[u]);if(!d){l=u-1;continue}e(d[0])?(c=u,f=u+1):l=u-1}if(c>=0)return Uo(t,o[c]);if(!r)return null}let i=1,a=n.length-1,s=0;for(;i<=a;){let f=Math.floor((i+a)/2),l=Uo(t,f);if(!l){a=f-1;continue}e(l[0])?(s=f,i=f+1):a=f-1}return s>0?Uo(t,s):null}function ws(t,e,r){let n=t.cloneNode(!1);t.tagName==="OL"&&r!==void 0&&r>1&&(n.start=r);for(let o of e)n.appendChild(o.cloneNode(!0));return n}function Fm(t,e,r){let n=Array.from(t.children).filter(a=>a.tagName==="LI");if(n.length===0)return null;let o=t.tagName==="OL"?t.start??1:1,i=0;for(let a=0;a=n.length?null:[ws(t,n.slice(0,i),o),ws(t,n.slice(i),o+i)]}function Ss(t,e){let r=t.cloneNode(!1),n=t.querySelector("caption");n&&r.appendChild(n.cloneNode(!0));let o=t.querySelector("colgroup");o&&r.appendChild(o.cloneNode(!0)),t.tHead&&r.appendChild(t.tHead.cloneNode(!0));let i=activeDocument.createElement("tbody");for(let a of e)i.appendChild(a.cloneNode(!0));return r.appendChild(i),r}function wm(t,e,r){let n=t.tBodies[0],o=n?Array.from(n.rows):Array.from(t.rows).filter(a=>a.parentElement?.tagName!=="THEAD");if(o.length===0)return null;let i=0;for(let a=0;a=o.length?null:[Ss(t,o.slice(0,i)),Ss(t,o.slice(i))]}function Sm(t){let e=[],r=0;for(let n of t)r+=n.length+1,e.push(r);return e}function Pm(t,e,r){let n=t.querySelector("code"),o=(n??t).textContent?.split(` `)??[];if(o.length>1&&o[o.length-1]===""&&o.pop(),o.length<2)return null;let i=Sm(o),a=l=>{if(!n){let p=h=>{let b=t.cloneNode(!1);return b.textContent=h,b};return[p(o.slice(0,l).join(` `)),p(o.slice(l).join(`