@@ -3,6 +3,15 @@ local logger = require('render-markdown.logger')
3
3
local str = require (' render-markdown.str' )
4
4
local util = require (' render-markdown.render.util' )
5
5
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
+
6
15
--- @alias render.md.table.Alignment ' left' | ' right' | ' center' | ' default'
7
16
8
17
--- @class render.md.table.DelimColumn
@@ -13,15 +22,6 @@ local util = require('render-markdown.render.util')
13
22
--- @field info render.md.NodeInfo
14
23
--- @field columns render.md.table.DelimColumn[]
15
24
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
-
25
25
--- @class render.md.table.Table
26
26
--- @field info render.md.NodeInfo
27
27
--- @field delim render.md.table.DelimRow
@@ -47,7 +47,7 @@ function Parser.parse(context, info)
47
47
if row .type == ' pipe_table_delimiter_row' then
48
48
delim = Parser .delim (row )
49
49
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
51
51
table.insert (table_rows , row )
52
52
else
53
53
logger .unhandled_type (' markdown' , ' row' , row .type )
@@ -73,8 +73,7 @@ function Parser.parse(context, info)
73
73
-- Store the max width information in the delimiter
74
74
for _ , row in ipairs (rows ) do
75
75
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 )
78
77
end
79
78
end
80
79
@@ -90,17 +89,12 @@ function Parser.delim(info)
90
89
if row_data == nil then
91
90
return nil
92
91
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
98
93
local columns = {}
99
94
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
102
96
if width < 0 then
103
- return {}
97
+ return nil
104
98
end
105
99
--- @type render.md.table.DelimColumn
106
100
local column = { width = width , alignment = Parser .alignment (cell ) }
@@ -137,18 +131,13 @@ function Parser.row(context, info, num_columns)
137
131
if row_data == nil then
138
132
return nil
139
133
end
140
-
141
- local pipes = row_data .pipes
142
- local cells = row_data .cells
134
+ local pipes , cells = row_data .pipes , row_data .cells
143
135
if # cells ~= num_columns then
144
136
return nil
145
137
end
146
-
147
- --- @type render.md.table.Column[]
148
138
local columns = {}
149
139
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
152
141
-- Account for double width glyphs by replacing cell spacing with text width
153
142
width = width - (cell .end_col - cell .start_col ) + str .width (cell .text )
154
143
-- Remove concealed and add inlined text
@@ -160,7 +149,6 @@ function Parser.row(context, info, num_columns)
160
149
local column = { info = cell , width = width }
161
150
table.insert (columns , column )
162
151
end
163
-
164
152
--- @type render.md.table.Row
165
153
return { info = info , pipes = pipes , columns = columns }
166
154
end
170
158
--- @param cell_type string
171
159
--- @return { pipes : render.md.NodeInfo[] , cells : render.md.NodeInfo[] }?
172
160
function 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 = {}, {}
177
162
info :for_each_child (function (cell )
178
163
if cell .type == ' |' then
179
164
table.insert (pipes , cell )
@@ -218,6 +203,7 @@ function Render:render(info)
218
203
if not self .config .enabled or self .config .style == ' none' then
219
204
return
220
205
end
206
+
221
207
local tbl = Parser .parse (self .context , info )
222
208
if tbl == nil then
223
209
return
@@ -350,9 +336,7 @@ function Render:full(tbl)
350
336
return math.max (str .leading_spaces (row .info .text ), row .info .start_col )
351
337
end
352
338
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 ]
356
340
if not width_equal (first , delim ) or not width_equal (last , delim ) then
357
341
return
358
342
end
0 commit comments