@@ -9,8 +9,8 @@ local str = require('render-markdown.core.str')
9
9
--- @field level integer
10
10
--- @field icon ? string
11
11
--- @field sign ? string
12
- --- @field foreground string
13
- --- @field background string
12
+ --- @field foreground ? string
13
+ --- @field background ? string
14
14
--- @field width render.md.heading.Width
15
15
--- @field left_margin number
16
16
--- @field left_pad number
@@ -59,11 +59,11 @@ function Render:setup()
59
59
sign = list .cycle (self .heading .signs , level ),
60
60
foreground = list .clamp (self .heading .foregrounds , level ),
61
61
background = list .clamp (self .heading .backgrounds , level ),
62
- width = list .clamp (self .heading .width , level ),
63
- left_margin = list .clamp (self .heading .left_margin , level ),
64
- left_pad = list .clamp (self .heading .left_pad , level ),
65
- right_pad = list .clamp (self .heading .right_pad , level ),
66
- min_width = list .clamp (self .heading .min_width , level ),
62
+ width = list .clamp (self .heading .width , level ) or ' full ' ,
63
+ left_margin = list .clamp (self .heading .left_margin , level ) or 0 ,
64
+ left_pad = list .clamp (self .heading .left_pad , level ) or 0 ,
65
+ right_pad = list .clamp (self .heading .right_pad , level ) or 0 ,
66
+ min_width = list .clamp (self .heading .min_width , level ) or 0 ,
67
67
end_row = self .info .end_row + (atx and 1 or 0 ),
68
68
}
69
69
84
84
--- @private
85
85
--- @return integer
86
86
function Render :icon ()
87
- local icon , highlight = self .data .icon , { self .data .foreground , self .data .background }
87
+ local icon , highlight = self .data .icon , {}
88
+ if self .data .foreground ~= nil then
89
+ table.insert (highlight , self .data .foreground )
90
+ end
91
+ if self .data .background ~= nil then
92
+ table.insert (highlight , self .data .background )
93
+ end
88
94
89
95
if not self .data .atx then
90
- if icon == nil then
96
+ if icon == nil or # highlight == 0 then
91
97
return 0
92
98
end
93
99
local added = true
@@ -107,7 +113,7 @@ function Render:icon()
107
113
-- `#` characters, one is added to account for the space after the last `#` but before
108
114
-- the heading title, and concealed text is subtracted since that space is not usable
109
115
local width = self .data .level + 1 - self .context :concealed (self .info )
110
- if icon == nil then
116
+ if icon == nil or # highlight == 0 then
111
117
return width
112
118
end
113
119
157
163
--- @private
158
164
--- @param width render.md.width.Heading
159
165
function Render :background (width )
166
+ local highlight = self .data .background
167
+ if highlight == nil then
168
+ return
169
+ end
160
170
local win_col , padding = 0 , {}
161
171
if self .data .width == ' block' then
162
172
win_col = width .margin + width .content + self :indent (self .data .level )
@@ -165,7 +175,7 @@ function Render:background(width)
165
175
for row = self .info .start_row , self .data .end_row - 1 do
166
176
self .marks :add (true , row , 0 , {
167
177
end_row = row + 1 ,
168
- hl_group = self . data . background ,
178
+ hl_group = highlight ,
169
179
hl_eol = true ,
170
180
})
171
181
if win_col > 0 and # padding > 0 then
@@ -187,18 +197,29 @@ function Render:border(width)
187
197
return
188
198
end
189
199
190
- local foreground , background = self .data .foreground , colors .inverse_bg (self .data .background )
200
+ local foreground = self .data .foreground
201
+ local background = self .data .background and colors .bg_to_fg (self .data .background )
191
202
local prefix = self .heading .border_prefix and self .data .level or 0
192
203
local virtual = self .heading .border_virtual
193
204
194
205
--- @param icon string
195
206
--- @return { [1] : string , [2] : string } []
196
207
local function line (icon )
208
+ --- @param size integer
209
+ --- @param highlight ? string
210
+ --- @return { [1] : string , [2] : string }
211
+ local function section (size , highlight )
212
+ if highlight ~= nil then
213
+ return { icon :rep (size ), highlight }
214
+ else
215
+ return { str .pad (size ), self .config .padding .highlight }
216
+ end
217
+ end
197
218
return {
198
- { str . pad (width .margin ), self . config . padding . highlight } ,
199
- { icon : rep (width .padding ) , background } ,
200
- { icon : rep (prefix ) , foreground } ,
201
- { icon : rep (width .content - width .padding - prefix ) , background } ,
219
+ section (width .margin , nil ) ,
220
+ section (width .padding , background ) ,
221
+ section (prefix , foreground ) ,
222
+ section (width .content - width .padding - prefix , background ) ,
202
223
}
203
224
end
204
225
@@ -241,12 +262,15 @@ end
241
262
--- @param width render.md.width.Heading
242
263
function Render :left_pad (width )
243
264
local virt_text = {}
244
- if width .margin > 0 then
245
- table.insert (virt_text , { str .pad (width .margin ), self .config .padding .highlight })
246
- end
247
- if width .padding > 0 then
248
- table.insert (virt_text , { str .pad (width .padding ), self .data .background })
265
+ --- @param size integer
266
+ --- @param highlight ? string
267
+ local function append (size , highlight )
268
+ if size > 0 then
269
+ table.insert (virt_text , { str .pad (size ), highlight or self .config .padding .highlight })
270
+ end
249
271
end
272
+ append (width .margin , nil )
273
+ append (width .padding , self .data .background )
250
274
if # virt_text > 0 then
251
275
for row = self .info .start_row , self .data .end_row - 1 do
252
276
self .marks :add (false , row , 0 , {
0 commit comments