Skip to content

Commit f29fecd

Browse files
committed
ensure ID matches fname
1 parent cbfccee commit f29fecd

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3030
- Fixed `:ObsidianFollowLink` not creating a new note when following a dangling link; matches behavior in the official Obsidian app.
3131
- Fixed handling spaces in configured vault directory.
3232
- Fixed `:ObsidianFollowLink` not considering the vault's root directory.
33+
- Fixed bug where the note ID in the YAML frontmatter wasn't updated after the file is renamed.
3334

3435
### Changed
3536

lua/obsidian/note.lua

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ end
158158
---@return obsidian.Note
159159
note.from_lines = function(lines, path, root)
160160
local cwd = tostring(root and root or "./")
161-
local relative_path = tostring(Path:new(tostring(path)):make_relative(cwd))
162161

163162
local id = nil
164163
local title = nil
@@ -249,9 +248,13 @@ note.from_lines = function(lines, path, root)
249248
table.insert(aliases, title)
250249
end
251250

252-
-- Fall back to using the relative path as the ID.
253-
if id == nil then
254-
id = relative_path
251+
-- The ID should match the filename with or without the extension.
252+
local relative_path = tostring(Path:new(tostring(path)):make_relative(cwd))
253+
local relative_path_no_ext = vim.fn.fnamemodify(relative_path, ":r")
254+
local fname = vim.fs.basename(relative_path)
255+
local fname_no_ext = vim.fn.fnamemodify(fname, ":r")
256+
if id ~= relative_path and id ~= relative_path_no_ext and id ~= fname and id ~= fname_no_ext then
257+
id = fname_no_ext
255258
end
256259

257260
local n = note.new(id, aliases, tags, path)

0 commit comments

Comments
 (0)