Skip to content

Commit

Permalink
Merge pull request #219 from epwalsh/template-frontmatter
Browse files Browse the repository at this point in the history
Fix inserting daily note template w/ frontmatter
  • Loading branch information
epwalsh committed Nov 9, 2023
2 parents 6e72c9e + 460e8ec commit cbcce32
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fixed a completion bug (#212).
- Fixed a bug where the frontmatter of daily note template would be overwritten upon inserting the template.
- Skip templates directory when searching for notes.

### Removed

Expand Down
25 changes: 19 additions & 6 deletions lua/obsidian/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ end

---@param search string
---@param search_opts string[]|?
---@param find_opts string[]|?
---@return function
Client._search_iter_async = function(self, search, search_opts)
Client._search_iter_async = function(self, search, search_opts, find_opts)
local channel = require("plenary.async.control").channel
local search_async = require("obsidian.search").search_async
local find_async = require("obsidian.search").find_async
Expand Down Expand Up @@ -82,8 +83,13 @@ Client._search_iter_async = function(self, search, search_opts)

local cmds_done = 0 -- out of the two, one for 'search' and one for 'find'
search_opts = search_opts and search_opts or {}
find_opts = find_opts and find_opts or {}
if self.opts.templates ~= nil and self.opts.templates.subdir ~= nil then
search_opts[#search_opts + 1] = "-g!" .. self.opts.templates.subdir
find_opts[#find_opts + 1] = "-g!" .. self.opts.templates.subdir
end
search_async(self.dir, search, vim.tbl_flatten { search_opts, "-m=1" }, on_search_match, on_exit)
find_async(self.dir, search, self.opts.sort_by, self.opts.sort_reversed, on_find_match, on_exit)
find_async(self.dir, search, self.opts.sort_by, self.opts.sort_reversed, find_opts, on_find_match, on_exit)

return function()
while true do
Expand Down Expand Up @@ -301,14 +307,21 @@ Client._daily = function(self, datetime)
-- Create Note object and save if it doesn't already exist.
local note = Note.new(id, { alias }, { "daily-notes" }, path)
if not note:exists() then
local write_frontmatter = true
if self.opts.daily_notes.template then
util.clone_template(self.opts.daily_notes.template, tostring(path), self, note:display_name())
note = Note.from_file(path, self.dir)
if note.has_frontmatter then
write_frontmatter = false
end
end
local frontmatter = nil
if self.opts.note_frontmatter_func ~= nil then
frontmatter = self.opts.note_frontmatter_func(note)
if write_frontmatter then
local frontmatter = nil
if self.opts.note_frontmatter_func ~= nil then
frontmatter = self.opts.note_frontmatter_func(note)
end
note:save(nil, not self.opts.disable_frontmatter, frontmatter)
end
note:save(nil, not self.opts.disable_frontmatter, frontmatter)
echo.info("Created note " .. tostring(note.id) .. " at " .. tostring(note.path), self.opts.log_level)
end

Expand Down
14 changes: 8 additions & 6 deletions lua/obsidian/search.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ M.FIND_CMD = { "rg", "--no-config", "--files", "--type=md" }
---@param term string|?
---@param quote boolean|?
---@return string[]
M.build_find_cmd = function(path, sort_by, sort_reversed, term, quote)
M.build_find_cmd = function(path, sort_by, sort_reversed, term, opts, quote)
if quote == nil then
quote = true
end
Expand All @@ -138,7 +138,7 @@ M.build_find_cmd = function(path, sort_by, sort_reversed, term, quote)
if path ~= nil and path ~= "." then
additional_opts[#additional_opts + 1] = tostring(path)
end
return vim.tbl_flatten { M.FIND_CMD, additional_opts }
return vim.tbl_flatten { M.FIND_CMD, opts and opts or {}, additional_opts }
end

---Find markdown files in a directory matching a given term. Return an iterator
Expand All @@ -148,12 +148,13 @@ end
---@param term string
---@param sort_by string|?
---@param sort_reversed boolean|?
---@param opts string[]|?
---@return function
M.find = function(dir, term, sort_by, sort_reversed)
M.find = function(dir, term, sort_by, sort_reversed, opts)
local paths = Deque.new()
local done = false

M.find_async(dir, term, sort_by, sort_reversed, function(path)
M.find_async(dir, term, sort_by, sort_reversed, opts, function(path)
paths:pushright(path)
end, function(_, _, _)
done = true
Expand Down Expand Up @@ -181,11 +182,12 @@ end
---@param term string
---@param sort_by string|?
---@param sort_reversed boolean|?
---@param opts string[]|?
---@param on_match function(string)
---@param on_exit function(integer) |?
M.find_async = function(dir, term, sort_by, sort_reversed, on_match, on_exit)
M.find_async = function(dir, term, sort_by, sort_reversed, opts, on_match, on_exit)
local norm_dir = vim.fs.normalize(tostring(dir))
local cmd = M.build_find_cmd(norm_dir, sort_by, sort_reversed, term, false)
local cmd = M.build_find_cmd(norm_dir, sort_by, sort_reversed, term, opts, false)
Job:new({
command = cmd[1],
args = { unpack(cmd, 2) },
Expand Down

0 comments on commit cbcce32

Please sign in to comment.