How to get current file content without YAML frontmatter?

Hi,

I have found the following guide for getting the content of the current file:

const noteFile = this.app.workspace.getActiveFile(); // Currently Open Note
if(!noteFile.name) return; // Nothing Open

// Read the currently open note file. We are reading it off the HDD - we are NOT accessing the editor to do this.
let text = await this.app.vault.read(noteFile); 

This code is created by Binny V A and published in txt.binnyvs.com.

How can I remove YAML frontmatter from this? Or is there another way to get the file content so that YAML is already filtered out?

In theory, I could try to come up with a regular expression that would strip away everything that is between two --- lines (with the first --- line being the very first thing in the file). I just don’t think it’s a good idea to do this kind of thing myself, as it can be errorprone, and I would guess Obsidian already does a split like this. I just don’t know if there is API access for it.

Thank you for your support!

1 Like

I’ve been able to use the cached frontmatter information to do that

This is the relevant code (note that error checking is not present):

const file = app.workspace.getActiveFile();
let text = editor.getDoc().getValue()
let fmc = app.metadataCache.getFileCache(file)?.frontmatter;
let end = fmc.position.end.line + 1 // accont for ending ---
body = text.split("\n").slice(end).join("\n")

1 Like