I’d like to report the same issue as reported by @sararkin at Vault.rename(file, path) is not updating existing links - Bug graveyard - Obsidian Forum .
Since the moderator suggest the bug should belongs to API sections. I’d like to report here again.
My system and obsidian version is different from sararkin, but the problem remains.
Steps to reproduce
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
const newPath = "hoge.md";
this.app.vault.rename(view.file, newPath);
Expected result
Renames file and updates all links, same behavior as just renaming manually
Actual result
Renames the file, The vault.on(“rename”) event is indeed triggered. But existing links aren’t updated to point to the new file
Environment
Operating system: Windows 10
Obsidian version: v0.13.23
Thanks,
anselmwang
1 Like
Licat
February 16, 2022, 7:22pm
2
This is the API you should use for rename and update links:
* @public
*/
getNewFileParent(sourcePath: string): TFolder;
/**
* Rename or move a file safely, and update all links to it depending on the user's preferences.
* @param file - the file to rename
* @param newPath - the new path for the file
* @public
*/
renameFile(file: TAbstractFile, newPath: string): Promise<void>;
/**
* Generate a markdown link based on the user's preferences.
* @param file - the file to link to.
* @param sourcePath - where the link is stored in, used to compute relative links.
* @param subpath - A subpath, starting with `#`, used for linking to headings or blocks.
* @param alias - The display text if it's to be different than the file name. Pass empty string to use file name.
* @public
*/
generateMarkdownLink(file: TFile, sourcePath: string, subpath?: string, alias?: string): string;
Licat:
rename and update links:
Thanks a lot, I have verified the API with this.app.fileManager.renameFile(this.app.vault.getAbstractFileByPath("abc/abc.md"), "abc/abc2.md")
, it works perfectly!
2 Likes