epwalsh / epwalsh/obsidian.nvim

`ObsidianRename` does not replace the id of renamed file

Open
#688 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Lua
Stars
6.2k
Forks
250
PR merge metrics
No merged PRs in 30d

Description

### 🐛 Describe the bug

in file `20240820-old.md`, the original id might be `20240820-old`:
```md
---
id: 20240820-old
aliases: []
tags: []
---
```

if i run the command `:ObsidianRename new`, the filename of current file would be changed to `new.md` and all references from other notes will be replaced to `[[new]]`, however, the `id` property of current file remains `20240820-old`:
```md
---
id: 20240820-old
aliases: []
tags: []
---
```

And if then I would like to run `:ObsidianRename 20240820-old` for changing the name back to `20240820-old`, plugin would not allow me to to that and reports: `New note ID is the same, doing nothing`, due to the id of current file never changes.

Even if I want to change to another name, e.g. `:ObsidianRename another_new`, the filename of current file would be changed to `another_new.md`, the id of current file still remains `20240820-old`, and the references from all other files would be kept as
`[[new]]` due to the id does not match, which means backlink of current file could not be resolved.

### Config

```lua
return {
"epwalsh/obsidian.nvim",
-- version = "*",
-- lazy = true,
ft = "markdown",
dependencies = {
"nvim-lua/plenary.nvim",
"nvim-telescope/telescope.nvim",
"hrsh7th/nvim-cmp",
},
keys = {
{"fnd", "ObsidianDailies", desc = "[Obsidian] Open daily notes"},
{"fnt", "ObsidianTemplate", desc = "[Obsidian] Insert template"},
{"fnT", "ObsidianNewFromTemplate", desc = "[Obsidian] Create note using template"},
{"fnn", "ObsidianQuickSwitch", desc = "[Obsidian] Open/Create new note"},
{"fnp", "ObsidianPasteImg", desc = "[Obsidian] Paste image into note"},
{"fnr", "ObsidianRename", desc = "[Obsidian] Rename note"},
{"fnf", "ObsidianSearch", desc = "[Obsidian] Global Search"},
{"fnb", "ObsidianBacklinks", desc = "[Obsidian] Search Backlink"},
{"fno", "ObsidianOpen", desc = "[Obsidian] Open note in Obsidian"},
},
opts = {
workspaces = {
{
name = "Junk",
path = "~/Obsidian/Junk/"
},
},
notes_subdir = "notes",
new_notes_location = "notes_subdir",
log_level = vim.log.levels.INFO,
daily_notes = {
folder = "dailies",
date_format = "%Y-%m-%d",
alias_format = "%B %-d, %Y",
default_tags = { "daily-notes" },
-- Optional, if you want to automatically insert a template from your template directory like 'daily.md'
template = '[Daily].md'
},
completion = {
-- Set to false to disable completion.
nvim_cmp = true,
-- Trigger completion at 2 chars.
min_chars = 2,
},
-- Optional, configure key mappings. These are the defaults. If you don't want to set any keymappings this
-- way then set 'mappings = {}'.
mappings = {
-- Overrides the 'gf' mapping to work on markdown/wiki links within your vault.
["fnf"] = {
action = function()
return require("obsidian").util.gf_passthrough()
end,
opts = { noremap = false, expr = true, buffer = true },
},
-- Toggle check-boxes.
[""] = {
action = function()
return require("obsidian").util.toggle_checkbox()
end,
opts = { buffer = true },
},
-- Smart action depending on context, either follow link or toggle checkbox.
[""] = {
action = function()
return require("obsidian").util.smart_action()
end,
opts = { buffer = true, expr = true },
}
},
templates = {
folder = "../templates",
date_format = "%Y-%m-%d",
time_format = "%H:%M",
substitutions = {},
},
attachments = {
-- The default folder to place images in via `:ObsidianPasteImg`.
-- If this is a relative path it will be interpreted as relative to the vault root.
-- You can always override this per image by passing a full path to the command instead of just a filename.
img_folder = "assets/imgs" .. vim.fn.expand('%:t:r'), -- This is the default

-- Optional, customize the default name or prefix when pasting images via `:ObsidianPasteImg`.
---@return string
img_name_func = function()
-- Prefix image names with timestamp.
return string.format("%s-", os.date("%y%m%d%H%M%S"))
end,

-- A function that determines the text to insert in the note when pasting an image.
-- It takes two arguments, the `obsidian.Client` and an `obsidian.Path` to the image file.
-- This is the default implementation.
---@param client obsidian.Client
---@param path obsidian.Path the absolute path to the image file
---@return string
img_text_func = function(client, path)
path = client:vault_relative_path(path) or path
return string.format("![%s](%s)", string.sub(path.name, 14), path)
end,
},
-- Optional, customize how note IDs are generated given an optional title.
---@param title string|?
---@return string
note_id_func = function(title)
-- Create note IDs in a Zettelkasten format with a timestamp and a suffix.
-- In this case a note with the title 'My new note' will be given an ID that looks
-- like '1657296016-my-new-note', and therefore the file name '1657296016-my-new-note.md'
local suffix = ""
if title ~= nil then
-- If title is given, transform it into valid file name.
suffix = title:gsub(" ", "-"):gsub("[^A-Za-z0-9-]", ""):lower()
else
-- If title is nil, just add 4 random uppercase letters to the suffix.
for _ = 1, 4 do
suffix = suffix .. string.char(math.random(65, 90))
end
end
return tostring(os.time()) .. "-" .. suffix
end,

-- Optional, customize how note file names are generated given the ID, target directory, and title.
---@param spec { id: string, dir: obsidian.Path, title: string|? }
---@return string|obsidian.Path The full path to the new note.
note_path_func = function(spec)
-- This is equivalent to the default behavior.
local path = spec.dir / tostring(spec.title)
return path:with_suffix(".md")
end,
}
}
```

### Environment

```
NVIM v0.10.0
Build type: Release
LuaJIT 2.1.1713484068
Run "nvim -V1 -v" for more info
```

```
Obsidian.nvim v3.9.0 (14e0427bef6c55da0d63f9a313fd9941be3a2479)
Status:
? buffer directory: nil
? working directory: C:/Users/junk
Workspaces:
? active workspace: Workspace(name='Junk', path='C:/Users/junk/Obsidian/Junk', root='C:/Users/junk/Obsidian/Junk')
Dependencies:
? plenary.nvim: a3e3bc82a3f95c5ed0d7201546d5d2c19b20d683
? nvim-cmp: ae644feb7b67bf1ce4260c231d1d4300b19c6f30
? telescope.nvim: a0bbec21143c7bc5f8bb02e0005fa0b982edc026
Integrations:
? picker: TelescopePicker()
? completion: enabled (nvim-cmp) ? refs, ? tags, ? new
all sources:
? nvim_lsp
? luasnip
? buffer
? path
Tools:
? rg: ripgrep 14.1.0 (rev e50df40a19)
Environment:
? operating system: Windows
Config:
? notes_subdir: notes
```

Contributor guide

Open the contributing guide

Research direction

Search for the ObsidianRename command and its rename implementation; the issue does not name a source file or test. Reproduce the rename sequence described in the report, then verify that the note's frontmatter id and references stay consistent after renaming, including when changing the name back.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.