Skip to content

Commit 70f8f4f

Browse files
Use a string parameter rather than a query parameter, that way tbl_deep_extend does not remove the metatable
## Details Input parameter is currently expected to be a query object. However this does not work when we join the configs as metatables are ignored: neovim/neovim#23811. Use a string instead and parse as part of the setup function.
1 parent e64255d commit 70f8f4f

File tree

4 files changed

+49
-49
lines changed

4 files changed

+49
-49
lines changed

README.md

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,26 @@ by the user.
4141

4242
```lua
4343
require('render-markdown').setup({
44-
-- Capture groups that get pulled from the markdown file, these are later
45-
-- used to modify how the file gets rendered
46-
query = vim.treesitter.query.parse(
47-
'markdown',
48-
[[
49-
(atx_heading [
50-
(atx_h1_marker)
51-
(atx_h2_marker)
52-
(atx_h3_marker)
53-
(atx_h4_marker)
54-
(atx_h5_marker)
55-
(atx_h6_marker)
56-
] @heading)
57-
58-
(fenced_code_block) @code
59-
60-
(list_item) @item
61-
62-
(pipe_table_header) @table_head
63-
(pipe_table_delimiter_row) @table_delim
64-
(pipe_table_row) @table_row
65-
]]
66-
),
44+
-- Capture groups that get pulled from markdown, these are later used to
45+
-- modify how the file gets rendered
46+
markdown_query = [[
47+
(atx_heading [
48+
(atx_h1_marker)
49+
(atx_h2_marker)
50+
(atx_h3_marker)
51+
(atx_h4_marker)
52+
(atx_h5_marker)
53+
(atx_h6_marker)
54+
] @heading)
55+
56+
(fenced_code_block) @code
57+
58+
(list_item) @item
59+
60+
(pipe_table_header) @table_head
61+
(pipe_table_delimiter_row) @table_delim
62+
(pipe_table_row) @table_row
63+
]],
6764
-- Filetypes this plugin will run on
6865
file_types = { 'markdown' },
6966
-- vim modes that will show a rendered view of the markdown file, all other

demo/sample.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ if __name__ == "__main__":
2626
* Nested List Item 2
2727
* List Item 3
2828

29+
1. Item 1
30+
2. Item 2
31+
3. Item 3
32+
2933
| Heading 1 | Heading 2 |
3034
| ------------ | ------------ |
3135
| Row 1 Item 1 | Row 1 Item 2 |

lua/render-markdown/init.lua

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ local M = {}
1818
---@field public table? UserTableHighlights
1919

2020
---@class UserConfig
21-
---@field public query? Query
21+
---@field public markdown_query? string
2222
---@field public file_types? string[]
2323
---@field public render_modes? string[]
2424
---@field public headings? string[]
@@ -29,27 +29,24 @@ local M = {}
2929
function M.setup(opts)
3030
---@type Config
3131
local default_config = {
32-
query = vim.treesitter.query.parse(
33-
'markdown',
34-
[[
35-
(atx_heading [
36-
(atx_h1_marker)
37-
(atx_h2_marker)
38-
(atx_h3_marker)
39-
(atx_h4_marker)
40-
(atx_h5_marker)
41-
(atx_h6_marker)
42-
] @heading)
43-
44-
(fenced_code_block) @code
45-
46-
(list_item) @item
47-
48-
(pipe_table_header) @table_head
49-
(pipe_table_delimiter_row) @table_delim
50-
(pipe_table_row) @table_row
51-
]]
52-
),
32+
markdown_query = [[
33+
(atx_heading [
34+
(atx_h1_marker)
35+
(atx_h2_marker)
36+
(atx_h3_marker)
37+
(atx_h4_marker)
38+
(atx_h5_marker)
39+
(atx_h6_marker)
40+
] @heading)
41+
42+
(fenced_code_block) @code
43+
44+
(list_item) @item
45+
46+
(pipe_table_header) @table_head
47+
(pipe_table_delimiter_row) @table_delim
48+
(pipe_table_row) @table_row
49+
]],
5350
file_types = { 'markdown' },
5451
render_modes = { 'n', 'c' },
5552
headings = { '󰲡', '󰲣', '󰲥', '󰲧', '󰲩', '󰲫' },
@@ -74,8 +71,9 @@ function M.setup(opts)
7471
},
7572
},
7673
}
77-
state.config = vim.tbl_deep_extend('force', default_config, opts or {})
7874
state.enabled = true
75+
state.config = vim.tbl_deep_extend('force', default_config, opts or {})
76+
state.markdown_query = vim.treesitter.query.parse('markdown', state.config.markdown_query)
7977

8078
-- Call immediately to re-render on LazyReload
8179
vim.schedule(M.refresh)
@@ -143,8 +141,8 @@ end
143141
M.handle_markdown = function(tree)
144142
local highlights = state.config.highlights
145143
---@diagnostic disable-next-line: missing-parameter
146-
for id, node in state.config.query:iter_captures(tree:root(), 0) do
147-
local capture = state.config.query.captures[id]
144+
for id, node in state.markdown_query:iter_captures(tree:root(), 0) do
145+
local capture = state.markdown_query.captures[id]
148146
local value = vim.treesitter.get_node_text(node, 0)
149147
local start_row, start_col, end_row, end_col = node:range()
150148

lua/render-markdown/state.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
---@field public table TableHighlights
1414

1515
---@class Config
16-
---@field public query Query
16+
---@field public markdown_query string
1717
---@field public file_types string[]
1818
---@field public render_modes string[]
1919
---@field public headings string[]
@@ -23,5 +23,6 @@
2323
---@class State
2424
---@field enabled boolean
2525
---@field config Config
26+
---@field markdown_query Query
2627
local state = {}
2728
return state

0 commit comments

Comments
 (0)