-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
:ObsidianNewFromTemplate
command (#621)
* [feature] Add ObsidianNewFromTemplate feature The goal is to be able to create a new note while using an existing template * Reflect changes in CHANGELOG.md * [misc] Handle PR feedback * Update new_from_template.lua * Update new_from_template.lua * Handle feedback - move up picker check to early exit if needed - unwrap callback - update README to reflect `:ObsidianNewFromTemplate` addition * Update CHANGELOG.md * Update README.md --------- Co-authored-by: Maxime DAFFIS <[email protected]> Co-authored-by: Pete Walsh <[email protected]>
- Loading branch information
1 parent
7a9081a
commit 837e778
Showing
4 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
local util = require "obsidian.util" | ||
local log = require "obsidian.log" | ||
|
||
---@param client obsidian.Client | ||
return function(client, data) | ||
if not client:templates_dir() then | ||
log.err "Templates folder is not defined or does not exist" | ||
return | ||
end | ||
|
||
local picker = client:picker() | ||
if not picker then | ||
log.err "No picker configured" | ||
return | ||
end | ||
|
||
---@type obsidian.Note | ||
local note | ||
if data.args and data.args:len() > 0 then | ||
note = client:create_note { title = data.args, no_write = true } | ||
else | ||
local title = util.input("Enter title or path (optional): ", { completion = "file" }) | ||
if not title then | ||
log.warn "Aborted" | ||
return | ||
elseif title == "" then | ||
title = nil | ||
end | ||
note = client:create_note { title = title, no_write = true } | ||
end | ||
|
||
-- Open the note in a new buffer. | ||
client:open_note(note, { sync = true }) | ||
|
||
picker:find_templates { | ||
callback = function(name) | ||
client:write_note_to_buffer(note, { template = name }) | ||
end, | ||
} | ||
end |