diff options
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | doc/muwiki.txt | 23 | ||||
| -rw-r--r-- | lua/muwiki/links.lua | 34 |
3 files changed, 27 insertions, 32 deletions
@@ -14,7 +14,7 @@ A lightweight wiki plugin for Neovim using standard markdown syntax. - Link navigation and xdg-open for web/file links - Extensible via autocmds (mkdir, templates, custom handlers) -See `:help muwiki-autocmd-recipes` for implementation examples. +See `:help muwiki-autocmd` for implementation examples. See `:help muwiki` for complete documentation and configuration options. diff --git a/doc/muwiki.txt b/doc/muwiki.txt index c22be0f..1542c75 100644 --- a/doc/muwiki.txt +++ b/doc/muwiki.txt @@ -204,14 +204,20 @@ Add template for new files~ *muwiki-au Open with menu example~ *muwiki-autocmd-open-with* > - vim.keymap.set('n', '<leader>o', function() - muwiki.open_with_menu({ - { name = "Zathura", cmd = "zathura", exts = {"pdf"} }, - { name = "swayimg", cmd = "swayimg", exts = {"png", "jpg", "jpeg", "gif", "webp"} }, - { name = "mpv", cmd = "mpv", exts = {"mp4", "mkv", "avi", "mov", "webm"} }, - { name = "xdg-open", cmd = "xdg-open" }, - }) - end, { buffer = true, desc = "Open with..." }) + vim.api.nvim_create_autocmd("FileType", { + pattern = "markdown", + callback = function(args) + if not muwiki.wiki_root(args.buf) then return end + vim.keymap.set('n', '<leader>o', function() + muwiki.open_with_menu({ + { name = "Zathura", cmd = "zathura", exts = {"pdf"} }, + { name = "swayimg", cmd = "swayimg", exts = {"png", "jpg", "jpeg", "gif", "webp"} }, + { name = "mpv", cmd = "mpv", exts = {"mp4", "mkv", "avi", "mov", "webm"} }, + { name = "xdg-open", cmd = "xdg-open" }, + }) + end, { buffer = args.buf, desc = "Open with..." }) + end, + }) < ============================================================================== @@ -220,6 +226,7 @@ Open with menu example~ *muwiki-au Run |:checkhealth muwiki| to verify: - Wiki directories are configured and exist - Treesitter markdown parser is installed +- Treesitter markdown_inline parser is installed ============================================================================== RECOMMENDED PLUGINS 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) |
