diff --git a/neovim/lua/lazy_plugins.lua b/neovim/lua/lazy_plugins.lua index c17dcc8..1878bce 100644 --- a/neovim/lua/lazy_plugins.lua +++ b/neovim/lua/lazy_plugins.lua @@ -651,7 +651,7 @@ return { version = "3.x.x", opts = { workspaces = { - { name = "personal", path = "~/Documents/Obsidian" }, + { name = "personal", path = require("plugins.obsidian").vault_path }, }, ui = { checkboxes = { @@ -661,124 +661,19 @@ return { external_link_icon = { char = "🔗", hl_group = "ObsidianExtLinkIcon" }, }, }, - config = function(opts) - -- Setup obsidian - require("obsidian").setup(opts) - - -- Set up auto pull and commit - local group_id = vim.api.nvim_create_augroup("obsidian-git", { clear = true }) - - -- Create auto pull on open - local autopull = function() - local Job = require("plenary.job") - vim.notify("Pulling Obsidian notes", vim.log.levels.INFO, { title = "Obsidian" }) - Job:new({ - command = "git", - args = { "pull" }, - on_exit = function(j, return_val) - if return_val == 0 then - vim.notify("Pulled Obsidian notes", vim.log.levels.INFO, { title = "Obsidian" }) - else - vim.notify( - "Failed to pull Obsidian notes. " .. vim.inspect(j:result()), - vim.log.levels.ERROR, - { title = "Obsidian" } - ) - end - end, - }):start() - end - - vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, { - pattern = vim.fn.expand("~") .. "/Documents/Obsidian/**", - callback = autopull, - group = group_id, - }) - - local Job = require("plenary.job") - - -- Create autocommit on save - local auto_add = function(next_func) - return function(ev) - Job:new({ - command = "git", - args = { "add", ev.file }, - on_exit = function(add_j, add_return_val) - if add_return_val ~= 0 then - vim.notify( - "Failed to add file to git. " .. vim.inspect(add_j:result()), - vim.log.levels.ERROR, - { title = "Obsidian" } - ) - return - end - - if next_func then - next_func() - end - end, - }):start() - end - end - - local auto_commit = function(next_func) - return function() - local date_string = os.date("%Y-%m-%d %H:%M:%S") - Job - :new({ - command = "git", - args = { "commit", "-m", "Auto commit: " .. date_string }, - on_exit = function(commit_j, commit_return_val) - if commit_return_val ~= 0 then - vim.notify( - "Failed to commit file to git. " .. vim.inspect(commit_j:result()), - vim.log.levels.ERROR, - { title = "Obsidian" } - ) - return - end - if next_func then - next_func() - end - end, - }) - :start() - end - end - - local auto_push = function(next_func) - return function() - Job - :new({ - command = "git", - args = { "push" }, - on_exit = function(push_j, push_return_val) - if push_return_val ~= 0 then - vim.notify( - "Failed to push Obsidian notes. " .. vim.inspect(push_j:result()), - vim.log.levels.ERROR, - { title = "Obsidian" } - ) - end - - if next_func then - next_func() - end - end, - }) - :start() - end - end - - vim.api.nvim_create_autocmd({ "BufWritePost" }, { - pattern = vim.fn.expand("~") .. "/Documents/Obsidian/**", - callback = auto_add(auto_commit(auto_push())), - group = group_id, - }) + config = function(_, opts) + require("plugins.obsidian").config(opts) end, event = { - "BufRead " .. vim.fn.expand("~") .. "/Documents/Obsidian/**", - "BufNewFile " .. vim.fn.expand("~") .. "/Documents/Obsidian/**", + "BufRead " .. require("plugins.obsidian").vault_path .. "/**", + "BufNewFile " .. require("plugins.obsidian").vault_path .. "/**", + }, + cmd = { + "ObsidianOpen", + "ObsidianNew", + "ObsidianSearch", + "ObsidianNewFromTemplate", + "ObsidianWorkspace", }, }, diff --git a/neovim/lua/plugins/obsidian.lua b/neovim/lua/plugins/obsidian.lua new file mode 100644 index 0000000..e34e6a4 --- /dev/null +++ b/neovim/lua/plugins/obsidian.lua @@ -0,0 +1,121 @@ +local M = {} + +M.vault_path = vim.fn.expand("~/Documents/Obsidian") + +function M.auto_git() + -- Set up auto pull and commit + local group_id = vim.api.nvim_create_augroup("obsidian-git", { clear = true }) + + -- Create auto pull on open + local autopull = function() + local Job = require("plenary.job") + vim.notify("Pulling Obsidian notes", vim.log.levels.INFO, { title = "Obsidian" }) + Job:new({ + command = "git", + args = { "pull" }, + on_exit = function(j, return_val) + if return_val == 0 then + vim.notify("Pulled Obsidian notes", vim.log.levels.INFO, { title = "Obsidian" }) + else + vim.notify( + "Failed to pull Obsidian notes. " .. vim.inspect(j:result()), + vim.log.levels.ERROR, + { title = "Obsidian" } + ) + end + end, + }):start() + end + + vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, { + pattern = M.vault_path .. "/**", + callback = autopull, + group = group_id, + }) + + local Job = require("plenary.job") + + -- Create autocommit on save + local auto_add = function(next_func) + return function(ev) + Job:new({ + command = "git", + args = { "add", ev.file }, + on_exit = function(add_j, add_return_val) + if add_return_val ~= 0 then + vim.notify( + "Failed to add file to git. " .. vim.inspect(add_j:result()), + vim.log.levels.ERROR, + { title = "Obsidian" } + ) + return + end + + if next_func then + next_func() + end + end, + }):start() + end + end + + local auto_commit = function(next_func) + return function() + local date_string = os.date("%Y-%m-%d %H:%M:%S") + Job:new({ + command = "git", + args = { "commit", "-m", "Auto commit: " .. date_string }, + on_exit = function(commit_j, commit_return_val) + if commit_return_val ~= 0 then + vim.notify( + "Failed to commit file to git. " .. vim.inspect(commit_j:result()), + vim.log.levels.ERROR, + { title = "Obsidian" } + ) + return + end + if next_func then + next_func() + end + end, + }):start() + end + end + + local auto_push = function(next_func) + return function() + Job:new({ + command = "git", + args = { "push" }, + on_exit = function(push_j, push_return_val) + if push_return_val ~= 0 then + vim.notify( + "Failed to push Obsidian notes. " .. vim.inspect(push_j:result()), + vim.log.levels.ERROR, + { title = "Obsidian" } + ) + end + + if next_func then + next_func() + end + end, + }):start() + end + end + + vim.api.nvim_create_autocmd({ "BufWritePost" }, { + pattern = M.vault_path .. "/**", + callback = auto_add(auto_commit(auto_push())), + group = group_id, + }) +end + +function M.config(opts) + -- Setup obsidian + require("obsidian").setup(opts) + + M.auto_git() +end + +return M