aboutsummaryrefslogtreecommitdiff
path: root/lua/muwiki/utils.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/muwiki/utils.lua')
-rw-r--r--lua/muwiki/utils.lua73
1 files changed, 47 insertions, 26 deletions
diff --git a/lua/muwiki/utils.lua b/lua/muwiki/utils.lua
index af22c8e..bc6094e 100644
--- a/lua/muwiki/utils.lua
+++ b/lua/muwiki/utils.lua
@@ -1,25 +1,54 @@
local config = require('muwiki.config')
-local paths = require('muwiki.paths')
local M = {}
-function M.file_exists(path)
- local stat = vim.uv.fs_stat(path)
- return stat and stat.type == 'file'
+function M.open_in_buffer(filepath)
+ vim.cmd.edit(vim.fn.fnameescape(filepath))
end
-function M.dir_exists(path)
- local stat = vim.uv.fs_stat(path)
- return stat and stat.type == 'directory'
+-- Lookup wiki path by name (e.g., "default" -> "~/wiki/")
+function M.wiki_path(name)
+ if not config.options.dirs or #config.options.dirs == 0 then
+ vim.notify('MuWiki: No dirs configured. See :help muwiki-configuration', vim.log.levels.ERROR)
+ return nil
+ end
+
+ if name then
+ for _, dir in ipairs(config.options.dirs) do
+ if dir.name == name then
+ return dir.path
+ end
+ end
+ vim.notify(string.format('Wiki "%s" not found, using default', name), vim.log.levels.WARN)
+ end
+
+ return config.options.dirs[1].path
end
-function M.open_in_buffer(filepath)
- local bufnr = vim.fn.bufnr(filepath, true)
- vim.api.nvim_win_set_buf(0, bufnr)
+-- 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 filepath = vim.api.nvim_buf_get_name(bufnr)
+ local normalized = vim.fs.normalize(filepath)
+
+ for _, dir in ipairs(config.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.open_index(name)
- local wiki_path = config.wiki_path(name)
+ local wiki_path = M.wiki_path(name)
if not wiki_path then
return
end
@@ -29,18 +58,14 @@ function M.open_index(name)
end
function M.resolve(filepath, wiki_root)
- local path = paths.strip_file_protocol(filepath)
- return paths.resolve(path, wiki_root)
-end
-
-function M.is_text_file(ext)
- local ext_lower = ext:lower()
- for _, text_ext in ipairs(config.options.text_extensions) do
- if ext_lower == text_ext then
- return true
- end
+ local path = filepath:gsub('^file://', '')
+ local path_type = path:sub(1, 1)
+ if path_type ~= '/' and path_type ~= '~' then
+ local base = wiki_root and vim.fs.dirname(wiki_root)
+ or vim.fs.dirname(vim.api.nvim_buf_get_name(0))
+ path = vim.fs.joinpath(base, path)
end
- return false
+ return vim.fs.normalize(path)
end
function M.normalize_filename(text)
@@ -50,8 +75,4 @@ function M.normalize_filename(text)
return normalized
end
-function M.open_wiki_file(filepath)
- M.open_in_buffer(filepath)
-end
-
return M