I’m trying to develop a plugin which would let users view and edit a custom JSON-based filetype. Part of the data stored would be a link to an image in their vault. I’d like to make sure the links stored in these files don’t break if any linked image are moved in their vault.
Is there an API hook I can listen for to update links on moving an image file? Additionally, is there a cache of known file links I can reference and update so I don’t have to create my own cache of links?
Yes, you can use Obsidian’s metadataCache and listen to the file:rename and file:delete events via the vault.on() API to track file moves and update links accordingly.
To maintain link integrity in your JSON-based plugin:
Use app.vault.on('rename', callback) to detect when a file (like an image) is moved or renamed. This lets you update the stored path in your JSON file dynamically.
Leverage metadataCache.resolvedLinks to access existing link mappings across the vault. This avoids building your own cache and helps you track references.
Consider using app.metadataCache.on('changed', ...) if you need to monitor updates to file metadata that might affect link resolution.
For best results, store image links using Obsidian’s relative path format and update them reactively when file events occur. This ensures your plugin remains vault-aware and resilient to file changes.