UNPKG

@md2docx/table

Version:

Plugin to convert Markdown tables (MDAST) to DOCX with support for rich formatting and seamless integration into mdast2docx.

2 lines (1 loc) 2.3 kB
"use strict";var T=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var D=(n,l)=>{for(var t in l)T(n,t,{get:l[t],enumerable:!0})},V=(n,l,t,s)=>{if(l&&typeof l=="object"||typeof l=="function")for(let i of k(l))!z.call(n,i)&&i!==t&&T(n,i,{get:()=>l[i],enumerable:!(s=N(l,i))||s.enumerable});return n};var H=n=>V(T({},"__esModule",{value:!0}),n);var B={};D(B,{defaultTableOptions:()=>u,tablePlugin:()=>S});module.exports=H(B);var e=require("docx"),m=require("@m2d/core/utils"),p={style:e.BorderStyle.SINGLE,color:"auto",size:1},u={tableProps:{width:{size:100,type:e.WidthType.PERCENTAGE},margins:{top:(0,e.convertMillimetersToTwip)(2),right:(0,e.convertMillimetersToTwip)(4),bottom:(0,e.convertMillimetersToTwip)(2),left:(0,e.convertMillimetersToTwip)(4)},borders:{top:p,right:p,bottom:p,left:p,insideHorizontal:p,insideVertical:p}},rowProps:{},cellProps:{width:{size:100}},firstRowProps:{tableHeader:!0},firstRowCellProps:{shading:{type:e.ShadingType.SOLID,fill:"b79c2f"},alignment:e.AlignmentType.CENTER},alignments:{defaultVerticalAlign:e.VerticalAlignTable.CENTER,defaultHorizontalAlign:e.AlignmentType.CENTER,preferMdData:!0}},M=["paragraph","heading","code","list","blockquote","thematicBreak","fragment","table"],S=n=>({block:(l,t,s,i)=>{var b;if(t.type!=="table")return[];let{Table:P,TableRow:y,TableCell:h}=l,{tableProps:d,firstRowProps:w,firstRowCellProps:g,rowProps:R,cellProps:C,alignments:c}=(0,m.mergeOptions)(n,u),f=(b=t.align)==null?void 0:b.map(a=>{switch(a){case"left":return l.AlignmentType.LEFT;case"right":return l.AlignmentType.RIGHT;case"center":return l.AlignmentType.CENTER;default:return}}),A=a=>{let o=[],r=[];for(t of a.children)M.includes(t.type)?(r.length>0&&(o.push({type:"paragraph",children:[...r]}),r.length=0),o.push(t)):r.push(t);r.length>0&&o.push({type:"paragraph",children:r}),a.children=o},I=(a,o)=>new y({...R,...o===0?w:{},children:a.children.map((r,O)=>(A(r),new h({verticalAlign:c.defaultVerticalAlign,...C,...o===0?g:{},children:i(r,{alignment:o===0&&g.alignment?g.alignment:c.preferMdData?f==null?void 0:f[O]:c.defaultHorizontalAlign})})))}),E=t.children.map(I);return t._type=t.type,t.type="",[new P({...d,rows:E})]}});0&&(module.exports={defaultTableOptions,tablePlugin});