aboutsummaryrefslogtreecommitdiff
path: root/lua/muwiki/links.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/muwiki/links.lua')
-rw-r--r--lua/muwiki/links.lua112
1 files changed, 58 insertions, 54 deletions
diff --git a/lua/muwiki/links.lua b/lua/muwiki/links.lua
index 990ecb1..55ce692 100644
--- a/lua/muwiki/links.lua
+++ b/lua/muwiki/links.lua
@@ -1,6 +1,6 @@
local config = require('muwiki.config')
local paths = require('muwiki.paths')
-local io_module = require('muwiki.io')
+local utils = require('muwiki.utils')
local external = require('muwiki.external')
local M = {}
@@ -72,50 +72,6 @@ local function resolve_file_url(url)
return 'file://' .. vim.fs.normalize(path)
end
-function M.handle_web_link(url)
- if not config.options.use_external_handlers then
- return
- end
- external.open(url)
-end
-
-function M.handle_file_url(url)
- if not config.options.use_external_handlers then
- return
- end
- local absolute_url = resolve_file_url(url)
- external.open(absolute_url)
-end
-
-function M.handle_file_link(target, wiki_root)
- local ok, file_path = pcall(io_module.resolve, target, wiki_root)
- if not ok then
- vim.notify(string.format('Cannot resolve path: %s', target), vim.log.levels.ERROR)
- return
- end
-
- if not io_module.file_exists(file_path) then
- vim.notify(string.format('File not found: %s', file_path), vim.log.levels.ERROR)
- return
- end
-
- local ext = file_path:match('%.([^%.]+)$') or ''
- if not io_module.is_text_file(ext) then
- if not config.options.use_external_handlers then
- return
- end
- external.open(file_path)
- return
- end
-
- io_module.open_in_buffer(file_path)
-end
-
-function M.handle_wiki_link(target, wiki_root)
- local file_path = io_module.resolve(target, wiki_root)
- io_module.open_wiki_file(file_path)
-end
-
local function get_wiki_root_or_notify()
local wiki_root = config.wiki_root(0)
if not wiki_root then
@@ -133,7 +89,9 @@ function M.open_link()
end
if link.type == 'web' then
- M.handle_web_link(link.target)
+ if config.options.use_external_handlers then
+ external.open(link.target)
+ end
return
end
@@ -144,14 +102,38 @@ function M.open_link()
if link.type == 'file' then
if vim.startswith(link.target, 'file://') then
- M.handle_file_url(link.target)
+ if config.options.use_external_handlers then
+ local absolute_url = resolve_file_url(link.target)
+ external.open(absolute_url)
+ end
else
- M.handle_file_link(link.target, wiki_root)
+ 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)
+ return
+ end
+
+ if not utils.file_exists(file_path) then
+ vim.notify(string.format('File not found: %s', file_path), vim.log.levels.ERROR)
+ return
+ end
+
+ local ext = file_path:match('%.([^%.]+)$') or ''
+ if not utils.is_text_file(ext) then
+ if config.options.use_external_handlers then
+ external.open(file_path)
+ end
+ return
+ end
+
+ utils.open_in_buffer(file_path)
end
return
end
- M.handle_wiki_link(link.target, wiki_root)
+ -- wiki link
+ local file_path = utils.resolve(link.target, wiki_root)
+ utils.open_wiki_file(file_path)
end
function M.open_link_with()
@@ -172,10 +154,32 @@ function M.open_link_with()
if not wiki_root then
return
end
- url = io_module.resolve(url, wiki_root)
+ url = utils.resolve(url, wiki_root)
end
- local matching_handlers = external.get_matching(url)
+ -- Find matching handlers for URL
+ local function handler_matches(handler, url)
+ local pattern = handler.pattern
+ if pattern == nil then
+ return true
+ end
+ if type(pattern) == 'string' then
+ return url:match(pattern) ~= nil
+ end
+ for _, p in ipairs(pattern) do
+ if url:match(p) then
+ return true
+ end
+ end
+ return false
+ end
+
+ local matching_handlers = {}
+ for _, handler in ipairs(config.options.external_handlers) do
+ if handler_matches(handler, url) then
+ table.insert(matching_handlers, handler)
+ end
+ end
if #matching_handlers == 0 then
vim.notify('No handlers available for this URL', vim.log.levels.WARN)
@@ -223,7 +227,7 @@ function M.create_link()
end
local selected_text = region[1]
- local normalized = io_module.normalize_filename(selected_text)
+ local normalized = utils.normalize_filename(selected_text)
local link_target = normalized .. '.md'
local link_text = string.format('[%s](%s)', selected_text, link_target)
@@ -256,8 +260,8 @@ function M.create_link()
return
end
- local target_path = io_module.resolve(link_target, wiki_root)
- io_module.open_wiki_file(target_path)
+ local target_path = utils.resolve(link_target, wiki_root)
+ utils.open_wiki_file(target_path)
end
local function jump_link(direction)