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