Skip to content

Commit b2da013

Browse files
Allow table boarder characters to be configured
1 parent 308f9a8 commit b2da013

File tree

6 files changed

+77
-49
lines changed

6 files changed

+77
-49
lines changed

Diff for: README.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,16 @@ require('render-markdown').setup({
247247
-- overlay: writes completely over the table, removing conceal behavior and highlights
248248
-- raw: replaces only the '|' characters in each row, leaving the cells completely unmodified
249249
cell = 'overlay',
250-
-- Highlight for table heading, delimitter, and the line above
250+
-- Characters used to replace table boarder
251+
-- Correspond to top(3), delimiter(3), bottom(3), vertical, & horizontal
252+
-- stylua: ignore
253+
boarder = {
254+
'', '', '',
255+
'', '', '',
256+
'', '', '',
257+
'', '',
258+
},
259+
-- Highlight for table heading, delimiter, and the line above
251260
head = '@markup.heading',
252261
-- Highlight for everything else, main table rows and the line below
253262
row = 'Normal',
@@ -425,7 +434,16 @@ require('render-markdown').setup({
425434
-- overlay: writes completely over the table, removing conceal behavior and highlights
426435
-- raw: replaces only the '|' characters in each row, leaving the cells completely unmodified
427436
cell = 'overlay',
428-
-- Highlight for table heading, delimitter, and the line above
437+
-- Characters used to replace table boarder
438+
-- Correspond to top(3), delimiter(3), bottom(3), vertical, & horizontal
439+
-- stylua: ignore
440+
boarder = {
441+
'', '', '',
442+
'', '', '',
443+
'', '', '',
444+
'', '',
445+
},
446+
-- Highlight for table heading, delimiter, and the line above
429447
head = '@markup.heading',
430448
-- Highlight for everything else, main table rows and the line below
431449
row = 'Normal',

Diff for: doc/render-markdown.txt

+20-2
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,16 @@ Full Default Configuration ~
283283
-- overlay: writes completely over the table, removing conceal behavior and highlights
284284
-- raw: replaces only the '|' characters in each row, leaving the cells completely unmodified
285285
cell = 'overlay',
286-
-- Highlight for table heading, delimitter, and the line above
286+
-- Characters used to replace table boarder
287+
-- Correspond to top(3), delimiter(3), bottom(3), vertical, & horizontal
288+
-- stylua: ignore
289+
boarder = {
290+
'┌', '┬', '┐',
291+
'├', '┼', '┤',
292+
'└', '┴', '┘',
293+
'│', '─',
294+
},
295+
-- Highlight for table heading, delimiter, and the line above
287296
head = '@markup.heading',
288297
-- Highlight for everything else, main table rows and the line below
289298
row = 'Normal',
@@ -466,7 +475,16 @@ TABLES *render-markdown-setup-tables*
466475
-- overlay: writes completely over the table, removing conceal behavior and highlights
467476
-- raw: replaces only the '|' characters in each row, leaving the cells completely unmodified
468477
cell = 'overlay',
469-
-- Highlight for table heading, delimitter, and the line above
478+
-- Characters used to replace table boarder
479+
-- Correspond to top(3), delimiter(3), bottom(3), vertical, & horizontal
480+
-- stylua: ignore
481+
boarder = {
482+
'┌', '┬', '┐',
483+
'├', '┼', '┤',
484+
'└', '┴', '┘',
485+
'│', '─',
486+
},
487+
-- Highlight for table heading, delimiter, and the line above
470488
head = '@markup.heading',
471489
-- Highlight for everything else, main table rows and the line below
472490
row = 'Normal',

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

+22-36
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,27 @@ M.render_node = function(namespace, buf, capture, node)
4040
local background = list.clamp(heading.backgrounds, level)
4141
local foreground = list.clamp(heading.foregrounds, level)
4242

43-
local heading_text = { str.pad(icon, padding), { foreground, background } }
4443
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, 0, {
4544
end_row = end_row + 1,
4645
end_col = 0,
4746
hl_group = background,
48-
virt_text = { heading_text },
47+
virt_text = { { str.pad(icon, padding), { foreground, background } } },
4948
virt_text_pos = 'overlay',
5049
hl_eol = true,
5150
})
5251
elseif capture == 'dash' then
5352
local dash = state.config.dash
5453
local width = vim.api.nvim_win_get_width(util.buf_to_win(buf))
5554

56-
local dash_text = { dash.icon:rep(width), dash.highlight }
5755
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, 0, {
58-
virt_text = { dash_text },
56+
virt_text = { { dash.icon:rep(width), dash.highlight } },
5957
virt_text_pos = 'overlay',
6058
})
6159
elseif capture == 'code' then
6260
local code = state.config.code
6361
if not vim.tbl_contains({ 'normal', 'full' }, code.style) then
6462
return
6563
end
66-
6764
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, 0, {
6865
end_row = end_row,
6966
end_col = 0,
@@ -79,15 +76,12 @@ M.render_node = function(namespace, buf, capture, node)
7976
if not util.has_10 then
8077
return
8178
end
82-
8379
local icon, icon_highlight = icons.get(value)
8480
if icon == nil or icon_highlight == nil then
8581
return
8682
end
87-
88-
local icon_text = { icon .. ' ' .. value, { icon_highlight, code.highlight } }
8983
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
90-
virt_text = { icon_text },
84+
virt_text = { { icon .. ' ' .. value, { icon_highlight, code.highlight } } },
9185
virt_text_pos = 'inline',
9286
})
9387
elseif capture == 'list_marker' then
@@ -107,11 +101,10 @@ M.render_node = function(namespace, buf, capture, node)
107101
local level = ts.level_in_section(node, 'list')
108102
local icon = list.cycle(bullet.icons, level)
109103

110-
local list_marker_text = { str.pad(icon, leading_spaces), bullet.highlight }
111104
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
112105
end_row = end_row,
113106
end_col = end_col,
114-
virt_text = { list_marker_text },
107+
virt_text = { { str.pad(icon, leading_spaces), bullet.highlight } },
115108
virt_text_pos = 'overlay',
116109
})
117110
end
@@ -130,32 +123,29 @@ M.render_node = function(namespace, buf, capture, node)
130123
highlight = callout.highlight
131124
end
132125
end
133-
134-
local quote_marker_text = { value:gsub('>', quote.icon), highlight }
135126
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
136127
end_row = end_row,
137128
end_col = end_col,
138-
virt_text = { quote_marker_text },
129+
virt_text = { { value:gsub('>', quote.icon), highlight } },
139130
virt_text_pos = 'overlay',
140131
})
141132
elseif vim.tbl_contains({ 'checkbox_unchecked', 'checkbox_checked' }, capture) then
142133
local checkbox = state.config.checkbox.unchecked
143134
if capture == 'checkbox_checked' then
144135
checkbox = state.config.checkbox.checked
145136
end
146-
147-
local checkbox_text = { str.pad_to(value, checkbox.icon), checkbox.highlight }
148137
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
149138
end_row = end_row,
150139
end_col = end_col,
151-
virt_text = { checkbox_text },
140+
virt_text = { { str.pad_to(value, checkbox.icon), checkbox.highlight } },
152141
virt_text_pos = 'overlay',
153142
})
154143
elseif capture == 'table' then
155144
local pipe_table = state.config.pipe_table
156145
if pipe_table.style ~= 'full' then
157146
return
158147
end
148+
local boarder = pipe_table.boarder
159149

