Skip to content

Commit c203eae

Browse files
feat: allow nested markdown to be skipped
## Details Request: #510 Users would like the ability skip rendering `markdown` code blocks inside of `markdown` files. This is in order to make it appear more distinct from the overall context of the markdown file, to some extent treating it more like code than a document. For now I've made disabling the nested rendering opt-in, via the top level `nested` option which has a default value of `true`. To disable nested rendering: ```lua require('render-markdown').setup({ nested = false, }) ``` I initially thought this feature would be more complex to implement due to how we traverse the individual `markdown` trees, however using the `LanguageTree:parent()` API we can count the number of `markdown` trees on our path to the root node skip nodes that are `> 1` level deep. Note: opting in to the feature has a small performance penalty as we must check whether trees are nested for each individual tree, which for `markdown` includes all the `inline` nodes.
1 parent f626e90 commit c203eae

File tree

8 files changed

+44
-12
lines changed

8 files changed

+44
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
[146d4ff](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/146d4ff582017f1a1a16ada841aa08175a87aaea)
1111
- add scope_highlight to wiki config [ca86b59](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/ca86b5941a56b04ac140837ee7a366cf3fa5cd88)
1212
- add cell_offset function to pipe_table configuration [e5c3c50](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/e5c3c500d66e9aaf04c116cdfdb0b040d56a1521)
13+
- inline render single line latex equations [#508](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/508)
14+
[b7dad79](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/b7dad793f4750b7e95884c0db374b917898a979b)
1315

1416
### Bug Fixes
1517

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,13 @@ require('render-markdown').setup({
209209
log_runtime = false,
210210
-- Filetypes this plugin will run on.
211211
file_types = { 'markdown' },
212-
-- Takes buffer as input, if it returns true this plugin will not attach to the buffer
212+
-- Takes buffer as input, if it returns true this plugin will not attach to the buffer.
213213
ignore = function()
214214
return false
215215
end,
216+
-- Whether markdown should be rendered when nested inside markdown, i.e. markdown code block
217+
-- inside markdown file.
218+
nested = true,
216219
-- Additional events that will trigger this plugin's render loop.
217220
change_events = {},
218221
-- Whether the treesitter highlighter should be restarted after this plugin attaches to its

doc/render-markdown.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For NVIM v0.11.3 Last change: 2025 August 25
1+
*render-markdown.txt* For NVIM v0.11.3 Last change: 2025 August 29
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -275,10 +275,13 @@ Default Configuration ~
275275
log_runtime = false,
276276
-- Filetypes this plugin will run on.
277277
file_types = { 'markdown' },
278-
-- Takes buffer as input, if it returns true this plugin will not attach to the buffer
278+
-- Takes buffer as input, if it returns true this plugin will not attach to the buffer.
279279
ignore = function()
280280
return false
281281
end,
282+
-- Whether markdown should be rendered when nested inside markdown, i.e. markdown code block
283+
-- inside markdown file.
284+
nested = true,
282285
-- Additional events that will trigger this plugin's render loop.
283286
change_events = {},
284287
-- Whether the treesitter highlighter should be restarted after this plugin attaches to its

lua/render-markdown/core/handlers.lua

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local log = require('render-markdown.core.log')
22

33
---@class render.md.handlers.Config
4+
---@field nested boolean
45
---@field custom table<string, render.md.Handler>
56

67
---@class render.md.Handlers
@@ -31,13 +32,15 @@ function M.run(context, parser)
3132
-- parse markdown after other nodes to get accurate state
3233
local markdown = {} ---@type render.md.handler.Context[]
3334
parser:for_each_tree(function(tree, language_tree)
34-
---@type render.md.handler.Context
35-
local ctx = { buf = context.buf, root = tree:root() }
36-
local language = language_tree:lang()
37-
if language == 'markdown' then
38-
markdown[#markdown + 1] = ctx
39-
else
40-
vim.list_extend(marks, M.tree(context, ctx, language))
35+
if M.config.nested or M.level(language_tree) == 1 then
36+
---@type render.md.handler.Context
37+
local ctx = { buf = context.buf, root = tree:root() }
38+
local language = language_tree:lang()
39+
if language == 'markdown' then
40+
markdown[#markdown + 1] = ctx
41+
else
42+
vim.list_extend(marks, M.tree(context, ctx, language))
43+
end
4144
end
4245
end)
4346
for _, ctx in ipairs(markdown) do
@@ -46,6 +49,20 @@ function M.run(context, parser)
4649
return marks
4750
end
4851

52+
---@private
53+
---@param tree? vim.treesitter.LanguageTree
54+
---@return integer
55+
function M.level(tree)
56+
local result = 0
57+
while tree do
58+
if tree:lang() == 'markdown' then
59+
result = result + 1
60+
end
61+
tree = tree:parent()
62+
end
63+
return result
64+
end
65+
4966
---Run custom & builtin handlers when available. Custom handler is always
5067
---executed, builtin handler is skipped if custom does not specify extends.
5168
---@private

lua/render-markdown/health.lua

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

77
---@private
8-
M.version = '8.7.14'
8+
M.version = '8.7.15'
99

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

lua/render-markdown/init.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local M = {}
77
---@field log_runtime boolean
88
---@field file_types string[]
99
---@field ignore fun(buf: integer): boolean
10+
---@field nested boolean
1011
---@field change_events string[]
1112
---@field restart_highlighter boolean
1213
---@field injections render.md.injection.Configs
@@ -72,10 +73,13 @@ M.default = {
7273
log_runtime = false,
7374
-- Filetypes this plugin will run on.
7475
file_types = { 'markdown' },
75-
-- Takes buffer as input, if it returns true this plugin will not attach to the buffer
76+
-- Takes buffer as input, if it returns true this plugin will not attach to the buffer.
7677
ignore = function()
7778
return false
7879
end,
80+
-- Whether markdown should be rendered when nested inside markdown, i.e. markdown code block
81+
-- inside markdown file.
82+
nested = true,
7983
-- Additional events that will trigger this plugin's render loop.
8084
change_events = {},
8185
-- Whether the treesitter highlighter should be restarted after this plugin attaches to its

lua/render-markdown/state.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function M.setup(config)
1616
M.config = config
1717
M.enabled = config.enabled
1818
require('render-markdown.core.handlers').setup({
19+
nested = config.nested,
1920
custom = config.custom_handlers,
2021
})
2122
require('render-markdown.core.log').setup({
@@ -90,6 +91,7 @@ function M.validate()
9091
log_runtime = { type = 'boolean' },
9192
file_types = { list = { type = 'string' } },
9293
ignore = { type = 'function' },
94+
nested = { type = 'boolean' },
9395
change_events = { list = { type = 'string' } },
9496
restart_highlighter = { type = 'boolean' },
9597
injections = require('render-markdown.config.injections').schema(),

lua/render-markdown/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
---@field log_runtime? boolean
77
---@field file_types? string[]
88
---@field ignore? fun(buf: integer): boolean
9+
---@field nested? boolean
910
---@field change_events? string[]
1011
---@field restart_highlighter? boolean
1112
---@field injections? render.md.injection.UserConfigs

0 commit comments

Comments
 (0)