aboutsummaryrefslogtreecommitdiff
path: root/lua/muwiki/paths.lua
blob: c2f8fdbf29b690218c44e20857182e726e17a1c4 (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
local M = {}

function M.get_path_type(path)
  if path:sub(1, 1) == '/' then
    return 'absolute'
  elseif path:sub(1, 1) == '~' then
    return 'home'
  else
    return 'relative'
  end
end

function M.resolve(filepath, current_file)
  local path_type = M.get_path_type(filepath)

  if path_type == 'relative' then
    local base = current_file and vim.fs.dirname(current_file)
        or vim.fs.dirname(vim.api.nvim_buf_get_name(0))
    filepath = vim.fs.joinpath(base, filepath)
  end

  return vim.fs.normalize(filepath)
end

function M.strip_file_protocol(url)
  return url:gsub('^file://', '')
end

local ALLOWED_SCHEMES = {
  http = true,
  https = true,
  file = true,
}

function M.validate_url_scheme(url)
  local scheme = url:match('^([a-zA-Z]+)://')
  if scheme and not ALLOWED_SCHEMES[scheme:lower()] then
    return false, string.format('URL scheme not allowed: %s', scheme)
  end
  return true, nil
end

return M