diff options
Diffstat (limited to 'lua/muwiki/config.lua')
| -rw-r--r-- | lua/muwiki/config.lua | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/lua/muwiki/config.lua b/lua/muwiki/config.lua new file mode 100644 index 0000000..d9b6661 --- /dev/null +++ b/lua/muwiki/config.lua @@ -0,0 +1,109 @@ +local fs = require('muwiki.fs') + +local M = {} + +M.options = { + dirs = nil, + index_file = 'index.md', + date_fmt = '%Y-%m-%d', + use_template = false, + template = [[ +title: ${title} +date: ${date} +]], + text_extensions = { 'md', 'txt' }, + use_external_handlers = false, + external_handlers = { + { + name = 'xdg-open', + cmd = 'xdg-open', + pattern = '.*', + }, + }, + create_missing_dirs = false, +} + +function M.setup(opts) + opts = opts or {} + + if opts.dirs then + M.options.dirs = {} + for _, dir in ipairs(opts.dirs) do + local path = vim.fs.normalize(dir.path) + if not vim.endswith(path, '/') then + path = path .. '/' + end + table.insert(M.options.dirs, { name = dir.name, path = path }) + end + end + + for key, value in pairs(opts) do + if key == 'dirs' then + -- handled above + elseif M.options[key] ~= nil then + M.options[key] = value + end + end + + for _, dir in ipairs(M.options.dirs or {}) do + if not fs.dir_exists(dir.path) then + vim.notify('Wiki directory not found: ' .. dir.path, vim.log.levels.WARN) + end + end +end + +function M.get_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 + end + + if name then + for _, dir in ipairs(M.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 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 + end + + local normalized_path = vim.fs.normalize(filepath) + + for _, dir in ipairs(M.options.dirs) do + if vim.startswith(normalized_path, dir.path) then + return dir.path + end + end + + 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 |
