Skip to content

Commit af0dc75

Browse files
authored
Merge pull request #333 from SilverRainZ/rst-integration
feat: add reStructuredText (rst) integration
2 parents 2f80ec1 + 11b86ff commit af0dc75

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ require("image").setup({
392392
enabled = true,
393393
filetypes = { "norg" },
394394
},
395+
rst = {
396+
enabled = true,
397+
},
395398
typst = {
396399
enabled = true,
397400
filetypes = { "typst" },
@@ -431,6 +434,7 @@ All the backends support rendering inside Tmux.
431434

432435
- `markdown` - uses [tree-sitter-markdown](https://github.com/MDeiml/tree-sitter-markdown) and supports any Markdown-based grammars (Quarto, VimWiki Markdown)
433436
- `neorg` - uses [tree-sitter-norg](https://github.com/nvim-neorg/tree-sitter-norg) (also check https://github.com/nvim-neorg/neorg/issues/971)
437+
- `rst` (reStructuredText) - uses [tree-sitter-rst](https://github.com/stsewd/tree-sitter-rst)
434438
- `typst` - thanks to @etiennecollin (https://github.com/3rd/image.nvim/pull/223)
435439
- `html` and `css` - thanks to @zuloo (https://github.com/3rd/image.nvim/pull/163)
436440

lua/image/integrations/rst.lua

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
local document = require("image/utils/document")
2+
3+
return document.create_document_integration({
4+
name = "rst",
5+
debug = true,
6+
default_options = {
7+
clear_in_insert_mode = true,
8+
download_remote_images = true,
9+
only_render_image_at_cursor = false,
10+
only_render_image_at_cursor_mode = "popup",
11+
floating_windows = false,
12+
filetypes = { "rst" },
13+
},
14+
query_buffer_images = function(buffer)
15+
local buf = buffer or vim.api.nvim_get_current_buf()
16+
local parser = vim.treesitter.get_parser(buf, "rst")
17+
parser:parse(true)
18+
19+
local image_directive_query = vim.treesitter.query.parse("rst", [[
20+
((directive
21+
name: (type) @_type
22+
body: (body (arguments) @url)) @image
23+
(#any-of? @_type "image" "figure"))
24+
]])
25+
26+
local images = {}
27+
28+
local function get_images(tree)
29+
local root = tree:root()
30+
local current_image = nil
31+
32+
for id, node in image_directive_query:iter_captures(root, buf) do
33+
local key = image_directive_query.captures[id]
34+
local value = vim.treesitter.get_node_text(node, buf)
35+
36+
if key == "image" then
37+
local start_row, start_col, end_row, end_col = node:range()
38+
39+
current_image = {
40+
node = node,
41+
range = {
42+
start_row = start_row,
43+
start_col = start_col,
44+
end_row = end_row,
45+
end_col = end_col,
46+
},
47+
}
48+
49+
elseif current_image and key == "url" then
50+
current_image.url = value
51+
table.insert(images, current_image)
52+
current_image = nil
53+
end
54+
end
55+
end
56+
57+
parser:for_each_tree(get_images)
58+
59+
return images
60+
end
61+
})

0 commit comments

Comments
 (0)