@@ -35,33 +35,36 @@ const singleLineBlockNodeTypes = new Set([
3535] )
3636
3737/**
38- * Serialize a table row to markdown line by line
38+ * Pad all cells in a row to same height, width and content alignment.
39+ * Also make sure that cells with block node contents span at least two lines.
3940 *
40- * @param state - Markdown serializer state
41- * @param row - Table row to serialize
41+ * @param row - Table row to normalize
4242 * @param columnWidths - Array of column widths
4343 */
44- function rowToMarkdown (
45- state : MarkdownSerializerState ,
46- row : Row ,
47- columnWidths : number [ ] ,
48- ) {
49- const normalizedCells = row . cells . map ( ( cell , cellIdx ) => {
50- const normalizedCell = { ...cell }
44+ function normalizeCells ( row : Row , columnWidths : number [ ] ) : Cell [ ] {
45+ return row . cells . map ( ( cell , cellIdx ) => {
46+ const normalizedCell = { ...cell }
5147 // Append newline to single-line cells with a block node
5248 if (
5349 normalizedCell . lines . length === 1
54- && [ ...normalizedCell . nodeTypes ] . some ( ( type ) => singleLineBlockNodeTypes . has ( type ) )
50+ && [ ...normalizedCell . nodeTypes ] . some ( ( type ) =>
51+ singleLineBlockNodeTypes . has ( type ) ,
52+ )
5553 ) {
5654 normalizedCell . lines . push ( '' )
5755 row . length = Math . max ( row . length , 2 )
5856 }
5957
6058 // Normalize cells to have the same number of lines
61- while ( normalizedCell . lines . length < row . length ) normalizedCell . lines . push ( '' )
59+ while ( normalizedCell . lines . length < row . length )
60+ normalizedCell . lines . push ( '' )
6261 // Normalize lines in cell to have the same length
6362 normalizedCell . lines . forEach ( ( line , lineIdx ) => {
64- if ( [ ...normalizedCell . nodeTypes ] . some ( ( type ) => ! alignNodeTypes . has ( type ) ) ) {
63+ if (
64+ [ ...normalizedCell . nodeTypes ] . some (
65+ ( type ) => ! alignNodeTypes . has ( type ) ,
66+ )
67+ ) {
6568 // Enforced left alignment.
6669 normalizedCell . lines [ lineIdx ] = line . padEnd ( columnWidths [ cellIdx ] )
6770 return
@@ -72,7 +75,9 @@ function rowToMarkdown(
7275 const spaces = Math . max ( columnWidths [ cellIdx ] - line . length , 0 )
7376 const spacesStart = line . length + Math . floor ( spaces / 2 )
7477 const spacesEnd = line . length + Math . ceil ( spaces / 2 )
75- normalizedCell . lines [ lineIdx ] = line . padStart ( spacesStart ) . padEnd ( spacesEnd )
78+ normalizedCell . lines [ lineIdx ] = line
79+ . padStart ( spacesStart )
80+ . padEnd ( spacesEnd )
7681 } else if ( normalizedCell . align === 'right' ) {
7782 normalizedCell . lines [ lineIdx ] = line . padStart ( columnWidths [ cellIdx ] )
7883 } else {
@@ -81,10 +86,24 @@ function rowToMarkdown(
8186 } )
8287 return normalizedCell
8388 } )
89+ }
8490
91+ /**
92+ * Serialize a table row to markdown line by line
93+ *
94+ * @param state - Markdown serializer state
95+ * @param row - Table row to serialize
96+ * @param columnWidths - Array of column widths
97+ */
98+ function rowToMarkdown (
99+ state : MarkdownSerializerState ,
100+ row : Row ,
101+ columnWidths : number [ ] ,
102+ ) {
85103 // Write row line by line
86104 for ( let lineIdx = 0 ; lineIdx < row . length ; lineIdx ++ ) {
87105 state . write ( '| ' )
106+ const normalizedCells = normalizeCells ( row , columnWidths )
88107 normalizedCells . forEach ( ( cell , cellIdx ) => {
89108 state . write ( cell . lines [ lineIdx ] )
90109 if ( cellIdx < normalizedCells . length - 1 ) {
0 commit comments