From 09a0a36f9ba57bb3875c300f31dee7f90f5f90df Mon Sep 17 00:00:00 2001 From: shakesbeare <75107188+shakesbeare@users.noreply.github.com> Date: Fri, 31 Mar 2023 12:25:41 -0600 Subject: [PATCH] Fix for `ObsidianOpen` not working with links (#113) * 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 --- CHANGELOG.md | 1 + README.md | 12 ++++++++++++ lua/obsidian/command.lua | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4a39e144..1e52ebde6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 045da4327..37b43fd22 100644 --- a/README.md +++ b/README.md @@ -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 +``` + diff --git a/lua/obsidian/command.lua b/lua/obsidian/command.lua index 1e07464ba..9b388d11b 100644 --- a/lua/obsidian/command.lua +++ b/lua/obsidian/command.lua @@ -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)