diff --git a/CHANGELOG.md b/CHANGELOG.md index 3dfbbd5fa..5bc9901fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added `:ObsidianFollowLink` and companion function `util.cursor_on_markdown_link` - Added `:ObsidianLink` and `:ObsidianLinkNew` commands. - Added configuration option `disable_frontmatter` for frontmatter disabling +- Added line-navigation to `:ObsidianOpen` via the Obsidian Advanced URI plugin ### Fixed diff --git a/README.md b/README.md index 73bef482f..60127a75e 100644 --- a/README.md +++ b/README.md @@ -218,3 +218,17 @@ vim.keymap.set( ``` The other benefit of doing this is that it will now work even if your cursor is on the enclosing brackets (`[[` or `]]`) or the alias part of a reference (the part after `|`). + +#### Navigate to the current line when using `:ObsidianOpen` + +If you have the [Obsidian Advanced URI](https://github.com/Vinzent03/obsidian-advanced-uri) plugin enabled, the Obsidian editor can automatically navigate to the same line in the current NeoVim buffer. For files that are already open, it will update the cursor position within Obsidian's editor. To enable this feature, add `use_advanced_uri = true` to the setup options. For example: +```lua +require("obsidian").setup({ + dir = "~/my-vault", + notes_subdir = "notes", + daily_notes = { + folder = "notes/dailies", + }, + use_advanced_uri = true +}) +``` diff --git a/lua/obsidian/command.lua b/lua/obsidian/command.lua index faa0e5a89..4cb3ad0c0 100644 --- a/lua/obsidian/command.lua +++ b/lua/obsidian/command.lua @@ -97,16 +97,24 @@ command.open = function(client, data) local encoded_vault = util.urlencode(vault_name) local encoded_path = util.urlencode(tostring(path)) + + local uri + if client.opts.use_advanced_uri then + local line = vim.api.nvim_win_get_cursor(0)[1] or 1 + uri = ("obsidian://advanced-uri?vault=%s&filepath=%s&line=%i"):format(encoded_vault, encoded_path, line) + else + uri = ("obsidian://open?vault=%s&file=%s"):format(encoded_vault, encoded_path) + end + local cmd = nil - local url = ("obsidian://open?vault=%s&file=%s"):format(encoded_vault, encoded_path) local args = {} local sysname = vim.loop.os_uname().sysname if sysname == "Linux" then cmd = "xdg-open" - args = { url } + args = { uri } elseif sysname == "Darwin" then cmd = "open" - args = { "-a", "/Applications/Obsidian.app", "--background", url } + args = { "-a", "/Applications/Obsidian.app", "--background", uri } end if cmd == nil then diff --git a/lua/obsidian/config.lua b/lua/obsidian/config.lua index 071135b5f..888d36be2 100644 --- a/lua/obsidian/config.lua +++ b/lua/obsidian/config.lua @@ -7,9 +7,10 @@ local config = {} ---@field notes_subdir string|? ---@field note_id_func function|? ---@field note_frontmatter_func function|? ----@field disable_frontmatter bool|? +---@field disable_frontmatter boolean|? ---@field completion obsidian.config.CompletionOpts ---@field daily_notes obsidian.config.DailyNotesOpts +---@field use_advanced_uri boolean|? config.ClientOpts = {} ---Get defaults. @@ -23,6 +24,7 @@ config.ClientOpts.default = function() disable_frontmatter = false, completion = config.CompletionOpts.default(), daily_notes = config.DailyNotesOpts.default(), + use_advanced_uri = nil, } end