1+ local list = require (' markdown.list' )
12local state = require (' markdown.state' )
23
34local M = {}
@@ -6,17 +7,22 @@ local M = {}
67--- @field public head ? string
78--- @field public row ? string
89
10+ --- @class UserHeadingHighlights
11+ --- @field public backgrounds ? string[]
12+ --- @field public foregrounds ? string[]
13+
914--- @class UserHighlights
10- --- @field public heading ? string
15+ --- @field public heading ? UserHeadingHighlights
1116--- @field public code ? string
1217--- @field public bullet ? string
1318--- @field public table ? UserTableHighlights
1419
1520--- @class UserConfig
1621--- @field public query ? Query
1722--- @field public render_modes ? string[]
23+ --- @field public headings ? string[]
1824--- @field public bullet ? string
19- --- @field public highlights ? Highlights
25+ --- @field public highlights ? UserHighlights
2026
2127--- @param opts UserConfig | nil
2228function M .setup (opts )
@@ -44,9 +50,20 @@ function M.setup(opts)
4450 ]]
4551 ),
4652 render_modes = { ' n' , ' c' },
53+ headings = { ' ◉' , ' ○' , ' ✸' , ' ✿' },
4754 bullet = ' ○' ,
4855 highlights = {
49- headings = { ' DiffAdd' , ' DiffChange' , ' DiffDelete' },
56+ heading = {
57+ backgrounds = { ' DiffAdd' , ' DiffChange' , ' DiffDelete' },
58+ foregrounds = {
59+ ' markdownH1' ,
60+ ' markdownH2' ,
61+ ' markdownH3' ,
62+ ' markdownH4' ,
63+ ' markdownH5' ,
64+ ' markdownH6' ,
65+ },
66+ },
5067 code = ' ColorColumn' ,
5168 bullet = ' Normal' ,
5269 table = {
@@ -94,15 +111,23 @@ M.refresh = function()
94111 --- @diagnostic disable-next-line : missing-parameter
95112 for id , node in state .config .query :iter_captures (root , 0 ) do
96113 local capture = state .config .query .captures [id ]
114+ local value = vim .treesitter .get_node_text (node , 0 )
97115 local start_row , start_col , end_row , end_col = node :range ()
98116
99117 if capture == ' heading' then
100- local level = # vim .treesitter .get_node_text (node , 0 )
101- local highlight = highlights .headings [((level - 1 ) % # highlights .headings ) + 1 ]
118+ local level = # value
119+ local heading = list .cycle (state .config .headings , level )
120+ local background = list .clamp_last (highlights .heading .backgrounds , level )
121+ local foreground = list .clamp_last (highlights .heading .foregrounds , level )
122+
123+ local virt_text = { string.rep (' ' , level - 1 ) .. heading , { foreground , background } }
102124 vim .api .nvim_buf_set_extmark (0 , M .namespace , start_row , 0 , {
103125 end_row = end_row + 1 ,
104126 end_col = 0 ,
105- hl_group = highlight ,
127+ hl_group = background ,
128+ virt_text = { virt_text },
129+ virt_text_pos = ' overlay' ,
130+ hl_eol = true ,
106131 })
107132 elseif capture == ' code' then
108133 vim .api .nvim_buf_set_extmark (0 , M .namespace , start_row , 0 , {
@@ -112,19 +137,18 @@ M.refresh = function()
112137 hl_eol = true ,
113138 })
114139 elseif capture == ' item' then
140+ local virt_text = { state .config .bullet , highlights .bullet }
115141 vim .api .nvim_buf_set_extmark (0 , M .namespace , start_row , start_col , {
116142 end_row = end_row ,
117143 end_col = end_col ,
118- virt_text = { { state . config . bullet , highlights . bullet } },
144+ virt_text = { virt_text },
119145 virt_text_pos = ' overlay' ,
120146 })
121147 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 (' |' , ' │' )
148+ local row = value :gsub (' |' , ' │' )
124149 if capture == ' table_delim' then
125150 -- Order matters here, in particular handling inner intersections before left & right
126- modified_row = modified_row
127- :gsub (' -' , ' ─' )
151+ row = row :gsub (' -' , ' ─' )
128152 :gsub (' ' , ' ─' )
129153 :gsub (' ─│─' , ' ─┼─' )
130154 :gsub (' │─' , ' ├─' )
@@ -136,10 +160,11 @@ M.refresh = function()
136160 highlight = highlights .table .row
137161 end
138162
163+ local virt_text = { row , highlight }
139164 vim .api .nvim_buf_set_extmark (0 , M .namespace , start_row , start_col , {
140165 end_row = end_row ,
141166 end_col = end_col ,
142- virt_text = { { modified_row , highlight } },
167+ virt_text = { virt_text },
143168 virt_text_pos = ' overlay' ,
144169 })
145170 else
0 commit comments