@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) • 1.72 kB
JavaScript
import{BorderStyle as I,ShadingType as E,WidthType as O,AlignmentType as c,VerticalAlignTable as N,convertMillimetersToTwip as i}from"docx";import{mergeOptions as k}from"@m2d/core/utils";var r={style:I.SINGLE,color:"auto",size:1},z={tableProps:{width:{size:100,type:O.PERCENTAGE},margins:{top:i(2),right:i(4),bottom:i(2),left:i(4)},borders:{top:r,right:r,bottom:r,left:r,insideHorizontal:r,insideVertical:r}},rowProps:{},cellProps:{width:{size:100}},firstRowProps:{tableHeader:!0},firstRowCellProps:{shading:{type:E.SOLID,fill:"b79c2f"},alignment:c.CENTER},alignments:{defaultVerticalAlign:N.CENTER,defaultHorizontalAlign:c.CENTER,preferMdData:!0}},D=["paragraph","heading","code","list","blockquote","thematicBreak","fragment","table"],S=f=>({block:(o,e,V,T)=>{var g;if(e.type!=="table")return[];let{Table:b,TableRow:m,TableCell:u}=o,{tableProps:P,firstRowProps:y,firstRowCellProps:a,rowProps:h,cellProps:d,alignments:p}=k(f,z),s=(g=e.align)==null?void 0:g.map(n=>{switch(n){case"left":return o.AlignmentType.LEFT;case"right":return o.AlignmentType.RIGHT;case"center":return o.AlignmentType.CENTER;default:return}}),w=n=>{let l=[],t=[];for(e of n.children)D.includes(e.type)?(t.length>0&&(l.push({type:"paragraph",children:[...t]}),t.length=0),l.push(e)):t.push(e);t.length>0&&l.push({type:"paragraph",children:t}),n.children=l},R=(n,l)=>new m({...h,...l===0?y:{},children:n.children.map((t,A)=>(w(t),new u({verticalAlign:p.defaultVerticalAlign,...d,...l===0?a:{},children:T(t,{alignment:l===0&&a.alignment?a.alignment:p.preferMdData?s==null?void 0:s[A]:p.defaultHorizontalAlign})})))}),C=e.children.map(R);return e._type=e.type,e.type="",[new b({...P,rows:C})]}});export{z as defaultTableOptions,S as tablePlugin};