160150
---@param row integer
161151
---@param s string
@@ -183,26 +173,27 @@ M.render_node = function(namespace, buf, capture, node)
183173
if delim_width == start_width and start_width == end_width then
184174
local headings = vim.split(delim_value, '|', { plain = true, trimempty = true })
185175
local lengths = vim.tbl_map(function(part)
186-
return string.rep('', vim.fn.strdisplaywidth(part))
176+
return boarder[11]:rep(vim.fn.strdisplaywidth(part))
187177
end, headings)
188178

189-
local line_above = { { '' .. table.concat(lengths, '') .. '', pipe_table.head } }
179+
local line_above = boarder[1] .. table.concat(lengths, boarder[2]) .. boarder[3]
190180
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
191181
virt_lines_above = true,
192-
virt_lines = { line_above },
182+
virt_lines = { { { line_above, pipe_table.head } } },
193183
})
194184

195-
local line_below = { { '' .. table.concat(lengths, '') .. '', pipe_table.row } }
185+
local line_below = boarder[7] .. table.concat(lengths, boarder[8]) .. boarder[9]
196186
vim.api.nvim_buf_set_extmark(buf, namespace, end_row, start_col, {
197187
virt_lines_above = true,
198-
virt_lines = { line_below },
188+
virt_lines = { { { line_below, pipe_table.row } } },
199189
})
200190
end
201191
elseif vim.tbl_contains({ 'table_head', 'table_delim', 'table_row' }, capture) then
202192
local pipe_table = state.config.pipe_table
203193
if pipe_table.style == 'none' then
204194
return
205195
end
196+
local boarder = pipe_table.boarder
206197

