diff --git a/CHANGELOG.md b/CHANGELOG.md index b0ade7463..1321d16aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed `template_pattern` not escaping special characters. - Fixed new notes not getting passed args correctly +- Fixed `:ObsidianOpen` when note is in a subdirectory with the same name as the root vault directory. ## [v1.12.0](https://github.com/epwalsh/obsidian.nvim/releases/tag/v1.12.0) - 2023-07-15 diff --git a/lua/obsidian/command.lua b/lua/obsidian/command.lua index 9a50a4771..0d7d10a96 100644 --- a/lua/obsidian/command.lua +++ b/lua/obsidian/command.lua @@ -103,6 +103,7 @@ command.open = function(client, data) return end else + -- bufname is an absolute path to the buffer. 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 @@ -110,21 +111,22 @@ command.open = function(client, data) 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) + -- `make_relative` fails to work when vault path is configured to look behind a link + -- and returns an unaltered path if it cannot make the path relative. + if path == bufname then + -- 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 end