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 diff --git a/lua/obsidian/yaml/init.lua b/lua/obsidian/yaml/init.lua index f14c104b..3060214b 100644 --- a/lua/obsidian/yaml/init.lua +++ b/lua/obsidian/yaml/init.lua @@ -1,6 +1,5 @@ local util = require "obsidian.util" local parser = require "obsidian.yaml.parser" - local yaml = {} ---Deserialize a YAML string. @@ -10,6 +9,9 @@ yaml.loads = function(str) return parser.loads(str) end +-- New option to set quote style +yaml.quote_style = "double" -- Can be "single" or "double" + ---@param s string ---@return boolean local should_quote = function(s) @@ -27,6 +29,9 @@ local should_quote = function(s) -- Check if it's an empty string. elseif s == "" or string.match(s, "^[%s]+$") then return true + -- NEW: Check if it's an ISO date format (YYYY-MM-DDThh:mm:ss) + elseif string.match(s, "^%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:%d%d") then + return true else return false end @@ -36,27 +41,30 @@ end local dumps dumps = function(x, indent, order) local indent_str = string.rep(" ", indent) - if type(x) == "string" then if should_quote(x) then - x = string.gsub(x, '"', '\\"') - return { indent_str .. [["]] .. x .. [["]] } + -- Choose quote style based on configuration + if yaml.quote_style == "single" then + -- For single quotes, escape single quotes by doubling them + x = string.gsub(x, "'", "''") + return { indent_str .. [[']] .. x .. [[']] } + else + -- Default to double quotes (original behavior) + x = string.gsub(x, '"', '\\"') + return { indent_str .. [["]] .. x .. [["]] } + end else return { indent_str .. x } end end - if type(x) == "boolean" then return { indent_str .. tostring(x) } end - if type(x) == "number" then return { indent_str .. tostring(x) } end - if type(x) == "table" then local out = {} - if util.tbl_is_array(x) then for _, v in ipairs(x) do local item_lines = dumps(v, indent + 2) @@ -87,16 +95,14 @@ dumps = function(x, indent, order) end end end - return out end - error("Can't convert object with type " .. type(x) .. " to YAML") end ---Dump an object to YAML lines. ---@param x any ----@param order function +---@param order function|? ---@return string[] yaml.dumps_lines = function(x, order) return dumps(x, 0, order)