207198
local highlight = pipe_table.head
208199
if capture == 'table_row' then
@@ -212,37 +203,32 @@ M.render_node = function(namespace, buf, capture, node)
212203
if capture == 'table_delim' then
213204
-- Order matters here, in particular handling inner intersections before left & right
214205
local row = value
215-
:gsub('|', '')
216-
:gsub('-', '')
217-
:gsub(' ', '')
218-
:gsub('─│─', '─┼─')
219-
:gsub('│─', '├─')
220-
:gsub('─│', '─┤')
206+
:gsub(' ', '-')
207+
:gsub('%-|%-', boarder[11] .. boarder[5] .. boarder[11])
208+
:gsub('|%-', boarder[4] .. boarder[11])
209+
:gsub('%-|', boarder[11] .. boarder[6])
210+
:gsub('%-', boarder[11])
221211

222-
local table_delim_text = { row, highlight }
223212
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
224213
end_row = end_row,
225214
end_col = end_col,
226-
virt_text = { table_delim_text },
215+
virt_text = { { row, highlight } },
227216
virt_text_pos = 'overlay',
228217
})
229218
elseif pipe_table.cell == 'overlay' then
230-
local table_row_text = { value:gsub('|', ''), highlight }
231219
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
232220
end_row = end_row,
233221
end_col = end_col,
234-
virt_text = { table_row_text },
222+
virt_text = { { value:gsub('|', boarder[10]), highlight } },
235223
virt_text_pos = 'overlay',
236224
})
237225
elseif pipe_table.cell == 'raw' then
238226
for i = 1, #value do
239-
local ch = value:sub(i, i)
240-
if ch == '|' then
241-
local table_pipe_text = { '', highlight }
227+
if value:sub(i, i) == '|' then
242228
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, i - 1, {
243229
end_row = end_row,
244230
end_col = i - 1,
245-
virt_text = { table_pipe_text },
231+
virt_text = { { boarder[10], highlight } },
246232
virt_text_pos = 'overlay',
247233
})
248234
end

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

+3-8
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,33 @@ M.render_node = function(namespace, buf, capture, node)
2626
logger.debug_node(capture, node, buf)
2727

2828
if capture == 'code' then
29-
local code = state.config.code
3029
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
3130
end_row = end_row,
3231
end_col = end_col,
33-
hl_group = code.highlight,
32+
hl_group = state.config.code.highlight,
3433
})
3534
elseif capture == 'callout' then
3635
local callout = component.callout(value, 'exact')
3736
if callout ~= nil then
38-
local callout_text = { callout.text, callout.highlight }
3937
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
4038
end_row = end_row,
4139
end_col = end_col,
42-
virt_text = { callout_text },
40+
virt_text = { { callout.text, callout.highlight } },
4341
virt_text_pos = 'overlay',
4442
})
4543
else
4644
-- Requires inline extmarks
4745
if not util.has_10 then
4846
return
4947
end
50-
5148
local checkbox = component.checkbox(value, 'exact')
5249
if checkbox == nil then
5350
return
5451
end
55-
56-
local checkbox_text = { str.pad_to(value, checkbox.text), checkbox.highlight }
5752
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
5853
end_row = end_row,
5954
end_col = end_col,
60-
virt_text = { checkbox_text },
55+
virt_text = { { str.pad_to(value, checkbox.text), checkbox.highlight } },
6156
virt_text_pos = 'inline',
6257
conceal = '',
6358
})

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ local M = {}
1414
---@class render.md.UserPipeTable
1515
---@field public style? 'full'|'normal'|'none'
1616
---@field public cell? 'overlay'|'raw'
17+
---@field public boarder? string[]
1718
---@field public head? string
1819
---@field public row? string
1920

@@ -219,7 +220,16 @@ M.default_config = {
219220
-- overlay: writes completely over the table, removing conceal behavior and highlights
220221
-- raw: replaces only the '|' characters in each row, leaving the cells completely unmodified
221222
cell = 'overlay',
222-
-- Highlight for table heading, delimitter, and the line above
223+
-- Characters used to replace table boarder
224+
-- Correspond to top(3), delimiter(3), bottom(3), vertical, & horizontal
225+
-- stylua: ignore
226+
boarder = {
227+
'', '', '',
228+
'', '', '',
229+
'', '', '',
230+
'', '',
231+
},
232+
-- Highlight for table heading, delimiter, and the line above
223233
head = '@markup.heading',
224234
-- Highlight for everything else, main table rows and the line below
225235
row = 'Normal',

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---@class render.md.PipeTable
22
---@field public style 'full'|'normal'|'none'
33
---@field public cell 'overlay'|'raw'
4+
---@field public boarder string[]
45
---@field public head string
56
---@field public row string
67

0 commit comments

Comments
 (0)