aboutsummaryrefslogtreecommitdiff
path: root/lua/muwiki/links/url_handlers.lua
diff options
context:
space:
mode:
authormoxie <moxie@3kgcat.fi>2026-03-13 09:58:53 +0200
committermoxie <moxie@3kgcat.fi>2026-03-13 09:58:53 +0200
commitbc2944651f4dabc68d7f34c796400d80ba132016 (patch)
tree3655716442a24ccb758983f0ddc89222916f64c1 /lua/muwiki/links/url_handlers.lua
chore: init
Diffstat (limited to 'lua/muwiki/links/url_handlers.lua')
-rw-r--r--lua/muwiki/links/url_handlers.lua71
1 files changed, 71 insertions, 0 deletions
diff --git a/lua/muwiki/links/url_handlers.lua b/lua/muwiki/links/url_handlers.lua
new file mode 100644
index 0000000..f84715a
--- /dev/null
+++ b/lua/muwiki/links/url_handlers.lua
@@ -0,0 +1,71 @@
+local config = require('muwiki.config')
+local paths = require('muwiki.paths')
+local files = require('muwiki.files')
+local fs = require('muwiki.fs')
+
+local M = {}
+
+function M.handle_web_link(url)
+ if not config.options.use_external_handlers then
+ return
+ end
+ files.open_external(url)
+end
+
+local function resolve_file_url(url)
+ local path = paths.strip_file_protocol(url)
+ local resolved_path
+
+ local path_type = paths.get_path_type(path)
+
+ if path_type == 'absolute' then
+ resolved_path = path
+ elseif path_type == 'home' then
+ resolved_path = vim.fs.normalize(path)
+ else
+ local current_dir = vim.fs.dirname(vim.api.nvim_buf_get_name(0))
+ resolved_path = paths.resolve_relative(path, current_dir)
+ end
+
+ return 'file://' .. vim.fs.normalize(resolved_path)
+end
+
+function M.handle_file_url(url)
+ if not config.options.use_external_handlers then
+ return
+ end
+
+ local absolute_url = resolve_file_url(url)
+ files.open_external(absolute_url)
+end
+
+function M.handle_file_link(target, wiki_root)
+ local ok, file_path = pcall(files.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 fs.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 files.is_text_file(ext) then
+ if not config.options.use_external_handlers then
+ return
+ end
+ files.open_external(file_path)
+ return
+ end
+
+ files.open_in_buffer(file_path)
+end
+
+function M.handle_wiki_link(target, wiki_root)
+ local file_path = files.resolve(target, wiki_root)
+ files.open_wiki_file(file_path)
+end
+
+return M