Skip to content

Commit 43a971e

Browse files
feat: latex position below
## Details Request: #347 Adds `position` option to `latex` configuration which supports 2 values, `above` or `below`. The default value is set to `above` and results in no change in behavior from before. Setting it `below` will instead render the virtual lines below the block but otherwise they are rendered in the same way. ```lua require('render-markdown').setup({ latex = { position = 'below' }, }) ```
1 parent 0df6719 commit 43a971e

File tree

9 files changed

+75
-36
lines changed

9 files changed

+75
-36
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
[5c2440d](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/5c2440d932a4ba96840e5ce5a7bd40f0624bdaa2)
99
- allow html tags to be replaced with icons [#336](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/336)
1010
[6d446de](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/6d446de33937633bc2104f45c943f4fae632b822)
11+
- `autocommand` events and wiki link `body` customization [#228](https://github.com/MeanderingProgrammer/render-markdown.nvim/discussions/228)
12+
[#345](https://github.com/MeanderingProgrammer/render-markdown.nvim/pull/345) [0df6719](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/0df6719abc3b547bc5b7111a750d8d7e035a7234)
1113
- use default `render_modes = true` (all) for LSP docs [#326](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/326)
1214
[17a7746](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/17a77463f945c4b9e4f371c752efd90e3e1bf604)
1315
- update troubleshooting doc [f6c9e18](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/f6c9e1841cf644a258eb037dae587e3cf407d696)
@@ -21,6 +23,7 @@
2123
### Collaborator Shoutouts
2224

2325
- @dsully
26+
- @mcDevnagh
2427

2528
## 8.0.0 (2025-02-06)
2629

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ require('render-markdown').setup({
246246
converter = 'latex2text',
247247
-- Highlight for LaTeX blocks
248248
highlight = 'RenderMarkdownMath',
249+
-- Determines where latex formula is rendered relative to block:
250+
-- above: above latex block
251+
-- below: below latex block
252+
position = 'above',
249253
-- Amount of empty lines above LaTeX blocks
250254
top_pad = 0,
251255
-- Amount of empty lines below LaTeX blocks

doc/render-markdown.txt

+4
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ Default Configuration ~
307307
converter = 'latex2text',
308308
-- Highlight for LaTeX blocks
309309
highlight = 'RenderMarkdownMath',
310+
-- Determines where latex formula is rendered relative to block:
311+
-- above: above latex block
312+
-- below: below latex block
313+
position = 'above',
310314
-- Amount of empty lines above LaTeX blocks
311315
top_pad = 0,
312316
-- Amount of empty lines below LaTeX blocks

lua/render-markdown/handler/latex.lua

+5-2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ function M.parse(ctx)
5252
return { { expression, latex.highlight } }
5353
end)
5454

55+
local above = latex.position == 'above'
56+
local row = above and node.start_row or node.end_row
57+
5558
local marks = List.new_marks(ctx.buf, true)
56-
marks:add_over(false, node, {
59+
marks:add(false, row, 0, {
5760
virt_lines = latex_lines,
58-
virt_lines_above = true,
61+
virt_lines_above = above,
5962
})
6063
return marks:get()
6164
end

lua/render-markdown/health.lua

+1-1
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.0.7'
8+
M.version = '8.0.8'
99

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

lua/render-markdown/init.lua

+7
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ local M = {}
4949
---@field public comment? render.md.UserHtmlComment
5050
---@field public tag? table<string, render.md.HtmlTag>
5151

52+
---@alias render.md.latex.Position 'above'|'below'
53+
5254
---@class (exact) render.md.UserLatex: render.md.UserBaseComponent
5355
---@field public converter? string
5456
---@field public highlight? string
57+
---@field public position? render.md.latex.Position
5558
---@field public top_pad? integer
5659
---@field public bottom_pad? integer
5760

@@ -377,6 +380,10 @@ M.default_config = {
377380
converter = 'latex2text',
378381
-- Highlight for LaTeX blocks
379382
highlight = 'RenderMarkdownMath',
383+
-- Determines where latex formula is rendered relative to block:
384+
-- above: above latex block
385+
-- below: below latex block
386+
position = 'above',
380387
-- Amount of empty lines above LaTeX blocks
381388
top_pad = 0,
382389
-- Amount of empty lines below LaTeX blocks

lua/render-markdown/state.lua

+1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ function M.validate()
291291
component_rules(latex)
292292
:type({ 'top_pad', 'bottom_pad' }, 'number')
293293
:type({ 'converter', 'highlight' }, 'string')
294+
:one_of('position', { 'above', 'below' })
294295
:check()
295296
end)
296297
:nested('html', function(html)

lua/render-markdown/types.lua

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
---@class (exact) render.md.Latex: render.md.BaseComponent
2929
---@field public converter string
3030
---@field public highlight string
31+
---@field public position render.md.latex.Position
3132
---@field public top_pad integer
3233
---@field public bottom_pad integer
3334

tests/latex_spec.lua

+49-33
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,67 @@
33
local stub = require('luassert.stub')
44
local util = require('tests.util')
55

6-
---@param start_row integer
7-
---@param end_row integer
8-
---@param col integer
6+
---@param converter string
7+
---@param responses table<string, string>
8+
local function set_responses(converter, responses)
9+
stub.new(vim.fn, 'executable', function(expr)
10+
assert.are.same(converter, expr)
11+
return 1
12+
end)
13+
stub.new(vim.fn, 'system', function(cmd, input)
14+
assert.are.same(converter, cmd)
15+
local result = responses[input]
16+
assert.truthy(result, 'No output for: ' .. input)
17+
return result
18+
end)
19+
end
20+
21+
---@param row integer
922
---@param lines string[]
1023
---@return render.md.MarkInfo
11-
local function latex(start_row, end_row, col, lines)
24+
local function latex(row, lines)
1225
local virt_lines = vim.iter(lines)
1326
:map(function(line)
1427
return { { line, util.hl('Math') } }
1528
end)
1629
:totable()
1730
---@type render.md.MarkInfo
1831
return {
19-
row = { start_row, end_row },
20-
col = { 0, col },
32+
row = { row },
33+
col = { 0 },
2134
virt_lines = virt_lines,
2235
virt_lines_above = true,
2336
}
2437
end
2538

26-
---@param converter string
27-
---@param responses table<string, string>
28-
local function set_responses(converter, responses)
29-
stub.new(vim.fn, 'executable', function(expr)
30-
assert.are.same(converter, expr)
31-
return 1
32-
end)
33-
stub.new(vim.fn, 'system', function(cmd, input)
34-
assert.are.same(converter, cmd)
35-
local result = responses[input]
36-
assert.truthy(result, 'No output for: ' .. input)
37-
return result
38-
end)
39+
---@param lines string[]
40+
---@param prefix string
41+
---@param suffix string
42+
---@return string
43+
local function text(lines, prefix, suffix)
44+
return prefix .. table.concat(lines, '\n') .. suffix
3945
end
4046

4147
describe('latex.md', function()
4248
it('default', function()
43-
local in_inline = '$\\sqrt{3x-1}+(1+x)^2$'
44-
local out_inline = '√(3x-1)+(1+x)^2'
45-
local in_block = { 'f(x,y) = x + \\sqrt{y}', 'f(x,y) = \\sqrt{y} + \\frac{x^2}{4y}' }
46-
local out_block = { ' f(x,y) = x + √(y)', ' f(x,y) = √(y) + x^2/4y' }
49+
local inline = {
50+
raw = { '$\\sqrt{3x-1}+(1+x)^2$' },
51+
out = { '√(3x-1)+(1+x)^2' },
52+
}
53+
local block = {
54+
raw = {
55+
'f(x,y) = x + \\sqrt{y}',
56+
'f(x,y) = \\sqrt{y} + \\frac{x^2}{4y}',
57+
},
58+
out = {
59+
' f(x,y) = x + √(y)',
60+
' f(x,y) = √(y) + x^2/4y',
61+
},
62+
}
4763

4864
set_responses('latex2text', {
49-
[in_inline] = out_inline .. '\n',
50-
['$$\n' .. table.concat(in_block, '\n') .. '\n$$'] = '\n' .. table.concat(out_block, '\n') .. '\n\n',
65+
[text(inline.raw, '', '')] = text(inline.out, '', '\n'),
66+
[text(block.raw, '$$\n', '\n$$')] = text(block.out, '\n', '\n\n'),
5167
})
5268
util.setup('demo/latex.md')
5369

@@ -56,21 +72,21 @@ describe('latex.md', function()
5672
vim.list_extend(expected, util.heading(row:get(), 1))
5773

5874
vim.list_extend(expected, {
59-
latex(row:increment(2), 2, 21, { out_inline }),
60-
latex(row:increment(2), 7, 2, out_block),
75+
latex(row:increment(2), inline.out),
76+
latex(row:increment(2), block.out),
6177
})
6278

6379
util.assert_view(expected, {
6480
'󰫎 1 󰲡 LaTeX',
6581
' 2',
66-
' ' .. out_inline,
67-
' 3 ' .. in_inline,
82+
' ' .. inline.out[1],
83+
' 3 ' .. inline.raw[1],
6884
' 4',
69-
' ' .. out_block[1],
70-
' ' .. out_block[2],
85+
' ' .. block.out[1],
86+
' ' .. block.out[2],
7187
' 5 $$',
72-
' 6 ' .. in_block[1],
73-
' 7 ' .. in_block[2],
88+
' 6 ' .. block.raw[1],
89+
' 7 ' .. block.raw[2],
7490
' 8 $$',
7591
})
7692
end)

0 commit comments

Comments
 (0)