diff --git a/lua/obsidian/config.lua b/lua/obsidian/config.lua index 4f3ae84d..72cf67db 100644 --- a/lua/obsidian/config.lua +++ b/lua/obsidian/config.lua @@ -295,6 +295,7 @@ config.CompletionOpts.default = function() end ---@class obsidian.config.MappingOpts +---@field save_on_nav boolean If true, save the file when navigating to a link config.MappingOpts = {} ---Get defaults. @@ -306,6 +307,7 @@ config.MappingOpts.default = function() ["gf"] = mappings.gf_passthrough(), ["ch"] = mappings.toggle_checkbox(), [""] = mappings.smart_action(), + save_on_nav = false, -- Default to false to maintain backward compatibility } end diff --git a/lua/obsidian/util.lua b/lua/obsidian/util.lua index 1db3d044..2753cd00 100644 --- a/lua/obsidian/util.lua +++ b/lua/obsidian/util.lua @@ -745,6 +745,19 @@ end util.gf_passthrough = function() if util.cursor_on_markdown_link(nil, nil, true) then + -- Get the client to check settings + local client = require("obsidian").get_client() + if client and client.opts and client.opts.mappings and client.opts.mappings.save_on_nav then + -- Before following the link, save the current buffer + -- This ensures any new links in the current file are written to disk + if vim.api.nvim_buf_get_option(0, "modified") then + -- Schedule the save to avoid BufWritePre issues + vim.schedule(function() + vim.cmd "write" + end) + end + end + return "ObsidianFollowLink" else return "gf" @@ -754,6 +767,19 @@ end util.smart_action = function() -- follow link if possible if util.cursor_on_markdown_link(nil, nil, true) then + -- Get the client to check settings + local client = require("obsidian").get_client() + if client and client.opts and client.opts.mappings and client.opts.mappings.save_on_nav then + -- Before following the link, save the current buffer + -- This ensures any new links in the current file are written to disk + if vim.api.nvim_buf_get_option(0, "modified") then + -- Schedule the save to avoid BufWritePre issues + vim.schedule(function() + vim.cmd "write" + end) + end + end + return "ObsidianFollowLink" end