Skip to content

readSafe

Reads the content of a file safely from the vault.

It covers the case when the file was removed during the reading.

This is NOT a pure read: it calls saveNote first, which saves any open editor view of the note that has unsaved changes, so the content it returns is what the user sees. Do not call it from an automatic trigger that only decides whether to act. See getCacheSafe in @mnaoumov/obsidian-lib/metadata-cache for why.

Import:

import { readSafe } from '@mnaoumov/obsidian-lib/vault';

Signature:

function readSafe(app: App, pathOrFile: PathOrFile): Promise<string | null>

Parameters:

Parameter Type Description
app App The application instance.
pathOrFile PathOrFile The path or file to read.

Returns: Promise<string | null> — A Promise that resolves to the content of the file or null if the file is missing or deleted.


Links to this page: