Skip to content

Commit

Permalink
Fix :ObsidianOpen
Browse files Browse the repository at this point in the history
  • Loading branch information
epwalsh committed Aug 7, 2023
1 parent f3f3479 commit 8f1c896
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 15 additions & 13 deletions lua/obsidian/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,28 +103,30 @@ 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
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)
-- `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

Expand Down

0 comments on commit 8f1c896

Please sign in to comment.