@@ -150,89 +150,90 @@ function Handler:render(row, nodes)
150150
151151 for _ , node in ipairs (nodes ) do
152152 local output = str .split (Handler .cache [Handler .text (node )], ' \n ' , true )
153+ if # output > 0 then
154+ -- add top and bottom padding around output
155+ for _ = 1 , self .config .top_pad do
156+ table.insert (output , 1 , ' ' )
157+ end
158+ for _ = 1 , self .config .bottom_pad do
159+ output [# output + 1 ] = ' '
160+ end
153161
154- -- add top and bottom padding around output
155- for _ = 1 , self .config .top_pad do
156- table.insert (output , 1 , ' ' )
157- end
158- for _ = 1 , self .config .bottom_pad do
159- output [# output + 1 ] = ' '
160- end
162+ -- pad lines to the same width
163+ local width = vim .fn .max (iter .list .map (output , str .width ))
164+ for i , line in ipairs (output ) do
165+ output [i ] = line .. str .pad (width - str .width (line ))
166+ end
161167
162- -- pad lines to the same width
163- local width = vim . fn . max ( iter . list . map ( output , str . width ))
164- for i , line in ipairs ( output ) do
165- output [ i ] = line .. str . pad ( width - str . width ( line ))
166- end
168+ -- center is only possible if formula is a single line
169+ local position = self . config . position
170+ if position == ' center ' and node : height () > 1 then
171+ position = ' above '
172+ end
167173
168- -- center is only possible if formula is a single line
169- local position = self .config .position
170- if position == ' center' and node :height () > 1 then
171- position = ' above'
172- end
174+ -- absolute formula column
175+ local col --- @type integer
176+ if position == ' below' and node :height () > 1 then
177+ -- latex blocks include last line, unlike markdown blocks
178+ local _ , line = node :line (' below' , 1 )
179+ col = line and str .spaces (' start' , line ) or 0
180+ else
181+ local _ , line = node :line (' above' , 0 )
182+ col = self .context :width ({
183+ text = line and line :sub (1 , node .start_col ) or ' ' ,
184+ start_row = node .start_row ,
185+ start_col = 0 ,
186+ end_row = node .start_row ,
187+ end_col = node .start_col ,
188+ })
189+ end
173190
174- -- absolute formula column
175- local col --- @type integer
176- if position == ' below' and node :height () > 1 then
177- -- latex blocks include last line, unlike markdown blocks
178- local _ , line = node :line (' below' , 1 )
179- col = line and str .spaces (' start' , line ) or 0
180- else
181- local _ , line = node :line (' above' , 0 )
182- col = self .context :width ({
183- text = line and line :sub (1 , node .start_col ) or ' ' ,
184- start_row = node .start_row ,
185- start_col = 0 ,
186- end_row = node .start_row ,
187- end_col = node .start_col ,
188- })
189- end
191+ -- convert column to relative offset, include padding between formulas
192+ local prefix = math.max (col - current , current == 0 and 0 or 1 )
190193
191- -- convert column to relative offset, include padding between formulas
192- local prefix = math.max (col - current , current == 0 and 0 or 1 )
194+ local above --- @type integer
195+ local below --- @type integer
196+ if position == ' above' then
197+ above = # output
198+ below = 0
199+ elseif position == ' below' then
200+ above = 0
201+ below = # output
202+ else
203+ assert (node :height () == 1 , ' invalid center height' )
204+ local center = math.floor (# output / 2 ) + 1
205+ above = center - 1
206+ below = # output - center
207+ self .marks :over (self .config , true , node , {
208+ virt_text = { { output [center ], self .config .highlight } },
209+ virt_text_pos = ' inline' ,
210+ conceal = ' ' ,
211+ })
212+ end
193213
194- local above --- @type integer
195- local below --- @type integer
196- if position == ' above' then
197- above = # output
198- below = 0
199- elseif position == ' below' then
200- above = 0
201- below = # output
202- else
203- assert (node :height () == 1 , ' invalid center height' )
204- local center = math.floor (# output / 2 ) + 1
205- above = center - 1
206- below = # output - center
207- self .marks :over (self .config , true , node , {
208- virt_text = { { output [center ], self .config .highlight } },
209- virt_text_pos = ' inline' ,
210- conceal = ' ' ,
211- })
212- end
214+ -- fill in new lines at top and bottom
215+ while # lines_above < above do
216+ table.insert (lines_above , 1 , str .pad (current ))
217+ end
218+ while # lines_below < below do
219+ lines_below [# lines_below + 1 ] = str .pad (current )
220+ end
213221
214- -- fill in new lines at top and bottom
215- while # lines_above < above do
216- table.insert (lines_above , 1 , str .pad (current ))
217- end
218- while # lines_below < below do
219- lines_below [# lines_below + 1 ] = str .pad (current )
220- end
222+ -- concatenate output onto lines
223+ for i , line in ipairs (lines_above ) do
224+ local index = i - (# lines_above - above )
225+ local body = output [index ] or str .pad (width )
226+ lines_above [i ] = line .. str .pad (prefix ) .. body
227+ end
228+ for i , line in ipairs (lines_below ) do
229+ local index = i + (# output - below )
230+ local body = output [index ] or str .pad (width )
231+ lines_below [i ] = line .. str .pad (prefix ) .. body
232+ end
221233
222- -- concatenate output onto lines
223- for i , line in ipairs (lines_above ) do
224- local index = i - (# lines_above - above )
225- local body = output [index ] or str .pad (width )
226- lines_above [i ] = line .. str .pad (prefix ) .. body
234+ -- update current width of lines
235+ current = current + prefix + width
227236 end
228- for i , line in ipairs (lines_below ) do
229- local index = i + (# output - below )
230- local body = output [index ] or str .pad (width )
231- lines_below [i ] = line .. str .pad (prefix ) .. body
232- end
233-
234- -- update current width of lines
235- current = current + prefix + width
236237 end
237238
238239 --- @param lines string[]
0 commit comments