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 line-navigation to :ObsidianOpen (AdvancedURI) #56

Merged
merged 3 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
```
14 changes: 11 additions & 3 deletions lua/obsidian/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we go. Thanks for pointing it out.

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
Expand Down
4 changes: 3 additions & 1 deletion lua/obsidian/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down