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