Skip to content

Add :ObsidianNewFromTemplate command #621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 11, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

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

### Fixed

Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ _Keep in mind this plugin is not meant to replace Obsidian, but to complement it

- `:ObsidianToggleCheckbox` to cycle through checkbox options.

- `:ObsidianNewFromTemplate [PATH] [TEMPLATE]` to create a new note from a template in the templates folder. Selecting from a list using your preferred picker.
This command has one optional argument: the path to the new note.

- `:ObsidianTOC` to load the table of contents of the current note into a picker list.

### Demo
Expand Down Expand Up @@ -655,8 +658,9 @@ mappings = {

### Using templates

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.

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.
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.
Substitutions for `{{id}}`, `{{title}}`, `{{path}}`, `{{date}}`, and `{{time}}` are supported out-of-the-box.
For example, with the following configuration

```lua
Expand Down
3 changes: 3 additions & 0 deletions lua/obsidian/commands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local command_lookups = {
ObsidianSearch = "obsidian.commands.search",
ObsidianTags = "obsidian.commands.tags",
ObsidianTemplate = "obsidian.commands.template",
ObsidianNewFromTemplate = "obsidian.commands.new_from_template",
ObsidianQuickSwitch = "obsidian.commands.quick_switch",
ObsidianLinkNew = "obsidian.commands.link_new",
ObsidianLink = "obsidian.commands.link",
Expand Down Expand Up @@ -152,6 +153,8 @@ M.register("ObsidianSearch", { opts = { nargs = "?", desc = "Search vault" } })

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

M.register("ObsidianNewFromTemplate", { opts = { nargs = "?", desc = "Create a new note from a template" } })

M.register("ObsidianQuickSwitch", { opts = { nargs = "?", desc = "Switch notes" } })

M.register("ObsidianLinkNew", { opts = { nargs = "?", range = true, desc = "Link selected text to a new note" } })
Expand Down
40 changes: 40 additions & 0 deletions lua/obsidian/commands/new_from_template.lua
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