Skip to main content
The CachedMetadata interface represents the parsed and cached metadata for a file in the vault. This includes links, embeds, tags, headings, frontmatter, and other structural elements.

Interface Definition

export interface CachedMetadata

Properties

Array of internal links found in the file. Each link includes position information and the link text.
embeds
EmbedCache[]
Array of embedded files (using ![[]] syntax). Includes images, PDFs, and other embedded content.
tags
TagCache[]
Array of tags found in the file (both #tag and frontmatter tags).
headings
HeadingCache[]
Array of headings found in the file, with information about heading level and position.
footnotes
FootnoteCache[]
Available since version 1.6.6
Array of footnote definitions in the file.
footnoteRefs
FootnoteRefCache[]
Available since version 1.8.7
Array of footnote references in the file.
Available since version 1.8.7
Array of reference-style link definitions (e.g., [link]: https://example.com).
sections
SectionCache[]
Sections are root level markdown blocks, which can be used to divide the document up.
listItems
ListItemCache[]
Array of list items found in the file.
frontmatter
FrontMatterCache
Parsed frontmatter data from the YAML front matter block.
frontmatterPosition
Pos
Available since version 1.4.0
Position of the frontmatter in the file.
Available since version 1.4.0
Array of links found in the frontmatter.
blocks
Record<string, BlockCache>
A mapping of block IDs to their cache information. Block IDs are defined using the ^block-id syntax.

CacheItem

Base interface for cached items that have a position in the file.
export interface CacheItem {
  position: Pos;
}

Pos

Represents a position range in a file.
export interface Pos {
  start: { line: number; col: number; offset: number };
  end: { line: number; col: number; offset: number };
}

Example

const file = this.app.workspace.getActiveFile();
if (file) {
  const cache = this.app.metadataCache.getFileCache(file);
  
  if (cache) {
    // Access headings
    if (cache.headings) {
      cache.headings.forEach(heading => {
        console.log(`Level ${heading.level}: ${heading.heading}`);
      });
    }
    
    // Access links
    if (cache.links) {
      cache.links.forEach(link => {
        console.log(`Link to: ${link.link}`);
      });
    }
    
    // Access tags
    if (cache.tags) {
      cache.tags.forEach(tag => {
        console.log(`Tag: ${tag.tag}`);
      });
    }
    
    // Access frontmatter
    if (cache.frontmatter) {
      console.log('Title:', cache.frontmatter.title);
      console.log('Tags:', cache.frontmatter.tags);
    }
    
    // Access blocks
    if (cache.blocks) {
      for (const blockId in cache.blocks) {
        console.log(`Block ID: ${blockId}`);
      }
    }
  }
}

Use Cases

Building a Table of Contents

function buildTableOfContents(file: TFile): string[] {
  const cache = this.app.metadataCache.getFileCache(file);
  if (!cache || !cache.headings) return [];
  
  return cache.headings.map(heading => {
    const indent = '  '.repeat(heading.level - 1);
    return `${indent}- ${heading.heading}`;
  });
}
function getAllLinks(file: TFile): string[] {
  const cache = this.app.metadataCache.getFileCache(file);
  if (!cache) return [];
  
  const links: string[] = [];
  
  // Links from content
  if (cache.links) {
    links.push(...cache.links.map(link => link.link));
  }
  
  // Links from embeds
  if (cache.embeds) {
    links.push(...cache.embeds.map(embed => embed.link));
  }
  
  // Links from frontmatter
  if (cache.frontmatterLinks) {
    links.push(...cache.frontmatterLinks.map(link => link.link));
  }
  
  return links;
}

Extracting All Tags

import { getAllTags } from 'obsidian';

function extractTags(file: TFile): string[] | null {
  const cache = this.app.metadataCache.getFileCache(file);
  if (!cache) return null;
  
  // Use the built-in helper function
  return getAllTags(cache);
}

Build docs developers (and LLMs) love