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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
local utils = require('muwiki.utils')
local config = require('muwiki.config')
local M = {}
---@class Link
---@field text string Display text of the link
---@field target string Link destination (URL or path)
---@field type 'web'|'file'|'wiki' Type of link
---@class Handler
---@field name string Display name of the handler
---@field cmd string|function Command or function to handle the URL
---@field exts string[]? Optional list of file extensions this handler supports
-- TODO: Support compound extensions like "tar.gz" or "min.js"
local function is_text_extension(ext)
local target = ext:gsub("^%.", "")
for _, cfg_ext in ipairs(config.options.text_extensions or {}) do
if cfg_ext == target then
return true
end
end
return false
end
local function get_link_type(target)
local lowered = target:lower()
if lowered:match('^https?://') then
return 'web'
end
if lowered:match('^file://') then
return 'file'
end
return 'wiki'
end
---Get link information at cursor position
---@return Link|nil link_info Table with text, target, and type, or nil if no link found
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 ---@type TSNode|nil
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
---Open the link under cursor
---Uses xdg-open for web links and unmatched file:// links
---Opens matching text_extensions in Neovim buffer
---Wiki links always open in Neovim
function M.open_link()
local link = M.get_link()
if not link then
vim.notify('No link found under cursor', vim.log.levels.WARN)
return
end
if link.type == 'web' then
vim.system({ 'xdg-open', link.target }, { detach = true })
return
end
if link.type == 'file' then
local file_path = utils.resolve(link.target)
local ext = file_path:match('%.([^%.]+)$')
if ext and is_text_extension(ext) then
utils.open_in_buffer(file_path)
else
vim.system({ 'xdg-open', file_path }, { detach = true })
end
return
end
local file_path = utils.resolve(link.target)
utils.open_in_buffer(file_path)
end
---Open link with a selectable handler from a menu
---@param handlers Handler[] List of handler tables with name, cmd, and optional exts
---@param link Link? Optional pre-fetched link info, will get from cursor if not provided
function M.open_with_menu(handlers, link)
link = link or M.get_link()
if not link then
vim.notify('No link under cursor', vim.log.levels.WARN)
return
end
local ext = link.target:match('%.([^%.]+)$')
ext = ext and ext:lower() or nil
local matching_handlers = {}
for _, handler in ipairs(handlers) do
if handler.exts then
for _, handler_ext in ipairs(handler.exts) do
if handler_ext:lower() == ext then
table.insert(matching_handlers, handler)
break
end
end
else
table.insert(matching_handlers, handler)
end
end
if #matching_handlers == 0 then
vim.notify('No handlers for file type: ' .. (ext or 'unknown'), vim.log.levels.WARN)
return
end
local url = link.target
if link.type == 'file' then
url = utils.resolve(url)
end
local handler_names = {}
for _, handler in ipairs(matching_handlers) do
table.insert(handler_names, handler.name)
end
vim.ui.select(handler_names, {
prompt = 'Open "' .. link.text .. '" with:',
}, function(choice, idx)
if choice and idx then
local handler = matching_handlers[idx]
if type(handler.cmd) == 'function' then
handler.cmd(url)
else
vim.system({ handler.cmd, url }, { detach = true })
end
end
end)
end
---Create a link from visual selection
---Converts selected text to [text](normalized_text.md)
---Must be called in visual mode
function M.create_link()
local mode = vim.fn.mode()
if mode ~= 'v' and mode ~= 'V' then
vim.notify('Must be in visual mode to create a link', vim.log.levels.WARN)
return
end
local start_pos = vim.fn.getpos('v')
local end_pos = vim.fn.getpos('.')
local region = vim.fn.getregion(start_pos, end_pos, { type = mode })
if not region or #region == 0 then
vim.notify('No text selected', vim.log.levels.WARN)
return
end
if #region > 1 then
vim.notify('Multi-line selection not supported', vim.log.levels.WARN)
return
end
local selected_text = region[1]
local normalized = utils.normalize_filename(selected_text)
local link_target = normalized .. '.md'
local link_text = string.format('[%s](%s)', selected_text, link_target)
local start_row = start_pos[2]
local start_col = start_pos[3]
local end_row = end_pos[2]
local end_col = end_pos[3]
if start_row > end_row or (start_row == end_row and start_col > end_col) then
start_row, end_row = end_row, start_row
start_col, end_col = end_col, start_col
end
start_row = start_row - 1
start_col = start_col - 1
end_row = end_row - 1
if mode == 'V' then
start_col = 0
local line = vim.api.nvim_buf_get_lines(0, end_row, end_row + 1, false)[1]
end_col = #line
end
vim.api.nvim_buf_set_text(0, start_row, start_col, end_row, end_col, { link_text })
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Esc>', true, false, true), 'n', false)
local wiki_root = utils.wiki_root(0)
if not wiki_root then
vim.notify('Not in a wiki buffer', vim.log.levels.ERROR)
return
end
local target_path = utils.resolve(link_target)
utils.open_in_buffer(target_path)
end
---Jump to next or previous markdown link in buffer
---@param direction 'next'|'prev' Direction to search
function M.jump_link(direction)
local flags = direction == 'next' and 'w' or 'bw'
local msg = direction == 'next' and 'No more links' or 'No previous links'
if vim.fn.search('\\[.\\{-}\\]', flags) == 0 then
vim.notify(msg, vim.log.levels.INFO)
end
end
-- Export for testing
M.get_link_type = get_link_type
return M
|