MetadataCache
Official
A cached metadata for a note.
Linktext is any internal link that is composed of a path and a subpath, such as ‘My note#Heading’ Linkpath (or path) is the path part of a linktext Subpath is the heading/block ID part of a linktext.
Import:
import { MetadataCache } from 'obsidian';Signature:
export class MetadataCache extends EventsExtends: Events
Constructor
new MetadataCache(app: App, vault: Vault)Constructor.
To get the constructor instance, use getMetadataCacheConstructor from obsidian-typings/implementations.
Properties
| Property | Type | Description | |
|---|---|---|---|
| _ | Record<string, EventsEntry[]> | Internal storage of registered event handlers by event name. (Inherited from Events) | |
| app | App | Reference to the obsidian#App. | |
| blockCache | BlockCache | Cache for block references in the vault. | |
| db | IDBDatabase | IndexedDB database | |
| didFinish | Debouncer<[], void> | Debounced callback that fires when the metadata cache finishes processing. | |
| fileCache | MetadataCacheFileCacheRecord | File contents cache | |
| initialized | boolean | Whether the cache is fully loaded | |
| inProgressTaskCount | number | Amount of tasks currently in progress | |
| linkResolverQueue | ItemQueue<null | TFile> | null | Queue of files pending link resolution. | |
| linkUpdaters | LinkUpdaters | Registered link updater strategies for different file types. | |
| metadataCache | MetadataCacheMetadataCacheRecord | File hash to metadata cache entry mapping | |
| onCleanCacheCallbacks | (() => void)[] | Callbacks to execute on cache clean | |
| preloadPromise | null | Promise<void> | Promise that resolves when the preload is complete, or null if not started. | |
| resolvedLinks | Record<string, Record<string, number>> | Contains all resolved links. This object maps each source file's path to an object of destination file paths with the link count. Source and destination paths are all vault absolute paths that comes from obsidian#TFile.path and can be used with obsidian#Vault.getAbstractFileByPath. | |
| transactionSave | Debouncer<[], void> | Debounced function for saving caches to the database. | |
| uniqueFileLookup | CustomArrayDict<TFile> | Mapping of filename to collection of files that share the same name | |
| unresolvedLinks | Record<string, Record<string, number>> | Contains all unresolved links. This object maps each source file to an object of unknown destinations with count. Source paths are all vault absolute paths, similar to resolvedLinks. | |
| userIgnoreFilterCache | Record<string, boolean> | Cache of paths checked against user ignore filters. | |
| userIgnoreFilters | null | RegExp[] | Compiled regular expressions for user-defined ignore filters. | |
| userIgnoreFiltersString | string | Raw string representation of user-defined ignore filters. | |
| vault | Vault | Reference to the obsidian#Vault. | |
| worker | Worker | Web Worker used for parsing metadata in the background. | |
| workerResolve | ((value: CachedMetadata | PromiseLike<CachedMetadata>) => void) | null | Resolve function for the current worker promise, or null if idle. | |
| workQueue | PromisedQueue | Queue for processing metadata computation tasks sequentially. |
Methods
| Method | Returns | Description | |
|---|---|---|---|
| _preload() | Promise<void> | Called by preload() which is in turn called by initialize() | |
| checkCleanCache() | void | Execute onCleanCache callbacks if cache is clean. | |
| cleanupDeletedCache() | void | Clear all caches to null values | |
| clear() | Promise<void> | Clear all metadata caches and reset state. | |
| computeFileMetadataAsync(file) | Promise<void> | Called by initialize() | |
| computeMetadataAsync(arrayBuffer) | Promise<CachedMetadata | undefined> | Compute metadata from raw file content asynchronously. | |
| deletePath(path) | void | Remove all entries that contain deleted path | |
| fileToLinktext(file, sourcePath, omitMdExtension?) | string | Generates a linktext for a file. If file name is unique, use the filename. If not unique, use full path. | |
| getAllPropertyInfos() | Record<string, PropertyInfo> | Get all property infos of the vault. | |
| getBacklinksForFile(file) | CustomArrayDict<Reference> | Get all backlink information for a file. | |
| getCache(path) | CachedMetadata | null | Get the cached metadata for a path. | |
| getCachedFiles() | string[] | Get paths of all files cached in the vault. | |
| getFileCache(file) | CachedMetadata | null | Get the cached metadata for a file. | |
| getFileInfo(path) | FileCacheEntry | undefined | Get an entry from the file cache. | |
| getFirstLinkpathDest(linkpath, sourcePath) | TFile | null | Get the best match for a linkpath. | |
| getFrontmatterPropertyValuesForKey(key) | string[] | Get property values for frontmatter property key. | |
| getLinkpathDest(origin, path) | TFile[] | Get destination of link path. | |
| getLinks() | Record<string, Reference[]> | Get all links within the vault per file. | |
| getLinkSuggestions() | LinkSuggestion[] | Get all links (resolved or unresolved) in the vault. If the note has multiple aliases, it will be returned multiple times for each alias. | |
| getTags() | Record<string, number> | Get all tags within the vault and their usage count. | |
| initialize() | Promise<void> | Initialize Database connection and load up caches | |
| isCacheClean() | boolean | Check whether there are no cache tasks in progress | |
| isSupportedFile(file) | boolean | Check whether file can support metadata (by checking extension support) | |
| isUnresolved(linkpath, sourcePath) | boolean | Check whether a link is unresolved. | |
| isUserIgnored(path) | boolean | Check whether string is part of the user ignore filters | |
| iterateAllRefs(callback) | void | Iterate over all link references in the vault with callback. | |
| iterateRefsForFile(path, callback) | void | Iterate over all references for a specific file. | |
| linkResolver() | void | Process the link resolver queue to resolve file links. | |
| off(name, callback) | void | Remove an event listener. (Inherited from Events) | |
| offref(ref) | void | Remove an event listener by reference. (Inherited from Events) | |
| on('changed', callback, ctx?) | EventRef | Called when a file has been indexed, and its (updated) cache is now available. | |
| on('deleted', callback, ctx?) | EventRef | Called when a file has been deleted. A best-effort previous version of the cached metadata is presented, but it could be null in case the file was not successfully cached previously. | |
| on('resolve', callback, ctx?) | EventRef | Called when a file has been resolved for resolvedLinks and unresolvedLinks. This happens sometimes after a file has been indexed. | |
| on('resolved', callback, ctx?) | EventRef | Called when all files has been resolved. This will be fired each time files get modified after the initial load. | |
| on('finished', callback) | EventRef | Called whenever the metadatacache has finished updating. | |
| on('initialized', callback) | EventRef | Called whenever the metadatacache is fully loaded in. | |
| on(name, callback, ctx?) | EventRef | Add an event listener. (Inherited from Events) | |
| onCleanCache(onCleanCacheCallback) | void | Execute onCleanCache callbacks if cache is clean | |
| onConfigChanged(configKey) | void | Handle a configuration change event. | |
| onCreate(file) | void | On creation of the cache: update metadata cache | |
| onDelete(file) | void | On deletion of the cache: update metadata cache | |
| onReceiveMessageFromWorker(message) | void | Handle a message received from the metadata parsing worker. | |
| onRename(file, oldPath) | void | On rename of the cache: update metadata cache | |
| preload() | Promise<void> | Preload the metadata cache from the database. | |
| queueFileForLinkResolution(file) | void | Queue a file for link resolution. | |
| resolveLinks(path) | void | Check editor for unresolved links and mark these as unresolved | |
| saveFileCache(path, entry) | void | Update file cache entry and sync to indexedDB | |
| saveMetaCache(hash, entry) | void | Update metadata cache entry and sync to indexedDB | |
| showIndexingNotice() | void | Show a notice that the cache is being rebuilt | |
| trigger(name, data?) | void | Trigger an event, executing all the listeners in order even if some of them throw an error. (Inherited from Events) | |
| tryTrigger(evt, args) | void | Try to trigger an event, executing all the listeners in order even if some of them throw an error. (Inherited from Events) | |
| updateInternalLinks(updates) | Promise<void> | Applies queued internal-link updates to the affected files. | |
| updateRelatedLinks(fileNames) | void | Re-queue for link resolution every file that links to any of the changed names. Called on file create, delete, and rename. Each entry is a name, not a full path: resolved links are matched by comparing the basename of each resolved target path against the entries, and unresolved links are matched by comparing the basename of each unresolved link (also trying the entry with a trailing .md removed). Every matching file is queued via MetadataCache.queueFileForLinkResolution. | |
| updateUserIgnoreFilters() | void | Update user ignore filters from settings | |
| watchVaultChanges() | void | Bind actions to listeners on vault | |
| work(arrayBuffer) | Promise<CachedMetadata> | Send message to worker to update metadata cache |
Links to this page: