/** * @file * * Mock of Obsidian's `MetadataCache`, which indexes each note's links, headings, tags and frontmatter. */ import type { CachedMetadata as CachedMetadataOriginal, MetadataCache as MetadataCacheOriginal, Reference as ReferenceOriginal } from 'obsidian'; import type { FileCacheEntry, MaybeReturn } from '../internal/types.cjs'; import type { App } from './App.cjs'; import type { TFile } from './TFile.cjs'; import type { Vault } from './Vault.cjs'; import { Events } from './Events.cjs'; /** * Mock of Obsidian's `MetadataCache`. * * It indexes Markdown files synchronously whenever the vault fires `create` or `modify`: the content is parsed by * the mock's own Markdown parser, the link graph is rebuilt for that file and `changed` is triggered. It fires no * `deleted`, `resolve` or `resolved` events, and does not react to renames or deletions. */ export declare class MetadataCache extends Events { /** * The app the cache belongs to. */ app: App; /** * Mock-only: the parsed metadata of each indexed file, keyed by file path; what {@link MetadataCache.getCache} * and {@link MetadataCache.getFileCache} read. */ cache__: Map; /** * The content hash, modification time and size recorded for each indexed file, keyed by file path. */ fileCache: Record; /** * The parsed metadata keyed by content hash, so files with identical content share one entry. */ metadataCache: Record; /** * The resolved links: maps each source file path to the destination file paths it links to, with link counts. */ resolvedLinks: Record>; /** * The unresolved links: maps each source file path to the link paths that match no file, with link counts. */ unresolvedLinks: Record>; /** * Creates a metadata cache that indexes a vault's files as they are created or modified. Use * {@link MetadataCache.create2__} from tests. * * @param app - The app the cache belongs to. * @param vault - The vault whose `create` and `modify` events trigger indexing. */ protected constructor(app: App, vault: Vault); /** * Mock-only factory: creates a metadata cache, spyable via `vi.spyOn(MetadataCache, 'create2__')`. The subclass * variant of {@link Events.create__}. * * @param app - The app the cache belongs to. * @param vault - The vault to index. * @returns The new metadata cache. */ static create2__(app: App, vault: Vault): MetadataCache; /** * Mock-only: views a value typed as Obsidian's `MetadataCache` as this mock. * * @param value - The value typed as the original `MetadataCache`. * @returns The same object, typed as the mock. */ static fromOriginalType2__(value: MetadataCacheOriginal): MetadataCache; /** * Mock-only: views this mock as Obsidian's `MetadataCache` type. * * @returns The same object, typed as the original `MetadataCache`. */ asOriginalType2__(): MetadataCacheOriginal; /** * Parses a file's raw bytes into metadata, the way Obsidian does when it indexes a file. * * @param arrayBuffer - The file's raw content. * @returns The parsed metadata. */ computeMetadataAsync(arrayBuffer: ArrayBuffer): Promise; /** * Mock-only construction hook, called at the end of the constructor; a no-op meant for * `vi.spyOn(MetadataCache.prototype, 'constructor2__')`. * * @param _app - The app the cache was created with. * @param _vault - The vault the cache was created with. */ constructor2__(_app: App, _vault: Vault): void; /** * Generates the linktext for a file, following the vault's `newLinkFormat` setting as Obsidian does: the full path * for `absolute`, a path relative to the source note's folder for `relative`, and otherwise (Obsidian's default, * shortest) the file name when a link by that name resolves to exactly this file, or the full path when it does * not. * * @param file - The file to link to. * @param sourcePath - The path of the note the link is in. * @param omitMdExtension - Whether to drop the `.md` extension from a Markdown file's name or path; `true` by * default, as in Obsidian. * @returns The linktext. */ fileToLinktext(file: TFile, sourcePath: string, omitMdExtension?: boolean): string; /** * Gets the cached metadata of a file by path. * * @param path - The file's vault path. * @returns The file's metadata, or `null` when it has not been indexed. */ getCache(path: string): CachedMetadataOriginal | null; /** * Gets the cached metadata of a file. * * @param file - The file to look up. * @returns The file's metadata, or `null` when it has not been indexed. */ getFileCache(file: TFile): CachedMetadataOriginal | null; /** * Gets the best-matching file for a linkpath. * * The mock tries, in order: a file at exactly `linkpath`, a file at `linkpath` plus `.md`, then the first vault * file whose basename or name equals `linkpath`; then the same three again ignoring case, as Obsidian's own link * index does, so `[[homepage]]` reaches `HomePage.md`. An exact match always wins over a case-folded one. * It does not prefer files near the source path. * * @param linkpath - The path part of a link, without any `#` subpath. * @param _sourcePath - The path of the note the link is in; ignored by the mock. * @returns The matching file, or `null` when none matches. */ getFirstLinkpathDest(linkpath: string, _sourcePath: string): null | TFile; /** * Checks whether a file can be a link target at all — the per-file test Obsidian's own `getLinkSuggestions` walk * applies. * * As in Obsidian, it is a view-registry question rather than an extension list: a file is supported when the vault's * `showUnsupportedFiles` setting is on, or when its extension opens in a registered view type. The mock's registry * starts with only Obsidian's Markdown view, so out of the box only `md` answers `true`; register a view for an * extension (`Plugin.registerExtensions`, or `app.viewRegistry` directly) to make it supported, the way Obsidian's * canvas, image, PDF and media views do in the app. * * @param file - The file to check. * @returns Whether the file is supported. */ isSupportedFile(file: TFile): boolean; /** * Iterates the file's own outgoing references, stopping as soon as the callback answers `true`. * * Obsidian looks the file's extension up in `MetadataCache.linkUpdaters` first and hands the whole walk to that * updater when one is registered, reading the metadata cache only as a fallback. **The mock always takes that * fallback branch** — nothing here registers a link updater, and there is no registry to register one in — so a * `.canvas` file is walked through its cached metadata like any other. That is the only behavior available here, * not an omission waiting to be rediscovered. * * @param file - The file whose references to walk. An unindexed one yields nothing. * @param callback - Called with each reference — the file's frontmatter links first, then its body links, then * its embeds. Returning exactly `true` stops the walk, as {@link iterateRefs} does; any other value, * `undefined` included, continues it. */ iterateRefsForFile(file: TFile, callback: (reference: ReferenceOriginal) => MaybeReturn): void; /** * Indexes a caller-supplied metadata cache for `path` as though Obsidian had just parsed the file, * overriding whatever the automatic parse produced. * * This runs the SAME bookkeeping the automatic index does — it writes `cache__`, refreshes * `fileCache` / `metadataCache` and the `resolvedLinks` / `unresolvedLinks` graph, and fires * `changed` as `(file, content, cache)`, the shape real Obsidian emits — so a consumer's handler * and a later `getCacheSafe` both see the override rather than a stale parse. * * The file's own content is read from the vault and passed as the event's `data`; only the metadata * is overridden, never the bytes. * * @param path - The path of an EXISTING file to override the metadata of. * @param cache - The metadata to index for it. * @throws A `TypeError` if no file exists at `path` — there is no `file` to put on the event, and a * `changed` event without one is a shape no real Obsidian consumer is written against. */ setCache__(path: string, cache: CachedMetadataOriginal): void; /** * Records `cache` as `file`'s metadata and brings every derived structure with it, the way Obsidian * does when it finishes indexing a file. The single owner of that bookkeeping — both the automatic * parse and the manual `setCache__` override go through here, so neither can drift from the other. * * @param file - The file being indexed. * @param content - The file's content, passed on as the `changed` event's `data`. * @param cache - The metadata to record. */ private applyCache; private getFilesNamed; /** * Resolves a bare link name the way Obsidian's `getLinkpathDest` does: by case-insensitive file name, trying the * name itself when it has an extension and the name plus `.md` otherwise, and preferring a root-level file of that * name when several match. * * @param linkName - The link name, without a folder or subpath. * @returns The files the name resolves to. */ private getNameDestinations; private parseFileMetadata; private updateLinks; }