Skip to content

Commit 88e6250

Browse files
authored
Merge pull request #223 from etiennecollin/typst-support
Added support for the typst language
2 parents c4e8260 + e0e4dbe commit 88e6250

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

lua/image/init.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ local default_options = {
88
markdown = {
99
enabled = true,
1010
},
11+
typst = {
12+
enabled = true,
13+
},
1114
neorg = {
1215
enabled = true,
1316
},

lua/image/integrations/typst.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

Comments
 (0)