Skip to content

metadata-cache

This module provides utility functions for working with the metadata cache in Obsidian.

Interface Description
CachedMetadataEx An extended CachedMetadata that records which parsing CachedMetadataExFeatures were applied (via CachedMetadataEx.features) and carries the resulting external links. Each links array is populated only when its corresponding feature is present in CachedMetadataEx.features.
GetBacklinksForFileSafeOptions Options for getBacklinksForFileSafe.
GetBacklinksForFileSafeParams Parameters for getBacklinksForFileSafe.
GetLinksParams Params for getLinks.
ParseCacheOptions Options for the parsing performed by getCacheSafe and parseMetadata beyond Obsidian’s native metadata parsing. Each option is opt-in; enabling it adds the corresponding CachedMetadataExFeature and populates the matching links array.
RegisterFileCacheForNonExistingFileParams Parameters for registerFileCacheForNonExistingFile.
Function Description
ensureMetadataCacheReady Ensures that the metadata cache is ready for all files.
getBacklinksForFileOrPath Retrieves the backlinks for a file or path. NOTE: The file may be non-existent. When Advanced Metadata Cache publishes its backlinks API and its Backlinks module is on, its cache answers. Otherwise the answer comes from a backlink index whose cost is proportional to the candidate notes rather than to every reference in the vault, and which gives the same references Obsidian’s own getBacklinksForFile gives.
getBacklinksForFileSafe Retrieves the backlinks for a file safely. When Advanced Metadata Cache publishes its backlinks API and its Backlinks module is on, that API answers. Otherwise this is NOT a pure read. It verifies each backlink against the note’s current text, and to do that it calls saveNote on every note that links to the file, which saves any open editor view of those notes that has unsaved changes. Call it from an explicit user action. Do not call it from an automatic trigger (a vault or metadata event, a save or render patch) that only decides whether to act. There, a forced save can race another plugin’s in-flight editor change.
getCacheSafe Retrieves the cached metadata for a given file or path. This is NOT a pure read: before it reads the cache, it calls saveNote, which saves any open editor view of the note that has unsaved changes. That save is why the cache matches what the user sees, and why a command that rewrites the note can use this safely without its rewrite being clobbered. It is also a write on the note. So do not call this from an automatic trigger (a vault or metadata event, a save or render patch, a layout hook) that only decides whether to act: a forced save one tick after another plugin has dispatched a change into the editor can leave Obsidian’s editor rendering stale state. A read-only probe on the note’s current text should use parseMetadata on that text instead. It needs no view, no cache and no save.
getFrontmatterSafe Retrieves the front matter from the metadata cache safely. It reads through getCacheSafe, so it saves any open editor view of the note that has unsaved changes. See getCacheSafe for when that save matters and what to use instead.
getLinks Retrieves the selected links from the provided cache.
hasBacklinkCachePlugin Determines whether a backlink cache plugin is currently loaded - either Advanced Metadata Cache (ADVANCED_METADATA_CACHE_PLUGIN_ID) or its retired predecessor Backlink Cache (BACKLINK_CACHE_PLUGIN_ID). Such a plugin keeps a drain-on-read backlink index and indexes canvas metadata, so a caller that compensates for Obsidian’s own gaps can stand down while one is present. Both ids are recognized; BACKLINK_CACHE_PLUGIN_ID records when the retired one may be dropped. This reads the LOADED plugin instance rather than app.plugins.enabledPlugins, deliberately: the question is whether the plugin’s patches are in place right now, and they are not until it has loaded. Plugins load in an unspecified order, so call this from a user-driven path rather than from plugin load - see rule L25 in AGENTS.md.
isCachedMetadataEx Determines whether a cache is a CachedMetadataEx (i.e. it was computed with external-link parsing enabled).
parseMetadata Parses the metadata for a given string.
registerFileCacheForNonExistingFile Registers the file cache for a non-existing file.
registerFiles Registers files in the Obsidian app.
unregisterFileCacheForNonExistingFile Unregisters the file cache for a non-existing file.
unregisterFiles Unregisters files from the Obsidian app.
Type Description
GetCacheSafeOptions Options for getCacheSafe.
ParseMetadataOptions Options for parseMetadata.
Enum Description
CachedMetadataExFeature A parsing feature that was applied when computing a CachedMetadataEx.
Variable Description
ADVANCED_METADATA_CACHE_PLUGIN_ID The plugin id of the Advanced Metadata Cache plugin, the successor to Backlink Cache. Used by hasBacklinkCachePlugin, and as the provider id under which getBacklinksForFileOrPath and getBacklinksForFileSafe look up the plugin’s published API.
BACKLINK_CACHE_PLUGIN_ID The plugin id of the retired Backlink Cache plugin, superseded by ADVANCED_METADATA_CACHE_PLUGIN_ID. Still recognized by hasBacklinkCachePlugin, deliberately. Removing a plugin from the community registry blocks new installs; it uninstalls nothing, so every existing user keeps it until they migrate by hand. It may be dropped when all three of the following hold, and not before: Advanced Metadata Cache is listed in the community registry, so migrating is possible at all; the final Backlink Cache handover release has been out for at least twelve months; and this library is cutting a major release anyway, because dropping the id silently un-gates canvas link handling for anyone who has not migrated. There is no cost pressure to drop it earlier - recognizing a dead id is one string comparison on a path that already reads a plugin registry.