Skip to content

Commit

Permalink
Apply work-around for nvim-lua/plenary.nvim#489
Browse files Browse the repository at this point in the history
  • Loading branch information
epwalsh committed Jan 26, 2024
1 parent 1c55eb1 commit 08890cb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lua/obsidian/img_paste.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ M.paste_img = function(fname, default_dir)
end

-- Ensure parent directory exists.
path:parent():mkdir { exists_ok = true, parents = true }
util.path_parent(path):mkdir { exists_ok = true, parents = true }

-- Paste image.
local result = save_clipboard_image(tostring(path))
Expand Down
2 changes: 1 addition & 1 deletion lua/obsidian/templates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ M.clone_template = function(template_name, note_path, client, title)
return
end

note_path:parent():mkdir { parents = true }
util.path_parent(note_path):mkdir { parents = true }

local template_path = Path:new(templates_dir) / template_name
local template_file = io.open(tostring(template_path), "r")
Expand Down
22 changes: 22 additions & 0 deletions lua/obsidian/util.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local Path = require "plenary.path"
local iter = require("obsidian.itertools").iter
local log = require "obsidian.log"

Expand Down Expand Up @@ -371,6 +372,27 @@ util.string_replace = function(s, what, with, n)
return s, count
end

------------------
-- Path helpers --
------------------

--- Get the parent directory of a path.
---
---@param path string|Path
---
---@return Path
util.parent_directory = function(path)
-- 'Path:parent()' has bugs on Windows, so we try 'vim.fs.dirname' first instead.
if vim.fs and vim.fs.dirname then
local dirname = vim.fs.dirname(tostring(path))
if dirname ~= nil then
return Path:new(dirname)
end
end

return Path:new(path):parent()
end

------------------------------------
-- Miscellaneous helper functions --
------------------------------------
Expand Down

0 comments on commit 08890cb

Please sign in to comment.