|
| 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