aboutsummaryrefslogtreecommitdiff
path: root/lua/muwiki/utils.lua
blob: af22c8e41edcd545f65c1458d08a21d891867c3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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'
end

function M.dir_exists(path)
  local stat = vim.uv.fs_stat(path)
  return stat and stat.type == 'directory'
end

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_index(name)
  local wiki_path = config.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 = 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
  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)
  M.open_in_buffer(filepath)
end

return M