mindnet_obsidian/src/vocab/VocabularyLoader.ts

28 lines
932 B
TypeScript

import type { App, TFile } from "obsidian";
import { normalizeVaultPath } from "../settings";
export class VocabularyLoader {
/**
* Loads a text file from the currently opened Obsidian vault.
* @param app Obsidian App
* @param vaultRelativePath e.g. "_system/dictionary/edge_vocabulary.md"
*/
static async loadText(app: App, vaultRelativePath: string): Promise<string> {
const p = normalizeVaultPath(vaultRelativePath);
const abstract = app.vault.getAbstractFileByPath(p);
if (!abstract) {
throw new Error(`Vocabulary file not found in vault: "${p}"`);
}
// Guard: Only files can be read
const file = abstract as TFile;
// TFile has 'extension' and 'path' properties; if it isn't a file this will usually fail at runtime.
if (!(file && typeof file.path === "string")) {
throw new Error(`Path is not a file: "${p}"`);
}
return await app.vault.read(file);
}
}