From 3e0eff72a86a5c003e946002e38cab973116278a Mon Sep 17 00:00:00 2001 From: oddish3 Date: Tue, 18 Mar 2025 18:13:58 +0000 Subject: [PATCH 1/2] add save_on_nav so backlinks work automatically --- lua/obsidian/config.lua | 2 ++ lua/obsidian/util.lua | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) 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..425767a7 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 From 2955fa679d97ab8c4b7b130483418c493aaaea16 Mon Sep 17 00:00:00 2001 From: oddish3 Date: Tue, 18 Mar 2025 21:10:31 +0000 Subject: [PATCH 2/2] linted --- lua/obsidian/util.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lua/obsidian/util.lua b/lua/obsidian/util.lua index 425767a7..2753cd00 100644 --- a/lua/obsidian/util.lua +++ b/lua/obsidian/util.lua @@ -750,14 +750,14 @@ util.gf_passthrough = function() 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 + if vim.api.nvim_buf_get_option(0, "modified") then -- Schedule the save to avoid BufWritePre issues vim.schedule(function() - vim.cmd('write') + vim.cmd "write" end) end end - + return "ObsidianFollowLink" else return "gf" @@ -772,14 +772,14 @@ util.smart_action = function() 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 + if vim.api.nvim_buf_get_option(0, "modified") then -- Schedule the save to avoid BufWritePre issues vim.schedule(function() - vim.cmd('write') + vim.cmd "write" end) end end - + return "ObsidianFollowLink" end