Skip to content

Commit fe508e1

Browse files
committed
feat(utils): add optional parameter base to substitute_path()
1 parent 1c86a3d commit fe508e1

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lua/orgmode/utils/fs.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ local utils = require('orgmode.utils')
33
local M = {}
44

55
---@param path_str string
6+
---@param base? string
67
---@return string | false
7-
function M.substitute_path(path_str)
8+
function M.substitute_path(path_str, base)
89
if path_str:match('^/') then
910
return path_str
1011
elseif path_str:match('^~/') then
1112
local home_path = os.getenv('HOME')
1213
return home_path and path_str:gsub('^~', home_path) or false
1314
elseif path_str:match('^%./') then
14-
local base = vim.fn.fnamemodify(utils.current_file_path(), ':p:h')
15+
if not base then
16+
base = vim.fn.fnamemodify(utils.current_file_path(), ':p:h')
17+
end
1518
return base .. '/' .. path_str:gsub('^%./', '')
1619
elseif path_str:match('^%.%./') then
17-
local base = vim.fn.fnamemodify(utils.current_file_path(), ':p:h')
20+
if not base then
21+
base = vim.fn.fnamemodify(utils.current_file_path(), ':p:h')
22+
end
1823
return base .. '/' .. path_str
1924
end
2025
return false

tests/plenary/utils/fs_spec.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ describe('substitute_path', function()
4949
it('fails on all other relative paths', function()
5050
assert.is.False(fs_utils.substitute_path('a/b/c'))
5151
end)
52+
53+
it('allows passing a custom base', function()
54+
local output = fs_utils.substitute_path('./b/c', 'a/')
55+
assert(output)
56+
assert.are.same(output, 'a//b/c')
57+
end)
5258
end)
5359

5460
describe('get_real_path', function()

0 commit comments

Comments
 (0)