From bc2944651f4dabc68d7f34c796400d80ba132016 Mon Sep 17 00:00:00 2001 From: moxie Date: Fri, 13 Mar 2026 09:58:53 +0200 Subject: chore: init --- lua/muwiki/links/url_handlers.lua | 71 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 lua/muwiki/links/url_handlers.lua (limited to 'lua/muwiki/links/url_handlers.lua') 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 -- cgit v1.2.3