UNPKG

@mdast2docx/image

Version:

MDAST to DOCX plugin for resolving and embedding images. Supports base64, URLs, and custom resolvers for seamless DOCX image integration.

2 lines (1 loc) 2.27 kB
import{b as x}from"./chunk-T7HUA6AU.mjs";var y=n=>new Promise((t,i)=>{let a=new Blob([n],{type:"image/svg+xml"}),r=new FileReader;r.onloadend=()=>t(r.result),r.onerror=i,r.readAsDataURL(a)}),S=(n,t)=>new Promise((i,a)=>{let r=document.createElement("div");r.innerHTML=n,r.style="width:100%;height:100%;position:absolute;",t.appendChild(r);let e=r.querySelector("svg");if(!e||e.nodeType!==1)return a(new Error("No or invalid <svg> found"));requestAnimationFrame(()=>{try{let o=e.getBBox(),c=parseFloat(getComputedStyle(e).width)||0,p=parseFloat(getComputedStyle(e).height)||0,s=4,g=o.x-s,m=o.y-s,l=o.width+s*2,d=o.height+s*2,v=c>0?Math.min(l,c):l,w=p>0?Math.min(d,p):d,u=e.cloneNode(!0);u.setAttribute("viewBox",`${g} ${m} ${l} ${d}`),u.setAttribute("width",`${v}`),u.setAttribute("height",`${w}`),u.removeAttribute("style");let f=new XMLSerializer().serializeToString(u);r.remove(),i({svg:f,scale:Math.min(l/c,d/p,1)})}catch(o){r.remove(),a(o)}})}),h,T=n=>(h||(h=document.createElement("div"),h.style=`height:${n.maxH}in;width:${n.maxW}in;position:absolute;left:-2500vw;`,document.body.appendChild(h),n.dpi=parseFloat(getComputedStyle(h).width)/n.maxW),h),I=(n,t)=>t.diagramType==="pie"?n.replace(".pieTitleText{text-anchor:middle;",".pieTitleText{").replace(/<text[^>]*class="pieTitleText"[^>]*>(.*?)<\/text>/,(i,a)=>i.replace(a,a.replace(/^"|"$/g,"")).replace(/ x=".*?"/,' x="-20%"')):n,C=async(n,t)=>{let i=n.value,a,r=!1;if(typeof i=="string")a=i;else{let e=await i;if(!e)return x(t);a=e.svg,r=e.diagramType==="gantt",a=t.fixGeneratedSvg(a,e)}try{let e=new Image,o=T(t);o.appendChild(e);let c=r||!a?{svg:a,scale:1}:await S(a,o),p=await y(c.svg);e.src=p,await new Promise(w=>e.onload=w),r&&(t.scale=Math.max(t.scale,Math.floor(Math.min(innerWidth/e.width,innerHeight/e.height))));let s=e.width*t.scale,g=e.height*t.scale,m=document.createElement("canvas"),l=m.getContext("2d");if(!l)throw new Error("Canvas context not available");m.width=s,m.height=g,l.drawImage(e,0,0,s,g);let d=m.toDataURL(`image/${t.fallbackImageType}`);e.remove();let v=Math.min(t.maxW*t.dpi/s,t.maxH*t.dpi/g,1)*c.scale;return{type:t.fallbackImageType,data:d,transformation:{width:s*v,height:g*v}}}catch(e){return console.error("Error resolving SVG image: ",e),x(t)}};export{I as a,C as b};