blob: 356e0bab6f8f28fc02cf6613d50d563ad4f46c7d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
local M = {}
local function get_link_type(target)
if target:match('^https?://') then
return 'web'
end
if target:match('^file://') then
return 'file'
end
return 'wiki'
end
function M.get_link()
local cursor = vim.api.nvim_win_get_cursor(0)
local ok, node = pcall(vim.treesitter.get_node, {
bufnr = 0,
pos = { cursor[1] - 1, cursor[2] },
lang = 'markdown',
ignore_injections = false,
})
if not ok or not node then
return nil
end
local link_node = node
while link_node and link_node:type() ~= 'inline_link' do
link_node = link_node:parent()
end
if not link_node then
return nil
end
local text_node, dest_node
for child in link_node:iter_children() do
local t = child:type()
if t == 'link_text' then
text_node = child
elseif t == 'link_destination' then
dest_node = child
end
end
if not text_node or not dest_node then
return nil
end
local destination = vim.treesitter.get_node_text(dest_node, 0)
return {
text = vim.treesitter.get_node_text(text_node, 0),
target = destination,
type = get_link_type(destination),
}
end
return M
|