Skip to content

Commit 5b01324

Browse files
fix: top & bottom padding for latex
## Details The previous change to latex rendering did not correctly handle all the edge cases around how a formula could be rendered. In particular if we've added a line above and inlined a formula then padding below should insert a virtual line below, the current implementation added a gap to the bottom of the line above. The fix is pretty simple, add padding to the output directly, rather than trying to capture all the possible conditions and manipulating the final virtual lines. Added unit tests to validate this new behavior, as well as behavior introduced in the previous change. Minor refactor to test directory, store tests that use demo files as input in a subdirectory, avoids some clutter.
1 parent 9a746ff commit 5b01324

18 files changed

+216
-74
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
- remove delimiters around latex text before converting [2c6cf12](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/2c6cf127c577712bd29d38f6391b3045c5f0180a)
88
- convert latex asynchronously [44cbac6](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/44cbac697e50ae32e4644652da08cf692b9a5a57)
9+
- combine latex virtual lines use center positioning [#520](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/520)
10+
[9a746ff](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/9a746ff62a482f5eb722b3bfc7eefb2e9d638858)
911

1012
### Bug Fixes
1113

lua/render-markdown/handler/latex.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ function Handler:render(row, nodes)
141141
for _, node in ipairs(nodes) do
142142
local output = str.split(Handler.cache[Handler.text(node)], '\n', true)
143143

144+
-- add top and bottom padding around output
145+
for _ = 1, self.config.top_pad do
146+
table.insert(output, 1, '')
147+
end
148+
for _ = 1, self.config.bottom_pad do
149+
output[#output + 1] = ''
150+
end
151+
144152
-- pad lines to the same width
145153
local width = vim.fn.max(iter.list.map(output, str.width))
146154
for i, line in ipairs(output) do
@@ -218,19 +226,11 @@ function Handler:render(row, nodes)
218226
end
219227

220228
---@param lines string[]
221-
---@param top boolean
222-
---@param bottom boolean
223229
---@param above boolean
224-
local function add_lines(lines, top, bottom, above)
230+
local function add_lines(lines, above)
225231
if #lines == 0 then
226232
return
227233
end
228-
for _ = 1, (top and self.config.top_pad or 0) do
229-
table.insert(lines, 1, '')
230-
end
231-
for _ = 1, (bottom and self.config.bottom_pad or 0) do
232-
lines[#lines + 1] = ''
233-
end
234234
self.marks:add(self.config, 'virtual_lines', row, 0, {
235235
virt_lines = iter.list.map(lines, function(line)
236236
return indent:copy():text(line, self.config.highlight):get()
@@ -239,8 +239,8 @@ function Handler:render(row, nodes)
239239
})
240240
end
241241

242-
add_lines(lines_above, true, #lines_below == 0, true)
243-
add_lines(lines_below, #lines_above == 0, true, false)
242+
add_lines(lines_above, true)
243+
add_lines(lines_below, false)
244244
end
245245

246246
---@private

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local state = require('render-markdown.state')
55
local M = {}
66

77
---@private
8-
M.version = '8.8.6'
8+
M.version = '8.8.7'
99

1010
function M.check()
1111
M.start('versions')

tests/conceal_level_spec.lua

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
local util = require('tests.util')
44

5-
describe('table.md conceallevel', function()
6-
local lines = {
7-
'',
8-
'| Heading 1 | `Heading 2` |',
9-
'| --------- | ---------------------: |',
10-
'| `Item 行` | [link](https://行.com) |',
11-
'| <1> | ==Itém 2== |',
12-
'',
13-
'| Heading 1 | Heading 2 |',
14-
'| --------- | --------- |',
15-
'| Item 1 | Item 2 |',
16-
}
5+
local lines = {
6+
'',
7+
'| Heading 1 | `Heading 2` |',
8+
'| --------- | ---------------------: |',
9+
'| `Item 行` | [link](https://行.com) |',
10+
'| <1> | ==Itém 2== |',
11+
'',
12+
'| Heading 1 | Heading 2 |',
13+
'| --------- | --------- |',
14+
'| Item 1 | Item 2 |',
15+
}
1716

17+
describe('table conceallevel', function()
1818
it('0', function()
1919
util.setup.text(lines, {
2020
win_options = { conceallevel = { rendered = 0 } },

tests/custom_handler_spec.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
local util = require('tests.util')
44

5+
local lines = {
6+
'`Inline` code',
7+
'\\$1.50 \\$3.55',
8+
}
9+
510
---@param ctx render.md.handler.Context
611
---@return render.md.Mark[]
712
local function conceal_escape(ctx)
@@ -23,11 +28,6 @@ local function conceal_escape(ctx)
2328
end
2429

2530
describe('custom handler', function()
26-
local lines = {
27-
'`Inline` code',
28-
'\\$1.50 \\$3.55',
29-
}
30-
3131
it('default', function()
3232
util.setup.text(lines)
3333
-- inline code + no backslash escapes

tests/box_dash_quote_spec.lua renamed to tests/demo/box_dash_quote_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
local util = require('tests.util')
44

5-
describe('box_dash_quote.md', function()
5+
describe('demo/box_dash_quote.md', function()
66
it('default', function()
77
util.setup.file('demo/box_dash_quote.md')
88

tests/callout_spec.lua renamed to tests/demo/callout_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
local util = require('tests.util')
44

5-
describe('callout.md', function()
5+
describe('demo/callout.md', function()
66
it('default', function()
77
util.setup.file('demo/callout.md')
88

tests/heading_code_spec.lua renamed to tests/demo/heading_code_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
local util = require('tests.util')
44

5-
describe('heading_code.md', function()
5+
describe('demo/heading_code.md', function()
66
it('default', function()
77
util.setup.file('demo/heading_code.md')
88

tests/latex_spec.lua renamed to tests/demo/latex_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ local function join(lines)
88
return table.concat(lines, '\n')
99
end
1010

11-
describe('latex.md', function()
11+
describe('demo/latex.md', function()
1212
it('default', function()
1313
local inline = {
1414
raw = '\\sqrt{3x-1}+(1+x)^2',

tests/list_table_spec.lua renamed to tests/demo/list_table_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ local function shared()
6262
return marks
6363
end
6464

65-
describe('list_table.md', function()
65+
describe('demo/list_table.md', function()
6666
it('default', function()
6767
util.setup.file('demo/list_table.md')
6868

0 commit comments

Comments
 (0)