Skip to content

Commit 0bbc03c

Browse files
feature: Support left padding code blocks
# Details Add 'left_pad' parameter to code configuration, default to 0. Left padding requires 0.10.0. Use a footnote to identify features gated on the 0.10.0 version. Minor bug fix to padded cell type, subtract one of end col.
1 parent 14a316a commit 0bbc03c

File tree

8 files changed

+50
-22
lines changed

8 files changed

+50
-22
lines changed

README.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,23 @@ Plugin to improve viewing Markdown files in Neovim
1919
- Headings: highlight depending on level and replaces `#` with icon
2020
- Horizontal breaks: replace with full-width lines
2121
- Code blocks: highlight to better stand out
22-
- Adds language icon, requires icon provider (`mini.icons` or `nvim-web-devicons`)
23-
and neovim >= `0.10.0`
22+
- Adds language icon [^1], requires icon provider (`mini.icons` or `nvim-web-devicons`)
23+
- Left pad lines within block [^1]
2424
- Inline code: highlight to better stand out
2525
- List bullet points: replace with provided icon based on level
2626
- Checkboxes: replace with provided icon based on whether they are checked
2727
- Block quotes: replace leading `>` with provided icon
2828
- Tables: replace border characters, does NOT automatically align
2929
- [Callouts](https://github.com/orgs/community/discussions/16925)
3030
- Github & Obsidian out of the box, supports user defined as well
31-
- Custom checkbox states, function similar to `callouts`
32-
- Adds icon before images / links, requires neovim >= `0.10.0`
31+
- Custom checkbox states [^1], function similar to `callouts`
32+
- Adds icon before images / links [^1]
3333
- `LaTeX` blocks: renders formulas if `latex` parser and `pylatexenc` are installed
3434
- Disable rendering when file is larger than provided value
3535
- Support custom handlers which are ran identically to builtin handlers
3636

37+
[^1]: Requires neovim >= `0.10.0`
38+
3739
# Requirements
3840

3941
- neovim `>= 0.9.0` (minimum) `>= 0.10.0` (recommended)
@@ -206,10 +208,12 @@ require('render-markdown').setup({
206208
sign = true,
207209
-- Determines how code blocks & inline code are rendered:
208210
-- none: disables all rendering
209-
-- normal: adds highlight group to code blocks & inline code
211+
-- normal: adds highlight group to code blocks & inline code, adds padding to code blocks
210212
-- language: adds language icon to sign column if enabled and icon + name above code blocks
211213
-- full: normal + language
212214
style = 'full',
215+
-- Amount of padding to add to the left of code blocks
216+
left_pad = 0,
213217
-- Determins how the top / bottom of code block are rendered:
214218
-- thick: use the same highlight as the code body
215219
-- thin: when lines are empty overlay the above & below icons
@@ -426,10 +430,12 @@ require('render-markdown').setup({
426430
sign = true,
427431
-- Determines how code blocks & inline code are rendered:
428432
-- none: disables all rendering
429-
-- normal: adds highlight group to code blocks & inline code
433+
-- normal: adds highlight group to code blocks & inline code, adds padding to code blocks
430434
-- language: adds language icon to sign column if enabled and icon + name above code blocks
431435
-- full: normal + language
432436
style = 'full',
437+
-- Amount of padding to add to the left of code blocks
438+
left_pad = 0,
433439
-- Determins how the top / bottom of code block are rendered:
434440
-- thick: use the same highlight as the code body
435441
-- thin: when lines are empty overlay the above & below icons

doc/render-markdown.txt

+10-6
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ Plugin to improve viewing Markdown files in Neovim
4848
- Headings: highlight depending on level and replaces `#` with icon
4949
- Horizontal breaks: replace with full-width lines
5050
- Code blocks: highlight to better stand out
51-
- Adds language icon, requires icon provider (`mini.icons` or `nvim-web-devicons`)
52-
and neovim >= `0.10.0`
51+
- Adds language icon , requires icon provider (`mini.icons` or `nvim-web-devicons`)
52+
- Left pad lines within block
5353
- Inline code: highlight to better stand out
5454
- List bullet points: replace with provided icon based on level
5555
- Checkboxes: replace with provided icon based on whether they are checked
5656
- Block quotes: replace leading `>` with provided icon
5757
- Tables: replace border characters, does NOT automatically align
5858
- Callouts <https://github.com/orgs/community/discussions/16925>
5959
- Github & Obsidian out of the box, supports user defined as well
60-
- Custom checkbox states, function similar to `callouts`
61-
- Adds icon before images / links, requires neovim >= `0.10.0`
60+
- Custom checkbox states , function similar to `callouts`
61+
- Adds icon before images / links
6262
- `LaTeX` blocks: renders formulas if `latex` parser and `pylatexenc` are installed
6363
- Disable rendering when file is larger than provided value
6464
- Support custom handlers which are ran identically to builtin handlers
@@ -244,10 +244,12 @@ Full Default Configuration ~
244244
sign = true,
245245
-- Determines how code blocks & inline code are rendered:
246246
-- none: disables all rendering
247-
-- normal: adds highlight group to code blocks & inline code
247+
-- normal: adds highlight group to code blocks & inline code, adds padding to code blocks
248248
-- language: adds language icon to sign column if enabled and icon + name above code blocks
249249
-- full: normal + language
250250
style = 'full',
251+
-- Amount of padding to add to the left of code blocks
252+
left_pad = 0,
251253
-- Determins how the top / bottom of code block are rendered:
252254
-- thick: use the same highlight as the code body
253255
-- thin: when lines are empty overlay the above & below icons
@@ -464,10 +466,12 @@ CODE BLOCKS *render-markdown-setup-code-blocks*
464466
sign = true,
465467
-- Determines how code blocks & inline code are rendered:
466468
-- none: disables all rendering
467-
-- normal: adds highlight group to code blocks & inline code
469+
-- normal: adds highlight group to code blocks & inline code, adds padding to code blocks
468470
-- language: adds language icon to sign column if enabled and icon + name above code blocks
469471
-- full: normal + language
470472
style = 'full',
473+
-- Amount of padding to add to the left of code blocks
474+
left_pad = 0,
471475
-- Determins how the top / bottom of code block are rendered:
472476
-- thick: use the same highlight as the code body
473477
-- thin: when lines are empty overlay the above & below icons

lua/render-markdown/handler/markdown.lua

+14-1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,19 @@ M.render_code = function(namespace, buf, info)
168168
hl_group = code.highlight,
169169
hl_eol = true,
170170
})
171+
-- Requires inline extmarks
172+
if not util.has_10 or code.left_pad <= 0 then
173+
return
174+
end
175+
for row = start_row, end_row - 1 do
176+
-- Uses a low priority so other marks are loaded first and included in padding
177+
vim.api.nvim_buf_set_extmark(buf, namespace, row, info.start_col, {
178+
end_row = row + 1,
179+
priority = 0,
180+
virt_text = { { str.pad('', code.left_pad), code.highlight } },
181+
virt_text_pos = 'inline',
182+
})
183+
end
171184
end
172185

173186
---@private
@@ -402,7 +415,7 @@ M.render_table_row = function(namespace, buf, row, highlight)
402415
if pipe_table.cell == 'padded' and util.has_10 then
403416
local offset = M.table_visual_offset(buf, cell)
404417
if offset > 0 then
405-
vim.api.nvim_buf_set_extmark(buf, namespace, cell.start_row, cell.end_col, {
418+
vim.api.nvim_buf_set_extmark(buf, namespace, cell.start_row, cell.end_col - 1, {
406419
virt_text = { { str.pad('', offset), pipe_table.filler } },
407420
virt_text_pos = 'inline',
408421
})

lua/render-markdown/init.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ local M = {}
6060
---@field public enabled? boolean
6161
---@field public sign? boolean
6262
---@field public style? 'full'|'normal'|'language'|'none'
63+
---@field public left_pad? integer
6364
---@field public border? 'thin'|'thick'
6465
---@field public above? string
6566
---@field public below? string
@@ -212,10 +213,12 @@ M.default_config = {
212213
sign = true,
213214
-- Determines how code blocks & inline code are rendered:
214215
-- none: disables all rendering
215-
-- normal: adds highlight group to code blocks & inline code
216+
-- normal: adds highlight group to code blocks & inline code, adds padding to code blocks
216217
-- language: adds language icon to sign column if enabled and icon + name above code blocks
217218
-- full: normal + language
218219
style = 'full',
220+
-- Amount of padding to add to the left of code blocks
221+
left_pad = 0,
219222
-- Determins how the top / bottom of code block are rendered:
220223
-- thick: use the same highlight as the code body
221224
-- thin: when lines are empty overlay the above & below icons

lua/render-markdown/state.lua

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ function state.validate()
113113
enabled = { code.enabled, 'boolean' },
114114
sign = { code.sign, 'boolean' },
115115
style = one_of(code.style, { 'full', 'normal', 'language', 'none' }),
116+
left_pad = { code.left_pad, 'number' },
116117
border = one_of(code.border, { 'thin', 'thick' }),
117118
above = { code.above, 'string' },
118119
below = { code.below, 'string' },

lua/render-markdown/types.lua

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
---@field public enabled boolean
5454
---@field public sign boolean
5555
---@field public style 'full'|'normal'|'language'|'none'
56+
---@field public left_pad integer
5657
---@field public border 'thin'|'thick'
5758
---@field public above string
5859
---@field public below string

tests/list_table_spec.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ async_tests.describe('list_table.md', function()
3131
util.table_pipe(19, 0, true), -- Heading pipe 1
3232
util.table_border(19, 'above', { 18, 20 }),
3333
util.inline_code(19, 2, 18), -- Inline code in heading
34-
util.table_padding(19, 19, 2), -- Heading padding 1
34+
util.table_padding(19, 18, 2), -- Heading padding 1
3535
util.table_pipe(19, 19, true), -- Heading pipe 2
36-
util.table_padding(19, 40, 2), -- Heading padding 2
36+
util.table_padding(19, 39, 2), -- Heading padding 2
3737
util.table_pipe(19, 40, true), -- Heading pipe 2
3838
util.table_border(20, 'delimiter', { 18, 20 }),
3939
util.table_pipe(21, 0, false), -- Row 1 pipe 1
4040
util.table_pipe(21, 19, false), -- Row 1 pipe 2
41-
util.table_padding(21, 40, 4), -- Row 1 padding 2
41+
util.table_padding(21, 39, 4), -- Row 1 padding 2
4242
util.table_pipe(21, 40, false), -- Row 1 pipe 3
4343
util.table_pipe(22, 0, false), -- Row 2 pipe 1
4444
util.table_border(22, 'below', { 18, 20 }),
4545
util.inline_code(22, 2, 15), -- Row 2 inline code
46-
util.table_padding(22, 19, 2), -- Row 2 padding 1
46+
util.table_padding(22, 18, 2), -- Row 2 padding 1
4747
util.table_pipe(22, 19, false), -- Row 2 pipe 2
4848
util.link(22, 21, 39, false), -- Row 2 link
49-
util.table_padding(22, 40, 7), -- Row 2 padding 2
49+
util.table_padding(22, 39, 7), -- Row 2 padding 2
5050
util.table_pipe(22, 40, false), -- Row 2 pipe 3
5151
})
5252

tests/table_spec.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ async_tests.describe('table.md', function()
1414
util.table_border(2, 'above', { 11, 24 }),
1515
util.table_pipe(2, 12, true), -- Heading pipe 2
1616
util.inline_code(2, 14, 25), -- Inline code in heading
17-
util.table_padding(2, 37, 2), -- Heading padding 2
17+
util.table_padding(2, 36, 2), -- Heading padding 2
1818
util.table_pipe(2, 37, true), -- Heading pipe 3
1919
util.table_border(3, 'delimiter', { 11, 24 }),
2020
util.table_pipe(4, 0, false), -- Row pipe 1
2121
util.table_border(4, 'below', { 11, 24 }),
2222
util.inline_code(4, 2, 12), -- Row inline code
23-
util.table_padding(4, 13, 2), -- Row padding 1
23+
util.table_padding(4, 12, 2), -- Row padding 1
2424
util.table_pipe(4, 13, false), -- Row pipe 2
2525
util.link(4, 15, 38, false), -- Row link
26-
util.table_padding(4, 39, 16), -- Row padding 2
26+
util.table_padding(4, 38, 16), -- Row padding 2
2727
util.table_pipe(4, 39, false), -- Row pipe 3
2828
})
2929

0 commit comments

Comments
 (0)