@m2d/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.64 kB
JavaScript
import{b as D,c as v}from"./chunk-66DCUUGU.mjs";import*as T from"docx";import{Bookmark as R,BorderStyle as m,CheckBox as E,ExternalHyperlink as L,FootnoteReferenceRun as z,InternalHyperlink as B,Paragraph as S,TextRun as b}from"docx";var F=(w,y,g,p)=>{let i=d=>p?d.replace(/ +/g," "):d,a=(d,x={})=>{var h,c;return((c=(h=d.children)==null?void 0:h.map(r=>{var t,e,o;let n=g.flatMap(l=>{var I,P;return(P=(I=l.inline)==null?void 0:I.call(l,T,r,x,w,y,a))!=null?P:[]}),s=Object.assign({},x,r.data),f=(e=r.url)!=null?e:w[(t=r.identifier)==null?void 0:t.toUpperCase()];switch(r.type){case"text":return[...n,...s.pre?r.value.split(`
`).map(l=>new b({text:i(l),...s,break:1})):[new b({text:i(r.value),...s})]];case"checkbox":return[...n,new E({checked:!!r.checked})];case"break":return[...n,new b({break:1})];case"inlineCode":return[...n,new b({text:i(r.value),...s,style:"code",font:{name:"Consolas"}})];case"emphasis":return s.italics=!0,[...n,...a(r,s)];case"strong":return s.bold=!0,[...n,...a(r,s)];case"delete":return s.strike=!0,[...n,...a(r,s)];case"link":case"linkReference":return s.underline={},[...n,f.startsWith("#")?new B({anchor:f.slice(1),children:a(r,s)}):new L({link:f,children:a(r,s)})];case"footnoteReference":return[...n,new z((o=y[r.identifier].id)!=null?o:0)];case"fragment":return[...n,...a(r,s)];case"":return[...n];default:return console.warn(`Unsupported inline node type: ${r.type}`),[...n]}}))!=null?c:[]).flat()};return a},G=async(w,y,g,p)=>{var r;let{plugins:i,useTitle:a,trimInnerSpaces:d,...x}={...v,...p};for(let n of i)await((r=n.preprocess)==null?void 0:r.call(n,w,y));let h=F(y,g,i,d),c=(n,s)=>{var f;return(f=n.children)==null?void 0:f.flatMap(t=>{var l,I,P,C;let e={...s},o=i.flatMap(u=>{var k,N;return(N=(k=u.block)==null?void 0:k.call(u,T,t,e,c,h))!=null?N:[]});switch(Object.assign(e,t.data),t.type){case"paragraph":{let u=e.pre?{alignment:"left"}:{},k=typeof e.checked=="boolean"?[new E({checked:e.checked,checkedState:{value:"2611"},uncheckedState:{value:"2610"}}),new b(" ")]:[];return[...o,new S({...u,...e,children:[...k,...h(t,e)]})]}case"heading":return[new S({...e,...o,heading:a?t.depth===1?"Title":`Heading${t.depth-1}`:`Heading${t.depth}`,children:[new R({id:D(t).replace(/[. ]+/g,"-").toLowerCase(),children:h(t,e)})]})];case"code":return[...o,new S({border:{bottom:{style:m.SINGLE,space:5,size:1},left:{style:m.SINGLE,space:10,size:1},right:{style:m.SINGLE,space:5,size:1},top:{style:m.SINGLE,space:6,size:1}},...e,alignment:"left",style:"blockCode",keepLines:!0,children:t.value.split(`
`).map((u,k)=>new b({...e,text:u,break:k===0?0:1,style:"code",font:{name:"Consolas"}}))})];case"list":return t.ordered?(e.bullet={level:((I=(l=e.bullet)==null?void 0:l.level)!=null?I:-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.")):e.bullet={level:((C=(P=e.bullet)==null?void 0:P.level)!=null?C:-1)+1},[...o,...c(t,e)];case"blockquote":return e.indent={left:720,hanging:360},e.border={...e.border,left:{style:m.INSET,size:20,space:14,color:"aaaaaa"}},[...o,...c(t,e)];case"listItem":return e.checked=t.checked,[...o,...c(t,e)];case"thematicBreak":return[...o,new S({...t.data,border:{top:{style:m.SINGLE,size:6}}})];case"definition":case"footnoteDefinition":return o;case"table":return console.warn("Please add table plugin to support tables."),o;case"fragment":return[...o,...c(t,e)];case"":return o;case"yaml":case"html":return o;default:return console.warn(`May be an unsupported node type: ${t.type}`,t),o}})};return{...x,children:c(w,(p==null?void 0:p.footnoteProps)||{})}};export{T as a,G as b};