@@ -10,6 +10,7 @@ import { createMarkdownSerializer } from '../../extensions/Markdown.js'
1010type Cell = {
1111 md : string
1212 lines : string [ ]
13+ nodeTypes : Set < string >
1314 align : string
1415}
1516
@@ -37,7 +38,23 @@ function rowToMarkdown(
3738 while ( cell . lines . length < row . length ) cell . lines . push ( '' )
3839 // Normalize lines in cell to have the same length
3940 cell . lines . forEach ( ( line , lineIdx ) => {
40- cell . lines [ lineIdx ] = line . padEnd ( columnWidths [ cellIdx ] )
41+ // Node types with enforced left-alignment
42+ if ( [ 'listItem' , 'taskItem' , 'codeBlock' ] . some ( nodeType => cell . nodeTypes . has ( nodeType ) ) ) {
43+ cell . lines [ lineIdx ] = line . padEnd ( columnWidths [ cellIdx ] )
44+ return
45+ }
46+
47+ // Pad according to alignment
48+ if ( cell . align === 'center' ) {
49+ const spaces = Math . max ( columnWidths [ cellIdx ] - line . length , 0 )
50+ const spacesStart = line . length + Math . floor ( spaces / 2 )
51+ const spacesEnd = line . length + Math . ceil ( spaces / 2 )
52+ cell . lines [ lineIdx ] = line . padStart ( spacesStart ) . padEnd ( spacesEnd )
53+ } else if ( cell . align === 'right' ) {
54+ cell . lines [ lineIdx ] = line . padStart ( columnWidths [ cellIdx ] )
55+ } else {
56+ cell . lines [ lineIdx ] = line . padEnd ( columnWidths [ cellIdx ] )
57+ }
4158 } )
4259 return cell
4360 } )
@@ -80,6 +97,7 @@ function headerRowToMarkdown(
8097 // No space padding next to pipes in horizontal separator
8198 state . write ( '|' )
8299 row . cells . forEach ( ( cell , cellIdx ) => {
100+ // Separator alignment
83101 const separatorWidth = columnWidths [ cellIdx ] + 2
84102 let separator = ''
85103 switch ( cell . align ) {
@@ -123,17 +141,24 @@ function tableToMarkdown(state: MarkdownSerializerState, node: Node) {
123141 const cellNodes = row . node . content . content
124142 cellNodes . forEach ( ( node , cellIdx ) => {
125143 columnWidths [ cellIdx ] = columnWidths [ cellIdx ] ?? 0
144+
145+ // Serialize cell content with all child nodes and split lines
126146 const md = serializer . serialize ( node )
147+ const nodeTypes = new Set < string > ( ) ;
148+ node . descendants ( ( descendant ) => {
149+ nodeTypes . add ( descendant . type . name )
150+ } )
127151 const lines = md . split ( / \r ? \n / ) . map ( ( line ) => {
128152 // Escape pipe character
129153 line = line . replace ( / \| / , '\\$&' )
130154 return line . trim ( )
131155 } )
156+
132157 row . length = Math . max ( row . length , lines . length )
133158 const lineLength = Math . max ( ...lines . map ( ( line ) => line . length ) )
134159 columnWidths [ cellIdx ] = Math . max ( columnWidths [ cellIdx ] , lineLength )
135160 const align = node . attrs ?. textAlign ?? ''
136- row . cells . push ( { md, lines, align } )
161+ row . cells . push ( { md, lines, nodeTypes , align } )
137162 } )
138163 } )
139164
0 commit comments