Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lua/obsidian/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -306,6 +307,7 @@ config.MappingOpts.default = function()
["gf"] = mappings.gf_passthrough(),
["<leader>ch"] = mappings.toggle_checkbox(),
["<cr>"] = mappings.smart_action(),
save_on_nav = false, -- Default to false to maintain backward compatibility
}
end

Expand Down
26 changes: 26 additions & 0 deletions lua/obsidian/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<cmd>ObsidianFollowLink<CR>"
else
return "gf"
Expand All @@ -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 "<cmd>ObsidianFollowLink<CR>"
end

Expand Down
28 changes: 17 additions & 11 deletions lua/obsidian/yaml/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local util = require "obsidian.util"
local parser = require "obsidian.yaml.parser"

local yaml = {}

---Deserialize a YAML string.
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading