UNPKG

@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) 4.42 kB
"use strict";var M=Object.create;var I=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var v=Object.getPrototypeOf,E=Object.prototype.hasOwnProperty;var B=(t,i)=>{for(var l in i)I(t,l,{get:i[l],enumerable:!0})},D=(t,i,l,p)=>{if(i&&typeof i=="object"||typeof i=="function")for(let d of S(i))!E.call(t,d)&&d!==l&&I(t,d,{get:()=>i[d],enumerable:!(p=N(i,d))||p.enumerable});return t};var F=(t,i,l)=>(l=t!=null?M(v(t)):{},D(i||!t||!t.__esModule?I(l,"default",{value:t,enumerable:!0}):l,t)),H=t=>D(I({},"__esModule",{value:!0}),t);var X={};B(X,{toSection:()=>L});module.exports=H(X);var O=require("docx");var w=t=>{var i,l;return(i=t.children)!=null&&i.length?t.children.map(w).join(""):(l=t.value)!=null?l:""},T={useTitle:!0,plugins:[]};var R=F(require("docx")),z=(t,i,l)=>{let p=async(d,y={})=>{var g,f;return(await Promise.all((f=(g=d.children)==null?void 0:g.map(async o=>{var r,n,c;let a=(await Promise.all(l.map(u=>{var b,m;return(m=(b=u.inline)==null?void 0:b.call(u,R,o,y,t,i,p))!=null?m:[]}))).flat(),s=Object.assign({},y,o.data),P=(n=o.url)!=null?n:t[(r=o.identifier)==null?void 0:r.toUpperCase()];switch(o.type){case"text":return[...a,...s.pre?o.value.split(` `).map(u=>new O.TextRun({text:u,...s,break:1})):[new O.TextRun({text:o.value,...s})]];case"checkbox":return[...a,new O.CheckBox({checked:!!o.checked})];case"break":return[...a,new O.TextRun({break:1})];case"inlineCode":return[...a,new O.TextRun({text:o.value,...s,style:"code",font:{name:"Consolas"}})];case"emphasis":return s.italics=!0,[...a,...await p(o,s)];case"strong":return s.bold=!0,[...a,...await p(o,s)];case"delete":return s.strike=!0,[...a,...await p(o,s)];case"link":case"linkReference":return s.underline={},[...a,P.startsWith("#")?new O.InternalHyperlink({anchor:P.slice(1),children:await p(o,s)}):new O.ExternalHyperlink({link:P,children:await p(o,s)})];case"footnoteReference":return[...a,new O.FootnoteReferenceRun((c=i[o.identifier].id)!=null?c:0)];case"fragment":return[...a,...await p(o,s)];case"":return[...a];default:return console.warn(`Unsupported inline node type: ${o.type}`),[...a]}}))!=null?f:[])).flat()};return p},L=async(t,i,l,p)=>{let{plugins:d,useTitle:y,...g}={...T,...p};d.forEach(a=>{var s;return(s=a==null?void 0:a.preprocess)==null?void 0:s.call(a,t)});let f=z(i,l,d),o=async(a,s)=>{var P;return(await Promise.all((P=a.children)==null?void 0:P.map(async r=>{var u,b,m,k;let n={...s},c=(await Promise.all(d.map(h=>{var x,C;return(C=(x=h.block)==null?void 0:x.call(h,R,r,n,o,f))!=null?C:[]}))).flat();switch(Object.assign(n,r.data),r.type){case"paragraph":{let h=n.pre?{alignment:"left"}:{},x=typeof n.checked=="boolean"?[new O.CheckBox({checked:n.checked,checkedState:{value:"2611"},uncheckedState:{value:"2610"}}),new O.TextRun(" ")]:[];return[...c,new O.Paragraph({...h,...n,children:[...x,...await f(r,n)]})]}case"heading":return[new O.Paragraph({...n,...c,heading:y?r.depth===1?"Title":`Heading${r.depth-1}`:`Heading${r.depth}`,children:[new O.Bookmark({id:w(r).replace(/[. ]+/g,"-").toLowerCase(),children:await f(r,n)})]})];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}},...n,alignment:"left",style:"blockCode",children:r.value.split(` `).map((h,x)=>new O.TextRun({...n,text:h,break:x===0?0:1,style:"code",font:{name:"Consolas"}}))})];case"list":return r.ordered?(n.bullet={level:((b=(u=n.bullet)==null?void 0:u.level)!=null?b:-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.")):n.bullet={level:((k=(m=n.bullet)==null?void 0:m.level)!=null?k:-1)+1},[...c,...await o(r,n)];case"blockquote":return[...c,...await o(r,n)];case"listItem":return n.checked=r.checked,[...c,...await o(r,n)];case"thematicBreak":return[...c,new O.Paragraph({...r.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,...await o(r,n)];case"":return c;case"yaml":case"html":default:return console.warn(`May be an unsupported node type: ${r.type}`,r),c}}))).flat()};return{...g,children:await o(t,{})}};0&&(module.exports={toSection});