@@ -3,6 +3,15 @@ local logger = require('render-markdown.logger')
33local str = require (' render-markdown.str' )
44local util = require (' render-markdown.render.util' )
55
6+ --- @class render.md.table.Column
7+ --- @field info render.md.NodeInfo
8+ --- @field width integer
9+
10+ --- @class render.md.table.Row
11+ --- @field info render.md.NodeInfo
12+ --- @field pipes render.md.NodeInfo[]
13+ --- @field columns render.md.table.Column[]
14+
615--- @alias render.md.table.Alignment ' left' | ' right' | ' center' | ' default'
716
817--- @class render.md.table.DelimColumn
@@ -13,15 +22,6 @@ local util = require('render-markdown.render.util')
1322--- @field info render.md.NodeInfo
1423--- @field columns render.md.table.DelimColumn[]
1524
16- --- @class render.md.table.Column
17- --- @field info render.md.NodeInfo
18- --- @field width integer
19-
20- --- @class render.md.table.Row
21- --- @field info render.md.NodeInfo
22- --- @field pipes render.md.NodeInfo[]
23- --- @field columns render.md.table.Column[]
24-
2525--- @class render.md.table.Table
2626--- @field info render.md.NodeInfo
2727--- @field delim render.md.table.DelimRow
@@ -47,7 +47,7 @@ function Parser.parse(context, info)
4747 if row .type == ' pipe_table_delimiter_row' then
4848 delim = Parser .delim (row )
4949 elseif context :contains_info (row ) then
50- if row . type == ' pipe_table_header' or row .type == ' pipe_table_row ' then
50+ if vim . tbl_contains ({ ' pipe_table_header' , ' pipe_table_row ' }, row .type ) then
5151 table.insert (table_rows , row )
5252 else
5353 logger .unhandled_type (' markdown' , ' row' , row .type )
@@ -73,8 +73,7 @@ function Parser.parse(context, info)
7373 -- Store the max width information in the delimiter
7474 for _ , row in ipairs (rows ) do
7575 for i , column in ipairs (row .columns ) do
76- local delim_column = delim .columns [i ]
77- delim_column .width = math.max (delim_column .width , column .width )
76+ delim .columns [i ].width = math.max (delim .columns [i ].width , column .width )
7877 end
7978 end
8079
@@ -90,17 +89,12 @@ function Parser.delim(info)
9089 if row_data == nil then
9190 return nil
9291 end
93-
94- local pipes = row_data .pipes
95- local cells = row_data .cells
96-
97- --- @type render.md.table.DelimColumn[]
92+ local pipes , cells = row_data .pipes , row_data .cells
9893 local columns = {}
9994 for i = 1 , # cells do
100- local cell = cells [i ]
101- local width = pipes [i + 1 ].start_col - pipes [i ].end_col
95+ local cell , width = cells [i ], pipes [i + 1 ].start_col - pipes [i ].end_col
10296 if width < 0 then
103- return {}
97+ return nil
10498 end
10599 --- @type render.md.table.DelimColumn
106100 local column = { width = width , alignment = Parser .alignment (cell ) }
@@ -137,18 +131,13 @@ function Parser.row(context, info, num_columns)
137131 if row_data == nil then
138132 return nil
139133 end
140-
141- local pipes = row_data .pipes
142- local cells = row_data .cells
134+ local pipes , cells = row_data .pipes , row_data .cells
143135 if # cells ~= num_columns then
144136 return nil
145137 end
146-
147- --- @type render.md.table.Column[]
148138 local columns = {}
149139 for i = 1 , # cells do
150- local cell = cells [i ]
151- local width = pipes [i + 1 ].start_col - pipes [i ].end_col
140+ local cell , width = cells [i ], pipes [i + 1 ].start_col - pipes [i ].end_col
152141 -- Account for double width glyphs by replacing cell spacing with text width
153142 width = width - (cell .end_col - cell .start_col ) + str .width (cell .text )
154143 -- Remove concealed and add inlined text
@@ -160,7 +149,6 @@ function Parser.row(context, info, num_columns)
160149 local column = { info = cell , width = width }
161150 table.insert (columns , column )
162151 end
163-
164152 --- @type render.md.table.Row
165153 return { info = info , pipes = pipes , columns = columns }
166154end
170158--- @param cell_type string
171159--- @return { pipes : render.md.NodeInfo[] , cells : render.md.NodeInfo[] }?
172160function Parser .row_data (info , cell_type )
173- --- @type render.md.NodeInfo[]
174- local pipes = {}
175- --- @type render.md.NodeInfo[]
176- local cells = {}
161+ local pipes , cells = {}, {}
177162 info :for_each_child (function (cell )
178163 if cell .type == ' |' then
179164 table.insert (pipes , cell )
@@ -218,6 +203,7 @@ function Render:render(info)
218203 if not self .config .enabled or self .config .style == ' none' then
219204 return
220205 end
206+
221207 local tbl = Parser .parse (self .context , info )
222208 if tbl == nil then
223209 return
@@ -350,9 +336,7 @@ function Render:full(tbl)
350336 return math.max (str .leading_spaces (row .info .text ), row .info .start_col )
351337 end
352338
353- local delim = tbl .delim
354- local first = tbl .rows [1 ]
355- local last = tbl .rows [# tbl .rows ]
339+ local delim , first , last = tbl .delim , tbl .rows [1 ], tbl .rows [# tbl .rows ]
356340 if not width_equal (first , delim ) or not width_equal (last , delim ) then
357341 return
358342 end
0 commit comments