Skip to content

Commit 6f73b46

Browse files
authored
fix: links with relative path to parent directories (#585)
1 parent 7834aea commit 6f73b46

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lua/orgmode/org/hyperlinks.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ local utils = require('orgmode.utils')
33
local Hyperlinks = {}
44

55
local function get_file_from_context(ctx)
6-
return (ctx.hyperlinks and ctx.hyperlinks.filepath and Files.get(ctx.hyperlinks.filepath) or Files.get_current_file())
6+
local filepath = (ctx.hyperlinks and ctx.hyperlinks.filepath)
7+
if not filepath then
8+
return Files.get_current_file()
9+
end
10+
local canonical = vim.loop.fs_realpath(filepath)
11+
if not canonical then
12+
return Files.get_current_file()
13+
end
14+
15+
return Files.get(canonical)
716
end
817

918
local function update_hyperlink_ctx(ctx)
@@ -23,6 +32,10 @@ local function update_hyperlink_ctx(ctx)
2332
file_match = Hyperlinks.get_file_real_path(file_match)
2433
end
2534

35+
if file_match then
36+
file_match = vim.loop.fs_realpath(file_match)
37+
end
38+
2639
if file_match and Files.get(file_match) then
2740
hyperlinks_ctx.filepath = Files.get(file_match).filename
2841
hyperlinks_ctx.headline = ctx.line:match('file:.-::(%*.-)$')

tests/plenary/org/autocompletion_spec.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,16 @@ describe('Autocompletion', function()
218218
})
219219

220220
mock_line(api, string.format(' [[file:%s::*', filename))
221+
local vim_loop = mock(vim.loop, true)
222+
vim_loop.fs_realpath.returns(filename)
221223
result = OrgmodeOmniCompletion(0, '*')
222224
assert.are.same({
223225
{ menu = '[Org]', word = '*' .. headlines[1].title },
224226
{ menu = '[Org]', word = '*' .. headlines[2].title },
225227
}, result)
226228

229+
mock.revert(vim_loop)
230+
227231
mock.revert(MockFiles)
228232

229233
mock.revert(api)

0 commit comments

Comments
 (0)