Skip to content
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

Add :ObsidianNewFromTemplate command #621

Merged
merged 13 commits into from
Jul 11, 2024
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) with respect to the public API, which currently includes the installation steps, dependencies, configuration, keymappings, commands, and other plugin functionality. At the moment this does *not* include the Lua `Client` API, although in the future it will once that API stabilizes.

### Added

- Added `ObsidianNewFromTemplate` command. This command will create a new note from a template.

malko42 marked this conversation as resolved.
Show resolved Hide resolved
## Unreleased

## [v3.7.14](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.7.14) - 2024-06-04
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 @@ -151,6 +152,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
45 changes: 45 additions & 0 deletions lua/obsidian/commands/new_from_template.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local util = require "obsidian.util"
local log = require "obsidian.log"

---@param client obsidian.Client
return function(client, data)
---@type obsidian.Note
malko42 marked this conversation as resolved.
Show resolved Hide resolved
if not client:templates_dir() then
log.err "Templates folder is not defined or does not exist"
return
end

local note
if data.args:len() > 0 then
malko42 marked this conversation as resolved.
Show resolved Hide resolved
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 })

local function insert_template(name)
client:write_note_to_buffer(note, { template = name })
end

malko42 marked this conversation as resolved.
Show resolved Hide resolved
local picker = client:picker()
if not picker then
log.err "No picker configured"
return
end
malko42 marked this conversation as resolved.
Show resolved Hide resolved

picker:find_templates {
callback = function(path)
insert_template(path)
malko42 marked this conversation as resolved.
Show resolved Hide resolved
end,
}
client:write_note_to_buffer(note)
malko42 marked this conversation as resolved.
Show resolved Hide resolved
end