@@ -3,10 +3,7 @@ local utils = require('orgmode.utils')
3
3
local Hyperlinks = {}
4
4
5
5
local function get_file_from_context (ctx )
6
- return (
7
- ctx .hyperlinks and ctx .hyperlinks .filepath and Files .get (ctx .hyperlinks .filepath , true )
8
- or Files .get_current_file ()
9
- )
6
+ return (ctx .hyperlinks and ctx .hyperlinks .filepath and Files .get (ctx .hyperlinks .filepath ) or Files .get_current_file ())
10
7
end
11
8
12
9
local function update_hyperlink_ctx (ctx )
@@ -22,7 +19,9 @@ local function update_hyperlink_ctx(ctx)
22
19
}
23
20
24
21
local file_match = ctx .line :match (' file:(.-)::' )
25
- file_match = file_match and vim .fn .fnamemodify (file_match , ' :p' ) or file_match
22
+ if file_match then
23
+ file_match = Hyperlinks .get_file_real_path (file_match )
24
+ end
26
25
27
26
if file_match and Files .get (file_match ) then
28
27
hyperlinks_ctx .filepath = Files .get (file_match ).filename
41
40
function Hyperlinks .find_by_filepath (ctx )
42
41
local filenames = Files .filenames ()
43
42
local file_base = ctx .base :gsub (' ^file:' , ' ' )
44
- if vim .trim (file_base ) ~= ' ' then
45
- filenames = vim .tbl_filter (function (f )
46
- return f :find (' ^' .. file_base )
47
- end , filenames )
43
+ local file_base_no_start_path = file_base :gsub (' ^%./' , ' ' ) .. ' '
44
+ local is_relative_path = file_base :match (' ^%./' )
45
+ local current_file_directory = vim .fn .fnamemodify (utils .current_file_path (), ' :p:h' )
46
+ local valid_filenames = {}
47
+ for _ , f in ipairs (filenames ) do
48
+ if is_relative_path then
49
+ local match = f :match (' ^' .. current_file_directory .. ' /(' .. file_base_no_start_path .. ' [^/]*%.org)$' )
50
+ if match then
51
+ table.insert (valid_filenames , ' ./' .. match )
52
+ end
53
+ else
54
+ if f :find (' ^' .. file_base ) then
55
+ table.insert (valid_filenames , f )
56
+ end
57
+ end
48
58
end
49
59
50
60
-- Outer checks already filter cases where `ctx.skip_add_prefix` is truthy,
51
61
-- so no need to check it here
52
62
return vim .tbl_map (function (path )
53
63
return ' file:' .. path
54
- end , filenames )
64
+ end , valid_filenames )
55
65
end
56
66
57
67
function Hyperlinks .find_by_custom_id_property (ctx )
@@ -135,4 +145,14 @@ function Hyperlinks.find_matching_links(ctx)
135
145
return all
136
146
end
137
147
148
+ function Hyperlinks .get_file_real_path (url_path )
149
+ local path = url_path
150
+ path = path :gsub (' ^file:' , ' ' )
151
+ if path :match (' ^/' ) then
152
+ return path
153
+ end
154
+ path = path :gsub (' ^./' , ' ' )
155
+ return vim .fn .fnamemodify (utils .current_file_path (), ' :p:h' ) .. ' /' .. path
156
+ end
157
+
138
158
return Hyperlinks
0 commit comments