diff --git a/CHANGELOG.md b/CHANGELOG.md index c42fb7415..4b495ef95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index cd6440e04..b255ba690 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 `` 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 `` 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 `` 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 diff --git a/lua/obsidian/commands/init.lua b/lua/obsidian/commands/init.lua index 4416a4233..8055b94ed 100644 --- a/lua/obsidian/commands/init.lua +++ b/lua/obsidian/commands/init.lua @@ -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", @@ -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" } }) diff --git a/lua/obsidian/commands/new_from_template.lua b/lua/obsidian/commands/new_from_template.lua new file mode 100644 index 000000000..e111c04b0 --- /dev/null +++ b/lua/obsidian/commands/new_from_template.lua @@ -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