@mdast2docx/core
Version:
Core engine to convert extended MDAST to DOCX. Supports plugins for footnotes, images, lists, tables, and more. Designed for seamless Markdown-to-DOCX conversion.
4 lines (3 loc) • 3.42 kB
JavaScript
import{b as D,c as N}from"./chunk-QDLXEYML.mjs";import{Bookmark as E,BorderStyle as b,CheckBox as v,ExternalHyperlink as T,FootnoteReferenceRun as B,InternalHyperlink as L,Paragraph as P,TextRun as p}from"docx";import*as R from"docx";var F=(u,d,x)=>{let c=(h,y={})=>{var I,l;return((l=(I=h.children)==null?void 0:I.map(e=>{var m,r,t;let o=x.map(s=>{var k,w;return(w=(k=s.inline)==null?void 0:k.call(s,R,e,y,u,d,c))!=null?w:[]}).flat(),n=Object.assign({},y,e.data),f=(r=e.url)!=null?r:u[(m=e.identifier)==null?void 0:m.toUpperCase()];switch(e.type){case"text":return[...o,...n.pre?e.value.split(`
`).map(s=>new p({text:s,...n,break:1})):[new p({text:e.value,...n})]];case"checkbox":return[...o,new v({checked:!!e.checked})];case"break":return[...o,new p({break:1})];case"inlineCode":return[...o,new p({text:e.value,...n,style:"code",font:{name:"Consolas"}})];case"emphasis":return n.italics=!0,[...o,...c(e,n)];case"strong":return n.bold=!0,[...o,...c(e,n)];case"delete":return n.strike=!0,[...o,...c(e,n)];case"link":case"linkReference":return n.underline={},[...o,f.startsWith("#")?new L({anchor:f.slice(1),children:c(e,n)}):new T({link:f,children:c(e,n)})];case"footnoteReference":return[...o,new B((t=d[e.identifier].id)!=null?t:0)];case"fragment":return[...o,...c(e,n)];case"":return[...o];default:return console.warn(`Unsupported inline node type: ${e.type}`),[...o]}}))!=null?l:[]).flat()};return c},H=async(u,d,x,c)=>{var o;let{plugins:h,useTitle:y,...I}={...N,...c};for(let n of h)await((o=n.preprocess)==null?void 0:o.call(n,u,d));let l=F(d,x,h),e=(n,f)=>{var m;return(m=n.children)==null?void 0:m.map(r=>{var k,w,S,C;let t={...f},s=h.map(a=>{var i,g;return(g=(i=a.block)==null?void 0:i.call(a,R,r,t,e,l))!=null?g:[]}).flat();switch(Object.assign(t,r.data),r.type){case"paragraph":{let a=t.pre?{alignment:"left"}:{},i=typeof t.checked=="boolean"?[new v({checked:t.checked,checkedState:{value:"2611"},uncheckedState:{value:"2610"}}),new p(" ")]:[];return[...s,new P({...a,...t,children:[...i,...l(r,t)]})]}case"heading":return[new P({...t,...s,heading:y?r.depth===1?"Title":`Heading${r.depth-1}`:`Heading${r.depth}`,children:[new E({id:D(r).replace(/[. ]+/g,"-").toLowerCase(),children:l(r,t)})]})];case"code":return[...s,new P({border:{bottom:{style:b.SINGLE,space:5,size:1},left:{style:b.SINGLE,space:10,size:1},right:{style:b.SINGLE,space:5,size:1},top:{style:b.SINGLE,space:6,size:1}},...t,alignment:"left",style:"blockCode",children:r.value.split(`
`).map((a,i)=>new p({...t,text:a,break:i===0?0:1,style:"code",font:{name:"Consolas"}}))})];case"list":return r.ordered?(t.bullet={level:((w=(k=t.bullet)==null?void 0:k.level)!=null?w:-1)+1},console.warn("Please add numbering plugin to support ordered lists. For now, we use only bullets for both the ordered and the unordered list.")):t.bullet={level:((C=(S=t.bullet)==null?void 0:S.level)!=null?C:-1)+1},[...s,...e(r,t)];case"blockquote":return[...s,...e(r,t)];case"listItem":return t.checked=r.checked,[...s,...e(r,t)];case"thematicBreak":return[...s,new P({...r.data,border:{top:{style:b.SINGLE,size:6}}})];case"definition":case"footnoteDefinition":return s;case"table":return console.warn("Please add table plugin to support tables."),s;case"fragment":return[...s,...e(r,t)];case"":return s;case"yaml":case"html":default:return console.warn(`May be an unsupported node type: ${r.type}`,r),s}}).flat()};return{...I,children:e(u,{})}};export{R as a,H as b};