Skip to content

Commit

Permalink
Don't write new note from :ObsidianFollowLink
Browse files Browse the repository at this point in the history
  • Loading branch information
epwalsh committed Mar 12, 2024
1 parent 7c9a337 commit 4e21c8c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
16 changes: 12 additions & 4 deletions lua/obsidian/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,13 @@ Client.follow_link_async = function(self, link, opts)
id = res.location
end

local note = self:create_note { title = res.name, id = id, aliases = aliases }
return self:open_note(note, { open_strategy = opts.open_strategy })
local note = self:create_note { title = res.name, id = id, aliases = aliases, no_write = true }
return self:open_note(note, {
open_strategy = opts.open_strategy,
callback = function(bufnr)
self:write_note_to_buffer(note, { bufnr = bufnr })
end,
})
else
log.warn "Aborted"
return
Expand Down Expand Up @@ -909,7 +914,7 @@ end
--- Open a note in a buffer.
---
---@param note_or_path string|obsidian.Path|obsidian.Note
---@param opts { line: integer|?, col: integer|?, open_strategy: obsidian.config.OpenStrategy|?, sync: boolean|? }|?
---@param opts { line: integer|?, col: integer|?, open_strategy: obsidian.config.OpenStrategy|?, sync: boolean|?, callback: fun(bufnr: integer)|? }|?
Client.open_note = function(self, note_or_path, opts)
opts = opts or {}

Expand All @@ -932,7 +937,10 @@ Client.open_note = function(self, note_or_path, opts)
local function open_it()
local open_cmd = util.get_open_strategy(opts.open_strategy and opts.open_strategy or self.opts.open_notes_in)
---@cast path obsidian.Path
util.open_buffer(path, { line = opts.line, col = opts.col, cmd = open_cmd })
local bufnr = util.open_buffer(path, { line = opts.line, col = opts.col, cmd = open_cmd })
if opts.callback then
opts.callback(bufnr)
end
end

if opts.sync then
Expand Down
13 changes: 12 additions & 1 deletion lua/obsidian/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1035,17 +1035,22 @@ end
---
---@param path string|obsidian.Path
---@param opts { line: integer|?, col: integer|?, cmd: string|? }|?
---@return integer bufnr
util.open_buffer = function(path, opts)
local Path = require "obsidian.path"

path = Path.new(path):resolve()
opts = opts and opts or {}
local cmd = util.strip_whitespace(opts.cmd and opts.cmd or "e")

---@type integer|?
local result_bufnr

-- Check for existing buffer and use 'drop' command if one is found.
for _, bufname in util.get_named_buffers() do
for bufnr, bufname in util.get_named_buffers() do
if bufname == tostring(path) then
cmd = "drop"
result_bufnr = bufnr
break
end
end
Expand All @@ -1054,6 +1059,12 @@ util.open_buffer = function(path, opts)
if opts.line then
vim.api.nvim_win_set_cursor(0, { tonumber(opts.line), opts.col and opts.col or 0 })
end

if not result_bufnr then
result_bufnr = vim.api.nvim_get_current_buf()
end

return result_bufnr
end

--- Get a nice icon for a file or URL, if possible.
Expand Down

0 comments on commit 4e21c8c

Please sign in to comment.