Skip to content

Commit 477997f

Browse files
MeanderingProgrammerbezlant
andcommitted
feat: disable code based on languages
## Details Provides a `code.disable` configuration option that accepts a list of languages. For those languages all code block related rendering will be disabled. Co-authored-by: bezlant <abezlyudniy@Snowcrash.local>
1 parent 1c95813 commit 477997f

7 files changed

Lines changed: 87 additions & 30 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ require('render-markdown').setup({
463463
-- Amount of padding to add around the language.
464464
-- If a float < 1 is provided it is treated as a percentage of available window space.
465465
language_pad = 0,
466+
-- A list of language names for which rendering will be disabled.
467+
disable = {},
466468
-- A list of language names for which background highlighting will be disabled.
467469
-- Likely because that language has background highlights itself.
468470
-- Use a boolean to make behavior apply to all languages.
@@ -1164,6 +1166,8 @@ require('render-markdown').setup({
11641166
-- Amount of padding to add around the language.
11651167
-- If a float < 1 is provided it is treated as a percentage of available window space.
11661168
language_pad = 0,
1169+
-- A list of language names for which rendering will be disabled.
1170+
disable = {},
11671171
-- A list of language names for which background highlighting will be disabled.
11681172
-- Likely because that language has background highlights itself.
11691173
-- Use a boolean to make behavior apply to all languages.

doc/render-markdown.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For NVIM v0.11.6 Last change: 2026 February 22
1+
*render-markdown.txt* For NVIM v0.11.6 Last change: 2026 March 02
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -529,6 +529,8 @@ Default Configuration ~
529529
-- Amount of padding to add around the language.
530530
-- If a float < 1 is provided it is treated as a percentage of available window space.
531531
language_pad = 0,
532+
-- A list of language names for which rendering will be disabled.
533+
disable = {},
532534
-- A list of language names for which background highlighting will be disabled.
533535
-- Likely because that language has background highlights itself.
534536
-- Use a boolean to make behavior apply to all languages.
@@ -1224,6 +1226,8 @@ Code Block Configuration ~
12241226
-- Amount of padding to add around the language.
12251227
-- If a float < 1 is provided it is treated as a percentage of available window space.
12261228
language_pad = 0,
1229+
-- A list of language names for which rendering will be disabled.
1230+
disable = {},
12271231
-- A list of language names for which background highlighting will be disabled.
12281232
-- Likely because that language has background highlights itself.
12291233
-- Use a boolean to make behavior apply to all languages.

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local state = require('render-markdown.state')
66
local M = {}
77

88
---@private
9-
M.version = '8.11.8'
9+
M.version = '8.11.9'
1010

1111
function M.check()
1212
M.start('versions')

lua/render-markdown/render/markdown/code.lua

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ local icons = require('render-markdown.lib.icons')
55
local str = require('render-markdown.lib.str')
66

77
---@class render.md.code.Data
8-
---@field language integer
8+
---@field info? render.md.Node
9+
---@field language? render.md.Node
10+
---@field language_padding integer
911
---@field padding integer
1012
---@field body integer
1113
---@field margin integer
@@ -27,26 +29,33 @@ function Render:setup()
2729
if self.node:height() <= 2 then
2830
return false
2931
end
32+
33+
local info = self.node:child('info_string')
34+
local language = info and info:child('language')
35+
3036
local widths = self.node:widths()
3137
local width = vim.fn.max(widths)
3238

33-
local language = self:offset(self.config.language_pad, width)
39+
local language_padding = self:offset(self.config.language_pad, width)
3440
local left = self:offset(self.config.left_pad, width)
3541
local right = self:offset(self.config.right_pad, width)
3642

3743
local body = str.width(self.config.language_left)
3844
+ str.width(self.config.language_right)
39-
+ language
45+
+ language_padding
4046
+ widths[1]
4147
body = math.max(body, left + width + right, self.config.min_width)
4248

4349
self.data = {
50+
info = info,
4451
language = language,
52+
language_padding = language_padding,
4553
padding = left,
4654
body = body,
4755
margin = self:offset(self.config.left_margin, body),
4856
}
49-
return true
57+
58+
return self:enabled(self.config.disable)
5059
end
5160

5261
---@private
@@ -66,43 +75,55 @@ function Render:offset(value, used)
6675
return result
6776
end
6877

78+
---@private
79+
---@param disable boolean|string[]
80+
---@return boolean
81+
function Render:enabled(disable)
82+
if type(disable) == 'boolean' then
83+
return not disable
84+
else
85+
local language = self.data.language
86+
return not language or not vim.tbl_contains(disable, language.text)
87+
end
88+
end
89+
6990
---@protected
7091
function Render:run()
7192
local start_row = self.node.start_row
7293
local end_row = self.node.end_row - 1
7394

7495
local above = self.node:child('fenced_code_block_delimiter', start_row)
7596
local below = self.node:child('fenced_code_block_delimiter', end_row)
76-
local info = self.node:child('info_string')
7797

7898
if self.config.conceal_delimiters then
99+
self.marks:over(self.config, true, self.data.info, { conceal = '' })
79100
self.marks:over(self.config, true, above, { conceal = '' })
80101
self.marks:over(self.config, true, below, { conceal = '' })
81-
self.marks:over(self.config, true, info, { conceal = '' })
82102
end
83103

84-
local language = info and info:child('language')
85-
if not self:language(info, language, above) then
104+
if not self:language(above) then
86105
self:border(above, self.config.above)
87106
end
88107
self:border(below, self.config.below)
89108

90-
local background = self:background_enabled(language)
109+
local background = self:enabled(self.config.disable_background)
91110
if background then
92111
self:background(start_row + 1, end_row - 1)
93112
end
94113
self:padding(background)
95114
end
96115

97116
---@private
98-
---@param info? render.md.Node
99-
---@param language? render.md.Node
100117
---@param delim? render.md.Node
101118
---@return boolean
102-
function Render:language(info, language, delim)
119+
function Render:language(delim)
103120
if not self.config.language then
104121
return false
105122
end
123+
124+
local info = self.data.info
125+
local language = self.data.language
126+
local padding = self.data.language_padding
106127
if not info or not language or not delim then
107128
return false
108129
end
@@ -151,15 +172,15 @@ function Render:language(info, language, delim)
151172
local suffix = 0
152173
-- space within block after accounting for white space and padding
153174
local width = self.data.body - delim.start_col
154-
local extra = width - prefix - body:width() - suffix - self.data.language ---@type integer
175+
local extra = width - prefix - body:width() - suffix - padding ---@type integer
155176
if self.config.position == 'left' then
156-
prefix = prefix + self.data.language
177+
prefix = prefix + padding
157178
suffix = suffix + extra
158179
elseif self.config.position == 'right' then
159180
prefix = prefix + extra
160-
suffix = suffix + self.data.language
181+
suffix = suffix + padding
161182
else
162-
prefix = prefix + self.data.language + math.floor(extra / 2)
183+
prefix = prefix + padding + math.floor(extra / 2)
163184
suffix = suffix + math.ceil(extra / 2)
164185
end
165186
if self.config.width == 'full' then
@@ -206,18 +227,6 @@ function Render:border(node, thin)
206227
})
207228
end
208229

209-
---@private
210-
---@param language? render.md.Node
211-
---@return boolean
212-
function Render:background_enabled(language)
213-
local disable = self.config.disable_background
214-
if type(disable) == 'boolean' then
215-
return not disable
216-
else
217-
return language == nil or not vim.tbl_contains(disable, language.text)
218-
end
219-
end
220-
221230
---@private
222231
---@param start_row integer
223232
---@param end_row integer

lua/render-markdown/settings.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ M.code = {}
388388
---@field language_name boolean
389389
---@field language_info boolean
390390
---@field language_pad number
391+
---@field disable string[]
391392
---@field disable_background boolean|string[]
392393
---@field width render.md.code.Width
393394
---@field left_margin number
@@ -468,6 +469,8 @@ M.code.default = {
468469
-- Amount of padding to add around the language.
469470
-- If a float < 1 is provided it is treated as a percentage of available window space.
470471
language_pad = 0,
472+
-- A list of language names for which rendering will be disabled.
473+
disable = {},
471474
-- A list of language names for which background highlighting will be disabled.
472475
-- Likely because that language has background highlights itself.
473476
-- Use a boolean to make behavior apply to all languages.
@@ -546,6 +549,7 @@ function M.code.schema()
546549
language_name = { type = 'boolean' },
547550
language_info = { type = 'boolean' },
548551
language_pad = { type = 'number' },
552+
disable = { list = { type = 'string' } },
549553
disable_background = {
550554
union = { { list = { type = 'string' } }, { type = 'boolean' } },
551555
},

lua/render-markdown/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
---@field language_name? boolean
100100
---@field language_info? boolean
101101
---@field language_pad? number
102+
---@field disable? string[]
102103
---@field disable_background? boolean|string[]
103104
---@field width? render.md.code.Width
104105
---@field left_margin? number

tests/code_spec.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,41 @@ describe('code', function()
288288
})
289289
end)
290290

291+
it('disable', function()
292+
util.setup.text(lines, {
293+
code = {
294+
disable = { 'lua', 'py', 'rust' },
295+
},
296+
})
297+
util.assert_screen({
298+
'```rust',
299+
'fn main() {',
300+
' println!("Hello, World!");',
301+
'}',
302+
'```',
303+
'',
304+
'● List Divider',
305+
'',
306+
' ```py',
307+
' print("hello")',
308+
'',
309+
' print("world")',
310+
' ```',
311+
'',
312+
'Paragraph Divider',
313+
'',
314+
' ```lua',
315+
" print('hello')",
316+
'',
317+
" print('world')",
318+
' ```',
319+
'',
320+
'● List Divider',
321+
'',
322+
" print('Hello, World!')",
323+
})
324+
end)
325+
291326
it('quarto executable', function()
292327
util.setup.text({ '``` {{rust} info', '```' })
293328
util.assert_screen({

0 commit comments

Comments
 (0)