Feature Request: An API function to prepend content to notes

Use case or problem

The Obsidian API has the Vault.append() for appending to notes (adding content to the end of a note), but doesn’t have one for prepending to notes, and its a little tricky if the note contains frontmatter / properties.

Proposed solution

Obsidian should have another function called Vault.prepend() which can handle prepending to notes with or without frontmatter.

Current workaround (optional)

This JS could be used for prepending:

// This adds the phrase "lorem Ipsum" to the top of a note if there's no frontmatter,
// or right below the fronmatter is present.
let file = app.workspace.getActiveFile();
let newFileContent = String(await app.vault.read(file));
let stringToPrepend = "lorem ipsum";
newFileContent = /^(---(.|\n)+?---)/.test(newFileContent) ? newFileContent.replace(/^(---(.|\n)+?---)/g, `$1\n${stringToPrepend}`) : `${stringToPrepend}\n${newFileContent}`;
app.vault.modify(file, newFileContent);

API requests should go to “developers and api” section.

1 Like