@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) • 5.74 kB
JavaScript
;var F=Object.create;var b=Object.defineProperty;var X=Object.getOwnPropertyDescriptor;var B=Object.getOwnPropertyNames;var H=Object.getPrototypeOf,A=Object.prototype.hasOwnProperty;var L=(t,n)=>{for(var i in n)b(t,i,{get:n[i],enumerable:!0})},N=(t,n,i,a)=>{if(n&&typeof n=="object"||typeof n=="function")for(let d of B(n))!A.call(t,d)&&d!==i&&b(t,d,{get:()=>n[d],enumerable:!(a=X(n,d))||a.enumerable});return t};var U=(t,n,i)=>(i=t!=null?F(H(t)):{},N(n||!t||!t.__esModule?b(i,"default",{value:t,enumerable:!0}):i,t)),z=t=>N(b({},"__esModule",{value:!0}),t);var G={};L(G,{toDocx:()=>j});module.exports=z(G);var O=require("docx");var g=t=>{let n={},i={};return t.forEach(a=>{var d;a.type==="definition"?n[a.identifier.toUpperCase()]=a.url:a.type==="footnoteDefinition"?i[a.identifier.toUpperCase()]={children:a.children}:(d=a.children)!=null&&d.length&&Object.assign(n,g(a.children))}),{definitions:n,footnoteDefinitions:i}},k=t=>{var n,i;return(n=t.children)!=null&&n.length?t.children.map(k).join(""):(i=t.value)!=null?i:""},C={useTitle:!0,plugins:[]},M={styles:{default:{document:{paragraph:{spacing:{before:175,line:300},alignment:"thaiDistribute"},run:{size:24}},heading1:{paragraph:{spacing:{before:350}}},heading2:{paragraph:{spacing:{before:350}}},heading3:{paragraph:{spacing:{before:350}}},heading4:{paragraph:{spacing:{before:350}}},heading5:{paragraph:{spacing:{before:350}}},heading6:{paragraph:{spacing:{before:350}}}}}};var v=U(require("docx")),_=(t,n,i)=>{let a=(d,x={})=>{var D,u;return((u=(D=d.children)==null?void 0:D.map(s=>{var p,e,r;let h=i.map(c=>{var P,m;return(m=(P=c.inline)==null?void 0:P.call(c,v,s,x,t,n,a))!=null?m:[]}).flat(),l=Object.assign({},x,s.data),f=(e=s.url)!=null?e:t[(p=s.identifier)==null?void 0:p.toUpperCase()];switch(s.type){case"text":return[...h,...l.pre?s.value.split(`
`).map(c=>new O.TextRun({text:c,...l,break:1})):[new O.TextRun({text:s.value,...l})]];case"checkbox":return[...h,new O.CheckBox({checked:!!s.checked})];case"break":return[...h,new O.TextRun({break:1})];case"inlineCode":return[...h,new O.TextRun({text:s.value,...l,style:"code",font:{name:"Consolas"}})];case"emphasis":return l.italics=!0,[...h,...a(s,l)];case"strong":return l.bold=!0,[...h,...a(s,l)];case"delete":return l.strike=!0,[...h,...a(s,l)];case"link":case"linkReference":return l.underline={},[...h,f.startsWith("#")?new O.InternalHyperlink({anchor:f.slice(1),children:a(s,l)}):new O.ExternalHyperlink({link:f,children:a(s,l)})];case"footnoteReference":return[...h,new O.FootnoteReferenceRun((r=n[s.identifier].id)!=null?r:0)];case"fragment":return[...h,...a(s,l)];case"":return[...h];default:return console.warn(`Unsupported inline node type: ${s.type}`),[...h]}}))!=null?u:[]).flat()};return a},R=async(t,n,i,a)=>{var h;let{plugins:d,useTitle:x,...D}={...C,...a};for(let l of d)await((h=l.preprocess)==null?void 0:h.call(l,t,n));let u=_(n,i,d),s=(l,f)=>{var p;return(p=l.children)==null?void 0:p.map(e=>{var P,m,T,w;let r={...f},c=d.map(y=>{var I,S;return(S=(I=y.block)==null?void 0:I.call(y,v,e,r,s,u))!=null?S:[]}).flat();switch(Object.assign(r,e.data),e.type){case"paragraph":{let y=r.pre?{alignment:"left"}:{},I=typeof r.checked=="boolean"?[new O.CheckBox({checked:r.checked,checkedState:{value:"2611"},uncheckedState:{value:"2610"}}),new O.TextRun(" ")]:[];return[...c,new O.Paragraph({...y,...r,children:[...I,...u(e,r)]})]}case"heading":return[new O.Paragraph({...r,...c,heading:x?e.depth===1?"Title":`Heading${e.depth-1}`:`Heading${e.depth}`,children:[new O.Bookmark({id:k(e).replace(/[. ]+/g,"-").toLowerCase(),children:u(e,r)})]})];case"code":return[...c,new O.Paragraph({border:{bottom:{style:O.BorderStyle.SINGLE,space:5,size:1},left:{style:O.BorderStyle.SINGLE,space:10,size:1},right:{style:O.BorderStyle.SINGLE,space:5,size:1},top:{style:O.BorderStyle.SINGLE,space:6,size:1}},...r,alignment:"left",style:"blockCode",children:e.value.split(`
`).map((y,I)=>new O.TextRun({...r,text:y,break:I===0?0:1,style:"code",font:{name:"Consolas"}}))})];case"list":return e.ordered?(r.bullet={level:((m=(P=r.bullet)==null?void 0:P.level)!=null?m:-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.")):r.bullet={level:((w=(T=r.bullet)==null?void 0:T.level)!=null?w:-1)+1},[...c,...s(e,r)];case"blockquote":return[...c,...s(e,r)];case"listItem":return r.checked=e.checked,[...c,...s(e,r)];case"thematicBreak":return[...c,new O.Paragraph({...e.data,border:{top:{style:O.BorderStyle.SINGLE,size:6}}})];case"definition":case"footnoteDefinition":return c;case"table":return console.warn("Please add table plugin to support tables."),c;case"fragment":return[...c,...s(e,r)];case"":return c;case"yaml":case"html":default:return console.warn(`May be an unsupported node type: ${e.type}`,e),c}}).flat()};return{...D,children:s(t,{})}};var j=async(t,n={},i=C,a="blob")=>{var f;let d=1,x={},D={...M,...n},u=(f=i==null?void 0:i.plugins)!=null?f:[],s=await Promise.all((Array.isArray(t)?t:[{ast:t}]).map(async({ast:p,props:e})=>{var P;let{definitions:r,footnoteDefinitions:c}=g(p.children);return await Promise.all(Object.values(c).map(async m=>{m.id=d,x[d]=await R({type:"root",children:m.children},r,{}),d++})),u.push(...(P=e==null?void 0:e.plugins)!=null?P:[]),{ast:p,props:{...i,...e},definitions:r,footnoteDefinitions:c}}));u==null||u.forEach(p=>{var e;return(e=p==null?void 0:p.root)==null?void 0:e.call(p,D)});let h=await Promise.all(s.map(({ast:p,props:e,definitions:r,footnoteDefinitions:c})=>R(p,r,c,e)));u==null||u.forEach(p=>{var e;return(e=p==null?void 0:p.postprocess)==null?void 0:e.call(p,h)});let l=new O.Document({...D,footnotes:x,sections:h});return O.Packer.pack(l,a)};0&&(module.exports={toDocx});