Skip to content

Commit fe2ebe7

Browse files
Add support for rendering tables
1 parent 180e23e commit fe2ebe7

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

README.md

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

33
Plugin to improve viewing Markdown files in Neovim
44

5+
Plugin is experimental at this time
6+
57
# Features
68

79
- Changes between `rendered` view in normal mode (exact modes are configurable)
810
and raw view in all other modes
911
- Highlights headings with different groups depending on level
1012
- Highlights code blocks to better stand out
1113
- Replaces whichever style bullet point is being used with provided character
14+
- Updates table boarders with better boarder characters, does NOT automatically align
1215

1316
# Dependencies
1417

@@ -18,8 +21,6 @@ Plugin to improve viewing Markdown files in Neovim
1821

1922
# Install
2023

21-
WIP
22-
2324
## Lazy.nvim
2425

2526
```lua
@@ -43,6 +44,10 @@ WIP
4344
(fenced_code_block) @code
4445
4546
(list_item) @item
47+
48+
(pipe_table_header) @table_head
49+
(pipe_table_delimiter_row) @table_delim
50+
(pipe_table_row) @table_row
4651
]]
4752
),
4853
render_modes = { 'n', 'c' },
@@ -51,6 +56,10 @@ WIP
5156
headings = { 'DiffAdd', 'DiffChange', 'DiffDelete' },
5257
code = 'ColorColumn',
5358
bullet = 'Normal',
59+
table = {
60+
head = '@markup.heading',
61+
row = 'Normal',
62+
},
5463
},
5564
})
5665
end,

lua/markdown/init.lua

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ local state = require('markdown.state')
22

33
local M = {}
44

5+
---@class UserTableHighlights
6+
---@field public head? string
7+
---@field public row? string
8+
59
---@class UserHighlights
610
---@field public heading? string
711
---@field public code? string
812
---@field public bullet? string
13+
---@field public table? UserTableHighlights
914

1015
---@class UserConfig
1116
---@field public query? Query
@@ -15,20 +20,6 @@ local M = {}
1520

1621
---@param opts UserConfig|nil
1722
function M.setup(opts)
18-
--[[
19-
Reference for pre-defined highlight groups and colors
20-
ColorColumn bg = 1f1d2e (dark gray / purple)
21-
PmenuExtra bg = 1f1d2e (dark gray / purple) fg = 6e6a86 (light purple)
22-
CursorColumn bg = 26233a (more purple version of 1f1d2e)
23-
PmenuSel bg = 26233a (more purple version of 1f1d2e) fg = e0def4 (white / pink)
24-
CurSearch bg = f6c177 (light orange) fg = 191724 (dark gray)
25-
DiffAdd bg = 333c48 (gray / little bit blue)
26-
DiffChange bg = 433842 (pink / gray)
27-
DiffDelete bg = 43293a (darker version of 433842)
28-
Visual bg = 403d52 (lighter version of 1f1d2e)
29-
MatchParen bg = 1f2e3f (deep blue) fg = 31748f (teel)
30-
]]
31-
3223
---@type Config
3324
local default_config = {
3425
query = vim.treesitter.query.parse(
@@ -46,6 +37,10 @@ function M.setup(opts)
4637
(fenced_code_block) @code
4738
4839
(list_item) @item
40+
41+
(pipe_table_header) @table_head
42+
(pipe_table_delimiter_row) @table_delim
43+
(pipe_table_row) @table_row
4944
]]
5045
),
5146
render_modes = { 'n', 'c' },
@@ -54,6 +49,10 @@ function M.setup(opts)
5449
headings = { 'DiffAdd', 'DiffChange', 'DiffDelete' },
5550
code = 'ColorColumn',
5651
bullet = 'Normal',
52+
table = {
53+
head = '@markup.heading',
54+
row = 'Normal',
55+
},
5756
},
5857
}
5958
state.config = vim.tbl_deep_extend('force', default_config, opts or {})
@@ -119,6 +118,30 @@ M.refresh = function()
119118
virt_text = { { state.config.bullet, highlights.bullet } },
120119
virt_text_pos = 'overlay',
121120
})
121+
elseif vim.tbl_contains({ 'table_head', 'table_delim', 'table_row' }, capture) then
122+
local row = vim.treesitter.get_node_text(node, 0)
123+
local modified_row = row:gsub('|', '')
124+
if capture == 'table_delim' then
125+
-- Order matters here, in particular handling inner intersections before left & right
126+
modified_row = modified_row
127+
:gsub('-', '')
128+
:gsub(' ', '')
129+
:gsub('─│─', '─┼─')
130+
:gsub('│─', '├─')
131+
:gsub('─│', '─┤')
132+
end
133+
134+
local highlight = highlights.table.head
135+
if capture == 'table_row' then
136+
highlight = highlights.table.row
137+
end
138+
139+
vim.api.nvim_buf_set_extmark(0, M.namespace, start_row, start_col, {
140+
end_row = end_row,
141+
end_col = end_col,
142+
virt_text = { { modified_row, highlight } },
143+
virt_text_pos = 'overlay',
144+
})
122145
else
123146
-- Should only get here if user provides custom capture, currently unhandled
124147
vim.print('Unhandled capture: ' .. capture)

lua/markdown/state.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
---@class TableHighlights
2+
---@field public head string
3+
---@field public row string
4+
15
---@class Highlights
26
---@field public headings string[]
37
---@field public code string
48
---@field public bullet string
9+
---@field public table TableHighlights
510

611
---@class Config
712
---@field public query Query

0 commit comments

Comments
 (0)