feat: implement concrete adapter classes

Add VaultAdapter, MetadataCacheAdapter, and FileManagerAdapter as
pass-through wrappers for Obsidian API objects.
This commit is contained in:
2025-10-19 23:14:54 -04:00
parent fc001e541d
commit e369904447
3 changed files with 81 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
import { FileManager, TAbstractFile, TFile } from 'obsidian';
import { IFileManagerAdapter } from './interfaces';
export class FileManagerAdapter implements IFileManagerAdapter {
constructor(private fileManager: FileManager) {}
async renameFile(file: TAbstractFile, newPath: string): Promise<void> {
await this.fileManager.renameFile(file, newPath);
}
async trashFile(file: TAbstractFile): Promise<void> {
await this.fileManager.trashFile(file);
}
async processFrontMatter(file: TFile, fn: (frontmatter: any) => void): Promise<void> {
await this.fileManager.processFrontMatter(file, fn);
}
}

View File

@@ -0,0 +1,22 @@
import { MetadataCache, TFile, CachedMetadata } from 'obsidian';
import { IMetadataCacheAdapter } from './interfaces';
export class MetadataCacheAdapter implements IMetadataCacheAdapter {
constructor(private cache: MetadataCache) {}
getFileCache(file: TFile): CachedMetadata | null {
return this.cache.getFileCache(file);
}
getFirstLinkpathDest(linkpath: string, sourcePath: string): TFile | null {
return this.cache.getFirstLinkpathDest(linkpath, sourcePath);
}
get resolvedLinks(): Record<string, Record<string, number>> {
return this.cache.resolvedLinks;
}
get unresolvedLinks(): Record<string, Record<string, number>> {
return this.cache.unresolvedLinks;
}
}

View File

@@ -0,0 +1,41 @@
import { Vault, TAbstractFile, TFile, TFolder, DataWriteOptions } from 'obsidian';
import { IVaultAdapter } from './interfaces';
export class VaultAdapter implements IVaultAdapter {
constructor(private vault: Vault) {}
async read(file: TFile): Promise<string> {
return this.vault.read(file);
}
stat(file: TAbstractFile): { ctime: number; mtime: number; size: number } | null {
if (file instanceof TFile) {
return file.stat;
}
return null;
}
getAbstractFileByPath(path: string): TAbstractFile | null {
return this.vault.getAbstractFileByPath(path);
}
getMarkdownFiles(): TFile[] {
return this.vault.getMarkdownFiles();
}
getRoot(): TFolder {
return this.vault.getRoot();
}
async process(file: TFile, fn: (data: string) => string, options?: DataWriteOptions): Promise<string> {
return this.vault.process(file, fn, options);
}
async createFolder(path: string): Promise<void> {
await this.vault.createFolder(path);
}
async create(path: string, data: string): Promise<TFile> {
return this.vault.create(path, data);
}
}