UNPKG

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