aboutsummaryrefslogtreecommitdiff
path: root/lua/muwiki/files.lua
diff options
context:
space:
mode:
authormoxie <moxie@3kgcat.fi>2026-03-13 09:58:53 +0200
committermoxie <moxie@3kgcat.fi>2026-03-13 09:58:53 +0200
commitbc2944651f4dabc68d7f34c796400d80ba132016 (patch)
tree3655716442a24ccb758983f0ddc89222916f64c1 /lua/muwiki/files.lua
chore: init
Diffstat (limited to 'lua/muwiki/files.lua')
-rw-r--r--lua/muwiki/files.lua98
1 files changed, 98 insertions, 0 deletions
diff --git a/lua/muwiki/files.lua b/lua/muwiki/files.lua
new file mode 100644
index 0000000..851feb4
--- /dev/null
+++ b/lua/muwiki/files.lua
@@ -0,0 +1,98 @@
+local config = require('muwiki.config')
+local paths = require('muwiki.paths')
+local fs = require('muwiki.fs')
+
+local M = {}
+
+function M.open_in_buffer(filepath)
+ local bufnr = vim.fn.bufnr(filepath, true)
+ vim.api.nvim_win_set_buf(0, bufnr)
+end
+
+function M.open_external(url)
+ if type(url) ~= 'string' then
+ vim.notify('Invalid URL type', vim.log.levels.ERROR)
+ return false
+ end
+
+ local valid, err = paths.validate_url_scheme(url)
+ if not valid then
+ vim.notify(err, vim.log.levels.ERROR)
+ return false
+ end
+
+ vim.system({ 'xdg-open', url }, { detach = true })
+ return true
+end
+
+function M.open_index(name)
+ local wiki_path = config.get_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)
+ if not wiki_root then
+ error('wiki_root parameter is required for secure path resolution')
+ end
+
+ local path = paths.strip_file_protocol(filepath)
+
+ local resolved = paths.resolve(path, wiki_root)
+
+ return paths.validate_within_wiki(resolved, wiki_root, filepath)
+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
+ end
+ return false
+end
+
+function M.normalize_filename(text)
+ local normalized = text:lower()
+ normalized = normalized:gsub('%s+', '_')
+ normalized = normalized:gsub('[^%w_%-%.]', '')
+ return normalized
+end
+
+function M.open_wiki_file(filepath)
+ local templates = require('muwiki.templates')
+
+ 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))
+ if wiki_root then
+ local mode = config.options.create_missing_dirs
+
+ if mode == 'prompt' then
+ fs.ensure_parent_dirs(filepath, wiki_root, mode, function(success)
+ if success then
+ M.open_in_buffer(filepath)
+ templates.init_file(vim.api.nvim_get_current_buf(), filepath)
+ end
+ end)
+ return
+ else
+ local success = fs.ensure_parent_dirs(filepath, wiki_root, mode)
+ if not success then
+ return
+ end
+ end
+ end
+ end
+
+ M.open_in_buffer(filepath)
+ templates.init_file(vim.api.nvim_get_current_buf(), filepath)
+end
+
+return M