Skip to content

Commit 837e778

Browse files
malko42Maxime DAFFISepwalsh
authored
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 <m.daffis@criteo.com> Co-authored-by: Pete Walsh <epwalsh10@gmail.com>
1 parent 7a9081a commit 837e778

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Added `:ObsidianTOC` command for loading the table of contents of the current note into a picker list.
13+
- Added `:ObsidianNewFromTemplate` command. This command creates a new note from a template.
1314

1415
### Fixed
1516

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ _Keep in mind this plugin is not meant to replace Obsidian, but to complement it
8686

8787
- `:ObsidianToggleCheckbox` to cycle through checkbox options.
8888

89+
- `:ObsidianNewFromTemplate [PATH] [TEMPLATE]` to create a new note from a template in the templates folder. Selecting from a list using your preferred picker.
90+
This command has one optional argument: the path to the new note.
91+
8992
- `:ObsidianTOC` to load the table of contents of the current note into a picker list.
9093

9194
### Demo
@@ -655,8 +658,9 @@ mappings = {
655658

656659
### Using templates
657660

658-
To insert a template, run the command `:ObsidianTemplate`. This will open a list of available templates in your templates folder with your preferred picker. Select a template and hit `<CR>` to insert. Substitutions for `{{id}}`, `{{title}}`, `{{path}}`, `{{date}}`, and `{{time}}` are supported out-of-the-box.
659-
661+
To insert a template in the current note, run the command `:ObsidianTemplate`. This will open a list of available templates in your templates folder with your preferred picker. Select a template and hit `<CR>` to insert.
662+
To create a new note from a template, run the command `:ObsidianNewFromTemplate`. This will prompt you for an optional path for the new note and will open a list of available templates in your templates folder with your preferred picker. Select a template and hit `<CR>` to create the new note with the selected template.
663+
Substitutions for `{{id}}`, `{{title}}`, `{{path}}`, `{{date}}`, and `{{time}}` are supported out-of-the-box.
660664
For example, with the following configuration
661665

662666
```lua

lua/obsidian/commands/init.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ local command_lookups = {
1414
ObsidianSearch = "obsidian.commands.search",
1515
ObsidianTags = "obsidian.commands.tags",
1616
ObsidianTemplate = "obsidian.commands.template",
17+
ObsidianNewFromTemplate = "obsidian.commands.new_from_template",
1718
ObsidianQuickSwitch = "obsidian.commands.quick_switch",
1819
ObsidianLinkNew = "obsidian.commands.link_new",
1920
ObsidianLink = "obsidian.commands.link",
@@ -152,6 +153,8 @@ M.register("ObsidianSearch", { opts = { nargs = "?", desc = "Search vault" } })
152153

153154
M.register("ObsidianTemplate", { opts = { nargs = "?", desc = "Insert a template" } })
154155

156+
M.register("ObsidianNewFromTemplate", { opts = { nargs = "?", desc = "Create a new note from a template" } })
157+
155158
M.register("ObsidianQuickSwitch", { opts = { nargs = "?", desc = "Switch notes" } })
156159

157160
M.register("ObsidianLinkNew", { opts = { nargs = "?", range = true, desc = "Link selected text to a new note" } })
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
local util = require "obsidian.util"
2+
local log = require "obsidian.log"
3+
4+
---@param client obsidian.Client
5+
return function(client, data)
6+
if not client:templates_dir() then
7+
log.err "Templates folder is not defined or does not exist"
8+
return
9+
end
10+
11+
local picker = client:picker()
12+
if not picker then
13+
log.err "No picker configured"
14+
return
15+
end
16+
17+
---@type obsidian.Note
18+
local note
19+
if data.args and data.args:len() > 0 then
20+
note = client:create_note { title = data.args, no_write = true }
21+
else
22+
local title = util.input("Enter title or path (optional): ", { completion = "file" })
23+
if not title then
24+
log.warn "Aborted"
25+
return
26+
elseif title == "" then
27+
title = nil
28+
end
29+
note = client:create_note { title = title, no_write = true }
30+
end
31+
32+
-- Open the note in a new buffer.
33+
client:open_note(note, { sync = true })
34+
35+
picker:find_templates {
36+
callback = function(name)
37+
client:write_note_to_buffer(note, { template = name })
38+
end,
39+
}
40+
end

0 commit comments

Comments
 (0)