Skip to content

Commit 87800bb

Browse files
committed
feat(core): enhance org-insert-link, add completion for relative path or ~/
_squash: keep Url.path, and add a realpath filed
1 parent ac9a6e0 commit 87800bb

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

lua/orgmode/org/hyperlinks/init.lua

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,13 @@ function Hyperlinks.find_by_filepath(url)
2424
return {}
2525
end
2626
--TODO integrate with orgmode.utils.fs or orgmode.objects.url
27-
local file_base_no_start_path = vim.pesc(file_base:gsub('^%./', '') .. '')
28-
local is_relative_path = file_base:match('^%./')
29-
local current_file_directory = vim.pesc(fs.get_current_file_dir())
3027
local valid_filenames = {}
3128
for _, f in ipairs(filenames) do
32-
if is_relative_path then
33-
local match = f:match('^' .. current_file_directory .. '/(' .. file_base_no_start_path .. '.*%.org)$')
34-
if match then
35-
table.insert(valid_filenames, './' .. match)
36-
end
37-
else
38-
if f:find('^' .. file_base) then
39-
table.insert(valid_filenames, f)
29+
if f:find('^' .. file_base) then
30+
if url.realpath then
31+
f = f:gsub(file_base, url.path)
4032
end
33+
table.insert(valid_filenames, f)
4134
end
4235
end
4336

lua/orgmode/org/hyperlinks/url.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local fs = require('orgmode.utils.fs')
12
---@alias OrgUrlPathType 'file' | 'headline' | 'custom-id' | 'id' | 'external-url' | 'plain' | nil
23
---@alias OrgUrlTargetType 'headline' | 'custom-id' | 'line-number' | 'unknown' | nil
34

@@ -116,7 +117,7 @@ function Url:get_file()
116117
if not self:is_file() then
117118
return nil
118119
end
119-
return self.path
120+
return self.realpath or self.path
120121
end
121122

122123
---@return string | nil
@@ -246,8 +247,12 @@ function Url:_parse_path_type()
246247
return
247248
end
248249

249-
if first_char == '.' and (self.path:sub(1, 3) == '../' or self.path:sub(1, 2) == './') then
250+
if
251+
(first_char == '.' and (self.path:sub(1, 3) == '../' or self.path:sub(1, 2) == './'))
252+
or (first_char == '~' and self.path:sub(2, 2) == '/')
253+
then
250254
self.path_type = 'file'
255+
self.realpath = fs.substitute_path(self.path) or self.path
251256
return
252257
end
253258

0 commit comments

Comments
 (0)