Skip to content

Commit 3fd818c

Browse files
Remove virtual text for headings and just use a highlight. Only render highlights within user defined modes, default to normal & command
1 parent 4fb7ea2 commit 3fd818c

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed

README.md

+41
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,53 @@
22

33
Plugin to improve viewing Markdown files in Neovim
44

5+
# Features
6+
7+
TODO
8+
59
# Dependencies
610

711
- [markdown](https://github.com/tree-sitter-grammars/tree-sitter-markdown/tree/split_parser)
812
parser for [treesitter](https://github.com/nvim-treesitter/nvim-treesitter/tree/master):
913
Used to parse `markdown` files
1014

15+
# Install
16+
17+
WIP
18+
19+
## Lazy.nvim
20+
21+
```lua
22+
{
23+
'MeanderingProgrammer/markdown.nvim',
24+
dependencies = { 'nvim-treesitter/nvim-treesitter' },
25+
config = function()
26+
require('markdown').setup({
27+
query = vim.treesitter.query.parse(
28+
'markdown',
29+
[[
30+
(atx_heading [
31+
(atx_h1_marker)
32+
(atx_h2_marker)
33+
(atx_h3_marker)
34+
(atx_h4_marker)
35+
(atx_h5_marker)
36+
(atx_h6_marker)
37+
] @heading)
38+
39+
(fenced_code_block) @code
40+
]]
41+
),
42+
render_modes = { 'n', 'c' },
43+
bullets = { '', '', '', '' },
44+
highlights = {
45+
headings = { 'DiffAdd', 'DiffChange', 'DiffDelete' },
46+
code = 'ColorColumn',
47+
},
48+
})
49+
end,
50+
}
51+
```
1152

1253
# Related Projects
1354

lua/markdown/init.lua

+35-8
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,31 @@ local M = {}
88

99
---@class UserConfig
1010
---@field public query? Query
11+
---@field public render_modes? string[]
1112
---@field public bullets? string[]
1213
---@field public highlights? Highlights
1314

1415
---@param opts UserConfig|nil
1516
function M.setup(opts)
17+
--[[
18+
Reference for pre-defined highlight groups and colors
19+
ColorColumn bg = 1f1d2e (dark gray / purple)
20+
PmenuExtra bg = 1f1d2e (dark gray / purple) fg = 6e6a86 (light purple)
21+
CursorColumn bg = 26233a (more purple version of 1f1d2e)
22+
PmenuSel bg = 26233a (more purple version of 1f1d2e) fg = e0def4 (white / pink)
23+
CurSearch bg = f6c177 (light orange) fg = 191724 (dark gray)
24+
DiffAdd bg = 333c48 (gray / little bit blue)
25+
DiffChange bg = 433842 (pink / gray)
26+
DiffDelete bg = 43293a (darker version of 433842)
27+
Visual bg = 403d52 (lighter version of 1f1d2e)
28+
MatchParen bg = 1f2e3f (deep blue) fg = 31748f (teel)
29+
]]
30+
31+
-- Some attempts to handle nested lists
32+
-- (list_item) @item1
33+
-- (list_item (list_item (list_item))) @item3
34+
-- (list) @item1
35+
1636
---@type Config
1737
local default_config = {
1838
query = vim.treesitter.query.parse(
@@ -30,17 +50,21 @@ function M.setup(opts)
3050
(fenced_code_block) @code
3151
]]
3252
),
53+
render_modes = { 'n', 'c' },
3354
bullets = { '', '', '', '' },
3455
highlights = {
35-
heading = '@comment.hint',
56+
headings = { 'DiffAdd', 'DiffChange', 'DiffDelete' },
3657
code = 'ColorColumn',
3758
},
3859
}
3960
state.config = vim.tbl_deep_extend('force', default_config, opts or {})
4061

62+
-- Call immediately to re-render on LazyReload
63+
M.refresh()
64+
4165
vim.api.nvim_create_autocmd({
4266
'FileChangedShellPost',
43-
'InsertLeave',
67+
'ModeChanged',
4468
'Syntax',
4569
'TextChanged',
4670
'WinResized',
@@ -62,6 +86,10 @@ M.refresh = function()
6286
-- Remove existing highlights / virtual text
6387
vim.api.nvim_buf_clear_namespace(0, M.namespace, 0, -1)
6488

89+
if not vim.tbl_contains(state.config.render_modes, vim.fn.mode()) then
90+
return
91+
end
92+
6593
local parser = vim.treesitter.get_parser(0, 'markdown')
6694
local root = parser:parse()[1]:root()
6795

@@ -74,15 +102,11 @@ M.refresh = function()
74102

75103
if capture == 'heading' then
76104
local level = #vim.treesitter.get_node_text(node, 0)
77-
local bullet = state.config.bullets[((level - 1) % #state.config.bullets) + 1]
105+
local highlight = highlights.headings[((level - 1) % #highlights.headings) + 1]
78106
vim.api.nvim_buf_set_extmark(0, M.namespace, start_row, 0, {
79107
end_row = end_row + 1,
80108
end_col = 0,
81-
hl_group = highlights.heading,
82-
-- This is done to exactly cover over the heading hashtags
83-
virt_text = { { string.rep(' ', level - 1) .. bullet, highlights.heading } },
84-
virt_text_pos = 'overlay',
85-
hl_eol = true,
109+
hl_group = highlight,
86110
})
87111
elseif capture == 'code' then
88112
vim.api.nvim_buf_set_extmark(0, M.namespace, start_row, 0, {
@@ -91,6 +115,9 @@ M.refresh = function()
91115
hl_group = highlights.code,
92116
hl_eol = true,
93117
})
118+
else
119+
vim.print('Unknown capture: ' .. capture)
120+
vim.print(vim.treesitter.get_node_text(node, 0))
94121
end
95122
end
96123
end

lua/markdown/state.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---@class Highlights
2-
---@field public heading string
2+
---@field public headings string[]
33
---@field public code string
44

55
---@class Config
66
---@field public query Query
7+
---@field public render_modes string[]
78
---@field public bullets string[]
89
---@field public highlights Highlights
910

0 commit comments

Comments
 (0)