Important
Active development and maintenance of @m2d/table has moved to the unified md2docx monorepo.
- π Monorepo: github.com/md2docx/md2docx
- π Package Source:
packages/table - π Documentation: md2docx.js.org/docs/table
- π¬ Issues & Pull Requests: Please submit all new issues, feedback, and PRs in the md2docx issue tracker.
This standalone repository is now archived and retained for historical reference only. Releases and npm updates continue from the monorepo.
A plugin that converts Markdown tables into rich, styled Word tables with full alignment, border, and header support.
npm install @m2d/tablepnpm add @m2d/tableyarn add @m2d/tableThe @m2d/table plugin for mdast2docx renders Markdown tables into Word-compatible tables with customizable layout, alignment, and cell styling using the docx library.
Automatically handles header rows, borders, shading, cell alignments, and padding β all configurable.
- Transforms Markdown tables into
docx.Tableelements - Auto-detects column alignment from MDAST (
left,center,right) - Customizable:
- Table width and border styles
- Cell padding and shading
- Header row formatting
- Horizontal and vertical alignment
- Graceful fallback to defaults if MDAST alignment is missing
import { toDocx } from "@m2d/core";
import { tablePlugin } from "@m2d/table";
const plugins = [tablePlugin()];
const buffer = await toDocx(mdastTree, {
plugins,
});The tablePlugin accepts an optional configuration object:
tablePlugin({
tableProps: { ... },
rowProps: { ... },
cellProps: {
... // CellProps
data: { bold: true, color: "#000000" } // Paragraph and Run styling options
},
firstRowProps: { ... },
firstRowCellProps: {
data: { bold: true, alignment: AlignmentType.CENTER } // Header cell styling
},
alignments: {
defaultHorizontalAlign: AlignmentType.CENTER,
defaultVerticalAlign: VerticalAlign.CENTER,
preferMdData: true,
},
});All options override the following sensible defaults:
| Property | Default Value |
|---|---|
| Table Width | 100% (percentage) |
| Border Style | SINGLE, size 1 |
| Cell Padding | 2β4mm margins (top/bottom/left/right) |
| Header Row | Bold with shaded background #b79c2f |
| Cell Styling | Full docx.js paragraph & run options |
| Vertical Alignment | CENTER |
| Horizontal Alignment | Based on Markdown or CENTER fallback |
The data property provides comprehensive styling control using docx.js paragraph and text run options:
bold,italics,underline,strike,doubleStrikecolor,size(font size in half-points)font(font family),highlight,shadingsuperScript,subScript,smallCaps,allCaps
alignment- text alignment (LEFT, CENTER, RIGHT, JUSTIFIED)spacing- line spacing and paragraph spacingindent- left, right, first line, hanging indentsnumbering,bullet,style
pre: true- preserves spaces, newline for code blocks
tablePlugin({
cellProps: {
data: {
font: "Arial",
size: 20, // 10pt font
color: "#333333",
spacing: { after: 120 }, // 6pt spacing after
},
},
firstRowCellProps: {
data: {
bold: true,
alignment: AlignmentType.CENTER,
color: "#ffffff",
size: 24, // 12pt font
font: "Calibri",
},
},
});| Name | Age | City |
| :---: | :-: | --------: |
| Alice | 24 | New York |
| Bob | 30 | San Diego |- The first row is treated as a header, with custom shading.
- Column alignment is preserved: center, center, right.
- Leverages
docx.Table,docx.TableRow,docx.TableCell, anddocx.Paragraph - Dynamically maps Markdown alignment via
MDAST.align[] - Uses
@m2d/coreβs block plugin API - Prevents re-processing of transformed nodes by setting
node.type = ""
- Does not support row/column spans
- MDAST source must conform to GFM tables
- Table styling is fixed to plugin options; no per-cell customization via Markdown yet
If you find this useful:
- β Star mdast2docx on GitHub
- β€οΈ Consider sponsoring
MIT Β© Mayank Chaudhari
Made with π by Mayank Kumar Chaudhari