Skip to content

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 Events

Extends: Events

Constructor

new MetadataCache(app: App, vault: Vault)

Constructor.

To get the constructor instance, use getMetadataCacheConstructor from obsidian-typings/implementations.

Properties

PropertyTypeDescription
_Record<string, EventsEntry[]>Internal storage of registered event handlers by event name.
(Inherited from Events)
appAppReference to the obsidian#App.
blockCacheBlockCacheCache for block references in the vault.
dbIDBDatabaseIndexedDB database
didFinishDebouncer<[], void>Debounced callback that fires when the metadata cache finishes processing.
fileCacheMetadataCacheFileCacheRecordFile contents cache
initializedbooleanWhether the cache is fully loaded
inProgressTaskCountnumberAmount of tasks currently in progress
linkResolverQueueItemQueue<null | TFile> | nullQueue of files pending link resolution.
linkUpdatersLinkUpdatersRegistered link updater strategies for different file types.
metadataCacheMetadataCacheMetadataCacheRecordFile hash to metadata cache entry mapping
onCleanCacheCallbacks(() => void)[]Callbacks to execute on cache clean
preloadPromisenull | Promise<void>Promise that resolves when the preload is complete, or null if not started.
resolvedLinksRecord<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.
transactionSaveDebouncer<[], void>Debounced function for saving caches to the database.
uniqueFileLookupCustomArrayDict<TFile>Mapping of filename to collection of files that share the same name
unresolvedLinksRecord<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.
userIgnoreFilterCacheRecord<string, boolean>Cache of paths checked against user ignore filters.
userIgnoreFiltersnull | RegExp[]Compiled regular expressions for user-defined ignore filters.
userIgnoreFiltersStringstringRaw string representation of user-defined ignore filters.
vaultVaultReference to the obsidian#Vault.
workerWorkerWeb Worker used for parsing metadata in the background.
workerResolve((value: CachedMetadata | PromiseLike<CachedMetadata>) => void) | nullResolve function for the current worker promise, or null if idle.
workQueuePromisedQueueQueue for processing metadata computation tasks sequentially.

Methods

MethodReturnsDescription
_preload()Promise<void>Called by preload() which is in turn called by initialize()
checkCleanCache()voidExecute onCleanCache callbacks if cache is clean.
cleanupDeletedCache()voidClear 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)voidRemove all entries that contain deleted path
fileToLinktext(file, sourcePath, omitMdExtension?)stringGenerates 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 | nullGet the cached metadata for a path.
getCachedFiles()string[]Get paths of all files cached in the vault.
getFileCache(file)CachedMetadata | nullGet the cached metadata for a file.
getFileInfo(path)FileCacheEntry | undefinedGet an entry from the file cache.
getFirstLinkpathDest(linkpath, sourcePath)TFile | nullGet 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()booleanCheck whether there are no cache tasks in progress
isSupportedFile(file)booleanCheck whether file can support metadata (by checking extension support)
isUnresolved(linkpath, sourcePath)booleanCheck whether a link is unresolved.
isUserIgnored(path)booleanCheck whether string is part of the user ignore filters
iterateAllRefs(callback)voidIterate over all link references in the vault with callback.
iterateRefsForFile(path, callback)voidIterate over all references for a specific file.
linkResolver()voidProcess the link resolver queue to resolve file links.
off(name, callback)voidRemove an event listener.
(Inherited from Events)
offref(ref)voidRemove an event listener by reference.
(Inherited from Events)
on('changed', callback, ctx?)EventRefCalled when a file has been indexed, and its (updated) cache is now available.
on('deleted', callback, ctx?)EventRefCalled 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?)EventRefCalled when a file has been resolved for resolvedLinks and unresolvedLinks. This happens sometimes after a file has been indexed.
on('resolved', callback, ctx?)EventRefCalled when all files has been resolved. This will be fired each time files get modified after the initial load.
on('finished', callback)EventRefCalled whenever the metadatacache has finished updating.
on('initialized', callback)EventRefCalled whenever the metadatacache is fully loaded in.
on(name, callback, ctx?)EventRefAdd an event listener.
(Inherited from Events)
onCleanCache(onCleanCacheCallback)voidExecute onCleanCache callbacks if cache is clean
onConfigChanged(configKey)voidHandle a configuration change event.
onCreate(file)voidOn creation of the cache: update metadata cache
onDelete(file)voidOn deletion of the cache: update metadata cache
onReceiveMessageFromWorker(message)voidHandle a message received from the metadata parsing worker.
onRename(file, oldPath)voidOn rename of the cache: update metadata cache
preload()Promise<void>Preload the metadata cache from the database.
queueFileForLinkResolution(file)voidQueue a file for link resolution.
resolveLinks(path)voidCheck editor for unresolved links and mark these as unresolved
saveFileCache(path, entry)voidUpdate file cache entry and sync to indexedDB
saveMetaCache(hash, entry)voidUpdate metadata cache entry and sync to indexedDB
showIndexingNotice()voidShow a notice that the cache is being rebuilt
trigger(name, data?)voidTrigger an event, executing all the listeners in order even if some of them throw an error.
(Inherited from Events)
tryTrigger(evt, args)voidTry 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)voidRe-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()voidUpdate user ignore filters from settings
watchVaultChanges()voidBind actions to listeners on vault
work(arrayBuffer)Promise<CachedMetadata>Send message to worker to update metadata cache

Links to this page: