Skip to content

Commit aa44c17

Browse files
committed
feat(utils): add function utils.fs.make_relative()
1 parent e18bbdf commit aa44c17

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

lua/orgmode/utils/fs.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,37 @@ function M.trim_common_root(paths)
6969
return result
7070
end
7171

72+
---@param filepath string an absolute path
73+
---@param base string an absolute path to an ancestor of filepath;
74+
--- here, `'.'` represents the current working directory, and
75+
--- *not* the current file's directory.
76+
---@return string filepath_relative_to_base
77+
function M.make_relative(filepath, base)
78+
vim.validate({
79+
filepath = { filepath, 'string', false },
80+
base = { base, 'string', false },
81+
})
82+
filepath = vim.fn.fnamemodify(filepath, ':p')
83+
base = vim.fn.fnamemodify(base, ':p')
84+
if base:sub(-1) ~= '/' then
85+
base = base .. '/'
86+
end
87+
local levels_up = 0
88+
for parent in vim.fs.parents(base) do
89+
if parent:sub(-1) ~= '/' then
90+
parent = parent .. '/'
91+
end
92+
if vim.startswith(filepath, parent) then
93+
filepath = filepath:sub(parent:len() + 1)
94+
if levels_up > 0 then
95+
return vim.fs.joinpath(string.rep('..', levels_up, '/'), filepath)
96+
end
97+
return vim.fs.joinpath('.', filepath)
98+
end
99+
levels_up = levels_up + 1
100+
end
101+
-- No common root, just return the absolute path.
102+
return filepath
103+
end
104+
72105
return M

0 commit comments

Comments
 (0)