Skip to content

Commit ae4197a

Browse files
author
Sebastian Flügge
committed
feat: insert intra-file links without id
Linking to other headlines within the same file doesn't create id links anymore.
1 parent bbe36e7 commit ae4197a

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

lua/telescope-orgmode/actions.lua

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,14 @@ function M.refile(closest_headline)
6565
end
6666
end
6767

68-
function M.insert(_)
68+
function M.insert(opts)
6969
return function(prompt_bufnr)
7070
actions.close(prompt_bufnr)
7171

7272
---@type MatchEntry
7373
local entry = action_state.get_selected_entry()
7474

75-
local api_object = entry.value.headline
76-
and org.get_api_headline(entry.filename, entry.value.headline.line_number)
77-
or org.get_api_file(entry.filename)
78-
79-
if not api_object then
80-
error('Could not find ' .. (entry.value.headline and 'headline' or 'file') .. ' for link')
81-
end
82-
83-
local destination = api_object:get_link()
75+
local destination = org.get_link_destination(entry, opts)
8476

8577
org.insert_link(destination)
8678
return true

lua/telescope-orgmode/org.lua

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,44 @@ function M.get_api_file(filename)
118118
return OrgApi.load(filename)
119119
end
120120

121+
--- Get intra-file link for headline (simple *Headline format)
122+
---@param entry table
123+
---@return string
124+
function M.get_intra_file_link(entry)
125+
return '*' .. entry.value.headline.title
126+
end
127+
128+
--- Get inter-file link using full API format
129+
---@param entry table
130+
---@return string
131+
function M.get_inter_file_link(entry)
132+
if entry.value.headline then
133+
local api_headline = M.get_api_headline(entry.filename, entry.value.headline.line_number)
134+
if api_headline then
135+
return api_headline:get_link()
136+
end
137+
error('Could not find headline for link')
138+
else
139+
local api_file = M.get_api_file(entry.filename)
140+
if api_file then
141+
return api_file:get_link()
142+
end
143+
error('Could not find file for link')
144+
end
145+
end
146+
147+
--- Get link destination (chooses intra-file vs inter-file format)
148+
---@param entry table
149+
---@param opts table
150+
---@return string
151+
function M.get_link_destination(entry, opts)
152+
-- Use intra-file format for headlines in the same file
153+
if entry.value.headline and opts.original_file and entry.filename == opts.original_file then
154+
return M.get_intra_file_link(entry)
155+
end
156+
157+
-- Use inter-file format for everything else
158+
return M.get_inter_file_link(entry)
159+
end
160+
121161
return M

lua/telescope-orgmode/picker/insert_link.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ return function(opts)
2020
sorter = conf.generic_sorter(opts),
2121
previewer = conf.grep_previewer(opts),
2222
attach_mappings = function(_, map)
23-
action_set.select:replace(actions.insert())
23+
action_set.select:replace(actions.insert(opts))
2424
mappings.attach_mappings(map, opts)
2525
return true
2626
end,

0 commit comments

Comments
 (0)