diff options
| author | moxie <moxie@3kgcat.fi> | 2026-03-14 04:28:05 +0200 |
|---|---|---|
| committer | moxie <moxie@3kgcat.fi> | 2026-03-14 04:28:05 +0200 |
| commit | 3461be4d2d880293c92bc7b4119ba53289ef03cc (patch) | |
| tree | 8f642fbf468779dfce16b6339d6d86acc8b8f6f5 /lua/muwiki/config.lua | |
| parent | d4e100dd5f84d77b3dd16af2d7f2590a8b2bd030 (diff) | |
refactor: simplify wiki discovery functions
Diffstat (limited to 'lua/muwiki/config.lua')
| -rw-r--r-- | lua/muwiki/config.lua | 41 |
1 files changed, 14 insertions, 27 deletions
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 |
