local config = require('muwiki.config') local M = {} function M.open_in_buffer(filepath) vim.cmd.edit(vim.fn.fnameescape(filepath)) end -- 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 -- 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 = M.wiki_path(name) if not wiki_path then return end local index_path = vim.fs.joinpath(wiki_path, config.options.index_file) M.open_in_buffer(index_path) end function M.resolve(filepath, wiki_root) local path = filepath:gsub('^file://', '') local path_type = path:sub(1, 1) if path_type == '.' then local current_dir = vim.fs.dirname(vim.api.nvim_buf_get_name(0)) path = vim.fs.joinpath(current_dir, path) elseif path_type ~= '/' and path_type ~= '~' then local base = wiki_root or vim.fs.dirname(vim.api.nvim_buf_get_name(0)) path = vim.fs.joinpath(base, path) end return vim.fs.normalize(path) end function M.normalize_filename(text) local normalized = text:lower() normalized = normalized:gsub('%s+', '_') normalized = normalized:gsub('[^%w_%-%.]', '') return normalized end return M