Skip to content

Commit 7ed174f

Browse files
committed
feat(@m2d/table): ✨ support dynamic cell and table styling from AST metadata
1 parent 657e9e3 commit 7ed174f

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@m2d/table": minor
3+
---
4+
5+
- Respect cell-level and table-level `shading`, `border`/`borders`, and `alignment` metadata passed via `cell.data` and `node.data`
6+
- Support dynamic cell margins/padding from AST node metadata

‎packages/table/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- 📊 **Native Word Tables**: Converts Markdown GFM tables into clean `docx.Table` elements with authentic Word OpenXML borders and paddings.
2929
- 🎯 **Automatic Column Alignment**: Honors Markdown column alignments (`left`, `center`, `right`) with fallback defaults.
3030
- 🎨 **Rich Cell Styling**: Style headers and data cells with background fills (`shading`), custom typography (fonts, sizes, colors), and paragraph spacing via the `data` option.
31+
- 🖌️ **Dynamic AST & CSS Styling**: Automatically respects cell-level and table-level `shading`, `borders`, `margins` (cell padding), and `alignment` metadata passed via `cell.data` and `node.data` (e.g. from embedded `<style>` blocks or raw HTML attributes processed by `@m2d/html`).
3132
- 📄 **Multi-Page Header Repetition**: Automatically repeats header rows across page breaks (`tableHeader: true`).
3233
- 🧱 **Block Content in Cells**: Seamlessly accommodates nested paragraphs, lists, code snippets, and blockquotes inside table cells (ideal when paired with `@m2d/html`).
3334

‎packages/table/src/index.ts‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,35 @@ export const tablePlugin: (options?: ITablePluginProps) => IPlugin = (
204204
...(rowIndex === 0 ? firstRowProps : {}),
205205
children: row.children.map((cell, cellIndex) => {
206206
wrapInBlockNodeIfNeeded(cell);
207+
// biome-ignore lint/suspicious/noExplicitAny: dynamic AST cell styling data
208+
const cData = (cell.data as any) ?? {};
209+
const cellShading =
210+
cData.shading ??
211+
(rowIndex === 0 ? firstRowCellProps.shading : cellProps.shading);
212+
const cellBorders =
213+
cData.border ??
214+
cData.borders ??
215+
(rowIndex === 0 ? firstRowCellProps.borders : cellProps.borders);
216+
const cellMargins =
217+
cData.margins ?? cData.margin ?? cellProps.margins;
218+
207219
return new TableCell({
208220
verticalAlign: alignments.defaultVerticalAlign,
209221
...cellProps,
210222
...(rowIndex === 0 ? firstRowCellProps : {}),
223+
...(cellBorders ? { borders: cellBorders } : {}),
224+
...(cellShading ? { shading: cellShading } : {}),
225+
...(cellMargins ? { margins: cellMargins } : {}),
211226
children: blockChildrenProcessor(cell, {
212227
alignment:
213-
rowIndex === 0 && alignment1
228+
cData.alignment ??
229+
(rowIndex === 0 && alignment1
214230
? alignment1
215231
: alignments.preferMdData
216232
? align?.[cellIndex]
217-
: alignments.defaultHorizontalAlign,
233+
: alignments.defaultHorizontalAlign),
218234
...(rowIndex === 0 ? data : cellData),
235+
...cData,
219236
}),
220237
});
221238
}),
@@ -225,7 +242,16 @@ export const tablePlugin: (options?: ITablePluginProps) => IPlugin = (
225242
(node as unknown as EmptyNode)._type = node.type;
226243
// @ts-expect-error - Setting type to empty string to avoid re-processing the node.
227244
node.type = "";
228-
return [new Table({ ...tableProps, rows })];
245+
// biome-ignore lint/suspicious/noExplicitAny: dynamic AST table styling data
246+
const tData = (node.data as any) ?? {};
247+
const tableBorders = tData.border ?? tData.borders ?? tableProps.borders;
248+
return [
249+
new Table({
250+
...tableProps,
251+
...(tableBorders ? { borders: tableBorders } : {}),
252+
rows,
253+
}),
254+
];
229255
},
230256
};
231257
};

0 commit comments

Comments
 (0)