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

Fix for ObsidianOpen not working with links #113

Merged
merged 9 commits into from
Mar 31, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed `nvim-cmp` completion for notes that have no `aliases` specified.
- `nvim-cmp` completion will search based on file names now too, not just contents.
- Fixed bug when `nvim-cmp` is not installed.
- Workaround error which prevented users from using `ObsidianOpen` when vault path was configured behind a link

## [v1.8.0](https://github.com/epwalsh/obsidian.nvim/releases/tag/v1.8.0) - 2023-02-16

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,15 @@ require("obsidian").setup({
use_advanced_uri = true
})
```

## Known Issues

### Configuring vault directory behind a link

If you are having issues with commands like `ObsidianOpen`, ensure that your vault is configured to use an absolute path rather than a link. If you must use a link in your configuration, make sure that the name of the vault is present in the file path of the link. For example:

```
Vault: ~/path/to/vault/parent/obsidian/
Link: ~/obsidian OR ~/parent
```

18 changes: 18 additions & 0 deletions lua/obsidian/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,28 @@ command.open = function(client, data)
end
else
local bufname = vim.api.nvim_buf_get_name(0)
local vault_name_escaped = vault_name:gsub("%W", "%%%0") .. "%/"
if vim.loop.os_uname().sysname == "Windows_NT" then
bufname = bufname:gsub("/", "\\")
vault_name_escaped = vault_name_escaped:gsub("/", [[\%\]])
end

-- make_relative fails to work when vault path is configured to look behind a link
-- make_relative returns an unaltered path if it cannot make the path relative
path = Path:new(bufname):make_relative(vault)

-- if the vault name appears in the output of make_relative
-- i.e. make_relative has failed
-- then remove everything up to and including the vault path
-- Example:
-- Config path: ~/Dropbox/Documents/0-obsidian-notes/
-- File path: /Users/username/Library/CloudStorage/Dropbox/Documents/0-obsidian-notes/Notes/note.md
-- ^
-- Proper relative path: Notes/note.md
local _, j = path:find(vault_name_escaped)
if j ~= nil then
path = bufname:sub(j)
end
end

local encoded_vault = util.urlencode(vault_name)
Expand Down