aboutsummaryrefslogtreecommitdiff
path: root/lua/muwiki
diff options
context:
space:
mode:
Diffstat (limited to 'lua/muwiki')
-rw-r--r--lua/muwiki/links.lua34
1 files changed, 11 insertions, 23 deletions
diff --git a/lua/muwiki/links.lua b/lua/muwiki/links.lua
index 1a681fa..5bcdc68 100644
--- a/lua/muwiki/links.lua
+++ b/lua/muwiki/links.lua
@@ -12,11 +12,7 @@ local function get_link_type(target)
return 'wiki'
end
-local function resolve_file_url(url)
- local path = url:gsub('^file://', '')
- local resolved = utils.resolve(path, nil)
- return 'file://' .. resolved
-end
+
function M.get_link()
local cursor = vim.api.nvim_win_get_cursor(0)
@@ -79,7 +75,6 @@ function M.open_link()
return
end
- -- Web links - open with xdg-open
if link.type == 'web' then
vim.system({'xdg-open', link.target}, {detach = true})
return
@@ -90,13 +85,11 @@ function M.open_link()
return
end
- -- File links - use xdg-open
if link.type == 'file' then
if vim.startswith(link.target, 'file://') then
- -- file:// URLs - open with xdg-open
- vim.system({'xdg-open', resolve_file_url(link.target)}, {detach = true})
+ local file_path = utils.resolve(link.target, nil)
+ vim.system({'xdg-open', file_path}, {detach = true})
else
- -- Local file paths - open with xdg-open
local ok, file_path = pcall(utils.resolve, link.target, wiki_root)
if not ok then
vim.notify(string.format('Cannot resolve path: %s', link.target), vim.log.levels.ERROR)
@@ -108,27 +101,22 @@ function M.open_link()
return
end
- -- Wiki links - open in buffer
local file_path = utils.resolve(link.target, wiki_root)
utils.open_in_buffer(file_path)
end
function M.open_with_menu(handlers, link)
- -- Get link automatically if not provided
link = link or M.get_link()
if not link then
vim.notify('No link under cursor', vim.log.levels.WARN)
return
end
- -- Get file extension
local ext = link.target:match('%.([^%.]+)$')
ext = ext and ext:lower() or nil
- -- Filter handlers by extension
local matching_handlers = {}
for _, handler in ipairs(handlers) do
- -- Check if handler matches this extension
if handler.exts then
for _, handler_ext in ipairs(handler.exts) do
if handler_ext:lower() == ext then
@@ -137,7 +125,6 @@ function M.open_with_menu(handlers, link)
end
end
else
- -- No exts specified - matches all
table.insert(matching_handlers, handler)
end
end
@@ -147,22 +134,23 @@ function M.open_with_menu(handlers, link)
return
end
- -- Resolve the path
local url = link.target
- if link.type == 'file' and not vim.startswith(url, 'file://') then
- local wiki_root = get_wiki_root_or_notify()
- if wiki_root then
- url = utils.resolve(url, wiki_root)
+ if link.type == 'file' then
+ if vim.startswith(url, 'file://') then
+ url = utils.resolve(url, nil)
+ else
+ local wiki_root = get_wiki_root_or_notify()
+ if wiki_root then
+ url = utils.resolve(url, wiki_root)
+ end
end
end
- -- Build menu items
local handler_names = {}
for _, handler in ipairs(matching_handlers) do
table.insert(handler_names, handler.name)
end
- -- Show menu with link text as title
vim.ui.select(handler_names, {
prompt = 'Open "' .. link.text .. '" with:',
}, function(choice, idx)