|
| 1 | +local document = require("image/utils/document") |
| 2 | +return document.create_document_integration({ |
| 3 | + name = "typst", |
| 4 | + -- debug = true, |
| 5 | + default_options = { |
| 6 | + clear_in_insert_mode = false, |
| 7 | + download_remote_images = true, |
| 8 | + only_render_image_at_cursor = false, |
| 9 | + filetypes = { "typst" }, |
| 10 | + }, |
| 11 | + query_buffer_images = function(buffer) |
| 12 | + local buf = buffer or vim.api.nvim_get_current_buf() |
| 13 | + |
| 14 | + local parser = vim.treesitter.get_parser(buf, "typst") |
| 15 | + local root = parser:parse()[1]:root() |
| 16 | + local query = |
| 17 | + vim.treesitter.query.parse("typst", '(call item: (ident) @name (#eq? @name "image") (group (string) @url))') |
| 18 | + |
| 19 | + local images = {} |
| 20 | + local current_image = nil |
| 21 | + |
| 22 | + ---@diagnostic disable-next-line: missing-parameter |
| 23 | + for id, node in query:iter_captures(root, 0) do |
| 24 | + local capture = query.captures[id] |
| 25 | + local value = vim.treesitter.get_node_text(node, buf) |
| 26 | + |
| 27 | + if capture == "name" then |
| 28 | + -- Get the parent since we want the "call" and not only the "ident" |
| 29 | + local start_row, start_col, end_row, end_col = node:parent():range() |
| 30 | + current_image = { |
| 31 | + node = node, |
| 32 | + range = { start_row = start_row, start_col = start_col, end_row = end_row, end_col = end_col }, |
| 33 | + } |
| 34 | + elseif current_image and capture == "url" then |
| 35 | + -- We need to remove the quotes from the string |
| 36 | + -- '"The URL"' -> "The URL" |
| 37 | + current_image.url = string.sub(value, 2, -2) |
| 38 | + |
| 39 | + table.insert(images, current_image) |
| 40 | + current_image = nil |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + return images |
| 45 | + end, |
| 46 | +}) |
0 commit comments