Use case or problem
When you rename a file inside Obsidian, it automatically updates all the links related to the renamed file.
For example, if you rename “old.md” to “new.md”, all the links that look like [[old]]
or [[old|alias]]
will also be renamed to [[new]]
or [[new|alias]]
.
This is the same for not only note titles but also block IDs (if you rename those IDs using right-click menu).
This is one of the most important core features of Obsidian, which makes it easy for us to connect our notes.
However, it is not implemented in the Obsidian API for plugin developers, if I understand correctly.
Indeed, Vault.rename only renames the target file. It doesn’t update the related links
As for block IDs, there is no equivalent to app.vault.rename
for files.
So currently, it is not very simple to rename files or block IDs using the API.
Moreover, once a link breaks, it will be very difficult to recover the connection between notes.
Thus we need an officially offered way to rename files or block IDs safely.
Proposed solution
Renaming files
Add updateLinks
parameter to the Vault.rename
method:
rename(file: TAbstractFile, newPath: string, updateLinks?: boolean): Promise<void>;
If updateLinks == true
, it automatically updates all the links related to file
.
A similar change should be made for, e.g., DataAdapter.rename().
Renaming block IDs
Currently, there is no function to rename block IDs with or without automatic update of related links. So it would be nice if Vault
has a method like
renameBlockId(path: string, oldId: string, newId: string, updateLinks?: boolean): Promise<void>;
that rename the ID of BlockCache (specified by path
and oldId
, like app.metadataCache.getCache(path).sections.filter((section) => section.id == oldId)
) to newId
, and if updateLinks == true
, it also updates all the links in the vault related to the block.
Current workaround (optional)
Manually iterate over the app.metadataCache.getCachedFiles()
, and for each cached file,
get the CachedMetada object by app.metadataCache.getCache(filename)
.
The returned object has links
properties, so I can check those links one by one.