Skip to content

Commit 695501b

Browse files
feat: Improve LaTeX compatibility for more advanced converters
## Details The current LaTeX parsing logic trims each line to remove spaces. This is okay for simple converters, but multi-line converters will use spaces to position components. Instead of removing the whitespace around each line, use the trimempty option to remove leading and trailing fully empty lines only. From there each line is left as returned from the converter. Add `latex.top_pad` and `latex.bottom_pad` options to allow users to specify how much spacing they want around the rendered LaTeX blocks. Co-Authored-By: joshuarayton <[email protected]>
1 parent d2b5393 commit 695501b

File tree

7 files changed

+26
-3
lines changed

7 files changed

+26
-3
lines changed

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ require('render-markdown').setup({
184184
converter = 'latex2text',
185185
-- Highlight for LaTeX blocks
186186
highlight = 'RenderMarkdownMath',
187+
-- Amount of empty lines above LaTeX blocks
188+
top_pad = 0,
189+
-- Amount of empty lines below LaTeX blocks
190+
bottom_pad = 0,
187191
},
188192
heading = {
189193
-- Turn on / off heading icon & background rendering

Diff for: doc/render-markdown.txt

+4
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ Full Default Configuration ~
225225
converter = 'latex2text',
226226
-- Highlight for LaTeX blocks
227227
highlight = 'RenderMarkdownMath',
228+
-- Amount of empty lines above LaTeX blocks
229+
top_pad = 0,
230+
-- Amount of empty lines below LaTeX blocks
231+
bottom_pad = 0,
228232
},
229233
heading = {
230234
-- Turn on / off heading icon & background rendering

Diff for: lua/render-markdown/handler/latex.lua

+7-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ function M.parse(root, buf)
3232
local expressions = cache.expressions[info.text]
3333
if expressions == nil then
3434
local raw_expression = vim.fn.system(latex.converter, info.text)
35-
local parsed_expressions = vim.split(vim.trim(raw_expression), '\n', { plain = true })
36-
expressions = vim.tbl_map(vim.trim, parsed_expressions)
35+
expressions = vim.split(raw_expression, '\n', { plain = true, trimempty = true })
36+
for _ = 1, latex.top_pad do
37+
table.insert(expressions, 1, '')
38+
end
39+
for _ = 1, latex.bottom_pad do
40+
table.insert(expressions, '')
41+
end
3742
cache.expressions[info.text] = expressions
3843
end
3944

Diff for: lua/render-markdown/init.lua

+6
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ local M = {}
9696
---@field public enabled? boolean
9797
---@field public converter? string
9898
---@field public highlight? string
99+
---@field public top_pad? integer
100+
---@field public bottom_pad? integer
99101

100102
---@class render.md.UserAntiConceal
101103
---@field public enabled? boolean
@@ -208,6 +210,10 @@ M.default_config = {
208210
converter = 'latex2text',
209211
-- Highlight for LaTeX blocks
210212
highlight = 'RenderMarkdownMath',
213+
-- Amount of empty lines above LaTeX blocks
214+
top_pad = 0,
215+
-- Amount of empty lines below LaTeX blocks
216+
bottom_pad = 0,
211217
},
212218
heading = {
213219
-- Turn on / off heading icon & background rendering

Diff for: lua/render-markdown/state.lua

+2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ function state.validate()
117117
enabled = { latex.enabled, 'boolean' },
118118
converter = { latex.converter, 'string' },
119119
highlight = { latex.highlight, 'string' },
120+
top_pad = { latex.top_pad, 'number' },
121+
bottom_pad = { latex.bottom_pad, 'number' },
120122
})
121123

122124
local heading = config.heading

Diff for: lua/render-markdown/types.lua

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181
---@field public enabled boolean
8282
---@field public converter string
8383
---@field public highlight string
84+
---@field public top_pad integer
85+
---@field public bottom_pad integer
8486

8587
---@class render.md.AntiConceal
8688
---@field public enabled boolean

Diff for: tests/latex_spec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async_tests.describe('latex.md', function()
4646
-- Inline
4747
latex(2, 2, 0, 21, { '√(3x-1)+(1+x)^2' }),
4848
-- Block
49-
latex(4, 7, 0, 2, { 'f(x,y) = x + √(y)', 'f(x,y) = √(y) + x^2/4y' }),
49+
latex(4, 7, 0, 2, { ' f(x,y) = x + √(y)', ' f(x,y) = √(y) + x^2/4y' }),
5050
})
5151

5252
local actual = util.get_actual_marks()

0 commit comments

Comments
 (0)