From 3461be4d2d880293c92bc7b4119ba53289ef03cc Mon Sep 17 00:00:00 2001 From: moxie Date: Sat, 14 Mar 2026 04:28:05 +0200 Subject: refactor: simplify wiki discovery functions --- doc/muwiki.txt | 20 +------------------- lua/muwiki/config.lua | 41 ++++++++++++++--------------------------- lua/muwiki/files.lua | 4 ++-- lua/muwiki/init.lua | 2 +- lua/muwiki/links/creation.lua | 2 +- lua/muwiki/links/open.lua | 2 +- 6 files changed, 20 insertions(+), 51 deletions(-) diff --git a/doc/muwiki.txt b/doc/muwiki.txt index 4344a1e..562ff5c 100644 --- a/doc/muwiki.txt +++ b/doc/muwiki.txt @@ -196,9 +196,6 @@ Configure keymaps in the `keys` table: > ft = 'markdown', desc = 'Open link menu to choose external handler' }, }, } - @@ -214,7 +211,7 @@ For other plugin managers, set up your own autocmds: > end, }) < -Note: Actions automatically check if the buffer is a wiki buffer. +Note: Actions check if the buffer is within a configured wiki directory and do nothing if not. *muwiki* Actions:~ @@ -328,7 +325,6 @@ Example: Open PNG files in Neovim (as text/hex) instead of external viewer:~ >lua text_extensions = { 'md', 'txt', 'png' } < - ============================================================================== 8. TEMPLATES *muwiki-templates* @@ -354,20 +350,6 @@ date: ${date} --- ]] < -Custom Template Example:~ ->lua - require("muwiki").setup({ - template = [[ ---- -title: ${title} -date: ${date} ---- - -# ${title} - -]], - }) -< ============================================================================== HEALTH CHECKING *muwiki-health* diff --git a/lua/muwiki/config.lua b/lua/muwiki/config.lua index d9b6661..2a89427 100644 --- a/lua/muwiki/config.lua +++ b/lua/muwiki/config.lua @@ -52,7 +52,8 @@ function M.setup(opts) end end -function M.get_wiki_path(name) +-- Lookup wiki path by name (e.g., "default" -> "~/wiki/") +function M.wiki_path(name) if not M.options.dirs or #M.options.dirs == 0 then vim.notify('MuWiki: No dirs configured. See :help muwiki-configuration', vim.log.levels.ERROR) return nil @@ -70,40 +71,26 @@ function M.get_wiki_path(name) return M.options.dirs[1].path end -function M.get_wiki_root_for_file(filepath) - if not M.options.dirs or #M.options.dirs == 0 then - return nil +-- Find which wiki contains this buffer's file (cached per-buffer) +function M.wiki_root(bufnr) + bufnr = bufnr or 0 + + if vim.b[bufnr].muwiki_root ~= nil then + return vim.b[bufnr].muwiki_root or nil end - local normalized_path = vim.fs.normalize(filepath) + local filepath = vim.api.nvim_buf_get_name(bufnr) + local normalized = vim.fs.normalize(filepath) - for _, dir in ipairs(M.options.dirs) do - if vim.startswith(normalized_path, dir.path) then + for _, dir in ipairs(M.options.dirs or {}) do + if vim.startswith(normalized, dir.path) then + vim.b[bufnr].muwiki_root = dir.path return dir.path end end + vim.b[bufnr].muwiki_root = false return nil end -function M.get_wiki_root_for_buffer(bufnr) - bufnr = bufnr or 0 - local ok, cached = pcall(vim.api.nvim_buf_get_var, bufnr, 'muwiki_root') - if ok then - return cached - end - - local root = M.get_wiki_root_for_file(vim.api.nvim_buf_get_name(bufnr)) - if root then - pcall(vim.api.nvim_buf_set_var, bufnr, 'muwiki_root', root) - end - return root -end - -function M.is_wiki_buffer(bufnr) - bufnr = bufnr or 0 - return vim.bo[bufnr].filetype == 'markdown' - and M.get_wiki_root_for_buffer(bufnr) ~= nil -end - return M diff --git a/lua/muwiki/files.lua b/lua/muwiki/files.lua index 851feb4..6ec2bc2 100644 --- a/lua/muwiki/files.lua +++ b/lua/muwiki/files.lua @@ -26,7 +26,7 @@ function M.open_external(url) end function M.open_index(name) - local wiki_path = config.get_wiki_path(name) + local wiki_path = config.wiki_path(name) if not wiki_path then return end @@ -70,7 +70,7 @@ function M.open_wiki_file(filepath) local exists = fs.file_exists(filepath) if not exists and config.options.create_missing_dirs then - local wiki_root = config.get_wiki_root_for_file(vim.api.nvim_buf_get_name(0)) + local wiki_root = config.wiki_root(0) if wiki_root then local mode = config.options.create_missing_dirs diff --git a/lua/muwiki/init.lua b/lua/muwiki/init.lua index 6dc088f..a898c88 100644 --- a/lua/muwiki/init.lua +++ b/lua/muwiki/init.lua @@ -4,7 +4,7 @@ local M = {} local config = require('muwiki.config') local function wiki_buffer() - return config.is_wiki_buffer(0) + return config.wiki_root(0) ~= nil end M.setup = function(opts) diff --git a/lua/muwiki/links/creation.lua b/lua/muwiki/links/creation.lua index 0b31b10..81c45a6 100644 --- a/lua/muwiki/links/creation.lua +++ b/lua/muwiki/links/creation.lua @@ -52,7 +52,7 @@ function M.create_link() vim.api.nvim_buf_set_text(0, start_row, start_col, end_row, end_col, { link_text }) vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('', true, false, true), 'n', false) - local wiki_root = config.get_wiki_root_for_file(vim.api.nvim_buf_get_name(0)) + local wiki_root = config.wiki_root(0) if not wiki_root then vim.notify('Not in a wiki buffer', vim.log.levels.ERROR) return diff --git a/lua/muwiki/links/open.lua b/lua/muwiki/links/open.lua index 5afec53..c8a1233 100644 --- a/lua/muwiki/links/open.lua +++ b/lua/muwiki/links/open.lua @@ -7,7 +7,7 @@ local url_handlers = require('muwiki.links.url_handlers') local M = {} local function get_wiki_root_or_notify() - local wiki_root = config.get_wiki_root_for_buffer(0) + local wiki_root = config.wiki_root(0) if not wiki_root then vim.notify('Not in a wiki buffer', vim.log.levels.ERROR) return nil -- cgit v1.2.3