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