Skip to content

Commit

Permalink
Fix for ObsidianOpen not working with links (#113)
Browse files Browse the repository at this point in the history
* Added demo to README

* Adjusted filetype for compaitibility

* Partial fix for links not being handled correctly

* Re-added plenary make_relative for cleaning and non-link cases

* Formatted with stylua

* Added comments explaining workaround and changelog entry

* Update README.md
  • Loading branch information
shakesbeare authored Mar 31, 2023
1 parent 0823e60 commit 09a0a36
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
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

0 comments on commit 09a0a36

Please sign in to comment.