Skip to content

Commit f5176ea

Browse files
authored
Add work-around for plenary path bug on Windows (#349)
See nvim-lua/plenary.nvim#489
1 parent 217f7c7 commit f5176ea

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
### Fixed
1919

2020
- Fixed a YAML parsing issue with unquoted URLs in an array item.
21+
- Fixed an issue on Windows when cloning a template into a new note. The root cause was this bug in plenary: https://github.com/nvim-lua/plenary.nvim/issues/489. We've added a work-around.
2122

2223
## [v2.7.1](https://github.com/epwalsh/obsidian.nvim/releases/tag/v2.7.1) - 2024-01-23
2324

lua/obsidian/client.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ Client._daily = function(self, datetime)
966966
if not note:exists() then
967967
local write_frontmatter = true
968968
if self.opts.daily_notes.template then
969-
templates.clone_template(self.opts.daily_notes.template, tostring(path), self, note:display_name())
969+
templates.clone_template(self.opts.daily_notes.template, path, self, note:display_name())
970970
note = Note.from_file(path, self.dir)
971971
if note.has_frontmatter then
972972
write_frontmatter = false

lua/obsidian/img_paste.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ M.paste_img = function(fname, default_dir)
125125
end
126126

127127
-- Ensure parent directory exists.
128-
path:parent():mkdir { exists_ok = true, parents = true }
128+
util.parent_directory(path):mkdir { exists_ok = true, parents = true }
129129

130130
-- Paste image.
131131
local result = save_clipboard_image(tostring(path))

lua/obsidian/templates.lua

+10-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ end
5252
---Clone Template
5353
---
5454
---@param template_name string - name of a template in the configured templates folder
55-
---@param note_path string
55+
---@param note_path Path
5656
---@param client obsidian.Client
5757
---@param title string
5858
M.clone_template = function(template_name, note_path, client, title)
@@ -61,19 +61,24 @@ M.clone_template = function(template_name, note_path, client, title)
6161
log.err "Templates folder is not defined or does not exist"
6262
return
6363
end
64+
65+
util.parent_directory(note_path):mkdir { parents = true }
66+
6467
local template_path = Path:new(templates_dir) / template_name
65-
Path:new(note_path):parent():mkdir { parents = true }
6668
local template_file = io.open(tostring(template_path), "r")
67-
local note_file = io.open(tostring(note_path), "wb")
6869
if not template_file then
69-
return error("Unable to read template at " .. template_path)
70+
return log.error("Unable to read template at '%s'", template_path)
7071
end
72+
73+
local note_file = io.open(tostring(note_path), "wb")
7174
if not note_file then
72-
return error("Unable to write note at " .. note_path)
75+
return log.error("Unable to write note at '%s'", note_path)
7376
end
77+
7478
for line in template_file:lines "L" do
7579
note_file:write(M.substitute_template_variables(line, client, title))
7680
end
81+
7782
template_file:close()
7883
note_file:close()
7984
end

lua/obsidian/util.lua

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local Path = require "plenary.path"
12
local iter = require("obsidian.itertools").iter
23
local log = require "obsidian.log"
34

@@ -371,6 +372,27 @@ util.string_replace = function(s, what, with, n)
371372
return s, count
372373
end
373374

375+
------------------
376+
-- Path helpers --
377+
------------------
378+
379+
--- Get the parent directory of a path.
380+
---
381+
---@param path string|Path
382+
---
383+
---@return Path
384+
util.parent_directory = function(path)
385+
-- 'Path:parent()' has bugs on Windows, so we try 'vim.fs.dirname' first instead.
386+
if vim.fs and vim.fs.dirname then
387+
local dirname = vim.fs.dirname(tostring(path))
388+
if dirname ~= nil then
389+
return Path:new(dirname)
390+
end
391+
end
392+
393+
return Path:new(path):parent()
394+
end
395+
374396
------------------------------------
375397
-- Miscellaneous helper functions --
376398
------------------------------------

0 commit comments

Comments
 (0)