Skip to content

Commit 208599b

Browse files
Update heading highlight logic to change # to a different character depending on level
1 parent 08060e3 commit 208599b

File tree

4 files changed

+75
-15
lines changed

4 files changed

+75
-15
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,24 @@ require('markdown').setup({
7878
-- vim modes that will show a rendered view of the markdown file, all other
7979
-- modes will be uneffected by this plugin
8080
render_modes = { 'n', 'c' },
81+
-- Characters that will replace the # at the start of markdown headings
82+
headings = { '', '', '', '' },
8183
-- Character to use for the bullet point in lists
8284
bullet = '',
8385
highlights = {
84-
-- Groups that get cycled through for rendering headings
85-
headings = { 'DiffAdd', 'DiffChange', 'DiffDelete' },
86+
heading = {
87+
-- Used for rendering heading line backgrounds
88+
backgrounds = { 'DiffAdd', 'DiffChange', 'DiffDelete' },
89+
-- Used for rendering the foreground of the heading character only
90+
foregrounds = {
91+
'markdownH1',
92+
'markdownH2',
93+
'markdownH3',
94+
'markdownH4',
95+
'markdownH5',
96+
'markdownH6',
97+
},
98+
},
8699
-- Used when displaying code blocks
87100
code = 'ColorColumn',
88101
-- Used when displaying bullet points in list

lua/markdown/init.lua

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local list = require('markdown.list')
12
local state = require('markdown.state')
23

34
local 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
2228
function 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

lua/markdown/list.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
local M = {}
2+
3+
---@param values string[]
4+
---@param index integer
5+
---@return string
6+
function M.cycle(values, index)
7+
return values[((index - 1) % #values) + 1]
8+
end
9+
10+
---@param values string[]
11+
---@param index integer
12+
---@return string
13+
function M.clamp_last(values, index)
14+
return values[math.min(index, #values)]
15+
end
16+
17+
return M

lua/markdown/state.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22
---@field public head string
33
---@field public row string
44

5+
---@class HeadingHighlights
6+
---@field public backgrounds string[]
7+
---@field public foregrounds string[]
8+
59
---@class Highlights
6-
---@field public headings string[]
10+
---@field public heading HeadingHighlights
711
---@field public code string
812
---@field public bullet string
913
---@field public table TableHighlights
1014

1115
---@class Config
1216
---@field public query Query
1317
---@field public render_modes string[]
18+
---@field public headings string[]
1419
---@field public bullet string
1520
---@field public highlights Highlights
1621

0 commit comments

Comments
 (0)