Skip to content

Commit

Permalink
Don't write note on :ObsidianNew (#490)
Browse files Browse the repository at this point in the history
Closes #489.
  • Loading branch information
epwalsh committed Mar 11, 2024
1 parent 52e91bc commit e6b7ce1
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Changed

- `:ObsidianNew` won't write the new note to disk, it will only populate the buffer.

### Fixed

- Fixed renaming when passing file path
Expand Down
36 changes: 35 additions & 1 deletion lua/obsidian/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1771,18 +1771,52 @@ Client.write_note = function(self, note, opts)
log.info("%s note '%s' at '%s'", verb, note.id, self:vault_relative_path(note.path) or note.path)
end

--- Write the note to a buffer.
---
---@param note obsidian.Note
---@param opts { bufnr: integer|?, template: string|? }|? Options.
---
--- Options:
--- - `bufnr`: Override the buffer to write to. Defaults to current buffer.
--- - `template`: The name of a template to use if the buffer is empty.
---
---@return boolean updated If the buffer was updated.
Client.write_note_to_buffer = function(self, note, opts)
opts = opts or {}

if opts.template and util.buffer_is_empty(opts.bufnr) then
require("obsidian.templates").insert_template(opts.template, self, util.get_active_window_cursor_location())
end

local frontmatter = nil
local should_save_frontmatter = self:should_save_frontmatter(note)
if should_save_frontmatter and self.opts.note_frontmatter_func ~= nil then
frontmatter = self.opts.note_frontmatter_func(note)
end

return note:save_to_buffer {
bufnr = opts.bufnr,
insert_frontmatter = should_save_frontmatter,
frontmatter = frontmatter,
}
end

--- Update the frontmatter in a buffer for the note.
---
---@param note obsidian.Note
---@param bufnr integer|?
---
---@return boolean updated If the the frontmatter was updated.
Client.update_frontmatter = function(self, note, bufnr)
if not self:should_save_frontmatter(note) then
return false
end

local frontmatter = nil
if self.opts.note_frontmatter_func ~= nil then
frontmatter = self.opts.note_frontmatter_func(note)
end
return note:save_to_buffer(bufnr, frontmatter)
return note:save_to_buffer { bufnr = bufnr, frontmatter = frontmatter }
end

--- Get the path to a daily note.
Expand Down
9 changes: 6 additions & 3 deletions lua/obsidian/commands/new.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ return function(client, data)
---@type obsidian.Note
local note
if data.args:len() > 0 then
note = client:create_note { title = data.args }
note = client:create_note { title = data.args, no_write = true }
else
local title = util.input "Enter title (optional): "
if not title then
Expand All @@ -15,7 +15,10 @@ return function(client, data)
elseif title == "" then
title = nil
end
note = client:create_note { title = title }
note = client:create_note { title = title, no_write = true }
end
client:open_note(note)

-- Open the note in a new buffer.
client:open_note(note, { sync = true })
client:write_note_to_buffer(note)
end
4 changes: 0 additions & 4 deletions lua/obsidian/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,6 @@ obsidian.setup = function(opts)
-- Run pre-write-note callback.
client.callback_manager:pre_write_note(note)

if not client:should_save_frontmatter(note) then
return
end

-- Update buffer with new frontmatter.
if client:update_frontmatter(note, bufnr) then
log.info "Updated frontmatter"
Expand Down
27 changes: 21 additions & 6 deletions lua/obsidian/note.lua
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ Note.save = function(self, opts)
local save_path = Path.new(assert(opts.path or self.path)):resolve()
assert(save_path:parent()):mkdir { parents = true, exist_ok = true }

-- Read contents from existing file, if there is one, skipping frontmatter.
-- Read contents from existing file or buffer, if there is one, skipping frontmatter.
-- TODO: check for open buffer?
---@type string[]
local content = {}
Expand Down Expand Up @@ -728,18 +728,33 @@ end

--- Save frontmatter to the given buffer.
---
---@param bufnr integer|?
---@param frontmatter table|?
---@param opts { bufnr: integer|?, insert_frontmatter: boolean|?, frontmatter: table|? }|? Options.
---
---@return boolean updated True if the buffer lines were updated, false otherwise.
Note.save_to_buffer = function(self, bufnr, frontmatter)
Note.save_to_buffer = function(self, opts)
opts = opts or {}

local bufnr = opts.bufnr
if not bufnr then
bufnr = self.bufnr or 0
end

local cur_buf_note = Note.from_buffer(bufnr)
local new_lines = self:frontmatter_lines(nil, frontmatter)
local cur_lines

---@type string[]
local new_lines
if opts.insert_frontmatter ~= false then
new_lines = self:frontmatter_lines(nil, opts.frontmatter)
else
new_lines = {}
end

if util.buffer_is_empty(bufnr) and self.title ~= nil then
table.insert(new_lines, "# " .. self.title)
end

---@type string[]
local cur_lines = {}
if cur_buf_note.frontmatter_end_line ~= nil then
cur_lines = vim.api.nvim_buf_get_lines(bufnr, 0, cur_buf_note.frontmatter_end_line, false)
end
Expand Down
19 changes: 19 additions & 0 deletions lua/obsidian/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1279,4 +1279,23 @@ util.resolve_date_macro = function(macro)
return out
end

--- Check if a buffer is empty.
---
---@param bufnr integer|?
---
---@return boolean
util.buffer_is_empty = function(bufnr)
bufnr = bufnr or 0
if vim.api.nvim_buf_line_count(bufnr) > 1 then
return false
else
local first_text = vim.api.nvim_buf_get_text(bufnr, 0, 0, 0, 0, {})
if vim.tbl_isempty(first_text) or first_text[1] == "" then
return true
else
return false
end
end
end

return util

0 comments on commit e6b7ce1

Please sign in to comment.