Skip to content

Commit 420058b

Browse files
feat: add border prefix heading option
# Details Request: #123 For heading borders some users use block characters. To improve the way those look it would be nice if the color of the icon extended to the border. Added the border_prefix option to do this. Highlights the border line under the icon section with the foreground highlight group. Minor bug fix for getting line below, did not update the if condition to work correctly. Unnecessary virtual lines were being previously added. Update CHANGELOG pre-release.
1 parent b700269 commit 420058b

File tree

9 files changed

+78
-29
lines changed

9 files changed

+78
-29
lines changed

Diff for: CHANGELOG.md

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22

33
## Pre-release
44

5+
### Features
6+
7+
- min_width code block option [4b80b4f](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/4b80b4fb8f81dc39da23a13a0b4e971731c5f849)
8+
- left_pad bullet option [e455c4f](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/e455c4f4886d250bd610165a24524da2c6adce80)
9+
- preset option, obsidian & lazy [96988cc](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/96988cc76414a2f69a57c5dbaca7bf9336c9cb52)
10+
- left_pad, right_pad, & min_width heading options [#121](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/121)
11+
[6392a5d](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/6392a5dfa10f367e10fe58ea9c2faf3179b145d5)
12+
- border heading option [#123](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/123)
13+
[b700269](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/b7002694a7a794f8d8a6a0cc54769628cf1cf9d8)
14+
15+
### Bug Fixes
16+
17+
- same buffer in multiple windows [#122](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/122)
18+
[1c7b5ee](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/1c7b5ee30d8cf6e52628862dbd06f2e23ecb888e)
19+
- link icon in headings [#124](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/124)
20+
[f365cef](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/f365cef5c1d05fd2dd390e1830d5c41f2d1f2121)
21+
522
## 6.0.0 (2024-08-05)
623

724
### ⚠ BREAKING CHANGES

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ require('render-markdown').setup({
253253
min_width = 0,
254254
-- Determins if a border is added above and below headings
255255
border = false,
256+
-- Highlight the start of the border using the foreground highlight
257+
border_prefix = false,
256258
-- Used above heading for border
257259
above = '',
258260
-- Used below heading for border
@@ -546,6 +548,8 @@ require('render-markdown').setup({
546548
min_width = 0,
547549
-- Determins if a border is added above and below headings
548550
border = false,
551+
-- Highlight the start of the border using the foreground highlight
552+
border_prefix = false,
549553
-- Used above heading for border
550554
above = '',
551555
-- Used below heading for border

Diff for: doc/render-markdown.txt

+4
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ Full Default Configuration ~
281281
min_width = 0,
282282
-- Determins if a border is added above and below headings
283283
border = false,
284+
-- Highlight the start of the border using the foreground highlight
285+
border_prefix = false,
284286
-- Used above heading for border
285287
above = '▄',
286288
-- Used below heading for border
@@ -573,6 +575,8 @@ HEADINGS *render-markdown-setup-headings*
573575
min_width = 0,
574576
-- Determins if a border is added above and below headings
575577
border = false,
578+
-- Highlight the start of the border using the foreground highlight
579+
border_prefix = false,
576580
-- Used above heading for border
577581
above = '▄',
578582
-- Used below heading for border

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

+46-27
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ function Handler:heading(info)
101101
})
102102

103103
local width = self:heading_width(info, icon_width)
104-
105104
if heading.width == 'block' then
106105
-- Overwrite anything beyond width with Normal
107106
self:add(true, info.start_row, 0, {
@@ -110,33 +109,8 @@ function Handler:heading(info)
110109
virt_text_win_col = width,
111110
})
112111
end
113-
114112
if heading.border then
115-
local text_above = { heading.above:rep(width), colors.inverse(background) }
116-
if info:line('above') == '' and info.start_row - 1 ~= self.last_heading_border then
117-
self:add(true, info.start_row - 1, 0, {
118-
virt_text = { text_above },
119-
virt_text_pos = 'overlay',
120-
})
121-
else
122-
self:add(false, info.start_row, 0, {
123-
virt_lines = { { text_above } },
124-
virt_lines_above = true,
125-
})
126-
end
127-
128-
local text_below = { heading.below:rep(width), colors.inverse(background) }
129-
if info:line('below') == '' then
130-
self:add(true, info.end_row + 1, 0, {
131-
virt_text = { text_below },
132-
virt_text_pos = 'overlay',
133-
})
134-
self.last_heading_border = info.end_row + 1
135-
else
136-
self:add(false, info.end_row, 0, {
137-
virt_lines = { { text_below } },
138-
})
139-
end
113+
self:heading_border(info, level, foreground, colors.inverse(background), width)
140114
end
141115

142116
if heading.left_pad > 0 then
@@ -205,6 +179,51 @@ function Handler:heading_width(info, icon_width)
205179
end
206180
end
207181

182+
---@private
183+
---@param info render.md.NodeInfo
184+
---@param level integer
185+
---@param foreground string
186+
---@param background string
187+
---@param width integer
188+
function Handler:heading_border(info, level, foreground, background, width)
189+
local heading = self.config.heading
190+
local prefix = heading.border_prefix and level or 0
191+
192+
local line_above = {
193+
{ heading.above:rep(heading.left_pad), background },
194+
{ heading.above:rep(prefix), foreground },
195+
{ heading.above:rep(width - heading.left_pad - prefix), background },
196+
}
197+
if str.width(info:line('above')) == 0 and info.start_row - 1 ~= self.last_heading_border then
198+
self:add(true, info.start_row - 1, 0, {
199+
virt_text = line_above,
200+
virt_text_pos = 'overlay',
201+
})
202+
else
203+
self:add(false, info.start_row, 0, {
204+
virt_lines = { line_above },
205+
virt_lines_above = true,
206+
})
207+
end
208+
209+
local line_below = {
210+
{ heading.below:rep(heading.left_pad), background },
211+
{ heading.below:rep(prefix), foreground },
212+
{ heading.below:rep(width - heading.left_pad - prefix), background },
213+
}
214+
if str.width(info:line('below')) == 0 then
215+
self:add(true, info.end_row + 1, 0, {
216+
virt_text = line_below,
217+
virt_text_pos = 'overlay',
218+
})
219+
self.last_heading_border = info.end_row + 1
220+
else
221+
self:add(false, info.end_row, 0, {
222+
virt_lines = { line_below },
223+
})
224+
end
225+
end
226+
208227
---@private
209228
---@param info render.md.NodeInfo
210229
function Handler:dash(info)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local M = {}
55

66
---@private
77
---@type string
8-
M.version = '6.0.8'
8+
M.version = '6.0.9'
99

1010
function M.check()
1111
vim.health.start('render-markdown.nvim [version]')

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

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ local M = {}
109109
---@field public right_pad? integer
110110
---@field public min_width? integer
111111
---@field public border? boolean
112+
---@field public border_prefix? boolean
112113
---@field public above? string
113114
---@field public below? string
114115
---@field public backgrounds? string[]
@@ -265,6 +266,8 @@ M.default_config = {
265266
min_width = 0,
266267
-- Determins if a border is added above and below headings
267268
border = false,
269+
-- Highlight the start of the border using the foreground highlight
270+
border_prefix = false,
268271
-- Used above heading for border
269272
above = '',
270273
-- Used below heading for border

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function NodeInfo:line(position)
116116
local start_row = nil
117117
if position == 'above' then
118118
start_row = self.start_row - 1
119-
elseif position == 'above' then
119+
elseif position == 'below' then
120120
start_row = self.end_row + 1
121121
else
122122
start_row = self.start_row

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

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ function M.validate()
173173
right_pad = { heading.right_pad, 'number', nilable },
174174
min_width = { heading.min_width, 'number', nilable },
175175
border = { heading.border, 'boolean', nilable },
176+
border_prefix = { heading.border_prefix, 'boolean', nilable },
176177
above = { heading.above, 'string', nilable },
177178
below = { heading.below, 'string', nilable },
178179
backgrounds = string_array(heading.backgrounds, nilable),

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

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
---@field public right_pad integer
9999
---@field public min_width integer
100100
---@field public border boolean
101+
---@field public border_prefix boolean
101102
---@field public above string
102103
---@field public below string
103104
---@field public backgrounds string[]

0 commit comments

Comments
 (0)