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.84 kB
import{a as T,b as u}from"./chunk-G7ADNCNO.mjs";import{a as R,b}from"./chunk-T7HUA6AU.mjs";import{createPersistentCache as S,simpleCleanup as G}from"@m2d/core/cache";var E=["jpeg","jpg","bmp","gif","png"],D="img",M=async(n,e)=>{var c;let t=e.scale,m=n.split(";")[0].split("/")[1],a=new Image;a.src=n,await new Promise(g=>a.onload=g);let s=a.width*t,r=a.height*t;if(E.includes(m))return{data:n,type:m,transformation:{width:s/t,height:r/t}};let i=document.createElement("canvas"),o=i.getContext("2d");if(!o)throw new Error("Canvas context not available");i.width=s,i.height=r,o.drawImage(a,0,0,s,r);let l=(c=e.fallbackImageType)!=null?c:"png";return{data:i.toDataURL(`image/${l}`),type:l,transformation:{width:s/t,height:r/t}}},k=async(n,e)=>{var r;let t=await fetch(n);if(/(svg|xml)/.test((r=t.headers.get("content-type"))!=null?r:"")||n.endsWith(".svg")){let i=await t.text();return u({type:"svg",value:i},e)}let m=await t.arrayBuffer(),a=R(m)||"png",s=await createImageBitmap(new Blob([m],{type:a}));return E.includes(a)?{type:a,data:m,transformation:{width:s.width,height:s.height}}:(console.warn(`${a} not supported by docx. Using fallback.`),M(n,e))},A=async(n,e,t)=>{var m;try{let a=await((t==null?void 0:t.type)==="svg"?u(t,e):n.startsWith("data:")?M(n,e):k(n,e)),{width:s,height:r}=a.transformation,{width:i,height:o}=(m=t==null?void 0:t.data)!=null?m:{};i&&!o?o=r*i/s:!i&&o?i=s*o/r:!i&&!o&&(i=s,o=r);let l=Math.min(e.maxW*e.dpi/i,e.maxH*e.dpi/o,1);return a.transformation={width:i*l,height:o*l},a}catch(a){return console.error(`Error resolving image: ${n}`,a),b(e)}},y={scale:3,fallbackImageType:"png",imageResolver:A,maxW:6.3,maxH:9.7,dpi:96,cacheConfig:{cacheMode:"both",ignoreKeys:["dpi","cacheConfig","type","alt"],parallel:!0},maxAgeMinutes:7*24*60,fixGeneratedSvg:T},H=n=>{var s,r,i,o,l;let e={...y,...n},t={cache:(s=e.cache)!=null?s:void 0,...y.cacheConfig,...e.cacheConfig,ignoreKeys:[...(i=(r=y.cacheConfig)==null?void 0:r.ignoreKeys)!=null?i:[],...(l=(o=e.cacheConfig)==null?void 0:o.ignoreKeys)!=null?l:[]]};e.imageResolver=S(e.imageResolver,D,t);let m=async(c,g)=>{let h=[],d=p=>{var P;(P=p.children)==null||P.forEach(d),/^(image|svg)/.test(p.type)&&h.push((async()=>{var v,w,C,x,O;let f=(w=p.url)!=null?w:g[(v=p.identifier)==null?void 0:v.toUpperCase()],I=(O=(x=p.alt)!=null?x:(C=f==null?void 0:f.split("/"))==null?void 0:C.pop())!=null?O:"";p.data={...await e.imageResolver(f,e,p),altText:{description:I,name:I,title:I},...p.data}})())};d(c),await Promise.all(h)},a=!1;return{preprocess:m,inline:(c,g,h)=>/^(image|svg)/.test(g.type)?(g._type=g.type,g.type="",[new c.ImageRun({...g.data,...h})]):[],postprocess:()=>{var c,g;(!((c=e==null?void 0:e.cacheConfig)!=null&&c.cacheMode)||((g=e==null?void 0:e.cacheConfig)==null?void 0:g.cacheMode)!=="memory")&&!a&&(a=!0,G(e.maxAgeMinutes,D))}}};export{H as imagePlugin};